HomePHP Page 6 - Building Your Own System Tray Application Using PHP-GTK
A note about subclassing a widget - 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!
One of the biggest advantages of subclassing a widget, as you can see from this example, is that you can neatly encapsulate all the signal handlers and related methods into a class.
As you start to build bigger and more complex PHP-GTK2 applications, you will find that the number of signal handlers corresponding to each widget become more and more difficult to manage and maintain.
The ability to encapsulate signal handlers and methods that are used only by the widget itself makes it particularly attractive for debugging and maintaining codes.
However, there are always two sides to a coin. The main “disadvantage” of subclassing a widget is that you are not able to use these subclassed widgets in Glade. Glade only recognizes standard GTK widgets. So all the widgets you use in Glade have to be the standard GTK widgets.
Of course, you can intermix both in your application. In this example, I used Glade in the main application, but used the subclass approach when setting up the system tray icon.
The complete sample code and glade file
Click on the following links to download the complete sample code and the corresponding glade file.
We’ve come to the end of this article. In this tutorial, you saw how you can easily convert any standard PHP-GTK2 application into a system tray application.
You first set up the main application as per your original desktop application. Then you add a system tray icon with the help of the widget GtkStatusIcon.
The system tray icon behaves like a button, responding to both left and right mouse clicks. For a left mouse click, we usually hide or reveal the main application. For a right mouse click, we usually pop up a menu listing some commonly-used functions.
Now that you know how to create system tray applications, I hope you will have lots of fun in the next couple of days converting some of your existing PHP-GTK applications into system tray applications!