To pop up a menu when the user presses the right mouse button, we first create a new GtkMenu: $menu = new GtkMenu(); Then we create a new GtkMenuItem for each of the menu items and append it to the menu. We also connect the "activate" signal so that we are able to process the menu items when the user selects them: foreach($menu_definition as $menuitem_definition) { $menu_item = new GtkMenuItem($menuitem_definition); $menu->append($menu_item); $menu_item->connect('activate', 'on_popup_menu_select'); } When we have finished setting up all the menu items, we pop up the menu: $menu->show_all(); $menu->popup(); When a user selects a menu item, the "on_popup_menu_select" function is called. The first thing we need to do is find out which item the user has selected: $item = $menu_item->child->get_label(); Once we know the menu item that the user has selected, we process it accordingly. switch($item) { case 'Show': $window->show_all(); break; case 'Hide': $window->hide_all(); break; case 'Exit': Gtk::main_quit(); break; } System Tray Notepad Application Now that we have seen two examples of setting up a system tray application, we are ready to convert the Notepad application we created in the article "Building Your Own Desktop Notebook Application Using PHP-GTK" to a system tray application. As with the previous two examples, we first set up the main application, then the status icon, and start the main loop: $app = new NotePad(); $statusicon = new StatusIcon($app); Gtk::main(); The NotePad class is exactly the same as that defined in the previous article. The StatusIcon class is a subclass of the GtkStatusIcon. The StatusIcon class In case you are not aware, you can subclass any standard GTK widget to define your own widget. In the system tray Notepad application, we created a new widget called StatusIcon that is a subclass of GtkStatusIcon. The class has a constructor, with two signal handlers (to process the left and right mouse clicks), and two other methods (to pop up and process right mouse menu items): class StatusIcon extends GtkStatusIcon { // constructor public function __construct($app) { } // signal handler for 'activate' public function on_activate($statusicon) { } // signal handler for 'popup_menu' public function on_popup_menu($statusicon) { } // popup menu protected function show_popup_menu($menu_definition) { } // process selected menu item protected function on_popup_menu_select($menu_item) { } } The StatusIcon class constructor When you create a subclass of a standard GTK widget, you should make a call to the parent’s constructor from the subclass’s constructor: parent::__construct(); The rest of the code in the constructor is almost the same as the “hello world GtkStatusIcon” we described earlier. public function __construct($app) { $this->app = $app; $this->glade = $app->glade; parent::__construct(); $this->set_from_stock(Gtk::STOCK_EDIT); $this->set_tooltip('Left click to view notepad'); $this->connect('activate', array(&$this, 'on_activate')); $this->connect('popup-menu', array(&$this, 'on_popup_menu')); } Signal handling for left and right mouse clicks The two signal handlers that process the left and right mouse clicks are also similar to the “hello world GtkStatusIcon” described earlier. public function on_activate($statusicon) { $window = $this->glade->get_widget('window1'); if ($window->is_visible()) { $statusicon->set_tooltip('Left click to view notepad'); $window->hide_all(); } else { $statusicon->set_tooltip('Left click to hide notepad'); $window->show_all(); } } function on_popup_menu($statusicon) { $menu_definition = array('Show','Hide', '<hr>', 'About','<hr>','Exit'); $menu = $this->show_popup_menu($menu_definition); }
blog comments powered by Disqus |
|
|
|
|
|
|
|