|« Main Page|
XFC Object Properties
Object properties are data members that define the internal state of an object. The GLib object system implements a generic mechanism to get and set object properties. When an object is instantiated, the object's 'class_init' handler is called to register any object properties and provide default values. GTK+ provides several functions that can get and set one or more object properties at a time, like g_object_get() and g_object_set(). As well most objects provides a getter and setter function for each property. For example, GtkWindow has a property called "gravity" and provides the functions gtk_window_get_gravity() and gtk_window_set_gravity() to get and set a window's gravity value.
GTK+ implements the getting and setting of object properties in terms of an object's individual getter and setter functions. When you set an object property via the generic set function g_object_set(), it's actually the property's setter function that gets called to set the value. While it's important in GTK+ to be able to get and set properties both generically and specifically using the getter and setter functions, it's not in C++. The use of variable arguments lists is frowned upon in C++ because they're not typesafe, but they're still legal.
In a C++ language binding it's overkill and unnecessary to provide both generic and specific wrappers to get and set properties, and it offers no real benefits. So why does XFC provide wrappers for some object properties? Because not all GTK+ objects provide getter and setter functions. Specifically the following objects don't:
For convenience the first line typedefs the size_property type G::Property<int> which is both a readable and writable property. The third line declares the static const variable size_property. You can use this variable to get and set the text tag's font size but only in dervied classes because it's declared protected. The functions used to get and set a property are:
These functions take a pointer to the object whose property is being accessed and a variable of type 'DataType'. DataType is the template argument and in the case of Gtk::TextTag::size_property it is an int.
You would get a tag's font size like this:
and you would set a tag's font size like this:
You wont set properties in derived classes very often. Instead you will use a property's public proxy function. That's what the second line in the size_property declaration is for. It typedefs a proxy object for size_property:
G::PropertyProxy is an inline proxy object that is used to access a protected property's get() and set() methods. Its constructor takes an object pointer and the address of the protected property as arguments. G::PropertyProxy has its own get() and set() methods that take only the property data type as an argument. These methods then call the protected property's get() and set() methods, passing as the object pointer the pointer passed to the G::PropertyProxy constructor.
The public proxy property for Gtk::TextTag's size_property is declared and defined inline like this:
Using size_property's public proxy function, you could get a tag's font size like this:
and you could set a tag's font size like this:
Or alternatively, you could simply use assignment which is a lot easier:
If you look at the property declarations in the header files you will see some property typedefs have two template arguments. Take a look at the declaration for the Gtk::TextTag 'weight_property'.
Here G::Property has two template arguments, Pango::Weight and int. Properties are accessed internally using a G::Value. G::Value handles an enum value as an integer, so Pango::Weight which is an enum value needs to be cast to an int. The first template argument is always the property data type and the second template argument is the standard data type G::Value needs to cast the property data type to. You can just ignore the second template argument because it's an implementation detail for internal use only.