######################################################### # # Example scripts for PDK 6.0.2 PerlTray and >= wxPerl 0.49 # # To package and test - # split the code where indicated and create the files # wxpltray.pl # Wx\PerlTray\App.pm # Wx\PerlTray\App.pm # Wx\PerlTray\App.pm # # use wxpdk to create a .perlapp file # # Note: If you do not detach from the console, the # app will not unload when user logs off. # If packaged 'normally' without a console, # all should work OK. # ######################################################### BEGIN { use Wx::Perl::Packager; } use strict; use warnings; use Wx 0.49; use threads; use threads::shared; use Storable qw(freeze); use Wx::PerlTray::Common; use Wx::PerlTray::App; use PerlTray; RegisterHotKey("alt+shift+F2", "onTrayEvent('HotKey')"); my $application = Wx::PerlTray::App->new; onTrayEvent(WXPT_EVT_STARTAPP, 'nodata'); #--- Perl Tray Callbacks ------------ sub Click { # do default action PerlTray::DisplayMenu(); } sub DoubleClick { # show the app onTrayEvent(WXPT_EVT_SHOWWINDOW,'nodata'); } sub PopupMenu { my $mainwindow = Wx::Window::FindWindowById(WXPT_MAINWINDOW_ID, undef); return $mainwindow->contextmenu(); } sub Shutdown { my $userlogoff = shift; onTrayEvent(WXPT_EVT_PERLTRAYSHUTDOWN, $userlogoff); } sub Singleton { my @args = @_; # another instance was executed my $event = { args => \@args }; my $eventdata = freeze($event); onTrayEvent(WXPT_EVT_SINGLETON, $eventdata); } sub Timer { onTrayEvent(WXPT_EVT_TIMER, 'nodata'); } # tooltip bug workaround { no warnings 'once'; *PerlTray::ToolTip = \&ToolTip; } sub ToolTip { return 'Wx Perl and PerlTray Example ToolTip'; } #--- event sender ------------ sub onTrayEvent { my($eventid, $eventdata) = @_; my $mainwindow = Wx::Window::FindWindowById(WXPT_MAINWINDOW_ID, undef); my $shared = $eventdata; threads::shared::share($shared); my $newevent = Wx::PlThreadEvent->new( -1, $eventid, $shared ); Wx::PostEvent( $mainwindow, $newevent ); } ########################################################### # # # NEW FILE App.pm # # # ########################################################### package Wx::PerlTray::App; use strict; use Wx qw(wxFRAME_NO_TASKBAR wxDEFAULT_FRAME_STYLE); use Wx::Event qw(EVT_END_SESSION); use base qw(Wx::App); use Wx::PerlTray::Common; use Wx::PerlTray::MainWindow; sub OnInit { my $self = shift; my $frame = Wx::PerlTray::MainWindow->new ( undef, # parent window WXPT_MAINWINDOW_ID, # ID -1 means any 'Wx + PerlTray Example', # title [-1, -1], # default position [500, 350], # size wxFRAME_NO_TASKBAR|wxDEFAULT_FRAME_STYLE); $self->SetTopWindow($frame); EVT_END_SESSION($self, \&OnEndSession); return 1; } sub OnEndSession { my ($self, $event) = shift; my $mainwindow = Wx::Window::FindWindowById(WXPT_MAINWINDOW_ID, undef); my $shared = 'EndSession'; # arbitray text in this case threads::shared::share($shared); my $newevent = Wx::PlThreadEvent->new( -1, WXPT_EVT_ENDSESSION, $shared ); Wx::PostEvent( $mainwindow, $newevent ); } 1; ########################################################### # # # NEW FILE Common.pm # # # ########################################################### package Wx::PerlTray::Common; use strict; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( WXPT_MAINWINDOW_ID WXPT_EVT_SHOWWINDOW WXPT_EVT_EXITWINDOW WXPT_EVT_ENDSESSION WXPT_EVT_ABOUT WXPT_EVT_TIMER WXPT_EVT_HOTKEY WXPT_EVT_STARTAPP WXPT_EVT_SINGLETON WXPT_EVT_PERLTRAYSHUTDOWN ); sub WXPT_MAINWINDOW_ID () { 15001 } sub WXPT_EVT_SHOWWINDOW () { 15002 } sub WXPT_EVT_EXITWINDOW () { 15003 } sub WXPT_EVT_ENDSESSION () { 15004 } sub WXPT_EVT_ABOUT () { 15005 } sub WXPT_EVT_TIMER () { 15006 } sub WXPT_EVT_HOTKEY () { 15007 } sub WXPT_EVT_STARTAPP () { 15008 } sub WXPT_EVT_SINGLETON () { 15009 } sub WXPT_EVT_PERLTRAYSHUTDOWN () { 15010 } 1; ########################################################### # # # NEW FILE MainWindow.pm # # # ########################################################### package Wx::PerlTray::MainWindow; use Wx qw(wxTheApp wxYES_NO wxICON_QUESTION wxCENTRE wxYES); use base qw(Wx::Frame); use Wx::Event qw(EVT_CLOSE EVT_COMMAND EVT_ICONIZE); use Storable qw(thaw); use Wx::PerlTray::Common; use PerlTray; sub new { my $class = shift; my $self = $class->SUPER::new(@_); EVT_CLOSE($self, \&OnClose); EVT_ICONIZE($self,\&OnIconize); EVT_COMMAND($self, -1, WXPT_EVT_STARTAPP, \&_handler_startapp); EVT_COMMAND($self, -1, WXPT_EVT_SHOWWINDOW, \&_handler_showwindow); EVT_COMMAND($self, -1, WXPT_EVT_EXITWINDOW, \&_handler_exitwindow); EVT_COMMAND($self, -1, WXPT_EVT_ENDSESSION, \&_handler_endsession); EVT_COMMAND($self, -1, WXPT_EVT_ABOUT, \&_handler_about); EVT_COMMAND($self, -1, WXPT_EVT_TIMER, \&_handler_timer); EVT_COMMAND($self, -1, WXPT_EVT_HOTKEY, \&_handler_hotkey); EVT_COMMAND($self, -1, WXPT_EVT_SINGLETON, \&_handler_singleton); EVT_COMMAND($self, -1, WXPT_EVT_PERLTRAYSHUTDOWN, \&_handler_perltrayshutdown); $self->Centre(); $self->_initialise_popup(); $self->_initialise_timer(); return $self; } sub _handler_singleton { my ($self, $event) = @_; $self->Iconize(0) if($self->IsIconized()); $self->Show(1); $self->SetFocus(); } sub _handler_showwindow { my ($self, $event) = @_; $self->Iconize(0) if($self->IsIconized()); $self->Show(1); PerlTray::Balloon("System Tray menu Show called", 'Wx + PerlTray Example', "info", 10); $self->SetFocus(); } sub _handler_exitwindow { my ($self, $event) = @_; if(Wx::MessageBox('Are you sure you want to exit the application?', 'Wx + PerlTray Example', wxYES_NO|wxICON_QUESTION|wxCENTRE, $self) == wxYES) { $self->{_exit_application} = 1; $self->Close; } } sub _handler_startapp { my ($self, $event) = @_; wxTheApp->MainLoop(); # Application Will Exit Here } sub _handler_about{ my ($self, $event) = @_; PerlTray::MessageBox('About wxPerl and PerlTray demonstration', 'WxPerl PerlTray Demo', PerlTray::MB_OK|PerlTray::MB_ICONINFORMATION); } sub _handler_hotkey{ my ($self, $event) = @_; PerlTray::MessageBox('User pressed Hot Key ALT+SHIFT+F2', 'WxPerl PerlTray Demo', PerlTray::MB_OK|PerlTray::MB_ICONINFORMATION); } sub _handler_timer{ my ($self, $event) = @_; $self->{_timer_handler}(); } sub _handler_endsession { my ($self, $event) = @_; $self->{_exit_application} = 1; $self->Close; } sub _handler_perltrayshutdown { my ($self, $event) = @_; # do nothing as we handle EVENT_END_SESSION # $self->{_exit_application} = 1; # $self->Close; } sub OnClose { my ($self, $event) = @_; if($self->{_exit_application}) { $event->Skip(1); $self->Destroy; } else { $event->Skip(0); $self->Show(0); } } sub OnIconize { my ($self, $event) = @_; $event->Skip(1); $self->Show(0) if($self->IsIconized()); } sub contextmenu { my $self = shift; # return a context specific menu my $menu = []; foreach my $mname (@{ $self->{popupmenuorder} }) { my $menuitem = $self->{popupmenucontext}->{$mname}; if($menuitem->{visible} == 1) { my $popitem = [ $menuitem->{displayname} ]; if($menuitem->{enabled} == 1) { push(@$popitem, $menuitem->{code}); } else { push(@$popitem, undef); } push(@$menu, $popitem); } } return $menu; } sub _initialise_popup { my $self = shift; $self->{popupmenucontext} = { showwx => { displayname => 'Show Wx', code => "onTrayEvent(WXPT_EVT_SHOWWINDOW)", visible => 1, enabled => 1, }, about => { displayname => 'About', code => "onTrayEvent(WXPT_EVT_ABOUT)", visible => 1, enabled => 1, }, exit => { displayname => 'E&xit', code => "onTrayEvent(WXPT_EVT_EXITWINDOW)", visible => 1, enabled => 1, }, }; $self->{popupmenuorder} = ['showwx','about','exit']; } sub _initialise_timer { my $self = shift; # start a timer and set a sub handler $self->{_timer_handler} = sub { PerlTray::Balloon("30 second Timer Handler Called", 'Wx + PerlTray Example', "info", 10); }; PerlTray::SetTimer(':30'); } 1;