Re: Can Anybody give me a code example of Changing font for TextView?



mili wrote:

Hi, anybody

I test the font changing to a TextView, but NO USE! Not only font family is not changed, also the font size is the same as before.

I hope you can give me a successful example, it'd better to select font with a GtkFontSelection.

 Thank you.
 mili

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list


Hello,
I've attached some code.

Olexiy


/*
	A little demo how to change the font of GtkTextView.
	Be aware: a couple of bugs can be found here.
                
Olexiy Avramchenko
*/

#include <stdio.h>
#include <gtk/gtk.h>


typedef struct __callback_params
{
	GtkTextView *tv;
	GtkFontSelectionDialog *fsd;
} CALLBACK_PARAMS;


static void set_font(GtkButton *button, CALLBACK_PARAMS *params)
{
char			*s;
PangoFontDescription	*pfd;

	/* getting the string with a font description from dialog */
	s = gtk_font_selection_dialog_get_font_name(params->fsd);
	/* killing the dialog */
	gtk_widget_destroy(GTK_WIDGET(params->fsd));

	/* creating PangoFontDescription from string we got, *sigh* why
	   there's no gtk_font_selection_dialog_get_font_pango_font_description() ? :)
	   (or, gtk_font_g46n() ? :)
	*/
	pfd = pango_font_description_from_string(s);
	/* applying selected font */
	gtk_widget_modify_font(GTK_WIDGET(params->tv), pfd);
	/* freeing description */
	pango_font_description_free(pfd);

	g_free(s);
}

/* setting up font selection dialog stuff */
static void setup_font_selection(GtkButton *button, CALLBACK_PARAMS *params)
{
	params->fsd = (GtkFontSelectionDialog*)gtk_font_selection_dialog_new("ch00$e the f0nT U wanT");

	/* <OK> button callback */
	g_signal_connect(
			G_OBJECT(GTK_FONT_SELECTION_DIALOG(params->fsd)->ok_button),
			"clicked",
			G_CALLBACK(set_font),
			params
	);

	/* <CANCEL> button callback: simply destroys the dialog */
	g_signal_connect_swapped(
				G_OBJECT(GTK_FONT_SELECTION_DIALOG(params->fsd)->cancel_button),
				"clicked",
				G_CALLBACK(gtk_widget_destroy),
				params->fsd
	);

	/* displaying the dialog */
	gtk_widget_show(GTK_WIDGET(params->fsd));
}

int main(int argc, char **argv)
{
int		i;
char		s[256];
GtkWidget	*window;
GtkWidget	*vbox;
GtkWidget	*frame;
GtkWidget	*tv;
GtkWidget	*button;
GtkTextIter	iter;
GtkTextBuffer	*buffer;
CALLBACK_PARAMS	*params;

	gtk_init(&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_container_set_border_width(GTK_CONTAINER(window), 8);
	g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);

	vbox = gtk_vbox_new(FALSE, 2);
	gtk_container_add(GTK_CONTAINER(window), vbox);

	frame = gtk_frame_new(NULL);
	gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE,TRUE, 0);

	tv = gtk_text_view_new();
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
	gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
	for (i=0;i<10;i++) {
		sprintf(s, "good looking line N%d\n", i);
		gtk_text_buffer_insert(buffer, &iter, s, -1);
	}
	gtk_widget_set_size_request(tv, 256,256);
	gtk_container_add(GTK_CONTAINER(frame), tv);

	button = gtk_button_new_with_label(" ch00$e f0nt ");
	gtk_box_pack_start(GTK_BOX(vbox), button, FALSE,FALSE, 0);
	params = g_malloc0(sizeof(*params));
	params->tv = GTK_TEXT_VIEW(tv);
	/* callback for font selection dialog launch */
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(setup_font_selection), params);

	button = gtk_button_new_with_label(" QuIt ");
	gtk_box_pack_start(GTK_BOX(vbox), button, FALSE,FALSE, 0);
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);

	gtk_widget_show_all(window);
	
	gtk_main();

	return 0;
}
text: text.c
	gcc -Wall `pkg-config gtk+-2.0 --cflags --libs` $< -o $@


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]