189. How to set up menu using GtkUIManager - Part 1?

Problem

In the article How to set up menu and radio menu - Part 1?, we have showed how to set up menus using GtkMenu.

In php-gtk2, there is an even more powerful widget called GtkUIManager for creating menus and toolbars. We will first show you in this article how to set up menu with an xml definition of the menu layout as shown below:

How to set up menu using GtkUIManager - Part 1?


Solution

The steps to create menus using GtkUIManager are as follows:

  • Define the menu layout in an xml format as shown in the codes below. It's quite self explanatory.
  • Create a new GtkUIManager.
  • Create a new GtkActionGroup for each group of actions. In this example, each action group corresponds to each top menu item (i.e. the File Menu and the Edit Menu).
  • For each action group, create the desired GtkActions, and set up the corresponding event handler for each action.
  • Create the menu from the xml menu definition using GtkUimanager::add_ui_from_string().

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// define menu definition
$menu_definition =  // note 1
'<ui>
  <menubar name="menubar1">
    <menu action="FileMenu">
      <menuitem action="New"/>
      <menuitem action="Open"/>
      <menuitem action="Close"/>
      <separator/>
      <menuitem action="Exit"/>
    </menu>
    <menu action="EditMenu">
      <menuitem action="Cut"/>
      <menuitem action="Copy"/>
      <menuitem action="Paste"/>
      <separator/>
      <menuitem action="SelectAll"/>
    </menu>
  </menubar>
</ui>';

setup_menu($vbox, $menu_definition);

// display title
$title = new GtkLabel("Set up menu using GtkUIManager\n".
"Part 1 - the xml menu definition");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$window->show_all();
Gtk::main();

// setup menu
function setup_menu($vbox, $menu_definition) {

    $ui_mgr = new GtkUiManager(); // note 2

    $file_action_group = new GtkActionGroup("FileActions"); // note 3
    $ui_mgr->insert_action_group($file_action_group, 0); // note 4

    $fileAction = new GtkAction('FileMenu','_File', '', '');
    $newAction = new GtkAction('New','_New', '', Gtk::STOCK_NEW); // note 5
    $newAction->connect('activate','on_menu_select'); // note 6
    $openAction = new GtkAction('Open','_Open','', Gtk::STOCK_OPEN);
    $openAction->connect('activate','on_menu_select');
    $closeAction = new GtkAction('Close','_Close','', Gtk::STOCK_CLOSE);
    $closeAction->connect('activate','on_menu_select');
    $exitAction = new GtkAction('Exit','E_xit','', Gtk::STOCK_QUIT);
    $exitAction->connect('activate', 'on_menu_select');

    $file_action_group->add_action($fileAction); // note 7
    $file_action_group->add_action($newAction);
    $file_action_group->add_action($openAction);
    $file_action_group->add_action($closeAction);
    $file_action_group->add_action($exitAction);

    $editAction = new GtkAction('EditMenu','_Edit','', '');
    $cutAction = new GtkAction('Cut','Cu_t','', Gtk::STOCK_CUT);
    $cutAction->connect('activate','on_menu_select');
    $copyAction = new GtkAction('Copy','_Copy','', Gtk::STOCK_COPY);
    $copyAction->connect('activate','on_menu_select');
    $pasteAction = new GtkAction('Paste','_Paste','', Gtk::STOCK_PASTE);
    $pasteAction->connect('activate','on_menu_select');
    $selectAllAction = new GtkAction('SelectAll','Select _All','', '');
    $selectAllAction->connect('activate', 'on_menu_select');

    $edit_action_group = new GtkActionGroup("EditActions");
    $ui_mgr->insert_action_group($edit_action_group, 0);

    $edit_action_group->add_action($editAction);
    $edit_action_group->add_action($cutAction);
    $edit_action_group->add_action($copyAction);
    $edit_action_group->add_action($pasteAction);
    $edit_action_group->add_action($selectAllAction);

    $ui_mgr->add_ui_from_string($menu_definition); // note 8
    $ui_mgr->ensure_update();

    $toplevels = $ui_mgr->get_toplevels(Gtk::UI_MANAGER_MENUBAR); // note 9
    $vbox->pack_start($toplevels['menubar1'], 0); // note 9

}

// process menu item selection
function on_menu_select($menu_item) {
    $item = $menu_item->get_name();
    echo "menu selected: $item\n";
    if ($item=='Exit') Gtk::main_quit();
}

?>

Output

As shown above.
 

Explanation

  1. The menu definition in xml format.
  2. Create a new UI manager.
  3. Create a new action group.
  4. Add the action group to the UI manager.
  5. Create a new action.
  6. Connect the signal activate to handle each action (which is the menu item).
  7. Add each action to the desired action group.
  8. Set up the menu from the xml menu definition.
  9. Add the menu to the vbox.

Note

You might want to compare this method of creating menu with that of using GtkMenu as described in How to set up menu and radio menu - Part 1?

Related Links

Add comment


Security code
Refresh