GNOME Bugzilla – Bug 673317
There's no way to install custom widget style properties
Last modified: 2014-11-13 14:39:03 UTC
Currently the gtk_widget_class_install_style_property() is not wrapped, thus there is no way to define custom style properties for custom widgets. Other bindings, such as pygtk or vala, provide this function, so we probably should wrap it too. I could write a patch for this. I have thought of two ways the needed functionality could be implemented: 1) A Gtk::Widget::install_style_property() function template with an interface similar to Gtk::Widget::get_style_property(). 2) Gtk::CustomStyleProperty or Gtk::StyleProperty class template with an interface similar to that of Glib::Property. Gtk::CustomStyleProperty should probably be chosen, because it avoids confusion with GtkStyleProperties). In my opinion the second design is better, because style properties conceptually are very similar to the object properties. Moreover, this class could make the get_style_property() function obsolete, since the function would be only useful for style properties installed by base classes. I couldn't think of a use case where this information can be useful, though, if need arises, a CustomStylePropertyProxy similar to Glib::PropertyProxy could be implemented. Any opinions?
(In reply to comment #0) > Moreover, this class could make the get_style_property() function obsolete, > since the function would be only useful for style properties installed by > base classes. I couldn't think of a use case where this information can be > useful, I made a quick and incomplete search in gtk+'s source code. GtkWidget installs style properties that are used by some of its subclasses. Examples: link-color is used by GtkAboutDialog, GtkLabel, GtkLinkButton. separator-height is used by GtkMenuItem, GtkSeparator, GtkToolbar, GtkTreeView. Don't you think someone who makes a custom widget might want to use some of the style properties installed by the base class Gtk::Widget (GtkWidget)? My only experience with style properties is that I made some changes for gtkmm3 to the custom widget example in the gtkmm tutorial. I can't really decide which of your suggested implementations is better. I suppose the first alternative is easier to implement. Probably very few gtkmm programmers will ever use style properties. Therefore I would vote for the simpler alternative.
(In reply to comment #1) > I made a quick and incomplete search in gtk+'s source code. GtkWidget installs > style properties that are used by some of its subclasses. Examples: > > link-color is used by GtkAboutDialog, GtkLabel, GtkLinkButton. > separator-height is used by GtkMenuItem, GtkSeparator, GtkToolbar, GtkTreeView. > > Don't you think someone who makes a custom widget might want to use some of the > style properties installed by the base class Gtk::Widget (GtkWidget)? > I supposed that if custom style properties are defined rarely, one would need style properties of base classes even in rarer cases. If we, however, do need access to the custom properties, then CustomStylePropertyProxy could be implemented and added to the existing classes in much the same way Glib::Property proxy is used to access the style properties defined in Gtk+. Anyway, I've already implemented both solutions and will attach them below. I still think that the CustomStyleProperty/CustomStylePropertyProxy is a better way to handle things, just because of the similarity with Glib::Property and Glib::PropertyProxy. Using this would IMO result in a cleaner API. However, since only few will use that API, the argument of simplicity wins. Thus I think adding install_style_property() is a indeed better option for the moment.
Created attachment 213313 [details] [review] [PATCH] Widget: Add install_style_property() Patch to add install_style_property() wrapper.
Created attachment 213314 [details] [review] Shows how CustomStyleProperty could be implemented An example how CustomStyleProperty could be implemented.
Created attachment 290117 [details] [review] patch: Add Gtk::StyleProperty Now when I return to this bug after more than two years, I have changed my mind. GObject properties, GtkContainer child properties and GtkWidget style properties should as far as possible and reasonable be handled similarly. This requires // For wrapped glib/gtk+ properties class Glib::PropertyProxy_Base class Glib::PropertyProxy<T> : public Glib::PropertyProxy_Base class Glib::PropertyProxy_ReadOnly<T> : public Glib::PropertyProxy_Base class Glib::PropertyProxy_WriteOnly<T> : public Glib::PropertyProxy_Base // For custom properties class Glib::PropertyBase class Glib::Property<T> : public Glib::PropertyBase // For wrapped gtk+ child properties class Gtk::ChildPropertyProxy_Base class Gtk::ChildPropertyProxy<T> : public Gtk::ChildPropertyProxy_Base class Gtk::ChildPropertyProxy_ReadOnly<T> : public Gtk::ChildPropertyProxy_Base class Gtk::ChildPropertyProxy_WriteOnly<T> : public Gtk::ChildPropertyProxy_Base // For custom child properties (not implemented) class Gtk::ChildPropertyBase class Gtk::ChildProperty<T> : public Gtk::ChildPropertyBase // For wrapped gtk+ style properties (not implemented, // Gtk::Widget::get_style_property() is a substitute) class Gtk::StylePropertyProxy_Base class Gtk::StylePropertyProxy<T> : public Gtk::StylePropertyProxy_Base class Gtk::StylePropertyProxy_ReadOnly<T> : public Gtk::StylePropertyProxy_Base // No Gtk::StylePropertyProxy_WriteOnly. All style properties are readable. // For custom style properties class Gtk::StylePropertyBase class Gtk::StyleProperty<T> : public Glib::StylePropertyBase I propose that we implement Gtk::StylePropertyBase and Gtk::StyleProperty<>. I have made some small changes to Mark's patch in comment 4.
Created attachment 290118 [details] Test case This test case is a modified version of https://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/custom/custom_widget
I have pushed a patch very similar to the one in comment 5. https://git.gnome.org/browse/gtkmm/commit/?id=20728720ea5930147c0b24b9b6fde87d526fa14e