GladeXML Constructor

GladeXML Constructor

GladeXML (string fname, [string root = NULL, [string domain = NULL]]);

Creates a new instance of the glade class, based on a glade file fname.

If you want to load the whole glade file, you can omit the root parameter. For loading only a part of the file, e.g. the menu definition, you can pass the name of the wished root element. In the example we could use "btnClose" to load the close button only. If the button had subwidgets, they would be loaded as well.

When using the GNU Gettext module for internationalization, you can specifiy the gettext domain as third parameter. Glade will translate all the properties marked with "translatable" via the gettext functions on this domain.

The following two files result in a sample application which loads the glade file and connect the button's "clicked" signal to exit the application. You don't need to call the show() and realize() functions as glade takes care of this.

Example 65. Basic glade file "hellowg.glade"

<?xml version="1.0"?>
<GTK-Interface>

<project>
  <name>example-gladexml.constructor</name>
  <program_name>example-gladexml.constructor</program_name>
  <directory></directory>
  <source_directory>src</source_directory>
  <pixmaps_directory>pixmaps</pixmaps_directory>
  <language>C</language>
  <gnome_support>False</gnome_support>
  <gettext_support>False</gettext_support>
</project>

<widget>
  <class>GtkWindow</class>
  <name>dlgHellow</name>
  <title>Hello World!</title>
  <type>GTK_WINDOW_TOPLEVEL</type>
  <position>GTK_WIN_POS_NONE</position>
  <modal>False</modal>
  <allow_shrink>False</allow_shrink>
  <allow_grow>True</allow_grow>
  <auto_shrink>False</auto_shrink>

  <widget>
    <class>GtkVBox</class>
    <name>vbox1</name>
    <homogeneous>False</homogeneous>
    <spacing>0</spacing>

    <widget>
      <class>GtkLabel</class>
      <name>lblHello</name>
      <label>Hello world!
This is the example program
for the GladeXML class</label>
      <justify>GTK_JUSTIFY_CENTER</justify>
      <wrap>False</wrap>
      <xalign>0.5</xalign>
      <yalign>0.5</yalign>
      <xpad>0</xpad>
      <ypad>0</ypad>
      <child>
	<padding>0</padding>
	<expand>True</expand>
	<fill>True</fill>
      </child>
    </widget>

    <widget>
      <class>GtkButton</class>
      <name>btnClose</name>
      <can_focus>True</can_focus>
      <signal>
	<name>clicked</name>
	<handler>test</handler>
	<last_modification_time>Sun, 29 Aug 2004 10:33:10 GMT</last_modification_time>
      </signal>
      <label>Close this window</label>
      <relief>GTK_RELIEF_NORMAL</relief>
      <child>
	<padding>0</padding>
	<expand>False</expand>
	<fill>False</fill>
      </child>
    </widget>
  </widget>
</widget>

</GTK-Interface>
When defining more than one window in the glade file, all windows are shown when loading it. You can set the "Visible" property in glade to avoid this.

A small explanation of the code above: The glade code was automatically created with glade. The instantiation of the glade requires the filename as first parameter, and we use dirname( __FILE__) . '/hellow.glade' to make sure the glade file is loaded from the same location where the php script is located. If you would pass the filename without the parameter to the glade constructor, the glade file wouldn't be found if the program is run in a working directory which is not the directory of the php script. To let the application exit tidily, we connect the "destroy" signal of the window as normal; but before we get the widget via the get_widget() function.

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.