Previous Up Next
Basic Widgets Quick start guide

Layouts

In this section, we will be discussing the different types of layout managers you can use for designing a GUI. These types are RowLayout and GridLayout.

RowLayout

RowLayout arranges widgets in rows, as indicated by its name. We can declare a RowLayout to be horizontal or vertical, making the widgets arranged in either horizontal rows or vertical columns.

When the wrap field is set to true, if a row is too long for the window, the layout will wrap it around to form two rows.

The following example shows how you can use the RowLayout. The wrap field is set to true, so depending on the size of the window, there can be more than one row:

$row_layout =& new RowLayout();
$shell->set_layout($row_layout);
$b1 =& new Button($shell, STYLE_PUSH);
$b1->set_text("Address");
$b2 =& new Button($shell, STYLE_PUSH);
$b2->set_text("Phone Numbers");
$b3 =& new Button($shell, STYLE_PUSH);
$b3->set_text("Credit Cards");
$b4 =& new Button($shell, STYLE_PUSH);
$b4->set_text("Organizations");
$b5 =& new Button($shell, STYLE_PUSH);
$b5->set_text("Cancel");
$b6 =& new Button($shell, STYLE_PUSH);
$b6->set_text("OK");

GridLayout

GridLayout offers much more flexibility that RowLayout does, but this flexibility comes at a cost. GridLayout has many fields that need manipulating, as well as GridData, which is almost always necessary to add to widgets in order to fully get the benefits of the GridLayout manager.

The most important field to get a handle on with respect to GridLayout are the num_column field. This allows you to determine the number of columns of widgets you would like on your window. You should always make the number of columns equal to the maximum number that you would desire in a particular row. Once you determine the number of columns, you can make any widget span as many columns as you wish. This allows the placement of the widgets to be very flexible.

As opposed to RowLayout, where RowData objects are available but not usually necessary, GridData objects are quite important in a GridLayout. The most important aspect of the GridData objects are the fields which allow you to control the behavior of widgets when the screen is resized. For example, letís look at the following piece of code, which demonstrates lining up labels and text boxes, a very common arrangement:

$grid_layout =& new GridLayout();
$grid_layout->num_columns = 2;
$shell->set_layout($grid_layout);
$label1 =& new Label($shell);
$label1->set_text("Name:");
$text1 =& new Text($shell);
$label2 =& new Label($shell);
$label2->set_text("Age:");
$text2 =& new Text($shell);
$label3 =& new Label($shell);
$label3->set_text("Gender:");
$text3 =& new Text($shell);
$button =& new Button($shell, STYLE_CHECK);
$button->set_text("Have you been employed in the past six months?");
$data =& new GridData();
$data->width_hint = 60;
$label1->set_layout_data($data);
$data =& new GridData();
$data->width_hint = 60;
$label2->set_layout_data($data);
$data =& new GridData();
$data->width_hint = 60;
$label3->set_layout_data($data);
$data2 =& new GridData(GRIDDATA_GRAB_HORIZONTAL);
$text1->set_layout_data($data2);
$data2 =& new GridData(GRIDDATA_GRAB_HORIZONTAL);
$text2->set_layout_data($data2);
$data2 =& new GridData(GRIDDATA_GRAB_HORIZONTAL);
$text3->set_layout_data($data2);
$data3 =& new GridData();
$data3->horizontal_span = 2;
$button->set_layout_data($data3);
Note that a separate GridData object has to be declared for each widget. We have two columns in this layout, and the button at the bottom of the screen spans both columns.

In this example, the text widgets are set to fill horizontally. This means that not only do the text widgets initialize their size to fill up the rest of the screen width, but also when we resize the screen, they will grab the extra space:

Previous Up Next
Basic Widgets Quick start guide

Documentation generated on Wed, 2 Feb 2005 09:33:48 +0100 by phpDocumentor 1.3.0RC3