#!/usr/bin/perl -w
######################################################################
# (c) Michael Schilli 1999
######################################################################
###############################################################
# stopwatch.pl: Stoppuhr mit start/stop/reset-Funktion und 
#               GUI-Anzeige
###############################################################
use Tk;
use Stopwatch;                     # Klasse 'Stopwatch' 
                                   # einbinden
my $top = MainWindow->new();
                                   # Label mit dynamisch ver-
                                   # änderbarem Text erzeugen
$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 (mit gleichzei-
$top->Button(-text    => "Reset",  # tigem Stoppuhr-Update)
             -command => sub { $sw->reset(); update_func($sw) }
            )->pack("-side" => "left");

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

update_func($sw);                  # Anzeige auf 00:00:00

MainLoop;                          # Event-Hauptschleife

###############################################################
sub update_func {
###############################################################
# Sekunden-Zähler der Stoppuhr auslesen, in HH:MM:SS-Format 
# umwandeln und die Variable $stopwatch_display setzen.
###############################################################
    my $self = shift;

    $seconds = $self->gettime();   # Stoppuhr-Zeitabfrage

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

                                   # GUI-Anzeige setzen
    $stopwatch_display = sprintf("%02d:%02d:%02d", 
                                 $hour, $min, $sec);
}
