About all my projects
Gnome::Gtk3::Builder

Gnome::Gtk3::Builder

Build an interface from an XML UI definition

Description

A Gnome::Gtk3::Builder is an auxiliary object that reads textual descriptions of a user interface and instantiates the described objects. To create a Gnome::Gtk3::Builder from a user interface description, call .new(:file), .new(:resource) or .new(:string).

In the (unusual) case that you want to add user interface descriptions from multiple sources to the same Gnome::Gtk3::Builder you can call .new() to get an empty builder and populate it by (multiple) calls to gtk_builder_add_from_file(), gtk_builder_add_from_resource() or gtk_builder_add_from_string().

A Gnome::Gtk3::Builder holds a reference to all objects that it has constructed and drops these references when it is finalized. This finalization can cause the destruction of non-widget objects or widgets which are not contained in a toplevel window. For toplevel windows constructed by a builder, it is the responsibility of the user to call gtk_widget_destroy() to get rid of them and all the widgets they contain.

The function gtk_builder_get_object() can be used to access the widgets in the interface by the names assigned to them (id's) inside the UI description. Toplevel windows returned by these functions will stay around until the user explicitly destroys them with gtk_widget_destroy(). Other widgets will either be part of a larger hierarchy constructed by the builder (in which case you should not have to worry about their lifecycle), or without a parent, in which case they have to be added to some container to make use of them. All widget classes have the ability to be initialized using the named argument .new(:build-id). This will end up using gtk_builder_get_object(). A builder must be created first and data fed to the builder before you are able to use it.

The function gtk_builder_connect_signals_full() and variants thereof can be used to connect handlers to the named signals defined in a handler table. The signals can also be handled individualy using .register-signal().

Gnome::Gtk3::Builder UI Definitions

Gnome::Gtk3::Builder parses textual descriptions of user interfaces which are specified in an XML format which can be roughly described by the RELAX NG schema below. We refer to these descriptions as “GtkBuilder UI definitions” or just “UI definitions” if the context is clear.

It is common to use `.ui` as the filename extension for files containing Gnome::Gtk3::Builder UI definitions.

<!--[RELAX NG Compact Syntax](https://git.gnome.org/browse/gtk+/tree/gtk/gtkbuilder.rnc)-->

The toplevel element is <interface>. It optionally takes a “domain” attribute, which will make the builder look for translated strings using dgettext() in the domain specified. This can also be done by calling gtk_builder_set_translation_domain() on the builder. Objects are described by <object> elements, which can contain <property> elements to set properties, <signal> elements which connect signals to handlers, and <child> elements, which describe child objects (most often widgets inside a container, but also e.g. actions in an action group, or columns in a tree model). A <child> element contains an <object> element which describes the child object. The target toolkit version(s) are described by <requires> elements, the “lib” attribute specifies the widget library in question (currently the only supported value is “gtk+”) and the “version” attribute specifies the target version in the form “<major>.<minor>”. The builder will error out if the version requirements are not met.

Typically, the specific kind of object represented by an <object> element is specified by the “class” attribute. If the type has not been loaded yet, GTK+ tries to find the get_type() function from the class name by applying heuristics. This works in most cases, but if necessary, it is possible to specify the name of the get_type() function explictly with the "type-func" attribute. As a special case, Gnome::Gtk3::Builder allows to use an object that has been constructed by a GtkUIManager in another part of the UI definition by specifying the id of the GtkUIManager in the “constructor” attribute and the name of the object in the “id” attribute.

Objects may be given a name with the “id” attribute, which allows the application to retrieve them from the builder with gtk_builder_get_object() which is also used indirectly when a widget is created using `.new(:$build-id)`. An id is also necessary to use the object as property value in other parts of the UI definition. GTK+ reserves ids starting and ending with ___ (3 underscores) for its own purposes.

Setting properties of objects is pretty straightforward with the <property> element: the “name” attribute specifies the name of the property, and the content of the element specifies the value. If the “translatable” attribute is set to a true value, GTK+ uses gettext() (or dgettext() if the builder has a translation domain set) to find a translation for the value. This happens before the value is parsed, so it can be used for properties of any type, but it is probably most useful for string properties. It is also possible to specify a context to disambiguate short strings, and comments which may help the translators.

Gnome::Gtk3::Builder can parse textual representations for the most common property types: characters, strings, integers, floating-point numbers, booleans (strings like “TRUE”, “t”, “yes”, “y”, “1” are interpreted as 1, strings like “FALSE”, “f”, “no”, “n”, “0” are interpreted as 0), enumerations (can be specified by their name, nick or integer value), flags (can be specified by their name, nick, integer value, optionally combined with “|”, e.g. “GTK_VISIBLE|GTK_REALIZED”) and colors (in a format understood by gdk_rgba_parse()).

Objects can be referred to by their name and by default refer to objects declared in the local xml fragment and objects exposed via expose_object(). In general, Gnome::Gtk3::Builder allows forward references to objects — declared in the local xml; an object doesn’t have to be constructed before it can be referred to. The exception to this rule is that an object has to be constructed before it can be used as the value of a construct-only property.

Signal handlers are set up with the <signal> element. The “name” attribute specifies the name of the signal, and the “handler” attribute specifies the function to connect to the signal. The remaining attributes, “after” and “swapped” attributes are ignored by the Raku modules. The "object" field has a meaning in Gnome::Gtk3::Glade.

Sometimes it is necessary to refer to widgets which have implicitly been constructed by GTK+ as part of a composite widget, to set properties on them or to add further children (e.g. the vbox of a Gnome::Gtk3::Dialog). This can be achieved by setting the “internal-child” propery of the <child> element to a true value. Note that Gnome::Gtk3::Builder still requires an <object> element for the internal child, even if it has already been constructed.

A number of widgets have different places where a child can be added (e.g. tabs vs. page content in notebooks). This can be reflected in a UI definition by specifying the “type” attribute on a <child>. The possible values for the “type” attribute are described in the sections describing the widget-specific portions of UI definitions.

A Gnome::Gtk3::Builder UI Definition

Note the class names are e.g. GtkDialog, not Gnome::Gtk3::Dialog. This is because those are the c-source class names of the GTK+ objects.

<interface>
  <object class="GtkDialog>" id="dialog1">
    <child internal-child="vbox">
      <object class="GtkBox>" id="vbox1">
        <property name="border-width">10</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox>" id="hbuttonbox1">
            <property name="border-width">20</property>
            <child>
              <object class="GtkButton>" id="ok_button">
                <property name="label">gtk-ok</property>
                <property name="use-stock">TRUE</property>
                <signal name="clicked" handler="ok_button_clicked"/>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

To load it and use it do the following (assume above text is in $gui).

my Gnome::Gtk3::Builder $builder .= new(:string($gui));
my Gnome::Gtk3::Button $button .= new(:build-id<ok_button>));

Synopsis

Declaration

unit class Gnome::Gtk3::Builder;
also is Gnome::GObject::Object;

Uml Diagram

No caption

Example

my Gnome::Gtk3::Builder $builder .= new;
my Gnome::Glib::Error $e = $builder.add-from-file($ui-file);
die $e.message if $e.is-valid;

my Gnome::Gtk3::Button .= new(:build-id<my-glade-button-id>);

Types

enum GtkBuilderError

Error codes that identify various errors that can occur while using Gnome::Gtk3::Builder.

  • GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION: A type-func attribute didn’t name a function that returns a GType.

  • GTK_BUILDER_ERROR_UNHANDLED_TAG: The input contained a tag that Gnome::Gtk3::Builder can’t handle.

  • GTK_BUILDER_ERROR_MISSING_ATTRIBUTE: An attribute that is required by Gnome::Gtk3::Builder was missing.

  • GTK_BUILDER_ERROR_INVALID_ATTRIBUTE: Gnome::Gtk3::Builder found an attribute that it doesn’t understand.

  • GTK_BUILDER_ERROR_INVALID_TAG: Gnome::Gtk3::Builder found a tag that it doesn’t understand.

  • GTK_BUILDER_ERROR_MISSING_PROPERTY_VALUE: A required property value was missing.

  • GTK_BUILDER_ERROR_INVALID_VALUE: Gnome::Gtk3::Builder couldn’t parse some attribute value.

  • GTK_BUILDER_ERROR_VERSION_MISMATCH: The input file requires a newer version of GTK+.

  • GTK_BUILDER_ERROR_DUPLICATE_ID: An object id occurred twice.

  • GTK_BUILDER_ERROR_OBJECT_TYPE_REFUSED: A specified object type is of the same type or derived from the type of the composite class being extended with builder XML.

  • GTK_BUILDER_ERROR_TEMPLATE_MISMATCH: The wrong type was specified in a composite class’s template XML

  • GTK_BUILDER_ERROR_INVALID_PROPERTY: The specified property is unknown for the object class.

  • GTK_BUILDER_ERROR_INVALID_SIGNAL: The specified signal is unknown for the object class.

  • GTK_BUILDER_ERROR_INVALID_ID: An object id is unknown

Methods

new

default, no options

Create an empty builder. This is only useful if you intend to make multiple calls to add-from-file(), add-from-resource() or add-from-string() in order to merge multiple UI descriptions into a single builder.

Most users will probably want to use .new(:file), .new(:resource) or .new(:string), particularly when there is only one file, resource or string.

multi method new ( )

:file

Create builder object and load gui design. Builds the UI definition from a file. If there is an error opening the file or parsing the description then the program will be aborted. You should only ever attempt to parse user interface descriptions that are shipped as part of your program.

multi method new ( Str :$file! )

:string

Same as above but read the design from the string. Builds the user interface described by $string (in the UI definition format). If there is an error parsing string then the program will be aborted. You should not attempt to parse user interface description from untrusted sources.

multi method new ( Str :$string! )

:resource

The interface is build using the UI definition from the given resource path. If there is an error locating the resource or parsing the description, then the program will be aborted.

multi method new ( Str :$resource! )

:native-object

Create a Builder object using a native object from elsewhere. See also Gnome::N::TopLevelClassSupport.

multi method new ( N-GObject :$native-object! )

add-callback-symbol

Adds the callback-symbol to the scope of builder under the given callback-name.

Using this function overrides the behavior of connect-signals() for any callback symbols that are added. Using this method allows for better encapsulation as it does not require that callback symbols be declared in the global namespace.

method add-callback-symbol ( Str $callback_name, GCallback $callback_symbol )
  • $callback_name; The name of the callback, as expected in the XML

  • $callback_symbol; (scope async): The callback pointer

add-callback-symbols

A convenience function to add many callbacks instead of calling add-callback-symbol() for each symbol.

method add-callback-symbols ( Str $first_callback_name, GCallback $first_callback_symbol )
  • $first_callback_name; The name of the callback, as expected in the XML

  • $first_callback_symbol; (scope async): The callback pointer @...: A list of callback name and callback symbol pairs terminated with undefined

add-from-file

Parses a file containing a [GtkBuilder UI definition][BUILDER-UI] and merges it with the current contents of builder.

Most users will probably want to use new-from-file().

If an error occurs, 0 will be returned and error will be assigned a Gnome::Gtk3::Error from the Gnome::Gtk3::TK-BUILDER-ERROR, Gnome::Gtk3::-MARKUP-ERROR or Gnome::Gtk3::-FILE-ERROR domain.

It’s not really reasonable to attempt to handle failures of this call. You should not use this function with untrusted files (ie: files that are not part of your application). Broken Gnome::Gtk3::Builder files can easily crash your program, and it’s possible that memory was leaked leading up to the reported failure.

Returns: An invalid error object on success, Otherwise call .message() on the error object to find out what went wrong.

method add-from-file ( Str $filename --> Gnome::Glib::Error )
  • $filename; the name of the file to parse

add-from-resource

Parses a resource file containing a [GtkBuilder UI definition][BUILDER-UI] and merges it with the current contents of builder.

Most users will probably want to use new-from-resource().

Returns: An invalid error object on success, Otherwise call .message() on the error object to find out what went wrong.

method add-from-resource ( Str $resource_path --> Gnome::Glib::Error )
  • $resource_path; the path of the resource file to parse

add-from-string

Parses a string containing a [GtkBuilder UI definition][BUILDER-UI] and merges it with the current contents of builder.

Most users will probably want to use new-from-string().

Upon errors 0 will be returned and error will be assigned a Gnome::Gtk3::Error from the Gnome::Gtk3::TK-BUILDER-ERROR, Gnome::Gtk3::-MARKUP-ERROR or Gnome::Gtk3::-VARIANT-PARSE-ERROR domain.

It’s not really reasonable to attempt to handle failures of this call. The only reasonable thing to do when an error is detected is to call g-error().

Returns: An invalid error object on success, Otherwise call .message() on the error object to find out what went wrong.

method add-from-string ( Str $buffer --> Gnome::Glib::Error )
  • $buffer; the string to parse

add-objects-from-file

Parses a file containing a [GtkBuilder UI definition][BUILDER-UI] building only the requested objects and merges them with the current contents of builder.

Upon errors 0 will be returned and error will be assigned a Gnome::Gtk3::Error from the Gnome::Gtk3::TK-BUILDER-ERROR, Gnome::Gtk3::-MARKUP-ERROR or Gnome::Gtk3::-FILE-ERROR domain.

If you are adding an object that depends on an object that is not its child (for instance a Gnome::Gtk3::TreeView that depends on its Gnome::Gtk3::TreeModel), you have to explicitly list all of them in object-ids.

Returns: A positive value on success, 0 if an error occurred

method add-objects-from-file (
  Str $filename, CArray[Str] $object_ids, N-GError $error --> UInt )
  • $filename; the name of the file to parse

  • $object_ids; (element-type utf8): nul-terminated array of objects to build

  • $error; return location for an error, or undefined

add-objects-from-resource

Parses a resource file containing a [GtkBuilder UI definition][BUILDER-UI] building only the requested objects and merges them with the current contents of builder.

Upon errors 0 will be returned and error will be assigned a Gnome::Gtk3::Error from the Gnome::Gtk3::TK-BUILDER-ERROR, Gnome::Gtk3::-MARKUP-ERROR or Gnome::Gtk3::-RESOURCE-ERROR domain.

If you are adding an object that depends on an object that is not its child (for instance a Gnome::Gtk3::TreeView that depends on its Gnome::Gtk3::TreeModel), you have to explicitly list all of them in object-ids.

Returns: A positive value on success, 0 if an error occurred

method add-objects-from-resource (
  Str $resource_path, CArray[Str] $object_ids, N-GError $error
  --> UInt
)
  • $resource_path; the path of the resource file to parse

  • $object_ids; (element-type utf8): nul-terminated array of objects to build

  • $error; return location for an error, or undefined

add-objects-from-string

Parses a string containing a [GtkBuilder UI definition][BUILDER-UI] building only the requested objects and merges them with the current contents of builder.

Upon errors 0 will be returned and error will be assigned a Gnome::Gtk3::Error from the Gnome::Gtk3::TK-BUILDER-ERROR or Gnome::Gtk3::-MARKUP-ERROR domain.

If you are adding an object that depends on an object that is not its child (for instance a Gnome::Gtk3::TreeView that depends on its Gnome::Gtk3::TreeModel), you have to explicitly list all of them in object-ids.

Returns: A positive value on success, 0 if an error occurred

method add-objects-from-string ( Str $buffer, UInt $length, CArray[Str] $object_ids, N-GError $error --> UInt )
  • $buffer; the string to parse

  • $length; the length of buffer (may be -1 if buffer is nul-terminated)

  • $object_ids; (element-type utf8): nul-terminated array of objects to build

  • $error; return location for an error, or undefined

connect-signals

This method is a simpler variation of connect-signals-full(). It uses symbols explicitly added to builder with prior calls to gtk-builder-add-callback-symbol(). In the case that symbols are not explicitly added; it uses Gnome::Gtk3::Module’s introspective features (by opening the module undefined) to look at the application’s symbol table. From here it tries to match the signal handler names given in the interface description with symbols in the application and connects the signals. Note that this function can only be called once, subsequent calls will do nothing.

Note that unless gtk-builder-add-callback-symbol() is called for all signal callbacks which are referenced by the loaded XML, this function will require that Gnome::Gtk3::Module be supported on the platform.

If you rely on Gnome::Gtk3::Module support to lookup callbacks in the symbol table, the following details should be noted:

When compiling applications for Windows, you must declare signal callbacks with Gnome::Gtk3::-MODULE-EXPORT, or they will not be put in the symbol table. On Linux and Unices, this is not necessary; applications should instead be compiled with the -Wl,--export-dynamic CFLAGS, and linked against gmodule-export-2.0.

method connect-signals ( Pointer $user_data )
  • $user_data; user data to pass back with all signals

connect-signals-full

This method will process the signal elements from the loaded XML and with the help of the provided $handlers table register each handler to a signal.

method gtk_builder_connect_signals_full ( Hash $handlers )
  • $handlers; a table used to register handlers to process a signal. Each entry in this table has a key which is the name of the handler method. The value is a list of which the first element is the object wherin the method is defined. The rest of the list are optional named attributes and are provided to the method. See also register-signal() in Gnome::GObject::Object.

An example where a gui is described in XML. It has a Window with a Button, both having a signal description;

my Str $ui = q:to/EOUI/;
    <?xml version="1.0" encoding="UTF-8"?>
    <interface>
      <requires lib="gtk+" version="3.20"/>

      <object class="GtkWindow" id="top">
        <property name="title">top window</property>
        <signal name="destroy" handler="window-quit"/>
        <child>
          <object class="GtkButton" id="help">
            <property name="label">Help</property>
            <signal name="clicked" handler="button-click"/>
          </object>
        </child>
      </object>
    </interface>
    EOUI

# First handler class
class X {
  method window-quit ( :$o1, :$o2 ) {
    # ... do something with options $o1 and $o2 ...

    Gnome::Gtk3::Main.new.gtk-main-quit;
  }
}

# Second handler class
class Y {
  method button-click ( :$o3, :$o4 ) {
    # ... do something with options $o3 and $o4 ...
  }
}

# Load the user interface description
my Gnome::Gtk3::Builder $builder .= new(:string($ui));
my Gnome::Gtk3::Window $w .= new(:build-id<top>);

# It is possible to devide the works over more than one class
my X $x .= new;
my Y $y .= new;

# Create the handlers table
my Hash $handlers = %(
  :window-quit( $x, :o1<o1>, :o2<o2>),
  :button-click( $y, :o3<o3>, :o4<o4>)
);

# Register all signals
$builder.connect-signals-full($handlers);

error-quark

Return the domain code of the builder error domain.

method error-quark ( --> UInt )

The following example shows the fields of a returned error when a faulty string is provided in the call.

my Gnome::Glib::Quark $quark .= new;
my Gnome::Glib::Error $e = $builder.add-from-string($text);
is $e.domain, $builder.gtk_builder_error_quark(),
   "domain code: $e.domain()";
is $quark.to-string($e.domain), 'gtk-builder-error-quark',
   "error domain: $quark.to-string($e.domain())";

expose-object

Add object to the builder object pool so it can be referenced just like any other object built by builder.

method expose-object ( Str $name, N-GObject() $object )
  • $name; the name of the object exposed to the builder

  • $object; the object to expose

extend-with-template

Main private entry point for building composite container components from template XML.

This is exported purely to let gtk-builder-tool validate templates, applications have no need to call this function.

Returns: A positive value on success, 0 if an error occurred

method extend-with-template (
  N-GObject() $widget, N-GObject() $template_type,
  Str $buffer, UInt $length, N-GError $error
  --> UInt
)
  • $widget; the widget that is being extended

  • $template_type; the type that the template is for

  • $buffer; the string to parse

  • $length; the length of buffer (may be -1 if buffer is nul-terminated)

  • $error; return location for an error, or undefined

get-application

Gets the Gnome::Gtk3::Application associated with the builder.

The Gnome::Gtk3::Application is used for creating action proxies as requested from XML that the builder is loading.

By default, the builder uses the default application: the one from g-application-get-default(). If you want to use another application for constructing proxies, use set-application().

Returns: the application being used by the builder, or undefined

method get-application ( --> N-GObject )

get-object

Gets the object named $name. Note that this function does not increment the reference count of the returned object.

Returns: the object named $name or undefined if it could not be found in the object tree.

method get-object ( Str $name --> N-GObject )
  • $name; name of object to get

get-objects

Gets all objects that have been constructed by builder. Note that this function does not increment the reference counts of the returned objects.

Returns: (element-type GObject) (transfer container): a newly-allocated Gnome::Gtk3::SList containing all the objects constructed by the Gnome::Gtk3::Builder instance. It should be freed by g-slist-free()

method get-objects ( --> Gnome::Glib::SList )

get-translation-domain

Gets the translation domain of builder.

Returns: the translation domain. This string is owned by the builder object and must not be modified or freed.

method get-translation-domain ( --> Str )

get-type-from-name

Looks up a type by name, using the virtual function that Gnome::Gtk3::Builder has for that purpose. This is mainly used when implementing the Gnome::Gtk3::Buildable interface on a type.

Returns: the Gnome::Gtk3::Type found for $type-name or Gnome::Gtk3::-TYPE-INVALID if no type was found

method get-type-from-name ( Str $type-name --> UInt )
  • $type_name; type name to lookup

lookup-callback-symbol

Fetches a symbol previously added to builder with add-callback-symbols()

This function is intended for possible use in language bindings or for any case that one might be cusomizing signal connections using gtk-builder-connect-signals-full()

Returns: The callback symbol in builder for callback-name, or undefined

method lookup-callback-symbol ( Str $callback_name --> GCallback )
  • $callback_name; The name of the callback

set-application

Sets the application associated with builder.

You only need this function if there is more than one Gnome::Gtk3::Application in your process. $application cannot be undefined.

method set-application ( N-GObject() $application )
  • $application; a Gnome::Gtk3::Application

set-translation-domain

Sets the translation domain of builder. See translation-domain.

method set-translation-domain ( Str $domain )
  • $domain; the translation domain or undefined

value-from-string

This function demarshals a value from a string. This function calls g-value-init() on the value argument, so it need not be initialised beforehand.

This function can handle char, uchar, boolean, int, uint, long, ulong, enum, flags, float, double, string, Gnome::Gtk3::Color, Gnome::Gtk3::RGBA and Gnome::Gtk3::Adjustment type values. Support for Gnome::Gtk3::Widget type values is still to come.

Upon errors False will be returned and error will be assigned a Gnome::Gtk3::Error from the Gnome::Gtk3::TK-BUILDER-ERROR domain.

Returns: True on success

method value-from-string ( GParamSpec $pspec, Str $string, N-GObject $value, N-GError $error --> Bool )
  • $pspec; the Gnome::Gtk3::ParamSpec for the property

  • $string; the string representation of the value

  • $value; the Gnome::Gtk3::Value to store the result in

  • $error; return location for an error, or undefined

value-from-string-type

Like value-from-string(), this function demarshals a value from a string, but takes a Gnome::Gtk3::Type instead of Gnome::Gtk3::ParamSpec. This function calls g-value-init() on the value argument, so it need not be initialised beforehand.

Upon errors False will be returned and error will be assigned a Gnome::Gtk3::Error from the Gnome::Gtk3::TK-BUILDER-ERROR domain.

Returns: True on success

method value-from-string-type ( N-GObject $type, Str $string, N-GObject $value, N-GError $error --> Bool )
  • $type; the Gnome::Gtk3::Type of the value

  • $string; the string representation of the value

  • $value; the Gnome::Gtk3::Value to store the result in

  • $error; return location for an error, or undefined

_gtk_builder_new

Creates a new empty builder object.

This function is only useful if you intend to make multiple calls to add-from-file(), gtk-builder-add-from-resource() or gtk-builder-add-from-string() in order to merge multiple UI descriptions into a single builder.

Most users will probably want to use gtk-builder-new-from-file(), gtk-builder-new-from-resource() or gtk-builder-new-from-string().

Returns: a new (empty) Gnome::Gtk3::Builder object

method _gtk_builder_new ( --> N-GObject )

_gtk_builder_new_from_file

Builds the [GtkBuilder UI definition][BUILDER-UI] in the file filename.

If there is an error opening the file or parsing the description then the program will be aborted. You should only ever attempt to parse user interface descriptions that are shipped as part of your program.

Returns: a Gnome::Gtk3::Builder containing the described interface

method _gtk_builder_new_from_file ( Str $filename --> N-GObject )
  • $filename; filename of user interface description file

_gtk_builder_new_from_resource

Builds the [GtkBuilder UI definition][BUILDER-UI] at resource-path.

If there is an error locating the resource or parsing the description, then the program will be aborted.

Returns: a Gnome::Gtk3::Builder containing the described interface

method _gtk_builder_new_from_resource ( Str $resource_path --> N-GObject )
  • $resource_path; a Gnome::Gtk3::Resource resource path

_gtk_builder_new_from_string

Builds the user interface described by string (in the [GtkBuilder UI definition][BUILDER-UI] format).

If string is undefined-terminated, then length should be -1. If length is not -1, then it is the length of string.

If there is an error parsing string then the program will be aborted. You should not attempt to parse user interface description from untrusted sources.

Returns: a Gnome::Gtk3::Builder containing the interface described by string

method _gtk_builder_new_from_string ( Str $string, Int() $length --> N-GObject )
  • $string; a user interface (XML) description

  • $length; the length of string, or -1

Properties

translation-domain

The translation domain used by gettext

The Gnome::GObject::Value type of property translation-domain is G_TYPE_STRING.

  • Parameter is readable and writable.

  • Default value is undefined.

Properties

An example of using a string type property of a Gnome::Gtk3::Label object. This is just showing how to set/read a property, not that it is the best way to do it. This is because a) The class initialization often provides some options to set some of the properties and b) the classes provide many methods to modify just those properties. In the case below one can use new(:label('my text label')) or .set-text('my text label').

