最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - How to change the font size of a GtkText widget by applying PangoAttribute? - Stack Overflow

programmeradmin4浏览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 (".gtk.example", G_APPLICATION_DEFAULT_FLAGS); 
 #else
        app = gtk_application_new (".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;
}

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 (".gtk.example", G_APPLICATION_DEFAULT_FLAGS); 
 #else
        app = gtk_application_new (".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;
}
Share Improve this question edited Apr 3 at 1:52 dibyendu asked Mar 14 at 3:35 dibyendudibyendu 4423 silver badges8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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 (".gtk.example", G_APPLICATION_DEFAULT_FLAGS);
 #else
        app = gtk_application_new (".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.

发布评论

评论列表(0)

  1. 暂无评论