PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 337: How to set up an application to run in the system tray using GtkStatusIcon - Part 6 - make the application blink?
Written by kksou   
Monday, 01 October 2007
Problem

This is in response to NOT_Skeletor's Post titled "Alert the taskbar".

In the the previous article, I've showed you how to blink the tray icon.

In the post by Bob Majdak Jr, he shared with us the way to blink an application.

The following example shows both the blinking of tray icon and blinking of the application - to show you how you can mix the two, and also the key differences between the two as shown below:

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


Solution

  1. To blink the application, use the method GtkWindow::set_urgency_hint(true)
  2. To stop the blinking of the application, use the method GtkWindow::set_urgency_hint(false)

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   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
<?php
// setup GTK main application
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 300);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

$vbox->pack_start($hbox = new GtkHBox(), 0, 0);
$hbox->set_size_request(-1, 2);

// Create the top textview to display the conversation
$buffer1 = new GtkTextBuffer();
$view1 = new GtkTextView();
$view1->set_buffer($buffer1);
$view1->set_editable(false);
$view1->modify_font(new PangoFontDescription("Arial 9"));
$view1->set_wrap_mode(Gtk::WRAP_WORD);

$scrolled_win1 = new GtkScrolledWindow();
$scrolled_win1->set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
$frame1 = new GtkFrame();
$frame1->add($scrolled_win1);
$vbox->pack_start($frame1, 0, 0);
$scrolled_win1->add($view1);
$scrolled_win1->set_size_request(400,200);

$vbox->pack_start($hbox = new GtkHBox(), 0, 0);
$hbox->set_size_request(-1, 1);

// Create the bottom textview to type your message
$buffer2 = new GtkTextBuffer();
$buffer2->set_text('type your message here');
$view2 = new GtkTextView();
$view2->set_buffer($buffer2);
$view2->modify_font(new PangoFontDescription("Arial 9"));
$view2->set_wrap_mode(Gtk::WRAP_WORD);

$scrolled_win2 = new GtkScrolledWindow();
$scrolled_win2->set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
$frame2 = new GtkFrame();
$frame2->add($scrolled_win2);
$vbox->pack_start($frame2, 0, 0);
$scrolled_win2->add($view2);
$view2->connect('key-press-event', 'on_key_press',
    $view1, $buffer1, $view2, $buffer2);
$scrolled_win2->set_size_request(400,80);
$view2->grab_focus();

// setup system tray icon
$statusicon = new GtkStatusIcon();
$statusicon->set_from_stock(Gtk::STOCK_NETWORK);
$statusicon->set_tooltip('Left click to launch PHP-GTK Messenger');

$app_status = 0;
$statusicon->connect('activate', 'on_activate');
$statusicon->connect('popup-menu', 'on_popup_menu');

Gtk::timeout_add(10000, 'icon_start_blinking', $statusicon); // note 1

$window->hide_all();
Gtk::main();

function on_key_press($widget, $event, $view1, $buffer1, $view2, $buffer2) {
    if ($event->keyval==Gdk::KEY_Return) {
        if ($event->state & Gdk::SHIFT_MASK) return false;
        $input = $buffer2->get_text($buffer2->get_start_iter(),
            $buffer2->get_end_iter());
        $iter = $buffer1->get_end_iter();
        $buffer1->insert($iter, "You say: $input\n\n" );
        $view1->scroll_to_mark($buffer1->get_insert(), 0);
        $buffer2->set_text('');
        return true;
    } else {
        return false;
    }
}

function on_activate($statusicon) {
    global $window, $app_status, $view2;
    if ($app_status) {
        $statusicon->set_tooltip('Left click to launch PHP-GTK Messenger');
        $window->hide_all();
        $app_status = 0;
    } else {
        $statusicon->set_tooltip('Left click to hide PHP-GTK Messenger');
        $window->show_all();
        $view2->grab_focus();
        $app_status = 1;
    }
}

function on_popup_menu($statusicon) {
    echo "on_popup_menu\n";
    $menu_definition = array('Show','Hide', '<hr>',
    'Connect','Disconnect','<hr>','Exit');
    $menu = show_popup_menu($menu_definition);
}

// show popup menu
function show_popup_menu($menu_definition) {
  • 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

The sample code above is an extension of How to set up an application to run in the system tray using GtkStatusIcon - Part 5 - make the tray icon blink?

What's new here:

  1. Blink the tray icon every 10 seconds.
  2. Blink the icon.
  3. Blink the application.
  4. Stop the blinking after 3 seconds.
  5. Stop the blinking of the tray icon.
  6. Stop the blinking of the application.

Note
  • If the application is hidden, you will not see any blinking of the applicatioin. In this case, you will only see the tray icon blinking.
  • If the application is not hidden, but is hidden by other windows, you will see both the tray icon, and the application in the taskbar, blinking.
  • If the application is in view, you will see all three blinking - the application, the icon in the task bar, and the system tray icon.

Related Links

User reviews

There are no user reviews yet.

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