my Gnome::Gtk3::Label $label .= new;
my Gnome::GObject::Value $gv .= new(:init(G_TYPE_STRING));
$label.get-property( 'label', $gv);
$gv.set-string('my text label');

Supported properties

Translation Domain: translation-domain

The translation domain used when translating property values that have been marked as translatable in interface descriptions. If the translation domain is undefined, Gnome::Gtk3::Builder uses gettext(), otherwise g-dgettext().

The Gnome::GObject::Value type of property translation-domain is G_TYPE_STRING.

[[gtk_] builder_] error_quark

Return the domain code of the builder error domain.

method gtk_builder_error_quark ( --> Int )

The following example shows the fields of a returned error when a faulty string is provided in the call.

my Gnome::Glib::Quark $quark .= new;
my Gnome::Glib::Error $e = $builder.add-from-string($text);
is $e.domain, $builder.gtk_builder_error_quark(),
   "domain code: $e.domain()";
is $quark.to-string($e.domain), 'gtk-builder-error-quark',
   "error domain: $quark.to-string($e.domain())";

[gtk_] builder_new

Creates a new empty builder object.

This function is only useful if you intend to make multiple calls to gtk_builder_add_from_file(), gtk_builder_add_from_resource() or gtk_builder_add_from_string() in order to merge multiple UI descriptions into a single builder.

