Re: changing a widget's color at runtime



I found the following when skimming previous messages on the mailing list
... a search for color doesn't do much good when the subject contains
colour ;)

Anwyays ... I believe this gets much closer to the answers I was seeking
... it certainly does address the "memory leak" question I had ...


-----------------------------------------------------------------------


The correct way is:

 - create a GtkRcStyle
 - set the background color in the RcStyle, and set the flag in 
   the RcStyle indicating that the background color is set
 - call gtk_widget_modify_style() to override the widget's current
   style with your RcStyle
 - unref the RcStyle, the widget will have kept a reference to it

This method allows your changes to survive theme switches. If you just
modify the GtkStyle instead of adding the GtkRcStyle, changing themes
will make your modification go away.

The GTK 2.0 sources have a function gtk_widget_modify_bg() that does
this for you.

Havoc


Yes, sorry for the brevity; I've typed it in a few times before, this
is one of the many FAQs not in the FAQ.

Something like:

 GdkColor red = { 0, 65535, 0, 0 };
 GtkRcStyle *rc_style = gtk_rc_style_new ();
 rc_style->bg[GTK_STATE_NORMAL] = red;
 rc_style->color_flags[GTK_STATE_NORMAL] |= GTK_RC_BG;
 gtk_widget_modify_style (widget, rc_style);
 gtk_rc_style_unref (rc_style);

Not tested. You may want to modify states other than NORMAL, but it
should be obvious how to do so following this example.

Havoc




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