PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 191: How to set up menu using GtkUIManager - Part 2?
Written by kksou   
Sunday, 04 March 2007
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   
39   
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   
<?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);
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
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

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved