I am using Gtk 2.8.18. I would like to know how to set the background color in a
GtkButton. I cannot set the background in a style, theme, or resource
file because I am programming to requirements that specify different background
colors for different buttons. I have tried using gtk_widget_modify_bg on the button, using
all combinations of creating the button with and without a label. I also tried gtk_widget_modify_base. I have tried using gtk_widget_set_style: GtkStyle *BtnStyle; BtnStyle = gtk_style_new(); BtnStyle->bg[0] = BGColor; /* BG Color is a
GdkColor that works nicely setting colors in TextViews */ BtnStyle->bg[1] = BGColor; gtk_widget_set_style ( Button, BtnStyle ); I have tried using gtk_widget_modify_style: GtkRcStyle *ModStyle ModStyle = gtk_rc_style_new(); ModStyle->bg[0] = BGColor; ModStyle->bg[1] = BGColor; ModStyle->color_flags[0] |= GTK_RC_BG; ModStyle->color_flags[1] |= GTK_RC_BG; gtk_widget_modify_style ( Button, ModStyle ); I repeated the above process using foreground colors and
then tried it setting both foreground and background. I repeated both the gtk_widget_set_style and
gtk_widget_modify_style processes just before calling gtk_main. I used gtk_widget_get_parent to get the parent container of
the button (once again just before calling gtk_main since gtk_widget_get_parent
returns NULL prior to calling gtk_widget_show on the Main Window) and tried
using all of the above processes (gtk_widget_modify_bg, gtk_widget_modify_base,
gtk_widget_set_style, and gtk_widget_modify_style). I have put an EventBox around my button, yielding an
idiotic-looking colored border around the button. I have put an EventBox inside my button, and it fills the
center of the button with color but leaves a wide non-colored margin around it. It is utterly inconceivable that this process should be so
difficult. To change the color of buttons in Motif is trivial – I
use the XtSetArg process before creating the widget and simply have to set
XmNbackground to the RGB value that I want. Motif then takes this basic
background color and automatically figures out the colors needed for top,
bottom, and activate shadows. The concept of a big green green button to
start a process and a big red button to abort/stop/terminate a process is so
universal that it can be found on everything from 1920s machine tools to the
latest military and commercial aircraft control panels. Thus I am
expected to be able to emulate this with the buttons on my GUI. So far, I
have failed utterly with GTK. My web searches led me to try some of these processes
– in particular, Havoc Pennington’s “GTK colors
mini-FAQ” is what led me to try using the gtk_widget_get_parent to get
the container widget for the button. I have even tried traversing the entire ancestor tree by
using a loop to continue calling gtk_widget_get_parent until it returned
NULL. Only the MainWindow returned TRUE to GTK_WIDGET_NO_WINDOW( ), which
means I managed to set the background of the Main Window but nothing else, despite
trying gtk_widget_modify_bg on the entire ancestor tree. Please tell me how I can set the background color of my
GtkButtons to different colors. Thanks, Michael Kahn |