Create a Gtk Button with an image in the background, and a label in front of it
Okay, I'm trying to create a Gtk button in C++ (gtkmm 3.0) that has an image in the background that I can change depending on state, and a label in the foreground that I can change depending on a language string. I tried extending the Gtk::EventBox class which works great for the image, but I can't put a label in it. I tried extending Gtk::Container 开发者_高级运维class but then I can't put it in my Gtk::VButtonBox. There has to be a better way of doing this without re-inventing the wheel.
Any suggestions would be greatly appreciated.
Thanks, Paul
This should work:
GtkCssProvider* my_css_provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(my_css_provider,"GtkWidget { background:url(yourimage.jpg); }",-1,NULL);
GtkStyleContext* context = gtk_widget_get_style_context(yourbutton);
gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (my_css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
I tested it with background:#F00 which transformed the button to a nice red, so it should also work with images, just keep to css standards. You can do basically everything, like change the font with, change padding of your button label and so on :-)
Actually you will need to invent the wheel. What you need is composition of two widgets. I know there's a implementation of someting like that in Gedit, Nautilus, and the new Gtk+ 3.1.X includes a widget that does something like that called GtkOverlay.
- GtkOverlay is a new container that allows to place one or more
'overlay' widgets on top of another widget. This can be used for 'floating statusbars' and similar interfaces.
That comes from here. You have two choices, you wait for it to come out, or you copy and the code and adapt it to your app till the new Gtk+ get released.
精彩评论