2.16 A form with three fields - Part 3
Written by kksou   
Monday, 07 April 2008

Objective

In the previous example, the field label is centered. We will fix this to make it right-justified.

We'll fix this in this example.

Overview

GtkLabel, by default, centers the text. To make it left- or right-justified, just create a new hbox, pack it inside with expand set to false.

  • If you want it left-justified, add an expandable spacer to its right.
  • If you want it right-justified, add an expandable spacer to its left.

Sample Output

2.16.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   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy',array('Gtk','main_quit'));
$window->set_size_request(400, 200);
$vbox = new GtkVBox();
$window->add($vbox);
spacer($vbox, 4); // add a 4-pixel top margin

$input_field_def = array(
    'Item Code'=>120,
    'Item Description'=>240,
    'Price'=>80);

foreach($input_field_def as $label=>$field_width) {
    $hbox = new GtkHBox();
    spacer($hbox, 3); // add a 3-pixel left margin

    // the label
    $label = new GtkLabel("$label: ");
    $label_box = new GtkHBox(); // note 1
    $label_box->set_size_request(100, -1); // note 2
    expandable_spacer($label_box); // note 3
    $label_box->pack_start($label, false); // note 4
    $hbox->pack_start($label_box, false);

    // the entry field
    $input = new GtkEntry();
    $input->set_size_request($field_width, -1);
    $hbox->pack_start($input, false);

    $vbox->pack_start($hbox, false);
}

// the submit button
$hbox = new GtkHBox();
$button = new GtkButton('Submit');
$button->set_size_request(60,24);
$button->connect('clicked', 'on_submit', $input);
spacer($hbox, 3);
spacer($hbox, 100);
$hbox->pack_start($button, false);

spacer($vbox, 6); //add a 6-pixel gap
$vbox->pack_start($hbox, false);

$window->show_all();
Gtk::main();

  • 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 the previous example.

What's new here:

  1. Create a new hbox to hold the label.
  2. Set the hbox size to the desired width.
  3. We want the label to be right-justified in this example. So we add an expandable spacer first.
  4. Then we pack the label into the hbox with expand=false.

Note

If you have used GtkTable before, you will realize that by default the contents are also displayed centered. You can use the same technique as described above to perform left- or right-justification of the contents.



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