Sample Code 47: How to set up menu and radio menu - Part 1?
Written by kksou   
Thursday, 05 October 2006
Problem

You want to set up menus (with radio menus) as shown below:

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


Solution

The steps to create menus are as follows:


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   
<?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', '_Quit'),
    '_Test' => array('Test1', 'Test2', 'Test3', '<hr>',
                array('Selection 1', 'Selection 2', 'Selection 3'), // note 2
                '<hr>', 'Test4')
);
setup_menu($vbox, $menu_definition); // note 1

// setup menu
function setup_menu($vbox, $menus) { // note 1
    $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) {// note 3
                    $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 {
                    $menu->append($menu_item = new GtkMenuItem($submenu));
                    $menu_item->connect('activate', 'on_menu_select');
                }
            }
        }
    }
}

// process radio menu selection
function on_toggle($radio) { // note 3
    $label = $radio->child->get_label();
    $active = $radio->get_active();
  • 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
  1. We write a setup_menu() here to simplify the setup of menus.
  2. For radio menus, enclose the options in an array in the menu definition.
  3. The display and processing of radio buttons is exactly the same as the method as described in the article How to display and process grouped radio buttons?

Related Links
 
< Prev   Next >

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