#!/usr/bin/wksh -openlook

#	Copyright (c) 1991, 1992 UNIX System Laboratories, Inc.
#	All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
#	UNIX System Laboratories, Inc.
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"@(#)wksh:olexamples/timer	1.5.1.1"


# A simple stopwatch script
# attributes can be set as: timer -bg red -fg white, etc ...

function update_time {
	typeset -Z2 SEC
	let CURTIME=SECONDS-STARTTIME
	let MIN=CURTIME/60
	let SEC=CURTIME%60
	sv $TXT string:"$MIN:$SEC"
}
function clock_update {
	if [ "$STOPPED" != true ]
	then 
		update_time
		XtAddTimeOut $2 1000 "clock_update $1 $2"
	fi
}

function stop_clock {
	if [ $STOPPED = false ]
	then 
	     STOPPED=true; 
	     SAVE_TIME=$SECONDS
	     sv $STOP label:START
	else 
	     STOPPED=false; 
	     SECONDS=SAVE_TIME
	     sv $STOP label:STOP
	     clock_update $TXT $TOPLEVEL
	fi
}

STOPPED=false
BUTTONTYPE=oblongButton
oi TOPLEVEL stopwatch StopWatch -fn 12x24 -openlook "$@"

cmw FORM form form $TOPLEVEL
cmw TXT TXT staticText $FORM string:"00:00" x:100 y:10
cmw CON CON controlArea $FORM `under $TXT 10`
addbuttons -w $CON \
		QUIT QUIT "exit 0" \
		RESET RESET 'STARTTIME=$SECONDS; update_time' \
		STOP STOP  stop_clock
STARTTIME=$SECONDS
XtAddTimeOut $TOPLEVEL 1000 "clock_update $TXT $TOPLEVEL"
rw $TOPLEVEL
ml
