2.11 Introducing the expandable spacer
Written by kksou   
Friday, 28 March 2008

Objective

Now that we have the useful spacer() function, let's also create another very useful function – the expandable_spacer() function!

As an example on the use of expandable_spacer(), let's assume now you want the three buttons in the previous example to always stay at the bottom of the window.

Overview

This is the equivalent of the "spring box" which we used to right or center align the buttons in the earlier examples. The concept is very similar to a spacer, except this spacer has no fixed width or height. BUT, it has "springs" inside, always trying to grab whatever space it can get. So we will call this "spring box" an expandable spacer.

Note that for expandable spacer, you do not need to specify any width or height, since their width/height is dynamic. They will grab whatever space they can get when you do any resize of the windows.

Sample Output

2.11.gif

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   
<?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 1
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);
}

expandable_spacer($vbox); // note 2
$vbox->pack_start($hbox, false);
spacer($vbox, 4); // note 3
$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) {
    if ($container->get_name()=='GtkHBox') {
        echo "horizontal spacer: $gap\n";
        $spacer = new 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:

  1. As in the previous example, let's leave a gap of 3 pixels between left of window and first button.
  2. Insert an expandable spacer at the top of the vbox so that the buttons will be pushed to stay at the bottom of the window.
  3. Leave a gap of 4 pixels between the buttons and bottom of window.
  4. This is the expandable spacer function. Note that this is just a one-liner! As explained in the previous examples, all we need here is a empty rectangular widget to act as a spring box. We used GtkHBox here. But you could replace this with a GtkVBox, or an empty GtkLabel


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