274. How to set up an application to run in the system tray using GtkStatusIcon - Part 2 - display GTK window on left click?

Problem

You have set up the tray icon in How to set up an application to run in the system tray using GtkStatusIcon - Part 1?

An icon in the system tray is useless if there is no application associated with it. So how do you link the system tray icon to a PHP-GTK2 application?

In this Part 2, I will show you how you can display your main PHP-GTK2 window when the user left click on the system tray icon as shown below.

This example, although very simple, is extremely important. Once you understand this example, you will have no problem developing a full-fledge system tray application using PHP-GTK2!

How to set up an application to run in the system tray using GtkStatusIcon - Part 2 - display GTK window on left click?


Solution

First, read the article in my blog titled "Secrets" to using GtkStatusIcon.

Once you understand the four "secrets" listed there, you will know that developing a system tray application is as simple as:

  1. Develop your PHP-GTK2 as per normal.
  2. When you have fully-tested the PHP-GTK2 application, add an icon in the system tray with GtkStatusIcon (think of it as adding an additional GtkButton in the system tray).
  3. Hide your main application — so now the only thing visible is your additonal "button", or icon, in the system tray.
  4. Start the main loop.
  5. Show your main application when the user left click on the system tray icon.
  6. I will show you how to handle right click in the next article. You can place some of the commonly-used functions in the popup menu when the user clicks on the right mouse button.

Important Note: This only works for PHP-GTK2 compliled with gtk+ v2.10 and above. If you are using an older version, for linux, you may follow the step-by-step instructions to recompile php-gtk2 with gtk+ v2.10. For windows, please refer to How to install php gtk2 on windows? You may also want to take a look here to see some of the new exciting PHP-GTK2 Functionalities.


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
19   
20   
21   
22   
23   
25   
26   
28   
29   
30   
31   
33   
34   
35   
36   
37   
38   
43   
<?php
// setup main GTK window
$window = new GtkWindow(); // note 1
$window->set_title($argv[0]);
$window->set_size_request(240, 120);
$window->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
$vbox->pack_start(new GtkLabel("You have left clicked \nthe system tray icon!"));
$vbox->pack_start(new GtkLabel("Click one more time to hide this."));

// setup system tray icon
$statusicon = new GtkStatusIcon(); // note 2
$statusicon->set_from_stock(Gtk::STOCK_NETWORK);
$statusicon->set_tooltip('Left click to view GTK window');
$app_status = 0;
$statusicon->connect('activate', 'on_activate');

$window->hide_all(); // note 3
Gtk::main(); // note 4

function on_activate($statusicon) {
    global $window, $app_status;
    if ($app_status) { // note 5
        $statusicon->set_tooltip('Left click to view GTK window');
        $window->hide_all();
        $app_status = 0;
    } else {
        $statusicon->set_tooltip('Left click to hide GTK window');
        $window->show_all();
        $app_status = 1;
    }
}

?>

Output

As shown above.
 

Explanation

  1. Set up a GtkWindow just like a standard PHP-GTK2 application.
  2. Add a system tray icon.
  3. Hide the main GtkWindow initially, showing only the system tray icon.
  4. Start the main loop.
  5. Hide/show the main GtkWindow and display appropriate tooltip based on the current application status.

Note

  1. I've only tested GtkStatusIcon on winxp. If you're on linux or mac, please let me know if the code runs ok on your machine.
  2. On my machine running winxp, I got the following warning message:
    (php2.exe:xxxx): GLib-WARNING **: g_main_context_prepare() called recursively fr om within a source's check() or prepare() member.
    I've tried many different applications using GtkStatusIcon. All seems to run ok. So I'm not sure if this is a bug or not. Does it occur on your machine too?

Related Links

Add comment


Security code
Refresh