Re: [gtk-list] Re: [Q] gtk_widget_set_style




Robert Roebling <roebling@ruf.uni-freiburg.de> writes:

> Hi,
> 
> here is a late follow-up
> 
> Tero Pulkkinen wrote:
> > 
> > I want to change the label font
> > > dynamicaly. What I do is:
> > >
> > > > style = gtk_widget_get_style (label);
> > > > font = gdk_font_load (pattern);
> > > > style->font = font;
> > > > gtk_widget_set_style (label, style);
> > >
> > > Well, this works, but the new font is now used in all widgets 
> > > of my app, not only the label.
> > 
> > I'm not sure exactly how the style stuffs work, but by default 
> > I'd guess the get_style() function returns a ptr to a statically 
> > allocated "default" style and changing that will cause every 
> > widget in your app have that change.
> > 
> > Thus, you should make a new style with gtk_style_new() and copy 
> > the default to it.
> 
> How do I "copy the default style to it"? Do I have to copy
> every single field of the struct? That seems like a lot of
> work for changing only the font in one widget.

You can almost do:

  *style = *old_style

but that is a bad idea since there are some private fields.  (The
reference count included!)  Right now, you would have to copy each
field. Or better, just use a rc file to create the new style.

> Is there any reason for not supplying a new
> 
> GtkStyle *gtk_style_new( GtkStyle* template );
> 
> function, which would copy the field and you could
> then selectivly change the field you want to change.

I don't think so. I would call it gtk_style_copy () since
gtk_style_new is already taken. But otherwise, it is a good
idea. (Or maybe gtk_style_new_from_style ())

> Apart from that, I don't think that the font should
> be in the style struct. Changing an item's font is
> nothing unusual. Changing the style of all item seems
> like a drastic measure.

The point is that the font should be something configurable
by the user.

I _loved_ having that ability when someone decided to
change all the GIMP's fonts to 8-point. All I had to do
was edit my gimprc once, instead of patching the code with
every new release.

In anything but the most trivial demo applications, if you
want something to have a different font, you should use
a gtkrc and set up a separate named style for that usage,
instead of hard-coding it into your program.

Regards,
                                        Owen



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