#!/usr/bin/wish -f
#
#   xmclock, the metric clock for X 
#   version 1.0
#
#   From Anvilsoft
#   Copyright 1997
#
#   xmclock is distributed under the GNU General Public Licence.
#   xmclock comes with ABSOLUTELY NO WARRANTY or GUARANTEE OF FITNESS
#   FOR A PARTICULAR PURPOSE.  See the file COPYING or the ABOUT window
#   for complete information.
#
#   SET THE XMCDIR VARIABLE TO THE DIRECTORY YOU INSTALLED INTO, EXAMPLE:
#   
#   set xmcdir /usr/local/bin

# Changed by kms@linuxware.com for the RPM package
# set xmcdir      /usr/local/bin

set xmcdir      /usr/X11R6/lib/X11/xmclock/

set i           1
set k           25
set precision   3
set fontsize    250
set oldfntsz    250
set showdate    0
set oldshowdate 0
set chrono      0
set oldchrono   0
set alarm       0
set oldalarm    0
set standard    0
set oldstandard 0
set etime       0
set stime       0
set menuon      0
set alarm_time  0
set total_secs  0

frame .main

frame .main.time

label .main.time.mtime -textvariable mtime \
	-font "-*-courier-bold-r-normal-*-*-$fontsize-*-*-*-*-*-*"

pack .main.time.mtime
pack .main.time
pack .main

proc calc_mtime {} {
    global total_secs
    global k
    global std_hrs
    global std_min
    global std_sec
    global time_call

    if {$k < 25} then {
	after 90
	set total_secs [expr $total_secs + 0.1]
	incr k
    }

    if {$k == 25} then {
	set k 1
	set time_call 1
	#get time from system
	set time_cmd {date "+%H %M %S"}
	set std_time [eval exec $time_cmd]
	scan "$std_time" "%d %d %d" std_hrs std_min std_sec

	#calculate total elapsed seconds
	set total_secs [expr $std_hrs * 60 * 60 + $std_min * 60 + $std_sec]
    }
	#calculate metric time
	return [expr $total_secs / 86.4]
        
}

proc change_font {} {
    global fontsize
    global oldfntsz
    global standard
    set oldfntsz $fontsize
    destroy .main.time.mtime
    label .main.time.mtime -textvariable mtime \
	-font "-*-courier-bold-r-normal-*-*-$fontsize-*-*-*-*-*-*"
    pack .main.time.mtime
    if {$standard == 1} then {
	destroy .main.time.stime
	label .main.time.stime -textvariable stime \
		-font "-*-courier-bold-r-normal-*-*-$fontsize-*-*-*-*-*-*"
	pack .main.time.stime
    }
}

proc set_stime {} {
    global standard
    global oldstandard
    global fontsize
    set oldstandard $standard
    if {$standard == 0} then {destroy .main.time.stime}
    if {$standard == 1} then {
	label .main.time.stime -textvariable stime \
		-font "-*-courier-bold-r-normal-*-*-$fontsize-*-*-*-*-*-*"
	pack .main.time.stime
    }
}

proc set_chrono {} {
    global oldtime
    global oldchrono
    global chrono
    global chron_mtime
    global etime
    global showdate
    set oldchrono $chrono
    if {$chrono == 0} then {
	set etime 0
	destroy .main.chrono
    }
    if {$chrono == 1} then {
	set oldtime $chron_mtime
	label .main.chrono -textvariable etime -relief ridge -bg orange
	if {$showdate == 0} then {
	    pack .main.chrono -side left
	}
	if {$showdate == 1} then {
	    pack configure .main.chrono -before .main.date -side left
	} 
    }
}

proc set_date {} {
    global showdate
    global oldshowdate
    global date
    global chrono
    set oldshowdate $showdate
    if {$showdate == 0} then {
	destroy .main.date
    }
    if {$showdate == 1} then {
	set date [exec date]
	scan "$date" "%s %s %s %s %s %s" day month day_num wholetime tz year
	set date "$day $month $day_num , $year"
	label .main.date -textvariable date -bg green -relief ridge
	if {$chrono == 0} then {pack .main.date -side left}
	if {$chrono == 1} then {
	    pack configure .main.date -after .main.chrono -side left
	}
    }
}

