PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 273: How to set up an application to run in the system tray using GtkStatusIcon - Part 1?
Written by kksou   
Monday, 25 June 2007
Problem

You would like to set up set up an application to run in the system tray.

Let's suppose we are setting an MP3 player. When the application is first started, an icon will appear in the system tray. Since no music is playing, the icon displayed is the standard Gtk::STOCK_MEDIA_STOP. When the user click on the icon, it will start playing, and we change the icon to the standard Gtk::STOCK_MEDIA_PLAY as shown below:

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


Solution
  • Setting up the system tray icon is simple with the new widget GtkStatusIcon available in gtk+2.10 and above.
  • In this example, we set the icon using standard stock images with GtkStatusIcon::set_from_stock(). You can view the entire list of stock images here.
  • You can also set tooltips on the icon using GtkStatusIcon::set_tooltip()
  • The signal 'activate' allow us to detect left mouse click.
  • The signal 'popup-menu' allow us to detect right mouse click.

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   
18   
19   
20   
<?php
$statusicon = new GtkStatusIcon(); // note 1
$statusicon->set_from_stock(Gtk::STOCK_MEDIA_STOP); // note 2
$statusicon->set_tooltip('Left click to PLAY'); // note 3

$player_status = 0;
$statusicon->connect('activate', 'on_activate'); // note 4
$statusicon->connect('popup-menu', 'on_popup_menu'); // note 5

Gtk::main();

function on_activate($statusicon) {
    global $player_status;
    if ($player_status) { // note 6
        $statusicon->set_from_stock(Gtk::STOCK_MEDIA_STOP);
        $statusicon->set_tooltip('Left click to PLAY');
        $player_status = 0;
    } else {
        $statusicon->set_from_stock(Gtk::STOCK_MEDIA_PLAY);
        $statusicon->set_tooltip('Left click to STOP');
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Create a new status icon.
  2. Set the icon.
  3. Set the tooltip.
  4. Track left click.
  5. Track right click
  6. Change the icon according to the current player status.

Note
  • In this Part 1, I have showed the basics of setting up the tray icon.
  • In Part 2, I will show you how to popup a GTK window on left mouse click.
  • In Part 3, I will show you how to popup a menu on right mouse click.

Related Links

User reviews   Average user ratings:    4.5   (from 6 users)
  1. shin
    February 20, 2008 10:19am
    . . . . = (

    function on_popup_menu($statusicon) {
    echo "on_popup_menu\n";
    }

    don-t work to me ...

  2. kksou
    February 20, 2008 8:17pm

    This is part 1 of the series. I'm just trying to the show the fundamentals of setting up a GtkStatusIcon. As such, when you right click on the tray icon, you won't see any popup window in this example. You will only see the text "on_popup_menu" echoed in the command window.

    The sample code in:
    Sample Code 277: How to set up an application to run in the system tray using GtkStatusIcon - Part 4 - display popup menu on right click?
    will show you the actual setup and display the popup window from the tray icon.

    Regards,
    /kksou

  3. Edwin Koster
    February 28, 2008 9:34am

  4. Boyan Dzambazov
    March 11, 2008 2:54am

  5. Radoslav Kolarov
    March 25, 2008 3:25am

  6. J Hipps
    July 09, 2008 11:45am

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved