Overriding property defaults



While reviewing at a patch to clean up GtkButtonBox, I noticed a
problem with the way properties work currently.

The problem is that GtkButtonBox inherits GtkButtons' spacing
parameter, but wants to change the default value. It is currently
impossible for a derived class to modify the default value for a
property.

I think this is a fairly common requirement. (Somewhat less common,
but also legitimate, would be wanting to use more restrictive
validation for a property.)

The essence of my proposal would be:

  GParamSpec *spec = g_object_class_copy_parent_property (object_class, "spacing");

  g_param_spec_int_set_default (G_PARAM_SPEC_INT (spec), BUTTON_BOX_SPACING);
 
  g_object_class_install_property (object_class, 0, spec);

That is:

 - We add a way of copying paramspecs, and a convenience function
   in GObject to copy the parameter spec of a property you are overriding.

 - We add a way of setting default values; another possibility would be
   to have a virtual function in GParamSpec taking a GValue, so you could
   do:

    g_param_spec_set_default (spec, BUTTON_BOX_SPACING);

 - We allow properties to be installed with a parameter ID of 0, meaning
   "ignore this property when looking for the getter/setter".

This requires some changes to the way the internals of GParam works,
in particular GParamSpecPool. And for efficiency/simplicity one might
want to move the ParamID directly into the GParamSpec instance as a
private member.

But I think, in general, it is a fairly small change and a quite
important feature we are currently lacking.

Regards,
                                        Owen




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]