|
Objective
Are you confused about the expand parameter that exists in the methods of many widgets?
Read this chapter. You'll never get confused again!
Overview
Sample Output

Sample Code
1 2 3 4 5 6 7 8 9
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkButton('button1'), true); // note 1
$vbox->pack_start(new GtkButton('button2'), true); // note 2
|
- 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
- Pack button1 and button2 into vbox. Note that we set the expand parameter to false.

|
 |
Compare the above with that of setting the expand parameter to true as shown in the figure on the lefet.
See the difference?
|
The "Spring Box" Model
|
The best way to understand the expand parameter is to think of a button with expand set to true as a button with a spring inside, as shown in the diagram on the right.
Because of the spring, a button with expand=true will fight for any space available to it.
|
 |

|
One Expandable Button
|
So for the code below, you will find that button1 will grab the entire window space because of the "expandable spring" inside it.
1 2 3 4 5 6 7 8
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkButton('button1'), true);
|
- 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.
|
 |

|
Two Expandable Buttons
|
If we have two buttons, both set to expand=true, you will find that button1 and button2 will both fight for the available space. As the two springs are of equal strength, the result is that each will grab half the space.
1 2 3 4 5 6 7 8 9
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkButton('button1'), true); $vbox->pack_start(new GtkButton('button2'), true);
|
- 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.
|
 |

|
Three Expandable Buttons
|
If we have two buttons, both set to expand=true, you will find that button1 and button2 will both fight for the available space. As the two springs are of equal strength, the result is that each will grab half the space.
1 2 3 4 5 6 7 8 9
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkButton('button1'), true); $vbox->pack_start(new GtkButton('button2'), true);
|
- 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.
|
 |

|
One Button – no spring
|
When you set expand=false, think of this as a button without any spring inside. Since there are no expandable spring, the widget will take on its default size:
1 2 3 4 5 6 7 8
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkButton('button1'), false);
|
- 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.
|
 |

|
Two Buttons – first no spring, second with spring
|
When you set expand=false, think of this as a button without any spring inside. Since there are no expandable spring, the widget will take on its default size:
1 2 3 4 5 6 7 8 9
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkButton('button1'), false); $vbox->pack_start(new GtkButton('button2'), true);
|
- 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.
|
 |

|
Three Buttons – off, on, off
|
Suppose now you have three buttons, the first and third with expand=false and the second with expand=true. Because of the spring in the second button, it will expand – pushing the first button all the way up and the third button all the way down:
1 2 3 4 5 6 7 8 9
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkButton('button1'), false); $vbox->pack_start(new GtkButton('button2'), true);
|
- 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.
|
 |

|
Same for GtkLabel
|
The "spring box" model applies to GtkLabel too. Because a label has no borders like that of a button, you only see the text. But the effect is the same.
1 2 3 4 5 6 7 8 9
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 240); $vbox = new GtkVBox(); $window->add($vbox);
$vbox->pack_start(new GtkLabel('label1'), false); $vbox->pack_start(new GtkLabel('label2'), true);
|
- 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.
|
 |

|
Important Note
Understanding the expand parameter is key to understanding layout and positioning in php-gtk. So it is important that you understand all the examples in this article before moving on.
User reviews Average user ratings: 4.5 (from 4 users) Note: You have to be a registered member to leave a comment. Free registration here. |
March 18, 2008 8:18am
a good idea
June 15, 2008 9:05am
June 19, 2008 7:03am
July 14, 2008 10:52am
Finally some good explanation to this!! xD