240. How to change tab order - Example 3?

Problem

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

Let's continue to extend Example 2.

Suppose now you would like to change the tab order to start from the bottom right button, followed by bottom left button, and then traverse upwards, as shown below:

How to change tab order - Example 3?


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.

Note: If you tried to use the previous example and do something like:

$vbox->set_focus_chain(array(
    $button['right'][3], $button['left'][3],
    $button['right'][2], $button['left'][2]));

It won't work because of Rule #2 above.

To be able to set the tab orders so that they traverse horizontally, you have to rework the buttons so that they are packed in horizontal hboxes. Once you have the hboxes in place, you then apply GtkContainer::set_focus_chain():

  • To change the tab order to start from bottom hbox to top hbox.
  • Within each hbox, change the tab order to start from the righ button first, then the left button.

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   
36   
37   
38   
39   
40   
41   
42   
43   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
<?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 4");
$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 bottom right button and traverse ".
"horizontally upwards"));
$label->set_justify(Gtk::JUSTIFY_CENTER);
$vbox->pack_start($alignment);
$vbox->pack_start(new GtkLabel(), 0);


$button = array();
$hbox = array();
for ($row=0; $row<4; ++$row) {
    setup_buttons($vbox, $row);
}

// set the tab order
//$vbox->set_focus_chain(array( //does not work
// $button['right'][3], $button['left'][3], //does not work
// $button['right'][2], $button['left'][2])); //does not work

$vbox->set_focus_chain(array(
    $hbox[3], $hbox[2], $hbox[1], $hbox[0]));

for ($row=0; $row<4; ++$row) {
    $hbox[$row]->set_focus_chain(array(
        $button['right'][$row], $button['left'][$row]));
}

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

function setup_buttons($container, $row) {
    global $button, $hbox;
    static $button_counter=8;
    $hbox[$row] = new GtkHBox();
    $container->pack_start($hbox[$row]);

    foreach(array('left', 'right') as $index) {
        $button[$index][$row] = new GtkButton(
            "button[$index][$row] (tab order $button_counter)");
        $hbox[$row]->pack_start($button[$index][$row]);
        $button[$index][$row]->connect('clicked', 'on_click', $index, $row);
        --$button_counter;
    }
}

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

?>

Output

As shown above.

 

Explanation

  1. Set the tab order to start from the bottom hbox to the top hbox.
  2. Set the tab order to start from the right button, then left button. Note that you have to do this for each of the hbox.

Related Links

Add comment


Security code
Refresh