436. How to set up toolbar using GtkAction - Part 1?

Problem

You have seen how to set up toolbars in the article How to set up toolbar?

You can also create toolbars using another way: using the GtkAction. The result is the same – you'll get the same toolbar as shown below:

However, try to compare the differences between using the two methods, and you will understand why they created this interesting widget GtkAction.

How to set up toolbar using GtkAction - Part 1?


Solution

  • For each toolbutton, first create a GtkAction, specifying the stock image if there's any.
  • Use the method GtkAction::create_tool_item() to automatically create a toolbar item from the action.

Sample Code

1   
2   
3   
4   
5   
6   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
22   
23   
24   
25   
26   
27   
28   
30   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
50   
51   
52   
53   
58   
59   
60   
61   
62   
63   
64   
65   
67   
69   
70   
71   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

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

// display title
$title = new GtkLabel("Set up Toolbar using GtkAction - Part 1");
$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();

function setup_toolbar($vbox, $toolbar_definition) { // note 1
    $toolbar = new GtkToolBar(); // note 2
    $vbox->pack_start($toolbar, 0, 0);
    foreach($toolbar_definition as $item) {
        if ($item=='<hr>') {
            $toolbar->insert(new GtkSeparatorToolItem(), -1);
        } else {
            $item2 = str_replace('_', '', $item);
            $stock_image_name = 'Gtk::STOCK_'.strtoupper($item2);
            $stock_image = '';
            if (defined($stock_image_name))
                $stock_image = constant($stock_image_name); // note 3

            $action = new GtkAction($item, $item, '', $stock_image); // note 4
            $toolitem = $action->create_tool_item(); // note 5

            $action->connect('activate', 'on_toolbar_button', $item); // note 6
            $toolbar->insert($toolitem, -1); // note 7
        }
    }
}

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

?>

Output

As shown above.
 

Explanation

  1. This is the toolbar definition.
  2. Create a new toolbar.
  3. Get the stock image name from toolbar definition.
  4. Create a new GtkAction.
  5. Create a new toolitem from the action.
  6. Register for the 'activate' signal on the action.
  7. Insert the toolitem into the toolbar.
  8. Process the button click on the toolbar.

Related Links

Add comment


Security code
Refresh