242. How to change tab order - Example 5?

Problem

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

Suppose for one reason or another, you need to set the tab orders such that it traverse diagonally across two columns of buttons as shown below:

How to change tab order - Example 5?


Solution

Here are the rules to use GtkContainer::set_focus_chain() again:.

  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.

Because of Rule #2 above, if you use only hbox and vbox, there is no way you can set the tab orders such that it traverse diagonally across two columns.

So let's change our container here and use GtkTable instead! With GtkTable, all the buttons are at the same level, and we can set the tab orders in whatever ways we want!


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
17   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(480, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set tab order - Example 5");
$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);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0.5, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0);
$alignment = new GtkAlignment(0.5, 0.5, 0, 0);
$alignment->add($label = new GtkLabel(
"The tab orders are as indicated on the button.\n".
"It starts from the top left button and traverse ".
"diagonally downwards"));
$label->set_justify(Gtk::JUSTIFY_CENTER);
$vbox->pack_start($alignment);
$vbox->pack_start(new GtkLabel(), 0);

$button = array();
$table = setup_buttons($vbox, array(1, 8, 7, 2, 3, 6, 5, 4)); // note 1

// set the tab order
$table->set_focus_chain(array( // note 2
    $button[1], $button[2], $button[3], $button[4], 
    $button[5], $button[6], $button[7], $button[8])); 

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

function setup_buttons($container, $indexes) {
    global $button;
    $table = new GtkTable();
    $container->pack_start($table);
    $row = 0;
    $col = 0;
    foreach($indexes as $index) {
        $button[$index] = new GtkButton("tab order $index");
        $button[$index]->connect('clicked', 'on_click', $index);
        $table->attach($button[$index], $col, $col+1, $row, $row+1);
        ++$col;
        if ($col>1) {
            $col = 0;
            ++$row;
        }
    }
    return $table;
}

function on_click($button, $index) {
    echo "You clicked me: button[$index]!\n";
}

?>

Output

As shown above.

 

Add comment


Security code
Refresh