486. How to move tabs between two notebooks with drag and drop?

Problem

You have set up two GtkNotebooks.

This example shows you how you can move a tab (together with its contents) from one notebook to another using drag and drop as shown below:


Before:

After:


Solution

  • Use GtkNotebook::set_group_id() to assign a group_id to both the notebooks. Make sure both have the sample group_id.
  • Use GtkNotebook::set_tab_detachable() to allow moving of tabs between differnet notebooks that have the same group_id.

Important Note: This only works for PHP-GTK2 compliled with gtk+ v2.10 and above. If you are using an older version, for linux, you may follow the step-by-step instructions to recompile php-gtk2 with gtk+ v2.10. For windows, please refer to How to install php gtk2 on windows? You may also want to take a look here to see some of the new exciting PHP-GTK2 Functionalities.


Sample Code

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

$notebook1 = setup_notebook($vbox, 1);
$vbox->pack_start(new Gtklabel(), 0);
$notebook2 = setup_notebook($vbox, 2);

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

function setup_notebook($vbox, $id) {
    // setup notebook
    $notebook = new GtkNotebook();
    $vbox->pack_start($notebook);

    // add two tabs of GtkLabel
    add_new_tab($notebook, new GtkLabel('Notebook 1'), "Label #$id.1");
    add_new_tab($notebook, new GtkLabel('Notebook 2'), "Label #$id.2");

    // add a thrid tab of GtkTextView
    $buffer = new GtkTextBuffer();
    $view = new GtkTextView();
    $view->set_buffer($buffer);
    $view->set_wrap_mode(Gtk::WRAP_WORD);
    add_new_tab($notebook, $view, "TextView $id");

    $notebook->set_group_id(123); // note 1

    return $notebook;
}

// add new tab
function add_new_tab($notebook, $widget, $tab_label) {
    $eventbox = new GtkEventBox();
    $label = new GtkLabel($tab_label);
    $eventbox->add($label);
    $label->show();
    $eventbox->connect('button-press-event', 'on_tab', $tab_label);
    $notebook->append_page($widget, $eventbox);
    $notebook->set_tab_detachable($widget, 1); // note 2
}

// function that is called when user click on tab
function on_tab($widget, $event, $tab_label) {
    echo "tab clicked = $tab_label\n";
}

?>

Output

As shown above.
 

Add comment


Security code
Refresh