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...
Solution
- Create a new GtkAccelGroup and attach it to the GtkWindow with GtkWindow::add_accel_group()
- For each toolbutton, use the method GtkActionGroup::add_action_with_accel to specify the accelerator.
- Set the accelerator group for each action with GtkAction::set_accel_group().
- Then use GtkAction::connect_accelerator() to activate the accelerator.
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
- Create a new accelerator and attach it to the window.
- Create an action group.
- Define the accelerator key. Here we use the <control> key. You can change this to use 'Alt', or 'Ctrl-Alt'.
- Add the action to the action group, specifying the accelerator key at the same time.
- Specify the accelerator group.
- Connect the accelerator with the action.
Related Links
- How to set up toolbar using GtkAction - Part 1?
- How to setup and process a GtkButton using GtkAction - Part 1?
- How to setup and process a GtkButton using GtkAction - Part 2 - add stock image?
- How to activate deactivate a bunch of GtkButtons using GtkAction and GtkActionGroup?
- How to hide unhide a bunch of GtkLabels using GtkAction and GtkActionGroup?
- How to set up menu using GtkAction - Part 1?
- How to set up menu using GtkAction - Part 2 - add accelerators?
- How to set up menu using GtkAction - Part 3 - add accelerators with labels?
Read more...