#! /usr/bin/perl -w
use strict;
#external modules, needs to be separately loaded
use Gtk2::TrayIcon;
use Gtk2::NotificationBubble;

use Gtk2 -init;
use Glib qw/TRUE FALSE/;

my $flag = 1;
my $event_number;
my $bubble;

my $icon= Gtk2::TrayIcon->new("test");

	my $eventbox = Gtk2::EventBox->new;
        my $img= Gtk2::Image->new_from_file("./pix/1.png");
	$eventbox->add($img);
    
    #attach a callback which will stop the flashing
    #of the message as soon as we enter the the 
    #tray icon
    $eventbox->signal_connect(
        'enter-notify-event' => sub { 
            #Remove the toggle action
            Glib::Source->remove ($event_number);
            $bubble->set(
                'Sorry...',     #Heading
                undef,          #icon (Gtk2::Image)
                'For being so persistent ;-)',  #message
            );
            $bubble->show(-1); #to keep it forever set it to < 0
        }
    );

    #if the user click on the tray icon, we quit the program
    $eventbox->signal_connect('button-press-event' => sub{ Gtk2->main_quit;});
	
	
#The tray icon is a container, we
#can add widgets to it  
$icon->add($eventbox);
$icon->show_all;

    #The notification bubble attaches to widgets, 
    #here we attach to the eventbox.
    #We could attach to the tray icon to the
    #same effect
    $bubble = Gtk2::NotificationBubble->new;
    $bubble->attach($eventbox);

#We record the ID in order to remove this 
#callback later.
$event_number = Glib::Timeout->add (
    4000 => sub{

	   ($flag)&&($img->set_from_file('./pix/1.png'));
	   ($flag)||($img->set_from_file('./pix/2.png'));
	   $flag = !$flag;
       $bubble->set(
                'Hello',    #Heading
                Gtk2::Image->new_from_file('./pix/1.png'), #icon (Gtk2::Image)
                'Time for a Big Mac :)!',   #message
        );
       $bubble->show(1000);
	   return 1;
	}); 
  
Gtk2->main;
