Problem
You have set up a popup dialog to prompt user to make a selection from a group of radio buttons in How to setup a dialog box of radio buttons - Part 1?
Suppose it is necessary that the user make a selection (e.g. software download for different platform). If the user did not make any selection, you will popup an alert message as shown below:
Solution
- Set up the popup dialog with radio buttons as outlined in the article How to setup a dialog box of radio buttons - Part 1?
- When the user clicks on the OK button, we check if the user has made any selection. If there is no selection, we popup an alert message box. If yes, we destroy the dialog manually with GtkObject::destroy().
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 59 60 61 63 64 65 66 67 68 69 70 72 74 75 76 77 78 79 80 81 82 83 84 85 86 92 93 94 95 96 97 98 99 100 102 103 104 105 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | <?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\n". "Part 2 - user has to make a choice"); $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'); $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); $dialog->vbox->pack_start($radio2, 0, 0); $dialog->vbox->pack_start($radio3, 0, 0); $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)"); } function on_ok_button($button, $dialog) { global $selected_radio; if ($selected_radio=='') { alert("Please make a selection."); // note 1 } else { $dialog->destroy(); // note 2 } } 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) { $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; } } // display popup alert box function alert($msg) { // note 1 $dialog = new GtkDialog('Alert', null, Gtk::DIALOG_MODAL); $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS); $top_area = $dialog->vbox; $top_area->pack_start($hbox = new GtkHBox()); $stock = GtkImage::new_from_stock(Gtk::STOCK_DIALOG_WARNING, Gtk::ICON_SIZE_DIALOG); $hbox->pack_start($stock, 0, 0); $hbox->pack_start(new GtkLabel($msg)); $dialog->add_button(Gtk::STOCK_OK, Gtk::RESPONSE_OK); $dialog->set_has_separator(false); $dialog->show_all(); $dialog->set_keep_above(1); $dialog->run(); $dialog->destroy(); } ?> |
Output
As shown above.Explanation
The above example is an extension of the code from How to setup a dialog box of radio buttons - Part 1?
We also make use of the code from How to display a popup alert for required fields - Part 1? to display the alert box.
What's new here:
- If there is no selection, we popup an alert box.
- If the user has already made a selection, we manually close the dialog.
Related Links
- How to setup a dialog box of radio buttons - Part 1?
- 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...