|
Unix Programming - Applying Minilanguages - Case Study: Glade
Case Study: Glade
Glade is an interface
builder for the open-source GTK toolkit library for
X.[82]
Glade allows you to develop a
GUI interface by interactively picking, placing, and modifying widgets
on an interface panel. The GUI editor produces an XML file describing
the interface; this, in turn, can be fed to one of several code
generators that will actually grind out C,
C++,
Python or
Perl code for
the interface. The generated code then calls functions you write to
supply behavior to the interface.
Glade's XML format for
describing GUIs is a good example of a simple domain-specific
minilanguage. See Example8.1 for
a “Hello,
world!” GUI in Glade format.
Example8.1.Glade “Hello, World”.
<?xml version="1.0"?>
<GTK-Interface>
<widget>
<class>GtkWindow</class>
<name>HelloWindow</name>
<border_width>5</border_width>
<Signal>
<name>destroy</name>
<handler>gtk_main_quit</handler>
</Signal>
<title>Hello</title>
<type>GTK_WINDOW_TOPLEVEL</type>
<position>GTK_WIN_POS_NONE</position>
<allow_shrink>True</allow_shrink>
<allow_grow>True</allow_grow>
<auto_shrink>False</auto_shrink>
<widget>
<class>GtkButton</class>
<name>Hello World</name>
<can_focus>True</can_focus>
<Signal>
<name>clicked</name>
<handler>gtk_widget_destroy</handler>
<object>HelloWindow</object>
</Signal>
<label>Hello World</label>
</widget>
</widget>
</GTK-Interface>
A valid specification in Glade format implies a repertoire of actions
by the GUI in response to user behavior. The
Glade GUI treats these specifications as
structured data files; Glade code
generators, on the other hand, use them to write programs implementing
a GUI. For some languages (including Python), there are runtime libraries that
allow you to skip the code-generation step and simply instantiate the
GUI directly at runtime from the XML specification (interpreting Glade
markup, rather than compiling it to the host language). Thus, you get
the choice of trading space efficiency for startup speed or
vice versa.
Once you get past the verbosity of XML,
Glade markup is a fairly simple language.
It does just two things: declare GUI-widget hierarchies and associate
properties with widgets. You don't actually have to know a lot about
how glade works to read the specification
above. In fact, if you have any experience programming in GUI
toolkits, reading it will immediately give you a fairly good
visualization of what glade does with the
specification. (Hands up everyone who predicted that this particular
specification will give you a single button widget in a window
frame.)
This kind of transparency and simplicity is the mark of a good
minilanguage
design. The mapping between the notation and
domain objects is very clear. The relationships between objects are
expressed directly, rather than through name references or some other
sort of indirection that you have to think to follow.
The ultimate functional test of a minilanguage like this one is
simple: can I hack it without reading the manual? For a significant
range of cases, the Glade answer
is yes. For example, if you know the C-level constants that GTK uses
to describe window-positioning hints, you'll recognize GTK_WIN_POS_NONE as one and instantly be able to
change the positioning hint associated with this GUI.
The advantage of using Glade should be clear. It specializes in
code generation so you don't have to. That's one less routine task
you have to hand-code, and one fewer source of hand-coded bugs.
More information, including source code and documentation and
links to sample applications, is available at the Glade project page.
Glade has been ported to
Windows.
[an error occurred while processing this directive]
|