#!/usr/local/bin/wish -f
#
# This script was written as an entry in Tom LaStrange's rolodex
# benchmark.  It creates something that has some of the look and
# feel of a rolodex program, although it's lifeless and doesn't
# actually do the rolodex application.

foreach i [winfo child .] {
    catch {destroy $i}
}

proc tkerror err {
    global errorInfo
    puts stdout "$errorInfo"
}

#------------------------------------------
# Phase 0: create the front end.
#------------------------------------------

frame .frame -relief flat
pack append . .frame {top filly frame center}

set names {{} Name: Address: {} {} {Home Phone:} {Work Phone:} Fax:}
foreach i {1 2 3 4 5 6 7} {
    frame .frame.$i
    pack append .frame .frame.$i {top pady 4 frame e}

    label .frame.$i.label -text [lindex $names $i] -anchor e
    entry .frame.$i.entry -width 30 -relief sunken
    pack append .frame.$i .frame.$i.entry right .frame.$i.label right
}

frame .buttons
pack append . .buttons {bottom pady 4 frame center}
button .buttons.clear -text Clear
button .buttons.add -text Add
button .buttons.search -text Search
button .buttons.delete -text "Delete ..."
pack append .buttons .buttons.clear {left padx 4} \
    .buttons.add {left padx 4} .buttons.search {left padx 4} \
    .buttons.delete {left padx 4}

#------------------------------------------
# Phase 1: Add menus, dialog boxes
#------------------------------------------

frame .menu -relief raised -borderwidth 1
pack before .frame .menu {top fillx}

menubutton .menu.file -text "File" -menu .menu.file.m
menu .menu.file.m
.menu.file.m add command -label "Load ..." -command fileAction
.menu.file.m add command -label "Exit" -command {destroy .}

menubutton .menu.help -text "Help" -menu .menu.help.m
menu .menu.help.m

pack append .menu .menu.file left .menu.help right

# The mkDialog procedure below was pirated from the widget demo.  It
# was not written fresh for this benchmark.

# Create a dialog box.  Takes three or more arguments.  The first is
# the name of the window to use for the dialog box.  The second is a set
# of arguments for use in creating the message of the dialog box.  The
# third and following arguments consist of two-element lists, each
# describing one button.  The first element gives the text to be displayed
# in the button, the second gives the command to be invoked when the
# button is invoked.

proc mkDialog {w msgArgs args} {
    catch {destroy $w}
    toplevel $w -class Dialog
    set oldFocus [focus]

    # Create two frames in the main window. The top frame will hold the
    # message and the bottom one will hold the buttons.  Arrange them
    # one above the other, with any extra vertical space split between
    # them.

    frame $w.top -relief raised -border 1
    frame $w.bot -relief raised -border 1
    pack append $w $w.top {top fill expand} $w.bot {top fill expand}
    
    # Create the message widget and arrange for it to be centered in the
    # top frame.
    
    eval message $w.top.msg -justify center \
	    -font -Adobe-times-medium-r-normal--*-180* $msgArgs
    pack append $w.top $w.top.msg {top expand padx 5 pady 5}
    
    # Create as many buttons as needed and arrange them from left to right
    # in the bottom frame.  Embed the left button in an additional sunken
    # frame to indicate that it is the default button, and arrange for that
    # button to be invoked as the default action for clicks and returns in
    # the dialog.

    if {[llength $args] > 0} {
	set arg [lindex $args 0]
	frame $w.bot.0 -relief sunken -border 1
	pack append $w.bot $w.bot.0 {left expand padx 20 pady 20}
	button $w.bot.0.button -text [lindex $arg 0] \
		-command "[lindex $arg 1]; destroy $w; focus $oldFocus"
	pack append $w.bot.0 $w.bot.0.button {expand padx 12 pady 12}
	bind $w.top <Enter> "$w.bot.0.button activate"
	bind $w.top.msg <Enter> "$w.bot.0.button activate"
	bind $w.bot <Enter> "$w.bot.0.button activate"
	bind $w.top <Leave> "$w.bot.0.button deactivate"
	bind $w.top.msg <Leave> "$w.bot.0.button deactivate"
	bind $w.bot <Leave> "$w.bot.0.button deactivate"
	bind $w <1> "$w.bot.0.button config -relief sunken"
	bind $w <ButtonRelease-1> \
		"[lindex $arg 1]; $w.bot.0.button deactivate; destroy $w; focus $oldFocus"
	bind $w <Return> "[lindex $arg 1]; destroy $w; focus $oldFocus"
	focus $w

	set i 1
	foreach arg [lrange $args 1 end] {
	    button $w.bot.$i -text [lindex $arg 0] \
		    -command "[lindex $arg 1]; destroy $w; focus $oldFocus"
	    pack append $w.bot $w.bot.$i {left expand padx 20}
	    set i [expr $i+1]
	}
    }
    wm geometry $w +300+350
}

proc deleteAction {} {
    mkDialog .delete {-text "Are you sure?" -aspect 10000} \
	    "OK clearAction" "Cancel {}"
}
.buttons.delete config -command deleteAction

proc fileAction {} {
    mkDialog .fileSelection {-text "This is a dummy file selection dialog box, which is used because there isn't a good file selection dialog built into Tk yet." -aspect 400} "OK {puts stderr {dummy file name}}"
}

#------------------------------------------
# Phase 3: Print contents of card
#------------------------------------------

proc addAction {} {
    global names
    foreach i {1 2 3 4 5 6 7} {
	puts stderr [format "%-12s %s" [lindex $names $i] [.frame.$i.entry get]]
    }
}
.buttons.add config -command addAction

#------------------------------------------
# Phase 4: Miscellaneous other actions
#------------------------------------------

