429. How to set up radio tool buttons in toolbar - Part 1?

Problem

Suppose you want to set up radio tool buttons in the toolbar as shown below. In this example, only one of the buttons (stop, prev, play or next buttons) will be active at any one time.

How to set up radio tool buttons in toolbar - Part 1?


Solution


Sample Code

1   
2   
3   
4   
5   
9   
10   
11   
12   
13   
14   
15   
18   
19   
20   
21   
22   
23   
24   
25   
27   
28   
29   
30   
31   
32   
33   
34   
36   
38   
39   
40   
41   
43   
44   
46   
47   
48   
49   
51   
53   
55   
56   
57   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
75   
76   
77   
78   
81   
82   
83   
84   
85   
86   
87   
<?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
$toolbar_definition = array('New', 'Open', 'Save', '<hr>',
    array('Player Stop' => Gtk::STOCK_MEDIA_STOP, // note 1
    'Player Prev' => Gtk::STOCK_MEDIA_PREVIOUS, 
    'Player Play' => Gtk::STOCK_MEDIA_PLAY, 
    'Player Next' => Gtk::STOCK_MEDIA_NEXT) 
); 
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Set up radio tool buttons in Toolbar\n".
"        using GtkRadioToolButton");
$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();

// process tool button
function on_toolbar_button($button, $item) {
    echo "toolbar clicked: $item\n";
}

// process radio tool button
function on_toggle($radio, $item) {
    $active = $radio->get_active(); // note 3
    if ($active) echo("radio tool button selected: $item\n");
}

// setup toolbar
function setup_toolbar($vbox, $toolbar_definition) {
    $toolbar = new GtkToolBar();
    $toolbar->set_property('toolbar-style', Gtk::TOOLBAR_ICONS);
    $vbox->pack_start($toolbar, 0, 0);
    foreach($toolbar_definition as $item) {
        if (is_array($item)) { // set up radio tool buttons
            $i=0;
            $radio[0] = null;
            foreach($item as $radio_item => $stock_id) { // note 2
                $radio[$i] = new GtkRadioToolButton($radio[0], $stock_id);
                $radio[$i]->connect('toggled', "on_toggle", $radio_item);
                $toolbar->insert($radio[$i], -1);
                ++$i;
            }
            $radio[0]->set_active(1); // select the first item
        } else {
            if ($item=='<hr>') {
                $toolbar->insert(new GtkSeparatorToolItem(), -1);
            } else {
                $stock_image_name = 'Gtk::STOCK_'.strtoupper($item);
                if (defined($stock_image_name)) {
                    $toolbar_item = GtkToolButton::new_from_stock(
                        constant($stock_image_name));
                    $toolbar->insert($toolbar_item, -1);
                    $toolbar_item->connect('clicked', 'on_toolbar_button', $item);
                }
            }
        }
    }
}

?>

Output

As shown above.
 

Explanation

  1. This is the menu definitions for the radio tool buttons. The first parameter is a label to identify each radio button. The second parameter is the stock id.
  2. Set up the radio tool button using the technique as described in How to display and process grouped radio buttons?
  3. Check if the radio button is active.

Note

You may want to compare this with the setting up of GtkMenu - How to set up menu and radio menu - Part 1?. The two are very similar.

Related Links

Add comment


Security code
Refresh