А теперь пример. Объяснения и комментарии следуют.
#!/usr/bin/perl -w use Gtk ; use strict ; set_locale Gtk; init Gtk; my $false = 0; my $true = 1; # Эта the GtkItemFactoryEntry структура используется для генерации новых меню.. # Запись может включать в себя следующие пункты. # Item 1: path - Местонахождение пункта в меню. Буква после подчеркивания указывает на # на клавишу быстрого вызова, этого пункта после открытия меню. # 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, "<main>"); return ( $item_factory->get_widget( '<main>' ) ); } # END EXAMPLE PROGRAM
Item Factory Example Screenshot