Most users will probably want to use gtk_builder_new_from_file(), gtk_builder_new_from_resource() or gtk_builder_new_from_string().

Returns: a new (empty) Gnome::Gtk3::Builder object

method gtk_builder_new ( --> N-GObject  )

[[gtk_] builder_] add_from_file

Parses a file containing a UI definition and merges it with the current contents of builder.

Most users will probably want to use .new(:file).

If an error occurs, a valid Gnome::Glib::Error object is returned with an error domain of GTK_BUILDER_ERROR, G_MARKUP_ERROR or G_FILE_ERROR.

You should not use this function with untrusted files (ie: files that are not part of your application). Broken Gnome::Gtk3::Builder files can easily crash your program, and it’s possible that memory was leaked leading up to the reported failure. The only reasonable thing to do when an error is detected is to throw an Exception when necessary.

Returns: Gnome::Glib::Error. Test .is-valid() of that object to see if there was an error.

method gtk_builder_add_from_file (
  Str $filename, N-GObject $error
  --> Gnome::Glib::Error
)
  • $filename; the name of the file to parse

[[gtk_] builder_] add_from_resource

Parses a resource file containing a UI definition and merges it with the current contents of builder.

Most users will probably want to use .new(:resource).

If an error occurs, a valid Gnome::Glib::Error object is returned with an error domain of GTK_BUILDER_ERROR, G_MARKUP_ERROR or G_FILE_ERROR. The only reasonable thing to do when an error is detected is to throw an Exception when necessary.

