2.12 Add a Quit button that always stay at top right-hand corner |
|
Written by kksou
|
|
Monday, 31 March 2008 |
Objective
Let's have one more example on the combined use of spacer and expandable_spacer.
Suppose now you want to add a Quit button such that this button will always stay at the top right-hand corner, no matter how the user resize the window.
Overview
We just need to insert an expandable spacer between the third button and the Quit button.
Sample Output

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
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(400, 100); $vbox = new GtkVBox(); $window->add($vbox);
$hbox = new GtkHBox(); spacer($hbox, 3); for ($i=1; $i<=3; ++$i) { $button = new GtkButton('button'.$i); $button->set_size_request(60,32); $button->connect('clicked', 'on_click'); $hbox->pack_start($button, false); spacer($hbox, 10); }
// Add a quit button
$button = new GtkButton('Quit'); $button->set_size_request(60,32); $button->connect('clicked', 'on_click', $button); expandable_spacer($hbox); // note 1
$hbox->pack_start($button, false); spacer($hbox, 3); // note 2
spacer($vbox, 4); $vbox->pack_start($hbox, false); $window->show_all(); Gtk::main();
function on_click($button) { $button_label = $button->get_label(); echo "you have clicked the button: $button_label\n"; if ($button_label=='Quit') Gtk::main_quit(); // note 3
}
function spacer($container, $gap) { if ($container->get_name()=='GtkHBox') {
|
- 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 previous example.
What's new here:
- Insert an expandable spacer between the Button3 and the Quit button.
- Just like the first button, we also leave a 3-pixel gap between the Quit button and the right margin.
- Exit the application if user clicks the Quit button.
Try to resize the window. You will see that the Quit button will always stay at the top right-hand corner of the window.
|