#!demo -f

# a demo program on Tcl/Tk and C
# 	Written by Eunmi Choi

frame .mbar -relief raised -bd 2 
frame .first
frame .second
frame .third
frame .fourth

pack .mbar -side top -fill x
 
menubutton .mbar.help -text Help -underline 0 \
        -menu .mbar.help.menu
pack .mbar.help -side right
menu .mbar.help.menu
.mbar.help.menu add command -label "Help" 
.mbar.help.menu add separator
.mbar.help.menu add command -label "About" -command {
	dialog .ddd {About the demo} {This is a demo program on Tcl/Tk and C, which is made by Eunmi Choi. If you have any question about this, please send me an e-mail at choie@cps.msu.edu} {} -1 OK
    }
.mbar.help.menu add separator
.mbar.help.menu add command -label "Test" -command {exec calc.tk} -underline 0

tk_menuBar .mbar .mbar.help
focus .mbar
 
pack .first .second .third .fourth -side top -padx 3m -pady 3m 

entry .entryl -width 10 -relief sunken -textvariable boxl
entry .entryr -width 10 -relief sunken -textvariable boxr
entry .entryc -width 20 -relief sunken -textvariable boxc
button .bp -text "+" -command {set boxc [expr $boxl + $boxr]}
button .bm -text "-" -command {set boxc [expr $boxl - $boxr]} 
button .bml -text "*" -command {set boxc [expr $boxl * $boxr]} 
button .bcon -text "Concatenate" -command {set boxc [concat $boxl$boxr]} 
button .beq -text "Equal" -command {set boxc [eq $boxl $boxr]} 
button .bbt -text "Beat" -command {set boxc [beat $boxl]} 
button .be -text "Exit" -command "puts Good-bye; exit"

 
pack .entryl .entryr -in .first -side left  \
        -padx 1m -pady 1m -ipadx 2m -ipady 2m
pack .entryc -in .second \
        -padx 1m -pady 1m -ipadx 2m -ipady 2m
pack .bp .bm .bml .bcon .beq .bbt .be -in .third -side left \
        -padx 1m -pady 1m -ipadx 2m -ipady 2m
 

# timer
# from /opt/tk/lib/tk/demos

label .counter -text 0.00 -relief raised -width 10
button .start -text Start -command {
    if $stopped {
        set stopped 0
        tick
    }
}
button .stop -text Stop -command {set stopped 1}
label .entcnt -text 0 -relief sunken -width 10
pack .start .stop .counter .entcnt -in .fourth -side left \
	-padx 1m -pady 1m -ipadx 2m -ipady 2m
 
set seconds 0
set hundredths 0
set stopped 1
set numents 0

proc tick {} {
    global seconds hundredths stopped numents
    if $stopped return
    after 50 tick
    set hundredths [expr $hundredths+5]
    if {$hundredths >= 100} {
        set hundredths 0
        set seconds [expr $seconds+1]
	set numents [expr $numents+1]  
    }
    .counter config -text [format "%d.%02d" $seconds $hundredths]
    .entcnt config -bg honeydew1 -text [format "%d events" $numents]
}


# dialog box
# from John Ousterhout's book

proc dialog {w title text bitmap default args} {
        global button

        # 1. Create the top-level window and divide it into top
        # and bottom parts.

        toplevel $w -class Dialog
        wm title $w $title
        wm iconname $w Dialog
        frame $w.top -relief raised -bd 1
        pack $w.top -side top -fill both
        frame $w.bot -relief raised -bd 1
        pack $w.bot -side bottom -fill both

        # 2. Fill the top part with the bitmap and message.

        message $w.top.msg -width 3i -text $text\
                        -font -Adobe-Times-Medium-R-Normal-*-180-*
        pack $w.top.msg -side right -expand 1 -fill both\
                        -padx 3m -pady 3m
        if {$bitmap != ""} {
                label $w.top.bitmap -bitmap $bitmap
                pack $w.top.bitmap -side left -padx 3m -pady 3m
        }

        # 3. Create a row of buttons at the bottom of the dialog.

        set i 0
        foreach but $args {
                button $w.bot.button$i -text $but -command\
                                "set button $i"
                if {$i == $default} {
                        frame $w.bot.default -relief sunken -bd 1
                        raise $w.bot.button$i
                        pack $w.bot.default -side left -expand 1\
                                        -padx 3m -pady 2m
                        pack $w.bot.button$i -in $w.bot.default\
                                        -side left -padx 2m -pady 2m\
                                        -ipadx 2m -ipady 1m
                } else {
                        pack $w.bot.button$i -side left -expand 1\
                                        -padx 3m -pady 3m -ipadx 2m -ipady 1m
                }
                incr i
        }

        # 4. Set up a binding for <Return>, if there`s a default,
        # set a grab, and claim the focus too.

        if {$default >= 0} {
                bind $w <Return> "$w.bot.button$default flash; \
                        set button $default"
        }
        set oldFocus [focus]
        grab set $w
        focus $w

        # 5. Wait for the user to respond, then restore the focus
        # and return the index of the selected button.

        tkwait variable button
        destroy $w
        focus $oldFocus
        return $button
}


 
. configure -background blue

