059. How to set up menu and radio menu - Part 2 - add stock images?

Problem

You have set up menus (with radio menus) in Part 1.

How to set up menu and radio menu - Part 1?

Now you would like to add in stock images to the menu items as shown below:

How to set up menu and radio menu - Part 2 - add stock images?


Solution


Sample Code

1   
2   
3   
4   
5   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
24   
25   
26   
27   
28   
29   
30   
31   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
<?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 = array(
    '_File' => array('_New', '_Open', '_Close', '<hr>', '_Save', 'Save _As','<hr>', '_Quit'),
    '_Edit' => array('Cut', 'Copy', '_Paste', '<hr>', 'Select All', '<hr>', '_Undo','_Redo'),
    '_Test' => array('Test_1', 'Test_2', 'Test_3', '<hr>',
                array('Selection 1', 'Selection 2', 'Selection 3'),
                '<hr>', 'Test_4')
);
setup_menu($vbox, $menu_definition);

// display title
$title = new GtkLabel("Menu and RadioMenuItem - Part 2\n               Add Stock Images");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$vbox->pack_start($title);
$vbox->pack_start(new GtkLabel(''));

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

// setup menu
function setup_menu($vbox, $menus) {
    $menubar = new GtkMenuBar();
    $vbox->pack_start($menubar, 0, 0);
    foreach($menus as $toplevel => $sublevels) {
        $menubar->append($top_menu = new GtkMenuItem($toplevel));
        $menu = new GtkMenu();
        $top_menu->set_submenu($menu);
        foreach($sublevels as $submenu) {
            if (is_array($submenu)) { // set up radio menus
                $i=0;
                $radio[0] = null;
                foreach($submenu as $radio_item) {
                    $radio[$i] = new GtkRadioMenuItem($radio[0], $radio_item);
                    $radio[$i]->connect('toggled', "on_toggle");
                    $menu->append($radio[$i]);
                    ++$i;
                }
                $radio[0]->set_active(1); // select the first item
            } else {
                if ($submenu=='<hr>') {
                    $menu->append(new GtkSeparatorMenuItem());
                } else {
                    $submenu2 = str_replace('_', '', $submenu); // note 1
                    $submenu2 = str_replace(' ', '_', $submenu2); // note 1
                    $stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2); // note 1
                    if (defined($stock_image_name)) {
                        $menu_item = new GtkImageMenuItem(constant($stock_image_name)); // note 2
                    } else {
                        $menu_item = new GtkMenuItem($submenu);
                    }
                    $menu->append($menu_item);
                    $menu_item->connect('activate', 'on_menu_select');
                }
            }
        }
    }
}

// process radio menu selection
function on_toggle($radio) {
    $label = $radio->child->get_label();
    $active = $radio->get_active();
    echo("radio menu selected: $label\n");
}

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

?>

Output

As shown above.
 

Explanation

  1. You can view the entire list of built-in stock images at here. What we are doing here is to check if the menu name has a corresponding built-in stock images. First we need to remove all "_", then replace all <space> with "_", then add "Gtk::STOCK_" in front of the string.
  2. Then we check if such constant exists. If yes, we construct the menu using GtkImagemenuitem. If no, we use the standard GtkMenuitem

Related Links

Add comment


Security code
Refresh