0

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;
}

2 Answers 2

1

Is there any reason why you can't do this via CSS? That would be the way to do it. If there is any reason why you can't do it with CSS, you can use gtk_text_set_attributes, but each time you change the size, you should clear the contents of "attributes" and only then add the new contents. This will probably avoid increasing memory usage.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I made a custom font chooser widget. So I was avoiding the process of using css. Thanks for the advice. I will try it.
I tried it via CSS, but it consumes more RAM than the Pango method.
0

My proposal is as follows:

...
//(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);

  gtk_text_set_attributes((GtkText *)user_data,(PangoAttrList*)list); // new !!
...
// gtk_widget_queue_draw ((GtkWidget *)user_data); // unnecessary
...

Works with me without the additional memory needed.

Here again the complete script. With Ubuntu it works without any problems.

 
#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);

  //new 
  //PangoContext* context = gtk_widget_get_pango_context ((GtkWidget*)user_data);

  //(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);

  gtk_text_set_attributes((GtkText *)user_data,(PangoAttrList*)list);

/*
    //(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;
}

Have fun programming.

2 Comments

The font changed in this way is not being reflected in the GtkText Widget.. @LinuxLite7-xfce
Too bad, it works without any problems with me. Maybe someone else can try it out with their operating system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.