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.
blog comments powered by Disqus |
|
|
|
|
|
|
|