Returns: Gnome::Glib::Error. Test .is-valid() to see if there was an error.

method gtk_builder_add_from_resource (
  Str $resource_path
  --> Gnome::Glib::Error
)
  • $resource_path; the path of the resource file to parse

  • $error; (allow-none): return location for an error, or Any

[[gtk_] builder_] add_from_string

Parses a string containing a UI definition and merges it with the current contents of builder.

Most users will probably want to use .new(:string).

If an error occurs, a valid Gnome::Glib::Error object is returned with an error domain of GTK_BUILDER_ERROR, G_MARKUP_ERROR or G_FILE_ERROR. The only reasonable thing to do when an error is detected is to throw an Exception when necessary.

Returns: Gnome::Glib::Error. Test .is-valid() to see if there was an error.

method gtk_builder_add_from_string ( Str $buffer --> N-GObject $error )
  • $buffer; the string to parse

[[gtk_] builder_] add_objects_from_file

Parses a file containing a [Gnome::Gtk3::Builder UI definition] building only the requested objects and merges them with the current contents of builder.

If you are adding an object that depends on an object that is not its child (for instance a Gnome::Gtk3::TreeView that depends on its Gnome::Gtk3::TreeModel), you have to explicitly list all of them in object_ids.

If an error occurs, a valid Gnome::Glib::Error object is returned with an error domain of GTK_BUILDER_ERROR, G_MARKUP_ERROR or G_FILE_ERROR. The only reasonable thing to do when an error is detected is to throw an Exception when necessary.

