GConf Aware Widgets (GAW) Introduction
This document is somewhat outdated, it exists now in a project called Rat. Check it out!
Working with GConf is usually a repetitive task, you must make sure each key has the correct value type, synchronize it with your widget's data (and vice-versa), make the widget insensitive if the key is not writable, etc. GAW tries to solve this problem in an out-of-your face manner. GAW is a Python module to be used with PyGtk.
Unlike gconf_widgets.py - which is used in gnome-blog, GAW doesn't redefine widgets, it takes a different approach and uses existing widgets. For example gconf_widgets.py can't cope with LibGlade pre-generated widgets, whereas GAW can. It also has a different license, LGPL .
Let's begin with a simple example, we'll try to bind a
gtk.Entry
to a
GConf key,
/apps/gaw/str_key:
import gaw, gtk, gconf
# Monitor the key, so gaw can listen for gconf events
gconf.client_get_default ().add_dir ("/apps/gaw",
gconf.CLIENT_PRELOAD_NONE)
win = gtk.Window (gtk.WINDOW_TOPLEVEL)
entry = gtk.Entry ()
entry.show ()
# bind the key with the widget
gconf_data = gaw.data_entry (entry, "/apps/gaw/str_key")
win.add (entry)
win.show ()
gtk.main ()
As you can notice is pretty straightforward,
gaw.data_entry creates an integrity binding between
the key and the widget, this binding is always in favor of the
widget.
The
gaw.data_entry method is just a wrapper function for
calling a more generalized class called
gaw.Data, this is the real binding between the two
parts. The
gaw.Data uses another class called
gaw.GConfValue, so in order to explain how
gaw.Data works we need to explain what is a
gaw.GConfValue.
A
gaw.GConfValue is a simple representation of a
GConf key, it
wraps around it and provides three important features:
- a callback to when the GConf key is changed
- a way to retrieve the GConf directly, whichever the type is
- a way to retrieve default values when the key is invalid or non existant
To setup a callback you merely use the
gaw.GConfValue.set_callback method which wraps around
gconf.Client.notify_add method but with the
appropriate key.
To retrieve or set the value of that key you just access the
member
gaw.GConfValue.data.
Let's look at an example of it's usage:
import gaw, gconf, gtk
gconf.client_get_default ().add_dir ("/apps/gaw",
gconf.CLIENT_PRELOAD_NONE)
key_str = gaw.GConfValue (
key = "/apps/gaw/key_str",
data_spec = gaw.Spec.STRING
)
def on_changed (*args):
global key_str
print key_str.key, "=", key_str.data
gtk.main_quit ()
tmp.set_callback (on_changed)
tmp.data = "Hello world"
gtk.main ()
The
gaw.Spec.STRING is the definition of the key type,
there are the following specs:
STRING,
FLOAT,
INT and
BOOL.
Next we set the callback and print the key and it's contents, so as you can see it's really simple.
Now onto
gaw.Data this is a really generic class so it's
constructor is a bit
verbose:
gaw.Data (widget, widget_getter, widget_setter, changed_signal, gconf_value)
So the code behind
gaw.data_entry is the one liner:
def data_entry (entry, key, data_spec = Spec.STRING, default =
None, client = None):
return Data (entry, entry.get_text, entry.set_text,
"changed", GConfValue (key, data_spec, default, client))
Nothing better then a real life example to understand it's
usage. As you can notice the
widget_setter and
widget_getter are self explanatory, they are functions
that can set/get the data from your widget. The signal is the way
the
gaw.Data has to know that your widget's data has been
changed.