Is there any api like gtk_widget_modify_bg() to modify the background of a gtk toolbar by an image.
gtk_widget_modify_bg() only can change the color of the background.
Is there any api like gtk_widget_modify_bg() to modify the background of a gtk toolbar by an image.
gtk_widget_modify_bg() only can change the color of the background.
After one day's try, now I has known how to set an image backgroud for a gtk toolbar.
The keypoint is that you could not directly modify background for GtkToolbar or even GtkVBox (generally you would put GtkToolbar in a GtkVBox). Because they are all none window GtkWidget, so they couldn't catch expose-event, then can't draw their own background. Their background are same as their parent.
But the GtkEventBox can. So you can put the GtkToolbar into a GtkEventBox, and put GtkEventBox into a GtkVBox. Then you modify GtkEventBox's backgroud by an image, It seems GtkToolBar's background changed.
Following is my test codes:
int main(int argc, char* argv[])
{
GtkWidget* window;
GtkWidget* vbox;
GtkWidget* event_box;
GtkWidget* toolbar;
GtkToolItem* item;
gtk_init(&argc, &argv);
gtk_rc_parse("./gtk.rc");
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
gtk_window_set_title(GTK_WINDOW(window), "toolbar");
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
event_box = gtk_event_box_new();
//gtk_widget_set_name(vbox, "toolbar_event_box");
//set_event_box_background(event_box);
gtk_box_pack_start(GTK_BOX(vbox), event_box, FALSE, FALSE, 5);
toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2);
item = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_separator_tool_item_new();
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
item = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(gtk_main_quit), NULL);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
gtk_container_add(GTK_CONTAINER(event_box), toolbar);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
and I use gtkrc file to set the GtkEventBox backgroud. Following is the gtk.rc:
pixmap_path "/usr/share/pixmaps:/usr/share/myself"
style "window"
{
bg_pixmap[NORMAL] = "firefox.png"
}
style "toolbar"
{
bg_pixmap[NORMAL] = "bk.bmp"
}
widget_class "GtkWindow" style "window"
widget_class "GtkEventBox" style "toolbar"
The result of the program run:
If you don't use gtkrc file, you can use gtk_widget_set_style()
to change the GtkEventBox background. like this:
void set_event_box_background(GtkWidget* event_box)
{
GError* error = NULL;
GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file("/usr/share/myself/bk.bmp", &error);
GdkPixmap *pixmap = NULL;
GdkPixmap *mask = NULL;
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &mask, 255);
GtkStyle* orig_style = gtk_widget_get_style(event_box);
GtkStyle* style = gtk_style_copy(orig_style);
style->bg_pixmap[GTK_STATE_NORMAL] = pixmap;
gtk_widget_set_style(event_box, style);
}
I get this method from Yuren's Info Area.
When you use this method, you should comment gtk_rc_parse("./gtk.rc");
and uncomment set_event_box_background(event_box);
line at above main()
function.