Sample Code 17: How to display and process grouped radio buttons?
Written by kksou   
Saturday, 16 September 2006
Problem

You want to display grouped radio buttons as shown below:

How to display and process grouped radio buttons?


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   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);

$window->connect_simple('destroy', array('Gtk','main_quit'));

$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel();
$title->set_markup('<span color="blue" font_desc="Times New Roman Italic 12">
    Button Group Demo</span>');
$vbox->pack_start($title);

// setup grouped radio buttons
$radio1 = setup_radio(null, 'radio button 1', '101'); // note 1
$radio2 = setup_radio($radio1, 'radio button 2', '102'); // note 2
$radio3 = setup_radio($radio1, 'radio button 3', '103');

// pack them inside vbox
$vbox->pack_start($radio1, 0, 0);
$vbox->pack_start($radio2, 0, 0);
$vbox->pack_start($radio3, 0, 0);

// add a status area
$vbox->pack_start($status_area = new GtkLabel('Click a Button'));

// function to simplify the display of grouped radio buttons
function setup_radio($radio_button_grp, $button_label, $button_value) { // note 3
    $radio = new GtkRadioButton($radio_button_grp, $button_label);
    $radio->connect('toggled', "on_toggle", $button_value); // note 4
  • 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. For new GtkRadioButton, use null for the first radio button of the group.
  2. For subsequent buttons, put the pointer to the first radio button (in this case $radio1) as the first argument to new GtkRadioButton. PHP-GTK2 will then group all these into the same group.
  3. Wrote this small function to save some typing and make the codes cleaner.
  4. Set up the callback function when the radio button is toggled. Note that we also pass along the button value.
  5. This is the callback function that is being called when a radio button is being toggled.
  6. Get the label of the radio button.
  7. Check if the radio button is toggled.
  8. If yes, display the label and value in the status area. Note that we have the corresponding value of the radio button ($value) because we have set it up in [4].

Notes
This example simulates what we have always been doing in HTML:

Each radio button could have a label and a corresponding value.


Related Links

User reviews   Average user ratings:    5.0   (from 2 users)
  1. saul_fossil
    April 03, 2008 12:28pm

  2. Josh from Switzerland
    July 06, 2008 6:46pm

    Very useful !

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