|
Problem If you have followed the previous few articles on GtkAction, in particular How to set up toolbar using GtkAction - Part 2 - add accelerators? and How to set up menu using GtkAction - Part 3 - add accelerators with labels? did they get you excited about using GtkAction?
If you think about it, the menu items and toolbars are actually overlapping in functions. Both allow the users to select some functions. They just appear in different forms — one as a menu, the other as tool buttons. Also, the menus are usually more comprehensive, showing the users all the available functions. The toolbar, on the hand, usually only shows the most commonly used functions.
As such, we can conveniently use GtkAction to create menus and toolbars together. This makes the maintenance and new additions of menu and toolbar items much easier as shown below:

Solution We use exactly the same techniques as outlined in How to set up toolbar using GtkAction - Part 2 - add accelerators? and How to set up menu using GtkAction - Part 3 - add accelerators with labels?
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| <?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(); $window->add_accel_group($accel_group); $actiongrp = new GtkActionGroup('toolbar');
// global definitions for both menu and toolbar items
$global_definitions = array( // note 1
'file_new' => '_New|N', 'file_open' => '_Open|O', 'file_close' => '_Close', 'file_save' => '_Save|S', 'file_saveas' => 'Save _As', 'file_quit' => '_Quit', 'edit_cut' => 'Cut|X', 'edit_copy' => 'Copy|C', 'edit_paste' => '_Paste|V', 'edit_selectall' => 'Select All|A', 'edit_undo' => '_Undo|Z', 'edit_redo' => '_Redo|Y', '<hr>' => '<hr>' );
// define menu definition
$menu_definition = array( // note 2
'_File' => array('file_new', 'file_open', 'file_close', '<hr>', 'file_save', 'file_saveas', '<hr>', 'file_quit'), '_Edit' => array('edit_cut', 'edit_copy', 'edit_paste', '<hr>', 'edit_selectall', '<hr>', 'edit_undo', 'edit_redo'), );
$toolbar_definition = array( // note 3
'toolbar' => array('file_new', 'file_open', 'file_save', '<hr>', 'edit_cut', 'edit_copy', 'edit_paste', '<hr>', 'edit_undo', 'edit_redo'), );
setup_action($vbox, $menu_definition); // note 4
setup_action($vbox, $toolbar_definition); // note 5
// display title
$title = new GtkLabel("Set up menus and toolbars 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_action($vbox, $definition) { if (isset($definition['toolbar'])) { $toolbar = new GtkToolBar(); $toolbar->set_show_arrow(false); $vbox->pack_start($toolbar, 0, 0); } else { $menubar = new GtkMenuBar(); $vbox->pack_start($menubar, 0, 0); }
foreach($definition as $toplevel => $sublevels) { if ($toplevel != 'toolbar') { $menubar->append($top_menu = new GtkMenuItem($toplevel)); $menu = new GtkMenu(); $top_menu->set_submenu($menu); }
foreach($sublevels as $item_id) { global $global_definitions; $item = $global_definitions[$item_id]; // note 6
if (strpos("$item", '|') === false) { $accel_key = ''; } else { list($item, $accel_key) = explode('|', $item); }
if ($item=='<hr>') { if ($toplevel == 'toolbar') { $toolbar->insert(new GtkSeparatorToolItem(), -1); } else { $menu->append(new GtkSeparatorMenuItem()); }
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- The global definitions for menu and toolbar items.
- The menu definition.
- The toolbar definition.
- First set up the menu.
- Then set up the toolbar.
- Get the menu/toolbar definition from the global definitions.
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |