PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 288: How to setup a dialog box of checkboxes - Part 1?
Written by kksou   
Wednesday, 18 July 2007
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 checkboxes in a dialog box as shown below:

How to setup a dialog box of checkboxes - Part 1?


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   
<?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 Checkboxes - Part 1\n");
$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->set_title('');

    $dialog->vbox->pack_start(new GtkLabel("Which platforms are you using: \n".
    "(You can select more than one)"));

    global $checkbox1, $checkbox2, $checkbox3;
    $checkbox1 = setup_checkbox('Windows'); // note 1
    $checkbox2 = setup_checkbox('Mac');
    $checkbox3 = setup_checkbox('Linux');

    // pack them inside vbox
    $dialog->vbox->pack_start($checkbox1, 0, 0); // note 2
    $dialog->vbox->pack_start($checkbox2, 0, 0);
    $dialog->vbox->pack_start($checkbox3, 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, $selections;
    $response->set_text(implode(', ', $selections)); // note 7
}

function on_ok_button($button, $dialog) {
    global $checkbox1, $checkbox2, $checkbox3;
    $status1 = $checkbox1->get_active()?$checkbox1->get_label(): ''; // note 3
    $status2 = $checkbox2->get_active()?$checkbox2->get_label(): '';
    $status3 = $checkbox3->get_active()?$checkbox3->get_label(): '';

    global $selections;
    $selections = array();
    if ($status1!='') $selections[] = $status1; // note 4
    if ($status2!='') $selections[] = $status2;
    if ($status3!='') $selections[] = $status3;

    if (count($selections)==0) {
        alert("Please make a selection."); // note 5
  • 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

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 setup and process checkboxes? to process the selection of checkboxes.

What's new here:

  1. Set up the checkboxes.
  2. Pack the checkboxes into the dialog vbox.
  3. Get the status of each checkbox.
  4. If the checkbox is selected, add this to the array $selections.
  5. Popup an alert if the user didn't make any selections.
  6. Manually close the dialog.
  7. Echo the checkbox selection.

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