proc set_alarm {} {
    global alarm
    global alarm_time
    global oldalarm
    global showdate

    if {$alarm == 2} {
	destroy .main.alarm
	set alarm_time 0
	frame  .main.alarm -relief ridge -borderwidth 3 -bg red
	button .main.alarm.quitit -text "A L A R M" \
		-command {destroy .main.alarm} -bg red
	pack .main.alarm.quitit
	if {$showdate == 0} then {
	    pack .main.alarm -side right
	}
	if {$showdate == 1} then {
	    pack configure .main.alarm -after .main.date -side right
	}
	for {set j 0} {$j < 15} {incr j} {
	    bell
	    after 130
	}
    }

    set oldalarm $alarm

    if {$alarm == 1} {

	label .main.alarm -text "Alarm Set" -relief ridge -bg red
	if {$showdate == 0} then {
	    pack .main.alarm -side right
	}
	if {$showdate == 1} then {
	    pack configure .main.alarm -after .main.date -side right
	}

	set alarm_time x
	toplevel .getalarm
	label .getalarm.title -text "What time to set for?"
	entry .getalarm.enter -width 8
	set width [winfo screenwidth  .]
	set height [winfo screenwidth .]
	set x [expr ($width/2) - 150]
	set y [expr ($height/2) - 150]

	wm transient .getalarm .
	wm geometry .getalarm +$x+$y

	pack .getalarm.title .getalarm.enter -side left
	bind .getalarm.enter <Key-Return> {
	    set alarm_time [.getalarm.enter get]
	    destroy .getalarm
	}
    }

    if {$alarm == 0} then {
	if {$alarm_time == "x"} then {destroy .getalarm}
	destroy .main.alarm
    }
}

proc about_window {} {

    global xmcdir

    image create photo logo -file $xmcdir/support/xmcdlogo.gif

    toplevel .about -class Dialog
    frame .about.picholder -relief ridge -borderwidth 3
    label .about.title -text "Version 1.0, (c) 1997 Anvilsoft"
    label .about.authr -text "Written by Shawn Boyete"
    label .about.picholder.pic   -image logo
    button .about.quit -text "Kewl!" -command { destroy .about } \
	    -bg yellow -relief groove
    button .about.info -text "xmclock has ABSOLUTELY NO WARRANTY!" \
	    -command {exec xb "$xmcdir/support/COPYING"} -bg orange \
	    -relief groove
   
    set width [winfo screenwidth  .]
    set height [winfo screenwidth .]
    set x [expr ($width/2) - 150]
    set y [expr ($height/2) - 150]

    wm transient .about .
    wm geometry .about +$x+$y

    pack .about.title
    pack .about.picholder -padx 10 -pady 2
    pack .about.picholder.pic
    pack .about.authr
    pack .about.quit -side left -padx 1 -pady 5
    pack .about.info -side right -padx 1 -pady 5
}

proc kill_menu {} {
    global menuon
    set menuon 0
    destroy .menubar
}

proc build_menu {} {
global menuon
global xmcdir

frame .menubar -relief ridge -borderwidth 3

menubutton .menubar.ctrl -text "Control" -menu .menubar.ctrl.menu \
	-relief groove -borderwidth 3

pack .menubar.ctrl -side left

menu .menubar.ctrl.menu
.menubar.ctrl.menu add radiobutton -label "0 Digit Precision" \
	-variable precision -value 0 
.menubar.ctrl.menu add radiobutton -label "1 Digit Precision" \
	-variable precision -value 1 
.menubar.ctrl.menu add radiobutton -label "2 Digit Precision" \
	-variable precision -value 2
.menubar.ctrl.menu add radiobutton -label "3 Digit Precision" \
	-variable precision -value 3 
.menubar.ctrl.menu add separator
.menubar.ctrl.menu add radiobutton -label "Big Font" \
	-variable fontsize -value 400 
.menubar.ctrl.menu add radiobutton -label "Medium Font" \
	-variable fontsize -value 250 
.menubar.ctrl.menu add radiobutton -label "Small Font" \
	-variable fontsize -value 150 
.menubar.ctrl.menu add separator
.menubar.ctrl.menu add command -label "Kill Menu" -command { kill_menu }
.menubar.ctrl.menu add separator
.menubar.ctrl.menu add command -label "Quit"      -command { exit }

menubutton .menubar.opt -text "Options" -menu .menubar.opt.menu \
	-relief groove -borderwidth 3

pack .menubar.opt -side left

menu .menubar.opt.menu
.menubar.opt.menu add checkbutton -label "Standard Time" \
	-variable standard -onvalue 1 -offvalue 0
.menubar.opt.menu add separator
.menubar.opt.menu add checkbutton -label "Chronograph" \
	-variable chrono -onvalue 1 -offvalue 0
.menubar.opt.menu add separator
.menubar.opt.menu add checkbutton -label "Date" \
	-variable showdate -onvalue 1 -offvalue 0
.menubar.opt.menu add separator
.menubar.opt.menu add checkbutton -label "Alarm" \
	-variable alarm -onvalue 1 -offvalue 0

menubutton .menubar.help -text "Help" -menu .menubar.help.menu \
	-relief groove -borderwidth 3

pack .menubar.help -side right

menu .menubar.help.menu
.menubar.help.menu add command -label "xmclock guide" \
	-command { exec xb "$xmcdir/support/aboutxmc.txt" }
.menubar.help.menu add command -label "Metric Time Ref" \
	-command { exec xb "$xmcdir/support/aboutmt.txt" }
.menubar.help.menu add command -label "Num/Char Ref" \
	-command { exec xb "$xmcdir/support/ncr.txt"}
.menubar.help.menu add separator
.menubar.help.menu add command -label "About xmclock" \
	-command {about_window}

pack .menubar -side top -before .main -fill x -expand true
set menuon 1
}