Returns: Gnome::Glib::Error. Test .is-valid() flag to see if there was an error.

method gtk_builder_add_objects_from_file ( Str $filename, CArray[Str] $object_ids, N-GObject $error --> UInt  )
  • $filename; the name of the file to parse

  • $object_ids; (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build

  • $error; (allow-none): return location for an error, or Any

[[gtk_] builder_] add_objects_from_resource

Parses a resource file containing a [Gnome::Gtk3::Builder UI definition](https://developer.gnome.org/gtk3/3.24/GtkBuilder.html#BUILDER-UI) building only the requested objects and merges them with the current contents of builder.

Upon errors 0 will be returned and error will be assigned a GError from the GTK_BUILDER_ERROR, G_MARKUP_ERROR or G_RESOURCE_ERROR domain.

If you are adding an object that depends on an object that is not its child (for instance a Gnome::Gtk3::TreeView that depends on its Gnome::Gtk3::TreeModel), you have to explicitly list all of them in object_ids.

Returns: A positive value on success, 0 if an error occurred

method gtk_builder_add_objects_from_resource ( Str $resource_path, CArray[Str] $object_ids, N-GObject $error --> UInt  )
  • $resource_path; the path of the resource file to parse

  • $object_ids; (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build

  • $error; (allow-none): return location for an error, or Any

[[gtk_] builder_] add_objects_from_string

Parses a string containing a [Gnome::Gtk3::Builder UI definition](https://developer.gnome.org/gtk3/3.24/GtkBuilder.html#BUILDER-UI) building only the requested objects and merges them with the current contents of builder.

Upon errors 0 will be returned and error will be assigned a GError from the GTK_BUILDER_ERROR or G_MARKUP_ERROR domain.

If you are adding an object that depends on an object that is not its child (for instance a Gnome::Gtk3::TreeView that depends on its Gnome::Gtk3::TreeModel), you have to explicitly list all of them in object_ids.

Returns: A positive value on success, 0 if an error occurred

method gtk_builder_add_objects_from_string ( Str $buffer, UInt $length, CArray[Str] $object_ids, N-GObject $error --> UInt  )
  • $buffer; the string to parse

  • $length; the length of buffer (may be -1 if buffer is nul-terminated)

  • $object_ids; (array zero-terminated=1) (element-type utf8): nul-terminated array of objects to build

  • $error; (allow-none): return location for an error, or Any

[[gtk_] builder_] get_object

Gets the object named $name. Note that this function does not increment the reference count of the returned object.

Returns: the object named $name or undefined if it could not be found in the object tree.

method gtk_builder_get_object ( Str $name --> N-GObject  )
  • $name; name of object to get

[[gtk_] builder_] get_objects

Gets all objects that have been constructed by builder. Note that this function does not increment the reference counts of the returned objects.

Returns: (element-type GObject) (transfer container): a newly-allocated GSList containing all the objects constructed by the Gnome::Gtk3::Builder instance. It should be freed by g_slist_free()

method gtk_builder_get_objects ( --> N-GObject  )

[[gtk_] builder_] expose_object

Add object to the builder object pool so it can be referenced just like any other object built by builder.

method gtk_builder_expose_object ( Str $name, N-GObject $object )
  • $name; the name of the object exposed to the builder

  • $object; the object to expose

[[gtk_] builder_] connect_signals

This method is a simpler variation of gtk_builder_connect_signals_full(). It uses symbols explicitly added to builder with prior calls to gtk_builder_add_callback_symbol(). In the case that symbols are not explicitly added; it uses GModule’s introspective features (by opening the module Any) to look at the application’s symbol table. From here it tries to match the signal handler names given in the interface description with symbols in the application and connects the signals. Note that this function can only be called once, subsequent calls will do nothing.

Note that unless gtk_builder_add_callback_symbol() is called for all signal callbacks which are referenced by the loaded XML, this function will require that GModule be supported on the platform.

If you rely on GModule support to lookup callbacks in the symbol table, the following details should be noted:

When compiling applications for Windows, you must declare signal callbacks with G_MODULE_EXPORT, or they will not be put in the symbol table. On Linux and Unices, this is not necessary; applications should instead be compiled with the -Wl,--export-dynamic CFLAGS, and linked against gmodule-export-2.0.

method gtk_builder_connect_signals ( Pointer $user_data )
  • $user_data; user data to pass back with all signals

[[gtk_] builder_] connect_signals_full

This method will process the signal elements from the loaded XML and with the help of the provided $handlers table register each handler to a signal.

method gtk_builder_connect_signals_full ( Hash $handlers )
  • $handlers; a table used to register handlers to process a signal. Each entry in this table has a key which is the name of the handler method. The value is a list of which the first element is the object wherin the method is defined. The rest of the list are optional named attributes and are provided to the method. See also register-signal() in Gnome::GObject::Object.

An example where a gui is described in XML. It has a Window with a Button, both having a signal description;

my Str $ui = q:to/EOUI/;
    <?xml version="1.0" encoding="UTF-8"?>
    <interface>
      <requires lib="gtk+" version="3.20"/>

      <object class="GtkWindow" id="top">
        <property name="title">top window</property>
        <signal name="destroy" handler="window-quit"/>
        <child>
          <object class="GtkButton" id="help">
            <property name="label">Help</property>
            <signal name="clicked" handler="button-click"/>
          </object>
        </child>
      </object>
    </interface>
    EOUI

# First handler class
class X {
  method window-quit ( :$o1, :$o2 ) {
    # ... do something with options $o1 and $o2 ...

    Gnome::Gtk3::Main.new.gtk-main-quit;
  }
}

# Second handler class
class Y {
  method button-click ( :$o3, :$o4 ) {
    # ... do something with options $o3 and $o4 ...
  }
}

# Load the user interface description
my Gnome::Gtk3::Builder $builder .= new(:string($ui));
my Gnome::Gtk3::Window $w .= new(:build-id<top>);

# It is possible to devide the works over more than one class
my X $x .= new;
my Y $y .= new;

# Create the handlers table
my Hash $handlers = %(
  :window-quit( $x, :o1<o1>, :o2<o2>),
  :button-click( $y, :o3<o3>, :o4<o4>)
);

# Register all signals
$builder.connect-signals-full($handlers);

[[gtk_] builder_] set_translation_domain

Sets the translation domain of builder. See prop translation-domain.

method gtk_builder_set_translation_domain ( Str $domain )
  • $domain; (allow-none): the translation domain or Any

[[gtk_] builder_] get_translation_domain

Gets the translation domain of builder.

Returns: the translation domain. This string is owned by the builder object and must not be modified or freed.

method gtk_builder_get_translation_domain ( --> Str  )

[[gtk_] builder_] get_type_from_name

Looks up a type by name, using the virtual function that Gnome::Gtk3::Builder has for that purpose. This is mainly used when implementing the Gnome::Gtk3::Buildable interface on a type.

Returns: the GType found for type_name or G_TYPE_INVALID if no type was found

method gtk_builder_get_type_from_name ( Str $type_name --> UInt  )
  • $type_name; type name to lookup

[[gtk_] builder_] value_from_string

This function demarshals a value from a string. This function calls g_value_init() on the value argument, so it need not be initialised beforehand.

This function can handle char, uchar, boolean, int, uint, long, ulong, enum, flags, float, double, string, Gnome::Gdk3::Color, Gnome::Gdk3::RGBA and Gnome::Gtk3::Adjustment type values. Support for Gnome::Gtk3::Widget type values is still to come.

Upon errors 0 will be returned and error will be assigned a GError from the GTK_BUILDER_ERROR domain.

Returns: 1 on success

method gtk_builder_value_from_string ( GParamSpec $pspec, Str $string, N-GObject $value, N-GObject $error --> Int  )
  • $pspec; the GParamSpec for the property

  • $string; the string representation of the value

  • $value; (out): the GValue to store the result in

  • $error; (allow-none): return location for an error, or Any

[[gtk_] builder_] value_from_string_type

Like gtk_builder_value_from_string(), this function demarshals a value from a string, but takes a GType instead of GParamSpec. This function calls g_value_init() on the value argument, so it need not be initialised beforehand.

Upon errors 0 will be returned and error will be assigned a GError from the GTK_BUILDER_ERROR domain.

Returns: 1 on success

method gtk_builder_value_from_string_type ( N-GObject $type, Str $string, N-GObject $value, N-GObject $error --> Int  )
  • $type; the GType of the value

  • $string; the string representation of the value

  • $value; (out): the GValue to store the result in

  • $error; (allow-none): return location for an error, or Any

[[gtk_] builder_] new_from_file

Builds the [Gnome::Gtk3::Builder UI definition](https://developer.gnome.org/gtk3/3.24/GtkBuilder.html#BUILDER-UI) in the file filename.

If there is an error opening the file or parsing the description then the program will be aborted. You should only ever attempt to parse user interface descriptions that are shipped as part of your program.

Returns: a Gnome::Gtk3::Builder containing the described interface

method gtk_builder_new_from_file ( Str $filename --> N-GObject  )
  • $filename; filename of user interface description file

[[gtk_] builder_] new_from_resource

Builds the [Gnome::Gtk3::Builder UI definition](https://developer.gnome.org/gtk3/3.24/GtkBuilder.html#BUILDER-UI) at resource_path.

If there is an error locating the resource or parsing the description, then the program will be aborted.

Returns: a Gnome::Gtk3::Builder containing the described interface

method gtk_builder_new_from_resource ( Str $resource_path --> N-GObject )
  • $resource_path; a GResource resource path

[[gtk_] builder_] new_from_string

Builds the user interface described by string (in the [Gnome::Gtk3::Builder UI definition](https://developer.gnome.org/gtk3/3.24/GtkBuilder.html#BUILDER-UI) format).

If string is Any-terminated, then length should be -1. If length is not -1, then it is the length of string.

If there is an error parsing string then the program will be aborted. You should not attempt to parse user interface description from untrusted sources.

Returns: a Gnome::Gtk3::Builder containing the interface described by string

method gtk_builder_new_from_string ( Str $string, Int $length --> N-GObject  )
  • $string; a user interface (XML) description

  • $length; the length of string, or -1

[[gtk_] builder_] add_callback_symbol

Adds the callback_symbol to the scope of builder under the given callback_name.

Using this function overrides the behavior of gtk_builder_connect_signals() for any callback symbols that are added. Using this method allows for better encapsulation as it does not require that callback symbols be declared in the global namespace.

method gtk_builder_add_callback_symbol ( Str $callback_name, GCallback $callback_symbol )
  • $callback_name; The name of the callback, as expected in the XML

  • $callback_symbol; (scope async): The callback pointer

[[gtk_] builder_] add_callback_symbols

A convenience function to add many callbacks instead of calling gtk_builder_add_callback_symbol() for each symbol.

method gtk_builder_add_callback_symbols ( Str $first_callback_name, GCallback $first_callback_symbol )
  • $first_callback_name; The name of the callback, as expected in the XML

  • $first_callback_symbol; (scope async): The callback pointer @...: A list of callback name and callback symbol pairs terminated with Any

[[gtk_] builder_] lookup_callback_symbol

Fetches a symbol previously added to builder with gtk_builder_add_callback_symbols()

This function is intended for possible use in language bindings or for any case that one might be cusomizing signal connections using gtk_builder_connect_signals_full()

Returns: (nullable): The callback symbol in builder for callback_name, or Any

method gtk_builder_lookup_callback_symbol ( Str $callback_name --> GCallback  )
  • $callback_name; The name of the callback

[[gtk_] builder_] set_application

Sets the application associated with builder.

You only need this function if there is more than one Application in your process. application cannot be Any.

method gtk_builder_set_application ( N-GObject $application )
  • $application; a Gnome::Gtk3::Application

[[gtk_] builder_] get_application

Gets the Gnome::Gtk3::Application associated with the builder.

The Gnome::Gtk3::Application is used for creating action proxies as requested from XML that the builder is loading.

By default, the builder uses the default application: the one from g_application_get_default(). If you want to use another application for constructing proxies, use gtk_builder_set_application().

Returns: (nullable) (transfer none): the application being used by the builder, or Any

method gtk_builder_get_application ( --> N-GObject  )

[[gtk_] builder_] extend_with_template

Main private entry point for building composite container components from template XML.

This is exported purely to let gtk-builder-tool validate templates, applications have no need to call this function.

Returns: A positive value on success, 0 if an error occurred

method gtk_builder_extend_with_template ( N-GObject $widget, N-GObject $template_type, Str $buffer, UInt $length, N-GObject $error --> UInt  )
  • $widget; the widget that is being extended

  • $template_type; the type that the template is for

  • $buffer; the string to parse

  • $length; the length of buffer (may be -1 if buffer is nul-terminated)

  • $error; (allow-none): return location for an error, or Any

Properties

An example of using a string type property of a Gnome::Gtk3::Label object. This is just showing how to set/read a property, not that it is the best way to do it. This is because a) The class initialization often provides some options to set some of the properties and b) the classes provide many methods to modify just those properties. In the case below one can use new(:label('my text label')) or gtk_label_set_text('my text label').

my Gnome::Gtk3::Label $label .= new;
my Gnome::GObject::Value $gv .= new(:init(G_TYPE_STRING));
$label.g-object-get-property( 'label', $gv);
$gv.g-value-set-string('my text label');

Supported properties

translation-domain

The translation domain used when translating property values that have been marked as translatable in interface descriptions. If the translation domain is Any, Gnome::Gtk3::Builder uses gettext(), otherwise g_dgettext().

The Gnome::GObject::Value type of property translation-domain is G_TYPE_STRING.