File Coverage

blib/lib/SystemTray/Applet/Win32.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package SystemTray::Applet::Win32;
2            
3 1     1   24343 use warnings;
  1         2  
  1         32  
4 1     1   5 use strict;
  1         2  
  1         37  
5            
6 1     1   6 use base qw( SystemTray::Applet );
  1         6  
  1         546  
7            
8 1     1   396 use Win32::GUI();
  0            
  0            
9            
10             # this helps the applet die gracefully if windows shuts down
11             $SIG{QUIT} = "DEFAULT";
12            
13             sub import
14             {
15             my ( $class , $hidden ) = @_;
16            
17             if( !defined($hidden))
18             {
19             $hidden = 1;
20             }
21            
22             if( $hidden )
23             {
24             my ($DOS) = Win32::GUI::GetPerlWindow();
25             Win32::GUI::Hide($DOS);
26             }
27             }
28            
29             END
30             {
31             my ($DOS) = Win32::GUI::GetPerlWindow();
32             Win32::GUI::Show($DOS);
33             }
34            
35             =head1 NAME
36            
37             SystemTray::Applet::Win32 - Windows support for SystemTray::Applet
38            
39             =head1 VERSION
40            
41             Version 0.02
42            
43             =cut
44            
45             our $VERSION = '0.02';
46            
47            
48             =head1 SYNOPSIS
49            
50             This module provides windows support for SystemTray::Applet.
51            
52             use SystemTray::Applet::Win32;
53            
54             my $foo = SystemTray::Applet::Win32->create( "text" => "hello world" );
55            
56             By default the console window is hidden during program execution.
57             If you want to leave it visible then pass 0 to the module import.
58            
59             use SystemTray::Applet::Win32 qw(0);
60            
61             =head1 FUNCTIONS
62            
63             =head2 function1
64            
65             =cut
66            
67             =head2 init
68            
69             $self->init();
70            
71             Initialize the toolkit env. Creates the notification icon
72            
73             =cut
74            
75            
76             sub init
77             {
78             my ( $self ) = @_;
79            
80             my $rand = int(rand(1000000));
81             $self->{"win32"}->{"id"} = $rand;
82             $self->{"win32"}->{"window"} = Win32::GUI::Window->new( -name => 'Main$rand', -width => 100, -height => 100);
83             $self->{"win32"}->{"popup"} = Win32::GUI::Menu->new("Options" => "Options", ">Quit" => {-name => "Quit" , -onClick => sub { return -1; } } );
84             $self->{"win32"}->{"notify_icon"} = $self->{"win32"}->{"window"}->AddNotifyIcon(-name => "_NI$rand" );
85            
86             eval "package main; sub _NI" . $rand . "_RightClick { SystemTray::Applet::Win32::_RightClick(\$self);}";
87             print $@ . "\n";
88            
89             eval "package main; sub _Timerd" . $rand . "_Timer { SystemTray::Applet::Win32::_Timer(\$self);}";
90             print $@ . "\n";
91            
92             if( $self->{"frequency"} != -1 )
93             {
94             $self->{"win32"}->{"timer"} = $self->{"win32"}->{"window"}->AddTimer( "_Timerd$rand" , $self->{"frequency"} * 1000 );
95             }
96             else
97             {
98             $self->{"win32"}->{"timer"} = $self->{"win32"}->{"window"}->AddTimer( "_Timerd$rand" , 0 );
99             }
100            
101             return $self;
102             }
103            
104             sub _Timer
105             {
106             my ( $self ) = @_;
107            
108             $self->{"callback"}->($self);
109             if( $self->{"frequency"} != -1 )
110             {
111             $self->{"win32"}->{"timer"}->Interval($self->{"frequency"} * 1000);
112             }
113             }
114            
115            
116             sub _RightClick
117             {
118             my ( $self ) = @_;
119            
120             my ( $x, $y ) = Win32::GUI::GetCursorPos;
121             Win32::GUI::TrackPopupMenu($self->{"win32"}->{"window"}->{-handle}, $self->{"win32"}->{"popup"}->{Options}, $x , $y );
122             }
123            
124            
125             =head2 start
126            
127             $self->start();
128            
129             Start the gui up by starting the win32 mainloop. Never returns.
130            
131             =cut
132            
133             sub start
134             {
135             Win32::GUI::Dialog();
136             exit(0);
137             }
138            
139            
140             =head2 create_icon
141            
142             $self->create_icon("an_icon.jpg" );
143            
144             Create an icon from a file and return it. Supports whatever Win32::GUI::Icon does.
145            
146             =cut
147            
148             sub create_icon
149             {
150             my ( $self , $icon ) = @_;
151            
152             if( defined( $icon ) )
153             {
154             return Win32::GUI::Icon->new($icon);
155             }
156             else
157             {
158             return undef;
159             }
160             }
161            
162            
163             =head2 display
164            
165             $self->display();
166            
167             Display the icon with the text as hovertext if we have an icon or hovertext with no icon if none is specified.
168            
169             =cut
170            
171             sub display
172             {
173             my ( $self ) = @_;
174            
175             if( defined($self->{"icon"}) )
176             {
177             $self->{"win32"}->{"notify_icon"}->Change( -name => "_NI" . $self->{"win32"}->{"id"} , -icon => $self->{"icon"} , -tip => $self->{"text"} );
178             }
179             else
180             {
181             $self->{"win32"}->{"notify_icon"}->Change( -name => "_NI" . $self->{"win32"}->{"id"} , -tip => $self->{"text"} );
182             }
183             }
184            
185            
186             =head2 schedule
187            
188             $self->schedule();
189            
190             Schedule the callback.
191            
192             =cut
193            
194             sub schedule
195             {
196             my ( $self ) = @_;
197            
198             if( $self->{"frequency"} != -1 )
199             {
200             $self->{"win32"}->{"timer"}->Interval( $self->{"frequency"} * 1000 );
201             }
202             else
203             {
204             $self->{"win32"}->{"timer"}->Interval(0);
205             }
206             }
207            
208            
209             =head2 _order
210            
211             This specifies the priority used by SystemTray::Applet->new when loading plugin. Win32 is 1 as if it is installed we should probably use it.
212            
213             =cut
214            
215             sub _order
216             {
217             return 1;
218             }
219            
220             =head1 AUTHOR
221            
222             Peter Sinnott, C<< >>
223            
224             =head1 BUGS
225            
226             Please report any bugs or feature requests to C, or through
227             the web interface at L. I will be notified, and then you'll
228             automatically be notified of progress on your bug as I make changes.
229            
230            
231            
232            
233             =head1 SUPPORT
234            
235             You can find documentation for this module with the perldoc command.
236            
237             perldoc SystemTray::Applet::Win32
238            
239            
240             You can also look for information at:
241            
242             =over 4
243            
244             =item * RT: CPAN's request tracker
245            
246             L
247            
248             =item * AnnoCPAN: Annotated CPAN documentation
249            
250             L
251            
252             =item * CPAN Ratings
253            
254             L
255            
256             =item * Search CPAN
257            
258             L
259            
260             =back
261            
262            
263             =head1 ACKNOWLEDGEMENTS
264            
265            
266             =head1 COPYRIGHT & LICENSE
267            
268             Copyright 2008 Peter Sinnott, all rights reserved.
269            
270             This program is free software; you can redistribute it and/or modify it
271             under the same terms as Perl itself.
272            
273            
274             =cut
275            
276             1; # End of SystemTray::Applet::Win32