235. How to setup a dialog box - Part 5 - get text entry?

Problem

The GtkDialog constructor example in the offical PHP-GTK2 documentation shows only how to get a yes/no response using a dialog box.

Suppose you would like to get inputs from user using textentry in a dialog box as shown below:

How to setup a dialog box - Part 5 - get text entry?


Solution


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   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
53   
54   
55   
56   
57   
59   
60   
61   
62   
63   
64   
65   
66   
67   
69   
70   
71   
72   
<?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 - Part 5\n".
    "Getting text entry from a dialog box");
$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('Phone Number: '), 0);
$hbox->pack_start($response = new GtkLabel(), 0);
$hbox->pack_start($button = new GtkButton('Enter Phone Number'), 0);
$button->connect('clicked', 'on_click');

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

function on_click() {
    setup_yes_no_dialog();
}

function setup_yes_no_dialog() {

    $dialog = new GtkDialog();

    $dialog->vbox->pack_start($hbox = new GtkHBox());
    $hbox->pack_start(new GtkLabel('Phone Number: '), 0);
    $hbox->pack_start($phone = new GtkEntry(), 0); // note 1

    $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);
    $phone->connect('activate', 'on_enter', $button_ok); // note 2
    $button_ok->connect('clicked', 'on_ok_button', $dialog);

    $dialog->set_has_separator(false);
    $dialog->action_area->set_size_request(-1, 1);
    $dialog->show_all();
    $dialog->run();
    $dialog->destroy();

    global $response;
    $response->set_text($phone->get_text());
}

function on_ok_button($button, $dialog) { // note 3
    $dialog->destroy();
}

function on_enter($entry, $button) {
    $button->clicked(); // note 4
}

?>

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.

What's new here:

  1. Create a new gtkentry for phone number.
  2. Register the signal "activate" so that we know whne user presses Enter.
  3. Destroy the dialog.
  4. If the user presses Enter, we simulate a mouse click on the button.
  5. Echo the content of the GtkEntry.

Related Links

Add comment


Security code
Refresh