GNOME Bugzilla – Bug 557720
Make GtkButton _set_label and _set_image virtual
Last modified: 2015-03-07 21:40:35 UTC
Subclassing GtkButton is a common case, but the way _set_label and _set_image are implemented means that you often end up either breaking those accessors if your subclass has its own packing or your packing breaks as soon as these accessors are used. Consequently you want to be able to verride these functions to refer to your actual implementation. I propose set_label, get_label, set_image and get_image should be virtual functions in GtkButtonClass. I will come up with a patch in a bit.
Created attachment 121261 [details] [review] Implement GtkButton _label and _image virtually
+ GTK_BUTTON_CLASS (gtk_button_parent_class)->... Shouldn't these be GTK_BUTTON_GET_CLASS (button)->... instead ?
I have to admit that I personally never subclassed GtkButton. Some examples would be nice...
straight from gtk+ itself: GtkColorButton, GtkFontButton and GtkFileChooserButton.
well, strike GtkFileChooserButton from the list, as it is a GtkHBox.
You can also find examples for instance in Hildon widgets, which are used on the Maemo platform. At this point I'm actually not sure if the virtual _get_label should work with a string, or rather a label widget, which would make it possible to apply a little bit of "magic" regarding mnemonic and stock items. One thing that is currently hard to do in subclasses is imitating the different accessors and it should be nice if (almost) all of them could work out of the box. I'm going to post an improved patch and an alternative implementation soon, together with example code.
Created attachment 121515 [details] [review] Virtual GtkButton _label and _image, option 1 This is an improved version of the former patch, that actually works. This is option 1, as in obtaining a label string from the subclass.
Created attachment 121516 [details] Virtual GtkButton _label and _image, option 1, test A test program for option 1. It helped me verifying that regular buttons, strangely packed buttons and a demo subclass worked as intended.
Created attachment 121517 [details] [review] Virtual GtkButton _label and _image, option 2 This is option 2, as in obtaining a label *widget* from the subclass, based on the assumption that subclasses usually create a widget, but just pack it differently. This allows for accesors like _set_use_underline to be implemented in Gtk without the subclass having to imitate it.
Created attachment 121518 [details] Virtual GtkButton _label and _image, option 2, test A test program for option *2*. It helped me verifying that regular buttons, strangely packed buttons and a demo subclass worked as intended. Basically it's a slightly modified version of the other test program since the API is different.
(In reply to comment #6) > You can also find examples for instance in Hildon widgets, which are > used on the Maemo platform. Yes, in Hildon 2.2 we have HildonPickerButton, which is a button with two labels. Since we create an internal custom layout we can't use gtk_button_{get,set}_label() and that makes the API a bit uglier.