170. How to set default button of GtkDialog - Part 2 - using keypress_event?

Problem

In Part 1, you have set up three buttons in a GtkDialog, with "Button 2" as the default button when the user press Enter.

In Part 1, we use set_default_response.

In this example, we will use key-press-event to achive the same effect as shown below:

As as shown below:

How to set default button of GtkDialog - Part 2 - using keypress_event?


Solution


Sample Code

1   
2   
3   
4   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
18   
19   
20   
21   
22   
23   
24   
26   
27   
28   
29   
30   
32   
33   
34   
35   
36   
37   
38   
39   
40   
42   
43   
44   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
61   
62   
63   
64   
65   
66   
67   
<?php
$dialog = new GtkDialog();
$dialog->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$dialog->set_size_request(400,150);

// display title
$title = new GtkLabel("Set Default Button - Part 2\n".
"using key-press-event");
$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);
$dialog->vbox->pack_start($alignment, 0, 0);
$dialog->vbox->pack_start(new GtkLabel(), 0, 0);

$dialog->add_buttons(array('button 1', 100,
'button 2', 101,
'button 3', 102));

$dialog->connect('key-press-event', 'on_keypress'); // note 1

$dialog->set_has_separator(0);
$dialog->show_all();

$button2 = get_button($dialog, 'button 2'); // note 2
$button2->grab_focus(); // note 3

$response = $dialog->run();

echo "response = $response\n";

function on_keypress($dialog, $event) {
    if ($event->keyval==Gdk::KEY_Return) { // note 4
        foreach(array('button 1', 'button 2', 'button 3')  as $button_label) {
            $button=get_button($dialog, $button_label);
            if ($button->is_focus()) { // note 5
                $button->clicked(); // note 6
            }
        }
        return true;
    } else {
        return false;
    }
}

function get_button($dialog, $button_label) { // note 7
    $vbox_contents = $dialog->vbox->get_children();
    $buttonbox = $vbox_contents[2];
    $buttons = $buttonbox->get_children();
    foreach($buttons as $button) {
        if ($button->get_label() == $button_label) {
            return $button;
        }
    }
}

?>

Output

As shown above.
 

Explanation

We make use of the code in Part 1.

What's new here:

  1. Set up key-press-event.
  2. Get the ID of the default button (assuming button 2 is the default button).
  3. "Highlight" the button.
  4. Check if user presses Enter.
  5. Check which button the user is currently on.
  6. Manually generate button click signal.
  7. A function to return the button ID given the button label.

Note

You might feel that this method (using key-press-event) seems to be more complicated than Part 1 (using set_default_response).

Yes, it is. But the method illustrated in this example gives you much more control over button-press and keypress.

If this sounds confusing, don't worry. Just remember there's this method of "digging" into GtkButtonBox to get the button IDs. Maybe some day it might help you in your application.

Related Links

Add comment


Security code
Refresh