|
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:

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
| <?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
} }
|
- 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 in Part 1.
What's new here:
- Set up
key-press-event.
- Get the ID of the default button (assuming button 2 is the default button).
- "Highlight" the button.
- Check if user presses Enter.
- Check which button the user is currently on.
- Manually generate button click signal.
- 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
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |