Problem
You want to set up check items in menus as shown below:
Solution
- Check items are created in menu with GtkCheckmenuitem.
- The processing of the toggle on check item is exactly the same as that of GtkRadiomenuitem, which we've shown in How to set up menu and radio menu - Part 1?
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21 22 23 24 25 27 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 50 51 52 53 54 55 56 57 59 60 61 62 63 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 84 86 87 88 89 90 91 93 94 95 96 102 103 104 105 109 110 111 113 114 115 116 117 118 119 120 121 122 123 124 126 127 128 129 130 131 132 133 134 140 141 143 145 146 147 148 149 150 152 159 160 161 163 164 168 169 170 171 172 173 175 178 179 185 186 187 188 189 190 191 192 193 | <?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()); $accel_group = new GtkAccelGroup(); $window->add_accel_group($accel_group); // define menu definition $menu_definition = array( '_File' => array('_New|N', '_Open|O', '_Close|C', '<hr>', '_Save|S', 'Save _As','<hr>', 'E_xit'), '_Edit' => array('Cu_t|X', '_Copy|C', '_Paste|V', '<hr>', 'Select _All|A', '<hr>', '_Undo|Z','_Redo|Y'), '_Test' => array('Test_1|1', 'Test_2|2', 'Test_3|3', '<hr>', array('Selection 1', 'Selection 2', 'Selection 3'), '<hr>', '<checkitem>Test_4|4', '<checkitem>Test_5|5', // note 1 '<checkitem>Test_6|6') // note 1 ); $menu = new Menu($vbox, $menu_definition, $accel_group); // display title $title = new GtkLabel("Set up check items in menu"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $vbox->pack_start($title); $window->show_all(); Gtk::main(); // class Menu class Menu { var $prev_keyval = 0; var $prev_state = 0; var $prev_keypress = ''; function Menu($vbox, $menu_definition, $accel_group) { $this->menu_definition = $menu_definition; $menubar = new GtkMenuBar(); $vbox->pack_start($menubar, 0, 0); foreach($menu_definition as $toplevel => $sublevels) { $top_menu = new GtkMenuItem($toplevel); $menubar->append($top_menu); $menu = new GtkMenu(); $top_menu->set_submenu($menu); // let's ask php-gtk to tell us when user press the 2nd Alt key $menu->connect('key-press-event', array(&$this, 'on_menu_keypress'), $toplevel); foreach($sublevels as $submenu) { if (strpos("$submenu", '|') === false) { $accel_key = ''; } else { list($submenu, $accel_key) = explode('|', $submenu); } if (is_array($submenu)) { // set up radio menus $i=0; $radio[0] = null; foreach($submenu as $radio_item) { $radio[$i] = new GtkRadioMenuItem($radio[0], $radio_item); $radio[$i]->connect('toggled', array(&$this, "on_toggle")); $menu->append($radio[$i]); ++$i; } $radio[0]->set_active(1); // select the first item } else { if ($submenu=='<hr>') { $menu->append(new GtkSeparatorMenuItem()); } else { $submenu2 = str_replace('_', '', $submenu); $submenu2 = str_replace(' ', '_', $submenu2); $stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2); if (defined($stock_image_name)) { $menu_item = new GtkImageMenuItem(constant($stock_image_name)); } else { if (preg_match("/^<checkitem>(.*)/", $submenu, $matches)) { // note 2 $menu_item = new GtkCheckMenuItem($matches[1]); // note 3 $menu_item->connect('toggled', array(&$this, "on_toggle")); // note 4 } else { $menu_item = new GtkMenuItem($submenu); } } if ($accel_key!='') { $menu_item->add_accelerator("activate", $accel_group, ord($accel_key), Gdk::CONTROL_MASK, 1); } $menu->append($menu_item); $menu_item->connect('activate', array(&$this, 'on_menu_select')); $this->menuitem[$toplevel][$submenu] = $menu_item; } } } } } // process radio menu selection function on_toggle($radio) { // note 4 $label = $radio->child->get_label(); $active = $radio->get_active(); if ($active) echo("radio/check menu selected: $label\n"); } // process menu item selection function on_menu_select($menu_item) { $item = $menu_item->child->get_label(); echo "menu selected: $item\n"; if ($item=='E_xit') Gtk::main_quit(); } // processing of menu keypress function on_menu_keypress($menu, $event, $toplevel) { if (!$event->state & Gdk::MOD1_MASK) return false; // get the ascii equivalent of the keypress $keypress = ''; if ($event->keyval<255) { $keypress = chr($event->keyval); // ascii equivalent $keypress = strtolower($keypress); // convert to lowercase } $match = 0; // flag to see if there's a match foreach($this->menu_definition[$toplevel] as $submenu) { if (!preg_match("/.*_([a-zA-Z0-9]).*/", "$submenu", $matches)) continue; $key2 = strtolower($matches[1]); // convert to lowercase if ($keypress==$key2) { // check if there's a match if (strpos("$submenu", '|') === false) { $accel_key = ''; } else { list($submenu, $accel_key) = explode('|', $submenu); // remove any accelerator } $menuitem = $this->menuitem[$toplevel][$submenu]; // get the ID $menuitem->activate(); // simulate a click $menu->popdown(); // close the toplevel menu $match = 1; // set the match flag break; } } return $match; // return control back to php-gtk } } ?> |
Output
As shown above.Explanation
The above code is based on that of How to set up menu and radio menu - Part 4 - allow Alt F Alt N?
What's new here:
- Note how we define check items in the menu definitions.
- Check whether this is a check item.
- Create the check menu item.
- Register the
toggled
signal on the check menu item. - Toggle the check menu item. Note that we make use of the same signal hanlder as that of the radio menu items.
Related Links
- How to set up menu and radio menu - Part 1?
- How to set up menu and radio menu - Part 2 - add stock images?
- How to set up menu and radio menu - Part 3 - add accelerators?
- How to set up menu and radio menu - Part 4 - allow Alt F Alt N?
- How to set up menu and radio menu - Part 5 - add submenus?
- How to set up detachable menu?
Read more...