Re: Widget Colors



philippe vivarelli conexant com wrote:
> It works for buttons but not for other widgets (Label, Frames, ..)

Hi Phillippe, it works for widget that have their own windows.
Label/frame/etc. are client-type widgets ... they draw on their parent's
window. Wrap them in an event box to make sure they get their own
window.

> on_button1_clicked(GtkButton *button, gpointer window)
> 
> {
>      GtkWidget *ThisWidget;
>        GtkStyle* my_style;
> 
>         GdkColor myGdkColor = {0, 0xAFFF, 0xFFFF, 0xCFFF};
> 
>         my_style = gtk_style_copy( gtk_widget_get_default_style() );
> 
>         my_style->bg[GTK_STATE_NORMAL] = myGdkColor;
>         my_style->bg[GTK_STATE_ACTIVE] = myGdkColor;
>         my_style->bg[GTK_STATE_PRELIGHT] = myGdkColor;
>         my_style->bg[GTK_STATE_SELECTED] = myGdkColor;
>         my_style->bg[GTK_STATE_INSENSITIVE] = myGdkColor;
> 
>           gtk_widget_set_style(button, my_style);
> 
>      ThisWidget = lookup_widget(window, "label1");
>      gtk_widget_set_style(ThisWidget, my_style);
> 
>      ThisWidget = lookup_widget(window, "frame1");
>      gtk_widget_set_style(ThisWidget, my_style);
> 
> }

This is maybe not the best way. Havoc suggested:

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

Now your colour change will survive a theme change.

Even better (I think) is to use gtk_widget_set_name( widget,
"green_widget" ) to set a name for your widget. Then in your .gtkrc file
for your application, have lines like:

	style "green_style" = default {
		bg[ NORMAL ] = { 0.5, 0.7, 0.5 }
	}

	widget "*green_widget" style "green_style"

Much easier, and easy for your users to alter too.

HTH, John
--
John Cupitt, john cupitt ng-london org uk, +44 (0)20 7747 2570
VASARI Lab, The National Gallery, Trafalgar Square, London, WC2N 5DN




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