GtkItemFactory::create_items

GtkItemFactory::create_items

You usually create your menu by calling this method with an array full of menu item definitions. Such a item definition is an array itself and has the following structure: String path, String accelerator, Mixed callback, int callback_action, String type

The function can be called several times, causing the new items to be appended to the existing ones.

If you add a first-level menu item on a second create_items call, an existing <LastBranch> will lose its right alignment.

The path is the name describing the path form the top down to this item. The levels are separated with a slash /, and underscores _ can be used to create in-menu accelerators.
"/_File/_Recent files/_1 test.xml"
"/_File/O_pen"
It is not necessary to explicitly create the parent items if you create a submenu item; this is done automatically.

The accelerator sets the shortcut for the menu item; it can be NULL or a combination of modifiers and chars.

Table 4. Accelerator modifiers

<ALT>
<CTL> or <CTRL> or <CONTROL>
<MOD1> to <MOD5>
<SHFT> or <SHIFT>

As usually, the callback accepts the String of the function to call, or an array with the reference to an object as first, and the name of the object's function as the second parameter. This parameter can be NULL.

Unlike many other callback registering functions, this one doesn't support own parameters as callback_option. Instead, you can give a number (int) only to describe the function of the menu item. So you can use 1 for opening and 2 for saving to distinguish both when they use the same callback. The callback function needs to implement 2 parameters:int callback_option, GtkMenuItem item. The parameter has to be of type int, so a NULL cause an error.

The type is a string from this list:

Table 5. GtkItemFactory item types

TypeDescription
NULL or '' or <Item>Simple item
<Title>Title item which can't be clicked.
<CheckItem>Check item
<ToggleItem>Toggle item
<RadioItem>(Root) radio item
PathSister radio item for another radio item
<Tearoff>Tearoff
<Separator>Separator
<Branch>Item to hold submenus (optional)
<LastBranch>Right justified branch. This is only useful for one submenu of a menubar.

Example 19. Extensive GtkItemFactory::create_items example

<?php
if( !extension_loaded('gtk')) {	
    dl( 'php_gtk.' . PHP_SHLIB_SUFFIX); 
}

$window = &new GtkWindow();
$window->set_default_size( 300, 20);
$window->connect_object('destroy', array('gtk', 'main_quit'));

$accelgroup	= &new GtkAccelGroup();
$window->add_accel_group( $accelgroup);

$fac	= &new GtkItemFactory( GtkMenuBar::get_type(), '<mainmenu>', $accelgroup);

function menucall( $number, $item) {
	switch( $number) {
		case 1: echo 'New file'; break;
		case 15: echo 'This is a small GtkItemFactory example'; break;
		case 20: gtk::main_quit(); break;
		default: echo 'Unknown action ' . $number;
	}
	echo "\r\n";
}
$arItems	= array(
array( '/_File/_New', '<CTRL>N', 'menucall', 1, null ),
array( '/_File/_Open', '<CTRL>O', 'menucall', 2, '' ),
array( '/_File/sep1', null, null, 0, '<Separator>' ),
array( '/_File/_Save', '<CTRL>S', 'menucall', 0, null ),
array( '/_File/Save _as', null, 'menucall', 0, null ),
array( '/_File/sep2', null, null, 0, '<Separator>' ),
array( '/_File/_Quit', '<CTRL>Q', 'menucall', 20, null ),

array( '/_View', null, null, 0, '<Branch>' ),
array( '/_View/tearoff', null, null, 0, '<Tearoff>' ),
array( '/_View/_Toolbar', null, null, 0, '<CheckItem>' ),
array( '/_View/_Preview', null, null, 0, '<CheckItem>' ),

array( '/_Colors/tearoff', null, null, 0, '<Tearoff>' ),
array( '/_Colors/Foreground', null, null, 0, '<Title>' ),
array( '/_Colors/White', null, null, 0, '<RadioItem>' ),
array( '/_Colors/Yellow', null, null, 0, '/Colors/White' ),
array( '/_Colors/Orange', null, null, 0, '/Colors/White' ),

array( '/_Colors/sep1', null, null, 0, '<Separator>' ),
array( '/_Colors/Background', null, null, 0, '<Title>' ),
array( '/_Colors/Black', null, null, 0, '<RadioItem>' ),
array( '/_Colors/Blue', null, null, 0, '/Colors/Black' ),
array( '/_Colors/Red', null, null, 0, '/Colors/Black' ),

array( '/_Help', null, null, 0, '<LastBranch>' ),
array( '/_Help/_About', 'F1', 'menucall', 15, '<Item>' )
);

$fac->create_items( $arItems);

//By default, the first item of a radio group is selected. We change this
$mnuColorRed = $fac->get_widget( '/Colors/Red');
$mnuColorRed->set_active( true);

$arMoreItems	= array(
array( '/_View/sep1', null, null, 0, '<Separator>' ),
array( '/_View/more/items/are/appended/here', null, null, 0, null)
);
$fac->create_items( $arMoreItems);

$window->add( $fac->get_widget( "<mainmenu>"));
$window->show_all();
gtk::main();
?>
© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.