We added a couple of enhancements to the popup menu function. First, you can now add a separator in the menu by using <hr> in the menu definition: $menu_definition = array('Show','Hide', '<hr>','About','<hr>','Exit'); if ($menuitem_definition=='<hr>') { $menu->append(new GtkSeparatorMenuItem()); } Also, if you run the “hello world GtkStatusIcon” example above, you will find that if you keep pressing the right mouse button on the system tray icon without selecting any of the menu items, you will end up with many popup windows. To ensure that there is only one popup menu, we use a flag ($menu_popped_up) to keep track of whether or not there are active popup windows. If there is already an active popup window, we use the popdown() method to close the previous popup menu before displaying the new popup menu. if ($this->menu_popped_up && $this->popup_menu_ptr!=null) { $this->popup_menu_ptr->popdown(); $this->menu_popped_up = 0; } Of course, in order to use the popdown() method, we need to have the pointer to the menu. That is why we store a copy of the pointer to the menu in the $popup_menu_ptr variable. $this->popup_menu_ptr = $menu; The entire code for the popup menu function is as follows: function show_popup_menu($menu_definition) { if ($this->menu_popped_up && $this->popup_menu_ptr!=null) { $this->popup_menu_ptr->popdown(); $this->menu_popped_up = 0; } $menu = new GtkMenu(); foreach($menu_definition as $menuitem_definition) { if ($menuitem_definition=='<hr>') { $menu->append(new GtkSeparatorMenuItem()); } else { $menu_item = new GtkMenuItem($menuitem_definition); $menu->append($menu_item); $menu_item->connect('activate', array(&$this, 'on_popup_menu_select')); } } $menu->show_all(); $menu->popup(); $this->popup_menu_ptr = $menu; $this->menu_popped_up = 1; } Processing selected menu item The function to process a selected menu item is almost exactly the same as the “hello world GtkStatusIcon” example: function on_popup_menu_select($menu_item) { $window = $this->glade->get_widget('window1'); $item = $menu_item->child->get_label(); switch($item) { case 'Show': $window->show_all(); break; case 'Hide': $window->hide_all(); break; case 'About': $this->app->about(); break; case 'Exit': Gtk::main_quit(); break; } } Note that in order to be able to call the $this->app->about() function, you need to change the about() function in the NotePad class from protected function about() { to public function about() {
blog comments powered by Disqus |
|
|
|
|
|
|
|