#!/usr/bin/perl -w
#############################################################
# Title:     timer_wx.pl
#
# Purpouse:  Pop open a wx window after a ceritan amount of time.
#            using wxPerl
#
# Last Mod:  Sun Feb 27 13:24:52 PST 2005
# Refernece: http://www.perl.com/pub/a/2001/09/12/wxtutorial1.html
#############################################################

# Usage message
if ( @ARGV eq 0 ){ 
    print "This will count down the number of minutes specified and then open a window.
           Usage: timer.pl <# of minutes> <Message>\n";
    exit;
}

use strict;
use Wx;


# Set time and message
my $time = shift @ARGV ;
my @message = @ARGV;

# Verify 
print "$time minute countdown. \n",
      "Message will read:\n@message\n";

while ( $time gt 0 ){
  print "Countdown: $time\n";
  `sleep 60` ;
  $time-- ;
}

#######################################
package MyApp;
use strict;
use vars qw(@ISA);
@ISA = qw(Wx::App);
use Wx::App;
sub OnInit
{
    my $self = shift;
    my $frame = MyFrame->new(   undef,          # Parent window
                                -1,             # Window id
                                'Check My App', # Title
                                [1,1],          # position X, Y
                                [200, 150]      # size X, Y
                               );
    $self->SetTopWindow($frame);    # Define the toplevel window
    $frame->Show(1);                # Show the frame
    return 1;
}
#######################################
package MyFrame;
use strict; 
use Wx::Event qw( EVT_BUTTON );   # Inherit button event actions
use base qw(Wx::Frame);           # Inherit from Wx::Frame

use vars qw(@ISA);
@ISA = qw(Wx::Frame);
use Wx qw(:id
          :toolbar
          wxNullBitmap
          wxDefaultPosition
          wxDefaultSize
          wxDefaultPosition
          wxDefaultSize
          wxNullBitmap
          wxTB_VERTICAL
          wxSIZE
          wxTE_MULTILINE
          wxBITMAP_TYPE_BMP
);
use Wx::Event qw(EVT_SIZE
                 EVT_MENU
                 EVT_COMBOBOX
                 EVT_UPDATE_UI
                 EVT_TOOL_ENTER
);
#######################################
sub new {

    my( $class ) = shift;
    my( $self ) = $class->SUPER::new( @_ );

    # Set ID's for widgets
    my $BTNID = 1; 
    my( $ID_TOOLBAR, $ID_COMBO ) = ( 2 .. 100 );
    my(  $IDM_FILE_OPEN, $IDM_FILE_CLOSE ) = ( 10_000 .. 10_100 );
    
    # Then define a Panel to put the button and menu on
    my $panel = Wx::Panel->new(         $self,              # parent
                                        -1                  # id
                               );
    # Text On Panel
    $self->{txt} = Wx::StaticText->new( $panel,             # parent
                                        $BTNID,             # id
                                        "@message",         # label
                                        [50, 15]            # position
					
                                       );
    # Button On Panel
    $self->{btn} = Wx::Button->new(     $panel,             # parent
                                        $BTNID,             # id
                                        ">>> Close <<<",    # label
                                        [50,50]             # position
                                   );
   ######################################
   # Menu
   ###################################### 
   my $file_menu = Wx::Menu->new();
   # Menu items
   my @file_menu_entries = (   [$IDM_FILE_OPEN,"&Open\tCtrl-O","Open" ],
                               ['-'],
                               [wxID_EXIT, "E&xit\tCtrl-X", "Exit $0"],
			       ['-'],
			       
                           );
   # Append items to menu list
   foreach (@file_menu_entries) {
        if ($$_[0] eq '-') {
	    # if '-' add seperator
            $file_menu->AppendSeparator();
        } else {
	    # Add all the menu items to the menu	    
            $file_menu->Append (   $$_[0],  # MenuItemID
	                           $$_[1],  # MenuItemText
				   $$_[2],  # MouseHoverText
				)
        }
	
   }
   # Create the menubar that will contain the menu above.
   my $menubar = Wx::MenuBar->new();

   # Attach a menu to the menubar. named Timer
   $menubar->Append ($file_menu, '&Timer');
 
   # Attach the menubar to the window. 
   $self->SetMenuBar ($menubar);
   
   ###################################### 
   # Events
   ###################################### 
   # EVT_MENU is the event handling subroutine for menu-events.
   # takes a reference to the frame and the menu
    EVT_MENU(  $self, 
               wxID_EXIT, 
	       sub {$_[0]->Close( 1 )} 
	     );

   # EVT_BUTTON is the event handling subroutine for button-events.
   # takes the object, and the event
   EVT_BUTTON(   $self,          # Object to bind to
                 $BTNID,         # ButtonID
		 sub {$_[0]->Close( 1 )} 
		 #\&ButtonClicked # Subroutine to execute    
              );
   return $self;
}
###########################################################
sub ButtonClicked 
{ 
 my( $self, $event ) = @_; 
 ## Change the contents of $self->{txt}
 #$self->{txt}->SetLabel("@message\n");
 $self->{txt}->SetLabel("Close Window\n");
 exit 1;
} 
###########################################################
# The main program
package main;
my $wxobj = MyApp->new();
$wxobj->MainLoop;
