Home arrow PHP arrow Page 5 - Building Your Own System Tray Application Using PHP-GTK

Popup menu on right mouse click - PHP

You have seen how easy it is to write your own desktop applications using PHP-GTK in the article “Building Your Own Desktop Notepad Application Using PHP-GTK.” Once you have learned how to write desktop applications using PHP-GTK, you will be pleased to know that writing system tray applications is just a matter of adding a couple of lines of code. The bulk of your code remains exactly the same!

TABLE OF CONTENTS:
  1. Building Your Own System Tray Application Using PHP-GTK
  2. Set up the main application
  3. Second example of GtkStatusIcon
  4. Display a popup menu
  5. Popup menu on right mouse click
  6. A note about subclassing a widget
By: K.K.Sou
Rating: starstarstarstarstar / 7
September 30, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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() {



 
 
>>> More PHP Articles          >>> More By K.K.Sou
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: