Re: [gtk-list] How to change a label's font?




Cheng-Chang Wu <C.Wu@ping.de> writes:

> Hello,
> 
>   AT first, thank all who have answered my last question.
> 
>   I try to change the font of a label like this:
> 
>       label = gtk_label_new ("Quit");
>       gdk_font_free (label->style->font);
>       label->style->font = gdk_font_load 
> ("-adobe-times-bold-i-normal--25-180-100-100-p-128-iso8859-1");
>       gdk_font_ref (label->style->font);
>  
> 
>   It works, but I don't like it, especially the free- and ref-thing. The
> code looks dangerous and too low level. Have I done it right? Or are
> there better ideas?

This is about right. A few points:

- gdk_font_free() is renamed to gdk_font_unref() in 0.99.1 and later. 
- The gdk_font_ref() call isn't necessary because fonts (and all other
  GDK objects) start out with a reference count of 1. Since the only
  place you are using the font is in the style, you don't need to
  increment the reference count.
- You probably don't want to change default style for the label -
  styles are typically shared by many widgets. Instead you should do:

   GtkStyle *style;

   style = gtk_style_new();
   label = gtk_label_new ("Quit");
   gdk_font_unref (style->font);
   style->font = gdk_font_load 
       ("-adobe-times-bold-i-normal--25-180-100-100-p-128-iso8859-1");
   gtk_widget_set_style (label, style);

Hope this helps,
                                        Owen



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