|
Objective
It's a good time now to introduce the spacer function, which you will find useful in all the php-gtk applications that you'll be working on.
Overview
The idea of spacer should be very familiar to those people who have developed websites. Before CSS became popular, we usually use tables and "spacers" to achieve the desired layout. We use the spacer, which is just a small transparent image, set its width and height, to achieve proper layout and positioning.
If you have run through the previous example, you will find the technique to achieve precise positioning in php-gtk is almost entirely similar.
Since we use spacers so often in php-gtk, let's turn it into a function so that anytime we need to add a spacer, we just use a one-liner spacer($hbox, 10).
Now that we have the spacer function, let's also conveniently use it to add a gap of 4 pixels so that the buttons are 4 pixels away from the top of the window. Let's also leave a gap of 3 pixels between the first button and the left side of the window so that it's aesthetically more pleasing to the eyes.
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
| <?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); // note 2
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); // note 3
}
spacer($vbox, 4); // note 1
$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"; }
function spacer($container, $gap) { // note 4
|
- 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:
- Leave a gap of 4 pixels between top of window and the buttons.
- Leave a gap of 3 pixels between left of window and 1st button.
- Leave a gap of 10 pixels between the buttons.
- This is the spacer function. It just wraps the method as outlined in the previous example into a function. Note that GtkWidget::get_name() is a useful function that can be used to find out whether
$container is a hbox or a vbox.
For hbox, the gap will be horizontal. Hence we create a new hbox as the spacer and set its width using $spacer->set_size_request($gap, -1) .
For vbox, the gap will be vertical. Hence we create a new vbox as the spacer and set its height using $spacer->set_size_request(-1, $gap) .
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |