2.9 Precise positioning of buttons
Written by kksou   
Wednesday, 26 March 2008

Objective

In the previous example, we have arbitrarily left a gap between the buttons.

Suppose now you want to this gap to be exactly of 10 pixels wide between the buttons.

Overview

You might have read somewhere that it's not possible to do precise positioning in php-gtk, because everything is relative. The only way is to use GtkFixed but this is strongly discouraged because there will no longer be automatic layout management provided by php-gtk. This, is not really true.

If you understand the previous few examples, you already know how to do precise positioning and sizing in php-gtk! – yes, it is just a matter of stacking a pile of hboxes and vboxes on top of each other (just like the lego blocks), with the right setting of the expand parameter during packing.


Sample Output

2.9.gif

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
<?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();
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);

    // add precisely a 10-pixel gap
    $spacer = new GtkHBox(); // note 1
    $spacer->set_size_request(10, -1); // note 2
    $hbox->pack_start($spacer, false); // note 3
}

  • 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 the previous example.

  1. Create a new hbox. This hbox is the gap between the buttons.
  2. Set the width of the hbox (i.e. the gap) to width=10. Since we are only concerned about the width here, we set the height to -1.
  3. Now pack this gap into the outside hbox with expand=false.

Now try to resize the window. You will see that the gap will always remain precisely at 10 pixel because we have set expand=false.



User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved