PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 429: How to set up radio tool buttons in toolbar - Part 1?
Written by kksou   
Monday, 04 February 2008
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   
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   
<?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);
  • 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. 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

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 >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved