Gtk2::Buildable(3)

NAME
Gtk2::Buildable - Interface for objects that can be built by Gtk2::Builder

SYNOPSIS
  package Thing;
  use Gtk2;
  use Glib::Object::Subclass
      Glib::Object::,

      # 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.

HIERARCHY
  Glib::Interface
  +----Gtk2::Buildable

DESCRIPTION
The Gtk2::Buildable interface allows objects and widgets to have "<child>" objects, special property settings, or extra custom tags in a Gtk2::Builder UI description (http://library.gnome.org/devel/gtk/unstable/GtkBuilder.html#BUILDER-UI <http://library.gnome.org/devel/gtk/unstable/GtkBuilder.html#BUILDER-UI>).

The main user of the Gtk2::Buildable interface is Gtk2::Builder, so there should be very little need for applications to call any of the Gtk2::Buildable methods. So this documentation deals with implementing a buildable object.

Gtk2::Builder already supports plain Glib::Object or Gtk2::Widget with "<object>" construction and "<property>" settings, so often the "Gtk2::Buildable" interface is not needed. The only thing to note is that an object or widget implemented in Perl must be loaded before building.

OVERRIDING BUILDABLE INTERFACE METHODS
The buildable interface can be added to a Perl code object or widget subclass by putting "Gtk2::Buildable" in the interfaces list and implementing the following methods.

In current Gtk2-Perl 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.

SET_NAME ($self, $name)
    •   $name (string)

    This method should store $name in $self somehow. For example, Gtk2::Widget maps this to the Gtk2::Widget's "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.

string = GET_NAME ($self)
    If you implement "SET_NAME", you need to implement this method to retrieve that name.

ADD_CHILD ($self, $builder, $child, $type)
    •   $builder (Gtk2::Builder)

    •   $child (Glib::Object or undef)

    •   $type (string)

    "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.

SET_BUILDABLE_PROPERTY ($self, $builder, $name, $value)
    •   $builder (Gtk2::Builder)

    •   $name (string)

    •   $value (scalar)

    This will be called to set the object property $name on $self, directly from the "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).

parser or undef = CUSTOM_TAG_START ($self, $builder, $child, $tagname)
    •   $builder (Gtk2::Builder)

    •   $child (Glib::Object or undef)

    •   $tagname (string)

    When Gtk2::Builder encounters an unknown tag while parsing the definition of $self, it will call "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.)

    START_ELEMENT ($self, $context, $element_name, $attributes)
        •   $context (Gtk2::Buildable::ParseContext)

        •   $element_name (string)

        •   $attributes (hash reference) Dictionary of all attributes of this tag.

    TEXT ($self, $context, $text)
        •   $context (Gtk2::Buildable::ParseContext)

        •   $text (string) The text contained in the tag.

    END_ELEMENT ($self, $context, $element_name)
        •   $context (Gtk2::Buildable::ParseContext)

        •   $element_name (string)

    Any blessed perl object that implements these methods is valid as a parser. (Ain't duck-typing great?) Gtk2::Builder will hang on to this object until the parsing is complete, and will pass it to "CUSTOM_TAG_END" and "CUSTOM_FINISHED", so you shouldn't have to worry about its lifetime.

CUSTOM_TAG_END ($self, $builder, $child, $tagname, $parser)
    •   $builder (Gtk2::Builder)

    •   $child (Glib::Object or undef)

    •   $tagname (string)

    •   $parser (some perl object) as returned from "CUSTOM_TAG_START"

    This method will be called (if it exists) when the close tag for $tagname is encountered. $parser will be the object you returned from "CUSTOM_TAG_START". $child is the same object-or-undef as passed to "CUSTOM_TAG_START".

CUSTOM_FINISHED ($self, $builder, $child, $tagname, $parser)
    •   $builder (Gtk2::Builder)

    •   $child (Glib::Object or undef)

    •   $tagname (string)

    •   $parser (some perl object) as returned from "CUSTOM_TAG_START"

    This method will be called (if it exists) when the parser finishes dealing with the custom tag $tagname. $parser will be the object you returned from "CUSTOM_TAG_START". $child is the same object-or-undef as passed to "CUSTOM_TAG_START".

PARSER_FINISHED ($self, $builder)
    •   $builder (Gtk2::Builder)

    If this method exists, it will be invoked when the builder finishes parsing the description data. This method is handy if you need to defer any object initialization until all of the rest of the input is parsed, most likely because you need to refer to an object that is declared after $self or you need to perform special cleanup actions. It is not normally necessary to implement this method.

object or undef = GET_INTERNAL_CHILD ($self, $builder, $childname)
    •   $builder (Gtk2::Builder)

    •   $childname (string)

    This will be called to fetch an internal child of $self. Implement this method if your buildable has internal children that need to be accessed from a UI definition. For example, Gtk2::Dialog implements this to give access to its internal vbox child.

    If $childname is unknown then return "undef". (The builder will then generally report a GError for the UI description referring to an unknown child.)

SEE ALSO
Gtk2, Glib::Interface, http://library.gnome.org/devel/gtk/unstable/GtkBuilder.html#BUILDER-UI <http://library.gnome.org/devel/gtk/unstable/GtkBuilder.html#BUILDER-UI>, Gtk2::Buildable::ParseContext

COPYRIGHT
Copyright (C) 2003-2011 by the gtk2-perl team.

This software is licensed under the LGPL. See Gtk2 for a full notice.