PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 431: How to change radio button group on the fly?
Written by kksou   
Tuesday, 12 February 2008
Problem

I've showed you how to group radio buttons into different groups using GtkRadioButton::set_group() in the article How to setup grouped radio buttons using set group?

This article gives you a practical example of this: changing radio button group on-the-fly as shown below:

How to change radio button group on the fly?


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   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 300);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Change radio button group on the fly");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

setup_menu($vbox);

setup_numbers($vbox);

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

function setup_numbers($vbox) {

    function setup_radio_numbers($radio_button_grp, $button_label) {
        $radio = new GtkRadioButton($radio_button_grp, $button_label);
        $radio->connect('toggled', "on_toggle_numbers");
        return $radio;
    }

    function on_toggle_numbers($radio) {
        global $status_area;
        $label = $radio->child->get_label();
        $active = $radio->get_active();
        if ($active) print "radio button (number) pressed: $label\n";
    }

    $vbox->pack_start($hbox = new GtkHBox());
    $hbox->pack_start($vbox1 = new GtkVBox());
    $hbox->pack_start($vbox2 = new GtkVBox());

    global $radios;
    $vbox1->pack_start($hbox1 = new GtkHBox, 0);
    $hbox1->pack_start($label = new GtkLabel('Odd'), 0);
    $label->modify_font(new PangoFontDescription("Arial Bold"));
    $radiogrp = null;
    $radios[0] = setup_radio_numbers($radiogrp, 0);
    for ($i=1; $i<=10; $i+=2) {
        $radio = setup_radio_numbers($radiogrp, $i);
        $vbox1->pack_start($radio, 0);
        $radiogrp = $radio;
        $radios[$i] = $radio;
    }

    $vbox2->pack_start($hbox2 = new GtkHBox, 0);
    $hbox2->pack_start($label = new GtkLabel('Even'), 0);
    $label->modify_font(new PangoFontDescription("Arial Bold"));
    for ($i=2; $i<=10; $i+=2) {
        $radio = setup_radio_numbers($radiogrp, $i); // note 1
        $vbox2->pack_start($radio, 0);
        $radiogrp = $radio;
        $radios[$i] = $radio; // note 2
    }

}

function setup_menu($vbox) {

    function setup_radio($radio_button_grp, $button_label, $button_value) {
        $radio = new GtkRadioButton($radio_button_grp, $button_label);
        $radio->connect('toggled', "on_toggle", $button_value);
        return $radio;
    }

    function on_toggle($radio, $value) {
        global $status_area;
        $label = $radio->child->get_label();
        $active = $radio->get_active();

        global $radios, $mode;
        $mode = $value;

        if ($mode=='one_only') {

            for ($i=2; $i<=10; ++$i) {
                $radios[$i]->set_group($radios[1]); // note 3
            }

        } elseif ($mode=='odd_and_even') {

            for ($i=3; $i<=10; $i+=2) {
                $radios[$i]->set_group($radios[1]); // note 4
            }

            $radios[2]->set_group($radios[0]);
            for ($i=4; $i<=10; $i+=2) {
                $radios[$i]->set_group($radios[2]); // note 4
            }
            $radios[2]->set_active(1);

        }
    }

    function on_button() {
        global $radios, $mode;
        print "on_button. mode = $mode\n";

        if ($mode=='one_only') {
  • 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. Create all the 10 radio buttons in the same group.
  2. Take note of the pointers to these radio buttons.
  3. Switch to all 10 numbers in same group.
  4. Switch to all odd numbers in one group, and all even numbers in another group.
  5. Find out which number is selected.
  6. Find out which odd and even numbers are selected.

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