PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 243: How to change tab order - Example 6?
Written by kksou   
Wednesday, 23 May 2007
Problem

Note: This is a series of examples to show you how to change tab orders in PHP-GTK2.

This example shows setting tab orders for a mix of GtkEntry's and GtkButton. We start from the example as given in the article How to align GtkEntry fields - Part 2?

Suppose you would like to set the tab orders such that it will first start on the Submit button, followed by the tab orders through the GtkEntry's as shown below:

How to change tab order - Example 6?


Solution

For your reference, here are the rules to use GtkContainer::set_focus_chain():.

  1. The method takes only one argument - an array of GtkWidgets.
  2. All the widgets in the array must be direct descedents of the container.
  3. It's not necessary to define the tab orders of all levels.

If you have followed through Examples 1 to 5, you will now know how to set the tab orders for this example.

The key to this example is: you need to use the GtkAlignment of each GtkEntry, and not the GtkEntry itself — Rule #2 again!


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   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set tab order - Example 6");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$fields = array('Item number', 'Item Description', 'Unit price', 'Quantity');
$field_size = array(120, 200, 80, 80);
$entry = array(); // to store the text entries

$table = new GtkTable();
display_table($table, $fields, $field_size); // display the table
$vbox->pack_start($table);

function display_table($table, $fields, $field_size) {
    global $entry, $field_alignment;
    $row = 0;
    foreach ($fields as $field) {
        $label = new GtkLabel("  $field: ");
        $alignment = new GtkAlignment(1, .5, 0, 0);
        $alignment->add($label);
        $table->attach($alignment, 0, 1, $row, $row+1,
            Gtk::FILL, Gtk::SHRINK, 0, 0);

        $entry[$row] = new GtkEntry();
        $alignment = new GtkAlignment(0, .5, 0, 0);
        $alignment->add($entry[$row]);
        $entry[$row]->set_size_request($field_size[$row], -1);
        $table->attach($alignment, 1, 2, $row, $row+1);
        $field_alignment[$row] = $alignment; // note 1
        ++$row;
    }

}

// create a submit button
$button = new GtkButton('Submit');
$button->set_size_request(60, 28);
$button->connect('clicked', 'on_click');
  • 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

The above example makes use of the code from How to align GtkEntry fields - Part 2?

What's new here:

  1. Take note of the pointer to GtkAlignment for each of the GtkEntry.
  2. Set the tab orders of the GtkEntry via GtkAlignment.

Related Links

User reviews   Average user ratings:    5.0   (from 1 user)
  1. Veronica Bendersky
    April 03, 2008 11:05am

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved