Problem
The GtkDialog constructor example in the offical PHP-GTK2 documentation shows only how to get a yes/no response using a dialog box with GtkButtons.
Suppose you would like to get inputs from user using radio buttons in a dialog box as shown below:
Solution
- Set up the buttons manually as outlined in the article How to setup a dialog box - Part 3 - set up buttons manually?
- We process the selection of radio button using the technique as described in How to set up radio buttons with default all unselected?. Note how we "hide" one of the radio items so that the radio groups appear unselected when presented to the user initially.
- In the button click event handler, we destroy the dialog manually with GtkObject::destroy().
- Although the dialog is destroyed, the variable
$selected_radio
and$selected_radio_value
are still there.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 37 38 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 58 59 60 62 63 64 65 66 67 68 69 71 73 74 75 76 77 78 79 80 86 87 88 89 90 91 92 93 94 96 97 98 99 101 102 103 104 | <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->set_size_request(400, 120); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Set up Dialog Box of Radio Buttons - Part 1"); $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); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start(new GtkLabel('Response: '), 0); $hbox->pack_start($response = new GtkLabel(), 0); $hbox->pack_start($button = new GtkButton('Get Response'), 0); $button->connect('clicked', 'on_click'); $window->show_all(); Gtk::main(); function on_click() { setup_dialog(); } function setup_dialog() { $dialog = new GtkDialog(); $dialog->vbox->pack_start(new GtkLabel('Which platform are you using: ')); $radio0 = setup_radio(null, 'radio button 0', '100'); // note 1 $radio1 = setup_radio($radio0, 'Windows', 'win'); $radio2 = setup_radio($radio0, 'Mac', 'mac'); $radio3 = setup_radio($radio0, 'Linux', 'linux'); // pack them inside vbox $dialog->vbox->pack_start($radio1, 0, 0); // note 2 $dialog->vbox->pack_start($radio2, 0, 0); // note 2 $dialog->vbox->pack_start($radio3, 0, 0); // note 2 $dialog->vbox->pack_start($hbox2 = new GtkHBox()); $button_ok = GtkButton::new_from_stock(Gtk::STOCK_OK); $button_ok->set_size_request(87, 33); $hbox2->pack_start(new GtkLabel()); $hbox2->pack_start($button_ok, 0); $button_ok->connect('clicked', 'on_ok_button', $dialog); $dialog->set_has_separator(false); $dialog->action_area->set_size_request(-1, 1); $dialog->show_all(); global $selected_radio, $selected_radio_value; $selected_radio = $selected_radio_value = ''; $dialog->run(); $dialog->destroy(); global $response; $response->set_text("$selected_radio ($selected_radio_value)"); // note 5 } function on_ok_button($button, $dialog) { $dialog->destroy(); // note 4 } 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) { // note 3 $label = $radio->child->get_label(); $active = $radio->get_active(); if ($active) { global $response, $selected_radio, $selected_radio_value; $selected_radio = $label; $selected_radio_value = $value; } } ?> |
Output
As shown above.Explanation
We make use of the code from How to setup a dialog box - Part 4 - non integer response id? to manually set up the Yes/No buttons.
We also make use of the code from How to set up radio buttons with default all unselected? to process the selection of radio buttons.
What's new here:
- Set up the radio buttons.
- Pack the radio group into the dialog vbox.
- Process radio button toggle. Note that we save the selected radio item together with its value in the global variable
$selected_radio
and$selected_radio_value
. - When the user presses the OK button, we manually close the dialog.
- Echo the radio button selection.
Related Links
- How to setup a dialog box of radio buttons - Part 2 - make sure user selects?
- How to setup a dialog box of checkboxes - Part 1?
- How to setup a dialog box of checkboxes - Part 2 - check all and clear all?
- How to make a dialog stay for invalid input - Part 1?
- How to make a dialog stay for invalid input - Part 2 - ESC exit dialog?
- How to popup a dialog to select from list - Part 1 - select and click OK?
- How to popup a dialog to select from list - Part 2 - select with double click?
- How to popup a dialog to select from list - Part 3 - select by pressing enter?
- How to popup a dialog to select from list with search fields - Part 1?
- How to setup a dialog box - Part 1 - hello dialog?
- How to setup a dialog box - Part 2 - simple yes no dialog?
- How to setup a dialog box - Part 3 - set up buttons manually?
- How to setup a dialog box - Part 4 - non integer response id?
- How to setup a dialog box - Part 5 - get text entry?
Read more...