Chapter 26. Using ItemFactories

Inheritance Hierarchy

Object
   +--- ItemFactory
         

For now, there's only this example. An explanation and lots 'o' comments will follow later.

26.1. Item Factory Example

Now that we've shown you the hard way, here's how you do it using the ItemFactory calls.

Item Factory Example Source

      
#!/usr/bin/perl -w

use Gtk;
use strict;

set_locale Gtk;
init Gtk;

my $false = 0;
my $true = 1;

# This is the GtkItemFactoryEntry structure used to generate new menus.
# Item 1: The menu path. The letter after the underscore indicates an
#         accelerator key once the menu is open.
# Item 2: The accelerator key for the entry
# Item 3: The callback function.
# Item 4: The callback action.  This changes the parameters with
#         which the function is called.  The default is 0.
# Item 5: The item type, used to define what kind of an item it is.
#         Here are the possible values:
#          NULL               -> "<Item>"
#          ""                 -> "<Item>"
#          "<Title>"          -> create a title item
#          "<Item>"           -> create a simple item
#          "<CheckItem>"      -> create a check item
#          "<ToggleItem>"     -> create a toggle item
#          "<RadioItem>"      -> create a radio item
#          <path>             -> path of a radio item to link against
#          "<Separator>"      -> create a separator
#          "<Branch>"         -> create an item to hold sub items (optional)
#          "<LastBranch>"     -> create a right justified branch 

my @menu_items = ( { path        => '/_File',
		     type        => '<Branch>' },
		   { path        => '/File/_New',
		     accelerator => '<control>N',
		     callback    => \&print_hello },
		   { path        => '/File/_Open',
		     accelerator => '<control>O',
		     callback    => \&print_hello },
		   { path        => '/File/_Save',
		     accelerator => '<control>S',
		     callback    => \&print_hello },
		   { path        => '/File/Save _As' },
		   { path        => '/File/sep1',
		     type        => '<Separator>' },
		   { path        => '/File/Quit',
		     callback    => sub { Gtk->exit( 0 ); } },

		   { path        => '/_Options',
		     type        => '<Branch>' },
		   { path        => '/Options/Test' },

		   { path        => '/_Help',
		     type        => '<LastBranch>' },
		   { path        => '/_Help/About' } );

my $window;
my $main_vbox;
my $menubar;


# Create the window
$window = new Gtk::Window( 'toplevel' );
$window->signal_connect( 'destroy', sub { Gtk->exit( 0 ); } );
$window->set_title( "Item Factory" );
$window->set_usize( 300, 200 );

$main_vbox = new Gtk::VBox( $false, 1 );
$main_vbox->border_width( 1 );
$window->add( $main_vbox );
$main_vbox->show();

# Create the menu bar
$menubar = create_menu_bar( $window );
$main_vbox->pack_start( $menubar, $false, $true, 0 );
$menubar->show();

$window->show();
main Gtk;
exit( 0 );




### Subroutines

# Obligatory basic callback

sub print_hello
{
   print( "Hello World!\n" );
}


# Crete the menu bar, initialize its menus, and return the menu bar.

sub create_menu_bar
{
   my ( $window ) = @_;

   my $menubar;
   my $item_factory;
   my $accel_group;

   $accel_group = new Gtk::AccelGroup();

   # This function initializes the item factory.
   # Param 1: The type of menu - can be 'Gtk::MenuBar', 'Gtk::Menu',
   #          or 'Gtk::OptionMenu'.
   # Param 2: The path of the menu.
   # Param 3: The accelerator group.  The item factory sets up
   #          the accelerator table while generating menus.
   $item_factory = new Gtk::ItemFactory( 'Gtk::MenuBar',
					 '<main>',
					 $accel_group );

   # This function generates the menu items. Pass the item factory,
   # the number of items in the array, the array itself, and any
   # callback data for the the menu items.
   $item_factory->create_items( @menu_items );

   # Attach the new accelerator group to the window.
   $window->add_accel_group( $accel_group );

   # Finally, return the actual menu bar created by the item factory.
   #*menubar = gtk_item_factory_get_widget (item_factory, "&lt;main>");
   return ( $item_factory->get_widget( '<main>' ) );
}


# END EXAMPLE PROGRAM
      
   

Item Factory Example Screenshot