239. How to change tab order - Example 2?

Problem

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

Let's continue to extend Example 1.

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

How to change tab order - Example 2?


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.

In this example, we need to use GtkContainer::set_focus_chain() two times:

  • To change the tab order to start from the right column.
  • To change the tab order within the right column to start from the bottom right 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   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
<?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 2");
$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 bottom right button"), 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());

$button = 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
$right_buttons->set_focus_chain(array( // note 2
    $button['right'][3], $button['right'][2], 
    $button['right'][1], $button['right'][0])); 

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

function setup_buttons($container, $index) {
    global $button;
    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. Set the tab order to start from the right column.
  2. Set the tab order to start from the button3, then button2, button1 and button0. As you can see, you can set the tab order in whatever orders you desire.

Related Links

Add comment


Security code
Refresh