191. How to set up menu using GtkUIManager - Part 2?

Problem

When you have set up menus using GtkUIManager, you can activate or deactivate a group of menu items with just a one liner.

In this example, the Edit menu is only activated when there is a document. When the application is first started, there is no document, so the Edit menu is grayed out. When the user opens a document or starts a new document, we activate the Edit menu. When the user closes a document, we grayed out the Edit menu again as shown below:

How to set up menu using GtkUIManager - Part 2?


Solution


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   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
<?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 = '
<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 2 - the power of ActionGroup");
$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) {
    global $ui_mgr;

    $ui_mgr = new GtkUiManager();

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

    $fileAction = new GtkAction('FileMenu','_File', '', '');
    $newAction = new GtkAction('New','_New', '', Gtk::STOCK_NEW);
    $newAction->connect('activate','on_menu_select');
    $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);
    $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);
    $ui_mgr->ensure_update();

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

    $edit_action_group->set_sensitive(0); // note 1

}

// process menu item selection
function on_menu_select($menu_item) {
    global $ui_mgr;
    $action_grp = $ui_mgr->get_action_groups(); // note 2

    $item = $menu_item->get_name();
    echo "menu selected: $item\n";
    switch ($item) {
        case 'Exit': Gtk::main_quit();

        case 'Open':
        case 'New':
            $action_grp['EditActions']->set_sensitive(1); // note 3
            break;

        case 'Close':
            $action_grp['EditActions']->set_sensitive(0); // note 4
            break;

    }

}

?>

Output

As shown above.
 

Explanation

The above example makes use of the code in Part 1.

What's new here:

  1. When the application is first started, there is no active document, so we grayed out the Edit menu.
  2. Get the action group.
  3. When the user opens an existing document or starts a new document, we active the Edit menu.
  4. When the user closes the document, we deactive the Edit menu.

Related Links

Add comment


Security code
Refresh