238. How to change tab order - Example 1?

Problem

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

Example 1: Suppose you have two columns of buttons. By default, the tab order will start form the top left button, travels down the left column, and then go to the right column.

Suppose now you would like to change the tab order to start from the top right button as shown below:

How to change tab order - Example 1?


Solution

In PHP-GTK2, you can change tab orders with GtkContainer::set_focus_chain().

Yes, it's that easy! But there are some rules to follow to get the tab orders to work:

  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. So suppose you have defined a complicated layout of hboxes within vboxes, you have to set the tab orders of level 2 through level 1 container, then the tab orders of level 3 through level 2 container, etc. If you define tab orders of level 3 from level 1 container, it won't work.
  3. It's not necessary to define the tab orders of all levels. For example, if you have four levels of widgets, and only the tab orders of level 4 are changed, you only need to do set_focus_chain() for that level. For all the other undefined levels, PHP-GTK2 will use the default tab order.

Confused? Don't worry. Run the following examples and read the explanations. It will become crystal clear.


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   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
<?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 1");
$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);
$vbox->pack_start(new GtkLabel("Note that the tab order ".
"now start from the right column"), 0);
$vbox->pack_start(new GtkLabel(), 0);

$hbox = new GtkHBox();
$vbox->pack_start($hbox);
$hbox->pack_start($left_buttons = new GtkVBox());
$hbox->pack_start($right_buttons = new GtkVBox());

$buttons = array();
setup_buttons($left_buttons, 'left');
setup_buttons($right_buttons, 'right');

// set the tab order
$hbox->set_focus_chain(array($right_buttons, $left_buttons)); // note 1

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

function setup_buttons($container, $index) {
    global $buttons;
    for ($i=0; $i<4; ++$i) {
        $button[$index][$i] = new GtkButton("button[$index][$i]");
        $container->pack_start($button[$index][$i]);
        $button[$index][$i]->connect('clicked', 'on_click', $index, $i);
    }
}

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

?>

Output

As shown above.

 

Explanation

  1. Note the use of $hbox and not $vbox here. If you use $vbox, it won't work. See Rule #2 above: You need to use the immediate parent to change the tab orders of the direct descendent.

Related Links

Add comment


Security code
Refresh