128. How to create detachable toolbar?

Problem

You want to create detachable toolbars as shown below:

How to create detachable toolbar?


Solution

You will be surprised how easy it is to create detachable toolbars in php-gtk2.


Sample Code

1   
2   
3   
4   
5   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
20   
21   
22   
23   
24   
25   
26   
27   
29   
31   
32   
34   
35   
37   
38   
39   
40   
41   
42   
44   
45   
46   
47   
50   
51   
52   
53   
54   
55   
56   
58   
60   
61   
62   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// define toolbar definition
$toolbar_definition = array('New', 'Open', 'Save', '<hr>',
    'Cut', 'Copy', 'Paste', '<hr>',
    'Undo','Redo');
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Create Detachable Toolbars");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$vbox->pack_start($title);
$vbox->pack_start(new GtkLabel(''));

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

// setup toolbar
function setup_toolbar($vbox, $toolbar_definition) {
    $toolbar = new GtkToolBar();
    $handlebox = new GtkHandleBox(); // note 1
    $handlebox->add($toolbar); // note 2
    $toolbar->set_size_request(360,-1); // note 3
    $vbox->pack_start($handlebox, 0, 0);
    foreach($toolbar_definition as $item) {
        if ($item=='<hr>') {
            $toolbar->insert(new GtkSeparatorToolItem(), -1);
        } else {
            $stock_image_name = 'Gtk::STOCK_'.strtoupper($item);
            if (defined($stock_image_name)) {
                $toolbar_item = GtkToolButton::new_from_stock(
                    constant($stock_image_name));
                $toolbar->insert($toolbar_item, -1);
                $toolbar_item->connect('clicked', 'on_toolbar_button', $item);
            }
        }
    }
}

// process toolbar
function on_toolbar_button($button, $item) {
    echo "toolbar clicked: $item\n";
}

?>

Output

As shown above.
 

Explanation

This example make use of the code in How to set up toolbar?.

What's new here:

  1. Create a new handlebox.
  2. Stuff the toolbar in the handlebox.
  3. Set the size of the handlebox.

Related Links

Add comment


Security code
Refresh