#! /usr/local/tk3.2/bin/wish -f
# Given a window name w, and variabls names, values, and actual var names,
# get the new values from the user.
# Input Parameters:
#   w  - window name
#   title - window title
#   names - names 
#   values - current values
#   vars   - names of variables
#
# Output effect:
#   vars are changed as user requests
# This produces a multi-element dialog
#
# A <return> ends the dialog and returns
#
# Example
# dialog( . "Option" "Value of foo" 0 "foo" )
# asks the user to change the value of foo
# dialog( . "Options" { "Value of foo" "Value of bar" } { 0 1 } { foo bar } )
# asks the user to change the values of foo and/or bar
proc dialog { w title names values vars } {
    global $vars
    toplevel $w
    bind $w <Return> "dialogset $w {$vars}"
    wm title $w "$title"
    set cnt 0
    foreach name $vars {
	frame $w.$cnt
	pack append $w $w.$cnt { top pady 4 frame e }
	label $w.$cnt.label -text [lindex $names $cnt] -anchor e
	entry $w.$cnt.entry -width 30 -relief sunken
	$w.$cnt.entry insert end [lindex $values $cnt]
	bind $w.$cnt.entry <Return> "dialogset $w {$vars}"
	bind $w.$cnt.label <Return> "dialogset $w {$vars}"
	pack append $w.$cnt $w.$cnt.entry right $w.$cnt.label right
	set cnt [ expr $cnt+1 ]
	}
    button $w.ok -text OK -command "dialogset $w {$vars}"
    button $w.cancel -text Cancel -command "destroy $w"
    pack append $w $w.ok { bottom left fillx expand } \
	           $w.cancel { bottom right fillx expand }
}

#
# dialogset is used by dialog to set the values of the variables
proc dialogset {w names} {
set cnt 0
foreach i $names {
    global $i
    set $i [$w.$cnt.entry get]
    set cnt [ expr $cnt+1 ]
    }
destroy   $w
}

