home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/wish
-
- #apologies to David, I have hacked his Countdown program somewhat...
-
- # $Id: countdown.tcl,v 1.2 2003/10/27 16:33:15 davidw Exp $
- #package provide countdown 0.1
-
- namespace eval ::countdown {
- set seconds 0
- set started 0
- set starttime 0
- set paused 0
- }
-
- proc ::countdown::start {time} {
- variable started
- variable multiple
- variable starttime
- variable seconds
- variable paused
-
- if { $time == 0 } {
- return
- }
-
- if { $paused } {
- ::countdown::gui::unpause
- set paused 0
- }
-
- if { $started == 0 } {
- set starttime $time
- set seconds $time
- starttimer
- set started 1
- } else {
- starttimer
- }
- }
-
- proc ::countdown::pause {} {
- variable paused
-
- set paused 1
- stoptimer
- ::countdown::gui::pause
- }
-
- proc ::countdown::dotimer {} {
- variable seconds
-
- incr seconds -1
- ::countdown::gui::Update $seconds
- if { $seconds <= 0 } {
- ::countdown::gui::timeup
- return
- }
- starttimer
- }
-
- proc ::countdown::starttimer {} {
- after [expr 1000] ::countdown::dotimer
- }
-
- proc ::countdown::stoptimer {} {
- foreach Id [after info] {
- after cancel $Id
- }
- }
-
- proc ::countdown::reset {} {
- variable started
- variable seconds
- variable starttime
-
- set started 0
- set seconds 0
- stoptimer
- ::countdown::gui::reset $starttime
- update
- }
-
- proc ::countdown::getstart {} {
- variable starttime
- return $starttime
- }
-
- ################################################
-
- # countdowngui.tcl -- Tk Gui interface to cowntdown timer.
-
- # $Id: countdowngui.tcl,v 1.3 2003/10/27 16:33:15 davidw Exp $
-
- #package provide countdowngui 0.1
-
- namespace eval ::countdown::gui {
- if { [tk windowingsystem] == "x11" } {
- option add *borderWidth 1 widgetDefault
- option add *activeBorderWidth 1 widgetDefault
- option add *selectBorderWidth 1 widgetDefault
- option add *font -adobe-helvetica-medium-r-normal-*-12-*-*-*-*-*-*
-
- option add *padX 2
- option add *padY 4
-
- option add *Listbox.background white
- option add *Listbox.selectBorderWidth 0
- option add *Listbox.selectForeground white
- option add *Listbox.selectBackground #4a6984
-
- option add *Entry.background white
- option add *Entry.selectBorderWidth 0
- option add *Entry.selectForeground white
- option add *Entry.selectBackground #4a6984
-
- option add *Text.background white
- option add *Text.selectBorderWidth 0
- option add *Text.selectForeground white
- option add *Text.selectBackground #4a6984
-
- option add *Menu.activeBackground #4a6984
- option add *Menu.activeForeground white
- option add *Menu.activeBorderWidth 0
- option add *Menu.highlightThickness 0
- option add *Menu.borderWidth 2
-
- option add *MenuButton.activeBackground #4a6984
- option add *MenuButton.activeForeground white
- option add *MenuButton.activeBorderWidth 0
- option add *MenuButton.highlightThickness 0
- option add *MenuButton.borderWidth 0
-
- option add *highlightThickness 0
- option add *troughColor #bdb6ad
- }
-
- set bg 0
- set alarm 0
-
- set ch 60
- set cw 60
- set coffset 15
- set ovalx1 [expr 1 + $coffset]
- set ovalx2 [expr 59 + $coffset]
-
- wm title . "Countdown Timer"
- canvas .clock -height 60 -width 90 -bg white
- set oval [.clock create oval $ovalx1 1 $ovalx2 59 -fill black]
- set arc [.clock create arc $ovalx1 1 $ovalx2 59 -start 90 -extent 359 -fill green \
- -disabledstipple gray50]
-
- label .lmin -text "Minutes:" -anchor w
- label .lsec -text "Seconds:" -anchor w
- entry .min -vcmd {string is integer %P} -validate key -width 3
- .min insert 0 0
- entry .sec -vcmd {string is integer %P} -validate key -width 3
- .sec insert 0 0
- # label .min -text "Minutes:"
- # entry .time -textvariable ::countdown::minutes -background green
-
- button .start -text "Start" -command {::countdown::start [::countdown::gui::gettime]}
- button .pause -text "Pause" -command ::countdown::pause
- button .reset -text "Reset" -command ::countdown::reset
- button .exit -text "Exit" -command {exit 0}
-
- set cols 2
- grid .clock -columnspan $cols
- grid .lmin .min -sticky ew
- grid .lsec .sec -sticky ew
- grid .start -sticky ew -columnspan $cols
- grid .pause -sticky ew -columnspan $cols
- grid .reset -sticky ew -columnspan $cols
- grid .exit -sticky ew -columnspan $cols
-
- wm resizable . 0 0
- }
-
- proc ::countdown::gui::reset {starttime} {
- variable arc
- variable oval
- variable bg
-
- set bg 0
- set min [expr {$starttime / 60}]
- set sec [expr {$starttime % 60}]
-
- .min delete 0 end
- .min insert 0 $min
- .sec delete 0 end
- .sec insert 0 $sec
-
- .clock itemconfigure $arc -extent 359
- .clock itemconfigure $arc -fill green
- .clock itemconfigure $oval -fill black
- .clock configure -bg white
- }
-
- proc ::countdown::gui::pause {} {
- variable arc
- variable paused
- variable ovalx1
- variable coffset
-
- .clock itemconfigure $arc -state disabled
- set paused [.clock create text [expr {$coffset + 30}] 30 \
- -text "Paused" -fill blue -font {Helvetica -22 bold}]
- }
-
- proc ::countdown::gui::unpause {} {
- variable paused
- variable arc
-
- .clock itemconfigure $arc -state normal
- catch {.clock delete $paused}
- }
-
- proc ::countdown::gui::timeup {} {
- variable alarm
-
- set alarm 1
- update
- ::countdown::gui::alarm
- }
-
- proc ::countdown::gui::Update {seconds} {
- variable arc
- variable paused
-
- set min [expr {$seconds / 60}]
- set sec [expr {$seconds % 60}]
- set pct [expr {double($seconds) / [::countdown::getstart] * 360.0}]
- if {$pct < 120} {
- .clock itemconfigure $arc -fill red
- } elseif {$pct < 240} {
- .clock itemconfigure $arc -fill yellow
- }
- .clock itemconfigure $arc -extent $pct
- .min delete 0 end
- .min insert 0 $min
- .sec delete 0 end
- .sec insert 0 $sec
- }
-
- proc ::countdown::gui::gettime {} {
- return [expr {[.min get] * 60 + [.sec get]}]
- }
-
- #if { ![catch { package require snack }] } {
- # snack::sound alarmsound -file \
- # [file join [file dirname [info script]] attention.wav]
- #
- # set ::countdown::gui::alarmcmd [list alarmsound play -block 1]
- #} else {
- set ::countdown::gui::alarmcmd bell
- #}
-
- proc ::countdown::gui::alarm {} {
- variable alarm
- variable alarmcmd
- variable bg
- variable oval
-
- if { $bg } {
- .clock configure -bg white
- .clock itemconfigure $oval -fill black
- set bg 0
- } else {
- .clock configure -bg black
- .clock itemconfigure $oval -fill white
- set bg 1
- }
- eval $alarmcmd
- if { $alarm } {
- after idle [list after 100 ::countdown::gui::alarm]
- }
- }
-
-