proc clearAction {} {
    foreach i {1 2 3 4 5 6 7} {
	.frame.$i.entry delete 0 end
    }
}
.buttons.clear config -command clearAction

proc fillCard {} {
    clearAction
    .frame.1.entry insert 0 "John Ousterhout"
    .frame.2.entry insert 0 "CS Division, Department of EECS"
    .frame.3.entry insert 0 "University of California"
    .frame.4.entry insert 0 "Berkeley, CA 94720"
    .frame.5.entry insert 0 "private"
    .frame.6.entry insert 0 "510-642-0865"
    .frame.7.entry insert 0 "510-642-5775"
}
.buttons.search config -command "addAction; fillCard"

#----------------------------------------------------
# Phase 5: Accelerators, mnemonics, command-line info
#----------------------------------------------------

.buttons.clear config -text "Clear    Ctrl+C"
bind Entry <Control-c> clearAction
.buttons.add config -text "Add    Ctrl+A"
bind Entry <Control-a> addAction
.buttons.search config -text "Search    Ctrl+S"
bind Entry <Control-s> "addAction; fillCard"
.buttons.delete config -text "Delete...    Ctrl+D"
bind Entry <Control-d> deleteAction

.menu.file.m entryconfig 0 -accel Ctrl+F
bind Entry <Control-f> fileAction
.menu.file.m entryconfig 1 -accel Ctrl+Q
bind Entry <Control-q> {destroy .}

focus .frame.1.entry

#----------------------------------------------------
# Phase 6: help
#----------------------------------------------------

proc Help {topic {x 0} {y 0}} {
    global helpTopics helpCmds
    if {$topic == ""} return
    while {[info exists helpCmds($topic)]} {
	set topic [eval $helpCmds($topic)]
    }
    if [info exists helpTopics($topic)] {
	set msg $helpTopics($topic)
    } else {
	set msg "Sorry, but no help is available for this topic"
    }
    mkDialog .help "-text {Information on $topic:\n\n$msg} -justify left -aspect 300" "OK {}"
}

proc getMenuTopic {w x y} {
    return $w.[$w index @[expr $y-[winfo rooty $w]]]
}

bind Entry <Any-F1> {Help [winfo containing %X %Y] %X %Y}
bind Entry <Any-Help> {Help [winfo containing %X %Y] %X %Y}

# Help text and commands follow:

set helpTopics(.menu.file) {This is the "file" menu.  It can be used to invoke some overall operations on the rolodex applications, such as loading a file or exiting.}

set helpCmds(.menu.file.m) {getMenuTopic $topic $x $y}
set helpTopics(.menu.file.m.0) {The "Load" entry in the "File" menu posts a dialog box that you can use to select a rolodex file}
set helpTopics(.menu.file.m.1) {The "Exit" entry in the "File" menu causes the rolodex application to terminate}
set helpCmds(.menu.file.m.none) {set topic ".menu.file"}

set helpTopics(.frame.1.entry) {In this field of the rolodex entry you should type the person's name}
set helpTopics(.frame.2.entry) {In this field of the rolodex entry you should type the first line of the person's address}
set helpTopics(.frame.3.entry) {In this field of the rolodex entry you should type the second line of the person's address}
set helpTopics(.frame.4.entry) {In this field of the rolodex entry you should type the third line of the person's address}
set helpTopics(.frame.5.entry) {In this field of the rolodex entry you should type the person's home phone number, or "private" if the person doesn't want his or her number publicized}
set helpTopics(.frame.6.entry) {In this field of the rolodex entry you should type the person's work phone number}
set helpTopics(.frame.7.entry) {In this field of the rolodex entry you should type the phone number for the person's FAX machine}

set helpCmds(.frame.1.label) {set topic .frame.1.entry}
set helpCmds(.frame.2.label) {set topic .frame.2.entry}
set helpCmds(.frame.3.label) {set topic .frame.3.entry}
set helpCmds(.frame.4.label) {set topic .frame.4.entry}
set helpCmds(.frame.5.label) {set topic .frame.5.entry}
set helpCmds(.frame.6.label) {set topic .frame.6.entry}
set helpCmds(.frame.7.label) {set topic .frame.7.entry}

set helpTopics(context) {Unfortunately, this application doesn't support context-sensitive help in the usual way, because Tk doesn't yet have a grab mechanism and this is needed for context-sensitive help.  Instead, you can achieve much the same effect by simply moving the mouse over the window you're curious about and pressing the Help or F1 keys.  You can do this anytime.}
set helpTopics(help) {This application provides only very crude help.  Besides the entries in this menu, you can get help on individual windows by moving the mouse cursor over the window and pressing the Help or F1 keys.}
set helpTopics(window) {This window is a dummy rolodex application created as part of Tom LaStrange's toolkit benchmark.  It doesn't really do anything useful except to demonstrate a few features of the Tk toolkit.}
set helpTopics(keys) "The following accelerator keys are defined for this application (in addition to those already available for the entry windows):\n\nCtrl+A:\t\tAdd\nCtrl+C:\t\tClear\nCtrl+D:\t\tDelete\nCtrl+F:\t\tEnter file name\nCtrl+Q:\t\tExit application (quit)\nCtrl+S:\t\tSearch (dummy operation)"
set helpTopics(version) {This is version 1.0.}

# Entries in "Help" menu

.menu.help.m add command -label "On Context..." -command {Help context}
.menu.help.m add command -label "On Help..." -command {Help help}
.menu.help.m add command -label "On Window..." -command {Help window}
.menu.help.m add command -label "On Keys..." -command {Help keys}
.menu.help.m add command -label "On Version..." -command {Help version} 
