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

Display a popup menu - 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

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);

}



 
 
>>> 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: