#!/usr/bin/perl -w
######################################################################
# stopwatch.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

######################################################################
# stopwatch.pl: stopwatch with start/stop/reset function and GUI display
######################################################################
use Tk;
use Stopwatch;                     # include 'Stopwatch' class

my $top = MainWindow->new();
                                   # create label with dynamically 
                                   # modifiable text
$top->Label(-textvariable => \$stopwatch_display)->pack();

$top->Button(-text    => "Start",  # Start button
             -command => sub { $sw->start() })->pack(-side => "left");

$top->Button(-text    => "Stop",   # Stop button
             -command => sub { $sw->stop() })->pack(-side => "left");

                                   # Reset button (reset with
$top->Button(-text    => "Reset",  # simultaneous stopwatch update)
             -command => sub { $sw->reset(); update_func($sw) }
            )->pack("-side" => "left");

$top->Button(-text    => "Exit",   # Exit button
             -command => sub { exit(0) })->pack("-side" => "left");
  
                                   # new stopwatch
$sw = Stopwatch->new(\&update_func, 1000);

update_func($sw);                  # display at 00:00:00

MainLoop;                          # main event loop

######################################################################
sub update_func {
######################################################################
# read seconds counter of the stopwatch, convert into HH:MM:SS-Format
# and set the variable $stopwatch_display
######################################################################
    my $self = shift;

    $seconds = $self->gettime();   # stopwatch time check

                        # seconds -> HH:MM:SS 
                        # gmtime(0) is 00:00:00, localtime() in GMT
    ($sec, $min, $hour) = gmtime($seconds);

                                   # set GUI display
    $stopwatch_display = sprintf("%02d:%02d:%02d", $hour, $min, $sec);
}

