Next: Gtk2::Buildable::ParseContext | Previous: Gtk2::Box | [Gtk2-Perl - Table of Contents] | [Gtk2-Perl - Index] |
Gtk2::Buildable - Interface for objects that can be built by Gtk2::Builder
package Thing; use Gtk2; use Glib::Object::Subclass Glib::Object::, # The important bit -- add this GInterface to our class interfaces => [ Gtk2::Buildable:: ], # Some signals and properties on the object... signals => { exploderize => {}, }, properties => [ Glib::ParamSpec->int ('force', 'Force', 'Explosive force, in megatons', 0, 1000000, 5, ['readable', 'writable']), ], ; sub exploderize { my $self = shift; $self->signal_emit ('exploderize'); } # We can accept all defaults for Buildable; see the description # for details on custom XML. package main; use Gtk2 -init; my $builder = Gtk2::Builder->new (); $builder->add_from_string ('<interface> <object class="Thing" id="thing1"> <property name="force">50</property> <signal name="exploderize" handler="do_explode" /> </object> </interface>'); $builder->connect_signals (); my $thing = $builder->get_object ('thing1'); $thing->exploderize (); sub do_explode { my $thing = shift; printf "boom * %d!\n", $thing->get ('force'); } # This program prints "boom * 50!" on stdout.
Glib::Interface +----Gtk2::Buildable
In order to allow construction from a Gtk2::Builder UI description (http://library.gnome.org/devel/gtk/unstable/GtkBuilder.html#BUILDER-UI), an object must implement the Gtk2::Buildable interface. The interface includes methods for setting names and properties of objects, parsing custom tags, and constructing child objects.
The Gtk2::Buildable interface is implemented by all widgets and many of the non-widget objects that are provided by GTK+. The main user of this interface is Gtk2::Builder, so there should be very little need for applications to call any of the Gtk2::Buildable methods.
So, instead of focusing on how to call the methods of a Gtk2::Buildable, this documentation deals with implementing a buildable object.
Since Gtk2::Widget implements the Gtk2::Buildable interface, all widgets
get buildability gratis. If your widget requires no special markup
syntax to express its configuration, and all properties can be handled
through the standard mechanisms, you can simply add the name of your
perl-derived Glib::Object types to the object
tag in the builder UI
description. You don't even have to do anything special in your class
definition. For example, objects of this class:
package My::Frame; use Gtk2; use Glib::Object::Subclass Gtk2::Frame::, properties => [ Glib::ParamSpec->int ('foo', ...), ], ; ... 1;
could be expressed in a builder definition file like this:
<object class="My__Frame" id="myframe"> <property name="foo">15</property> </object>
Notice that the '::' package separator has been replaced with '__' in the
class
attribute; this is because the ':' character is not valid for
GType type names. The mapping from perl package names to GType names should,
in general, be as simple as transliterating the colons.
Glib::Object does not implement Gtk2::Buildable by itself, so to get a builder UI file to create your custom Glib::Object subtypes, you'll have add the Gtk2::Buildable interface to your class's interfaces list.
package My::Thing; use Gtk2; # to get Gtk2::Buildable use Glib::Object::Subclass Glib::Object::, interfaces => [ 'Gtk2::Buildable' ], ... ;
Again, if you have no special requirements, then that should be all you need to do.
In some cases, you need to override the default Buildable behavior. Maybe your objects already store their names, or you need some special markup tags to express configuration. In these cases, add the Gtk2::Buildable interface to your object declaration, and implement the following methods as necessary.
Note that in the current implementation the custom tags code doesn't chain up to any buildable interfaces in superclasses. This means for instance if you implement Gtk2::Buildable on a new widget subclass then you lose the <accelerator> and <accessibility> tags normally available from Gtk2::Widget. This will likely change in the future, probably by chaining up by default for unhandled tags, maybe with a way to ask deliberately not to chain.
name
property. If you don't
implement this method, the name will be attached in object data down in C
code. Implement this method if your object has some notion of "name" and
it makes sense to map the XML name attribute to that.
SET_NAME
, you need to implement this method to retrieve
that name.
ADD_CHILD
will be called to add $child to $self. $type can be
used to determine the kind of child. For example, Gtk2::Container implements
this method to add a child widget to the container, and Gtk2::Notebook uses
$type to distinguish between "page-label" and normal children. The value
of $type comes directly from the type
attribute of the XML child
tag.
property
XML tag. It is not normally necessary to implement this
method, as the fallback simply calls Glib::Object::set()
. Gtk2::Window
implements this method to delay showing itself (i.e., setting the "visible"
property) until the whole interface is created. You can also use this to
handle properties that are not wired up through the Glib::Object property
system (though simply creating the property is easier).
CUSTOM_TAG_START
to give your code a chance
to do something with it. If $tagname was encountered inside a child
tag, the corresponding object will be passed in $child; otherwise,
$child will be undef
.
Your CUSTOM_TAG_START
method should decide whether it supports $tagname.
If not, return undef
. If you do support it, return a blessed perl object
that implements three special methods to be used to parse that tag. (These
methods are defined by GLib's GMarkupParser, which is a simple SAX-style
setup.)
CUSTOM_TAG_END
and
CUSTOM_FINISHED
, so you shouldn't have to worry about its lifetime.
$builder (Gtk2::Builder)
$child (Glib::Object or undef)
$tagname (string)
$parser (some perl object) as returned from CUSTOM_TAG_START
CUSTOM_TAG_START
. $child is the same object-or-undef as passed to
CUSTOM_TAG_START
.
$builder (Gtk2::Builder)
$child (Glib::Object or undef)
$tagname (string)
$parser (some perl object) as returned from CUSTOM_TAG_START
CUSTOM_TAG_START
. $child is the same object-or-undef as passed
to CUSTOM_TAG_START
.
Gtk2, Glib::Interface, http://library.gnome.org/devel/gtk/unstable/GtkBuilder.html#BUILDER-UI, Gtk2::Buildable::ParseContext
Copyright (C) 2003-2008 by the gtk2-perl team.
This software is licensed under the LGPL. See Gtk2 for a full notice.