bind all <Control-x> { exit }
bind all <Control-m> { if {$menuon == 0} then {build_menu} }
bind all <Control-Alt-1> \
	{ tk_dialog .truth "The Truth" "Microsoft sucks!" info 0 Right_On! }

build_menu

while { $i == "1" } {
    if { $fontsize != $oldfntsz } then { change_font }
    if { $standard != $oldstandard } then {set_stime}
    if { $chrono != $oldchrono } then {set_chrono}
    if { $alarm != $oldalarm } then {set_alarm}
    if { $showdate != $oldshowdate } then {set_date}

    set chron_mtime [calc_mtime]
    if {$chron_mtime > 999.999} then {
	set k 25
	set chron_mtime [calc_mtime]
	if {$showdate == 1} then {
	    destroy .main.date
	    set_date
	}
    }

    if {$chron_mtime < 10} then {
	if {$precision == 0} then {set mtime [format "00%.0f" $chron_mtime ]}
	if {$precision == 1} then {set mtime [format "00%.1f" $chron_mtime ]}
	if {$precision == 2} then {set mtime [format "00%.2f" $chron_mtime ]}
	if {$precision == 3} then {set mtime [format "00%.3f" $chron_mtime ]}
    }
    if {$chron_mtime >= 10 && $chron_mtime < 100} then {
	if {$precision == 0} then {set mtime [format "0%.0f" $chron_mtime ]}
	if {$precision == 1} then {set mtime [format "0%.1f" $chron_mtime ]}
	if {$precision == 2} then {set mtime [format "0%.2f" $chron_mtime ]}
	if {$precision == 3} then {set mtime [format "0%.3f" $chron_mtime ]}
    }
    if {$chron_mtime >= 100} then {
	if {$precision == 0} then {set mtime [format "%.0f" $chron_mtime ]}
	if {$precision == 1} then {set mtime [format "%.1f" $chron_mtime ]}
	if {$precision == 2} then {set mtime [format "%.2f" $chron_mtime ]}
	if {$precision == 3} then {set mtime [format "%.3f" $chron_mtime ]}
    }

    if {$standard == 1} then {
	if {$time_call == 1} then {
	    if {$std_hrs < 10} then {set std_hrs [format "0%s" $std_hrs]}
	    if {$std_min < 10} then {set std_min [format "0%s" $std_min]}
	    if {$std_sec < 10} then {set std_sec [format "0%s" $std_sec]}
	    set time_call 0
	}
	set stime [format "%s:%s:%s" $std_hrs $std_min $std_sec]
    }

    if { $chrono == 1 } then {
	set diff [expr $chron_mtime - $oldtime]
	set etime [format "%.3f" [expr $etime + $diff]]
	set oldtime $chron_mtime
    }
    if { $alarm == 1 } then {
	if {$alarm_time != "x"} then {
	    set tminus [expr $alarm_time - $chron_mtime]
	    if {$tminus < 0.01 && $tminus > -0.02} then {
		set alarm 2
		set_alarm
		set alarm 0
		set oldalarm 0
	    }
	}
    }
    update
}






