2.1 Understand the Expand parameter
Written by kksou   
Friday, 14 March 2008

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

2.1.gif

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

  1. Pack button1 and button2 into vbox. Note that we set the expand parameter to false.

2.1.2.gif

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.

2.1_spring_box.gif

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.

2.1.1.gif

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.

2.1.2.gif

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.

2.1.3.gif



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.

2.1.4.gif

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.

2.1.5.gif

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.

2.1.6.gif

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.

2.1.7.gif

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)
  1. bescherelle
    March 18, 2008 8:18am
    Spring box

    a good idea

  2. Yuan Quan
    June 15, 2008 9:05am

  3. Doug
    June 19, 2008 7:03am

  4. XeKtRuM
    July 14, 2008 10:52am
    ufff!!

    Finally some good explanation to this!! xD

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