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

Set up the main application - 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

The program first sets up the application normally. It creates a new window and sets the size to 240 pixels by 120 pixels. We also register for the "destroy" signal so that the program will quit normally when the user clicks the close button.

$window = new GtkWindow();

$window->set_size_request(240, 120);

$window->connect_simple('destroy', array('Gtk','main_quit'));

We then add a label that says 'Hello World, GtkStatusIcon!' to the window.

$label = new GtkLabel('Hello World, GtkStatusIcon!');

$window->add($label);

Finally, we hide this window first so that the application starts hidden. It will only appear when the user clicks on the system tray icon.

$window->hide_all();

Now that the main application is set up, let us proceed to set up the status icon.

Set up the system tray icon

The key to understanding the GtkStatusIcon is to think of it as a “GtkButton.”

It is a special “button” that you can place in the system tray, as opposed to within your main application.

Setting up a system tray icon is as simple as creating a new instance of GtkStatusIcon and assigning an icon to it. In this example, we use the stock image Gtk::STOCK_FILE.

$statusicon = new GtkStatusIcon();

$statusicon->set_from_stock(Gtk::STOCK_FILE);

If you have a gif, jpg or png file, you can set the icon directly from the image file:

$statusicon->set_from_file($image_filename);

Since the tray icon behaves like a button, it responds to both left mouse and right mouse clicks.

For a standard GtkButton, we use the "clicked" signal for a left mouse click, and the "button-press-event" signal for a right mouse click.

For GtkStatusIcon, the signals are different. For a left mouse click, the signal is 'activate'. For a right mouse click, the signal is 'popup-menu'.

In this hello world example, we will only monitor the left mouse click:

$statusicon->connect('activate', 'on_activate');

In the callback function, we first test if the window is visible. If it is, we hide the window. If the window is hidden, we reveal it.

function on_activate($statusicon) {

global $window;

if ($window->is_visible()) $window->hide_all();

else $window->show_all();

}

We will show how to process a right mouse click on the tray icon and a popup  menu in the next example.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap

Dev Shed Tutorial Topics: