Introduction to Gtk2-Perl
[30 mn de lecture - paru le 10/7/2004 3:02:44 PM - Public : Confirmé]
|
   
|
Auteur
1. Eléments de base
We will discover in this chapter some basic elements.
The first stage is the creation of the window because without it, there will be no graphic application.
1.1. Windows
The window is the very first container which is allocated by the windows manager. As we saw in the introduction this procedure is as simplified as the placement of any object in the window. Let's see how a simple window is created:
my $fenetre = Gtk2::Window->new('toplevel');
The 'toplevel' argument means that the window, that will be created, is allocated by the window manager. It will have the default characteristics because we did not specify any of other parameters. The default size is of 200x200. the other type of possible window is a ' popup ' one.
The properties and the window's methods are very numerous. It is thus impossible to detail them one by one. Here are the most useful ones:.
Allows to put a title on the window:
$fenetre->set_title( $titre );
The window can contain others objects and it is thus possible to define the object which will get the focus and the one that will get it by default:
$fenetre->set_focus( $widget );
$fenetre->set_default( $widget );
The redimensioning policy:
$fenetre->set_policy( $allow_shrink, $allow_grow, $auto_shrink );
Arguments are boolean:
- allow_shrink autorise ou non la réduction de la fenêtre en dessous de la taille définie
- allow_grow autorise ou non l'agrandissement au dela de la taille définie
- auto_shrink spécifie ou non le redimensionnement automatique
The size of the window and its position:
$fenetre->set_default_size( $width, $height );
$fenetre->set_position( $position );
Dimensions are in pixels, and the position can be one of the following:
- none means the window's position is chosen by the window manager
- center will place the window in dhe middle of the screen
- mouse will place the window under the cursor
Don't forget to show the window :
$fenetre->show();
And to launch the main loop:
Gtk2->main ;
Windows have a strange characteristic because they can contain only one object. At this stage the graphic programming would allow only the creation of dialog boxes like "Clik OK". Fortunately there are objects which are able to contain other objects: boxes and the tables.
1.2. Boxes
A box is a container which contains other objects. There are two of them:
- Vertical filling Box (VBOX)
- Horizontal filling box (HBOX)
$boite= Gtk2::HBox->new($homogeneous,$spacing);
ou
$boite= Gtk2::VBox->new($homogeneous,$spacing);
The parameters are :
- homogenous (bool) sets the homogenous placement of objects (widgets)
- spacing (int) define de space between two widgets
To put the objects in the box:
$boite->pack_start($child,$expand,$fill,$padding);
ou
$boite->pack_end($child,$expand,$fill,$padding);
The first piles up the objects while starting by the beginning and the second by the end with those parameters:
- child is the object to put in the box
- expand announce or not the objects must use all the space which is reserved to them (like the option 'justify' for a text in an editor)
- fill announce or not the object must fill reserved space (like width=100% in HTML)
- padding is the padding between objects
There are of course properties allowing the modification of all the aspects of th box on the fly.
It is obvious that if one creates an object HBOX in which one arranges horizontally vboxes, we will get a calculator :). The operation of stacking to get the desired result can quickly become a nightmare. The solution comes from the tables which make it possible to divide the aplication into zones of equal size and to fill the zones with any object or to leave them empty.
1.3. Tables
The difference between a table and box is of course the second dimension. It will be always possible, to gett the desired result, while using both. The starting origin of a table is the corner in top left corner. The boxes are not numbered but use a system of coordinates.
0 1 2 3
0+----------+----------+----------+
| | | |
1+----------+----------+----------+
| | | |
2+----------+----------+----------+
| | | |
3+----------+----------+----------+
To create a table :
$table = Gtk2::Table->new( $nb_lignes, $nb_colonnes, $homogeneous );
Creates a table of nb_lignes of lines nb_colones of columns . Homogenous is a boolean which indicates if all fields have to be of the same dimension.
To add objects in the table:
$table->attach( $child, $left_attach, $right_attach, $top_attach, $bottom_attach,
$xoptions, $yoptions, $xpadding, $ypadding );
The parameters are:
- child is the object to add
- left/right/top/bottom_attach are the coordinates of the field
- xoptions/yoptions are options: 'shrink', 'fill' et 'expand'. We can use all of the at the same time like this: [opt1,opt2]
- xpadding/ypadding is the padding expressed in pixels
We can set the spacing too:
$table->set_row_spacing( $row, $spacing );
$table->set_col_spacing( $column, $spacing );
Spacing is an empty space added at the bottom/right of the chosen line/column. If there is no line/column specified, the spacing will apply to all of them
1.4. Labels & Cadres
Labels are simple text strings in the window:
$label = Gtk2::Label->new ($string);
$string the text to show.
We can find out this text and modify it:
$label->get();
$label->set_text($string);
The text may be justified or left/right/center alligned:
$label->set_justify( $type );
where type is: 'left', 'center', 'right' ou 'fill'.
We still can put a nice frame around the label that we create with :
$cadre = Gtk2::Frame->new( ”Titre du Cadre/Label ” ) ;
And set it around the label :
$cadre->add( $label ) ;
1.5. Buttons
Buttons are interactive elements. To create a button:
$bouton = Gtk2::Button->new( $label );
$label is the text to show in it, which may be left empty.
The button's label is a child widget that we can access using a particular method:
$bouton->child->set( "New Label" );
The buttons react to the actions and thus to the signals emitted during the execution of an action. The signals are intercepted by a method of an parrent object which is Glib::object and the method in question makes it possible to connect to the signals:
$bouton->signal_connect( ”clicked” , \&ClickedButtonEvent ) ;
The first parameter is the signal to be intercepted, and the second one is the action to run (a function). Buttons may generate those signals:
- pressed
- released
- clicked (pressed AND released)
- enter when the cursor passes over the button
- leave when the cursor leaves de button
We can set the button's relief :
$bouton->get_relief();
$bouton->set_relief($style);
where style is 'normal', 'half' or 'none'.
|