I am trying to change the font size of a GtkText widget by applying PangoAttribute and changing PangoAttrList. I am also using a GtkSpinButton to update the font size. But the problem is, when i click on the SpinButton, the font of the GtKText widget is not updating. After clicking the GtkSpinButton, i also need to click on the GtKText widget to update the font size.
I tried gtk_text_set_attributes function and it worked fine. But the problem is, RAM consumption increases with each click on the GtkSpinButton.
Can anybody check it please? Demo script Given below. Thank you.
#include <gtk/gtk.h>
void spinBtn_change_digits( GtkAdjustment *adj, gpointer user_data)
{
int size = gtk_adjustment_get_value (adj);
printf("font Size spin: %d\n", size);
/*
//(1)change font of GtkText by changing PangoAttrList
PangoAttrList *list = gtk_text_get_attributes ((GtkText *)user_data);
char *ch = pango_attr_list_to_string (list);
printf("PangoAttrList: %s\n", ch);
PangoAttribute *attrS = pango_attr_size_new (size * PANGO_SCALE);
pango_attr_list_change ((PangoAttrList *)list, attrS);
*/
//(2)change font of GtkText by new PangoAttrList
PangoAttrList *attrlist = pango_attr_list_new();
PangoAttribute *attrS = pango_attr_size_new_absolute (size * PANGO_SCALE);
pango_attr_list_insert (attrlist, attrS);
gtk_text_set_attributes ((GtkText *)user_data, attrlist);
pango_attr_list_unref(attrlist);
gtk_widget_queue_draw ((GtkWidget *)user_data);
}
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
GtkWidget *fixed = gtk_fixed_new ();
gtk_window_set_child (GTK_WINDOW (window), fixed);
GtkWidget *preview_text = gtk_text_new();
gtk_editable_set_text ((GtkEditable *)preview_text, "The quick brown fox jumps over the lazy dog.");
gtk_widget_set_size_request(preview_text, 245, 50);
gtk_fixed_put (GTK_FIXED (fixed), preview_text, 0, 3);
//change font of GtkText
PangoAttrList *attrLt = pango_attr_list_new();
PangoAttribute *attrS = pango_attr_size_new_absolute (10 * PANGO_SCALE);//font size fontSize
pango_attr_list_insert (attrLt, attrS);
gtk_text_set_attributes ((GtkText *)preview_text, attrLt);
pango_attr_list_unref(attrLt);
GtkAdjustment *spin_adj = gtk_adjustment_new (10, 1, 200, 1, 5, 0.0);
GtkWidget *spin_btn = gtk_spin_button_new (spin_adj, 0.1, 0);
gtk_widget_set_size_request(spin_btn, 110, 25);
gtk_fixed_put (GTK_FIXED (fixed), spin_btn, 250, 3);
g_signal_connect (spin_adj, "value_changed", G_CALLBACK (spinBtn_change_digits), preview_text);
gtk_window_present (GTK_WINDOW (window));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
#if GLIB_CHECK_VERSION(2, 74, 0)
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
#else
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
#endif
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}