169. How to set default button of GtkDialog - Part 1 - using set_default_response?

Problem

Suppose you have set up three buttons in a GtkDialog, and you want "Button 2" to be the default button when the user press Enter as shown below:

How to set default button of GtkDialog - Part 1 - using set_default_response?


Solution


Sample Code

1   
2   
3   
4   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
<?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 1\n".
"using set_default_response");
$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, // note 1
'button 2', 101, 
'button 3', 102)); 

$dialog->set_default_response(101); // note 2

$dialog->set_has_separator(0);
$dialog->show_all();
$response = $dialog->run();

echo "response = $response\n";
?>

Output

As shown above.
 

Explanation

  1. Create the buttons.
  2. Here we specify button 2 (response ID 101) as the default button.

Related Links

Add comment


Security code
Refresh