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

Second example of GtkStatusIcon - 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

Below is a second example of setting up a system tray application.

<?php

// setup main application

$window = new GtkWindow();

$window->set_size_request(240, 120);

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

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

$window->add($label);


// setup system tray icon

$statusicon = new GtkStatusIcon();

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

$statusicon->set_tooltip('Left click to view GTK window');

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

$statusicon->connect('popup-menu', 'on_popup_menu');


$window->hide_all();

Gtk::main();


function on_activate($statusicon) {

global $window;

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

else $window->show_all();

}


function on_popup_menu($statusicon) {

$menu_definition = array('Show','Hide', 'Exit');

$menu = show_popup_menu($menu_definition);

}


function show_popup_menu($menu_definition) {

$menu = new GtkMenu();

foreach($menu_definition as $menuitem_definition) {

$menu_item = new GtkMenuItem($menuitem_definition);

$menu->append($menu_item);

$menu_item->connect('activate', 'on_popup_menu_select');

}

$menu->show_all();

$menu->popup();

}


function on_popup_menu_select($menu_item) {

global $window;

$item = $menu_item->child->get_label();

echo "popup menu selected: $itemn";

switch($item) {

case 'Show': $window->show_all(); break;

case 'Hide': $window->hide_all(); break;

case 'Exit': Gtk::main_quit(); break;

}

}


?>

As with the previous example, we first set up the main application – a GtkWindow with a GtkLabel 'Hello World, GtkStatusIcon!':

$window = new GtkWindow();

$window->set_size_request(240, 120);

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

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

$window->add($label);

We then set up the system tray icon:

$statusicon = new GtkStatusIcon();

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

$statusicon->set_tooltip('Left click to view GTK window');

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

$statusicon->connect('popup-menu', 'on_popup_menu');

Note the use of

$statusicon->set_tooltip('Left click to view GTK window');

which sets a tooltip for the system tray icon. When you hover the mouse over the icon, a tooltip will appear to display the message "Left click to view GTK window."

We have also connected the "popup-menu" signal to monitor any right mouse click:

$statusicon->connect('popup-menu', 'on_popup_menu');



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: