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:
Solution
- We use the same technique as outlined in How to display and process grouped radio buttons? to set the group of a radio button.
- To change the radio button group, just use the method
GtkRadioButton::set_group()
to switch to a different group.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 72 74 75 76 77 78 79 80 82 83 84 85 87 88 89 90 91 92 93 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 145 146 147 148 151 152 153 154 155 156 157 158 159 160 161 162 | <?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') { for ($i=1; $i<=10; ++$i) { if ($radios[$i]->get_active()) { // note 5 echo "selected number = $i\n"; break; } } } elseif ($mode=='odd_and_even') { for ($i=1; $i<=10; $i+=2) { if ($radios[$i]->get_active()) { // note 6 echo "selected odd number = $i\n"; break; } } for ($i=2; $i<=10; $i+=2) { if ($radios[$i]->get_active()) { // note 6 echo "selected even number = $i\n"; break; } } } } // setup grouped radio buttons $radio1 = setup_radio(null, 'Select a number from 1 to 10', 'one_only'); $radio2 = setup_radio($radio1, 'Select one from odd and one from even', 'odd_and_even'); // pack them inside vbox $vbox->pack_start($radio1, 0); $vbox->pack_start($radio2, 0); $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start($button = new GtkButton('Process'), 0); $button->connect('clicked', 'on_button'); $vbox->pack_start(new GtkLabel()); global $mode; $mode = 'one_only'; } ?> |
Output
As shown above.Explanation
- Create all the 10 radio buttons in the same group.
- Take note of the pointers to these radio buttons.
- Switch to all 10 numbers in same group.
- Switch to all odd numbers in one group, and all even numbers in another group.
- Find out which number is selected.
- Find out which odd and even numbers are selected.
Read more...