437. How to set up toolbar using GtkAction - Part 2 - add accelerators?

Problem

You have learned how to create toolbars using GtkAction in the previous article

Suppose now you want to add accelerators to the toolbar so that the users can press Ctrl-N for New, Ctrl-O for Open, etc. as shown below.

The essense to get the accelerator working for GtkAction is just 4 lines. But it took me two whole nights of trials and errors to get it working! Take a look at the code below, and you'll understand why. There're just too little documentation on the Internet related to GtkAction...

How to set up toolbar using GtkAction - Part 2 - add accelerators?


Solution

You'll be surprised that all these methods are available way back in alpha version!


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
34   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
60   
61   
62   
63   
68   
69   
70   
71   
72   
73   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
86   
88   
89   
90   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 175);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
$accel_group = new GtkAccelGroup(); // note 1
$window->add_accel_group($accel_group); // note 1
$actiongrp = new GtkActionGroup('toolbar'); // note 2

// define menu definition
$toolbar_definition = array('New|N', 'Open|O', 'Save|S', '<hr>',
    'Cut|X', 'Copy|C', 'Paste|V', '<hr>',
    'Undo|Z','Redo|Y');
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Set up Toolbar using GtkAction\n".
"      Part 2 - Add Accelerators");
$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(''));
$vbox->pack_start(new GtkLabel('You can press Ctrl-N (New), Ctrl-O (Open), etc.', 0));
$vbox->pack_start(new GtkLabel('to activate the toolbar items.'), 0);
$vbox->pack_start(new GtkLabel(''));

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

function setup_toolbar($vbox, $toolbar_definition) {
    $toolbar = new GtkToolBar();
    $vbox->pack_start($toolbar, 0, 0);
    foreach($toolbar_definition as $item) {
        if (strpos("$item", '|') === false) {
            $accel_key = '';
        } else {
            list($item, $accel_key) = explode('|', $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);

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

            $action->connect('activate', 'on_toolbar_button', $item);
            $toolbar->insert($toolitem, -1);

            global $accel_group, $actiongrp;
            if ($accel_key!='') {
                $accel = "<control>$accel_key"; // note 3
                $actiongrp->add_action_with_accel($action, $accel); // note 4
                $action->set_accel_group($accel_group); // note 5
                $action->connect_accelerator(); // note 6
            }
        }
    }
}

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

?>

Output

As shown above.
 

Explanation

  1. Create a new accelerator and attach it to the window.
  2. Create an action group.
  3. Define the accelerator key. Here we use the <control> key. You can change this to use 'Alt', or 'Ctrl-Alt'.
  4. Add the action to the action group, specifying the accelerator key at the same time.
  5. Specify the accelerator group.
  6. Connect the accelerator with the action.

Related Links

Add comment


Security code
Refresh