
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# This is a query box that allows one to answer questions.

# Query stuff.

proc query {w} {
    global query_prev_focus query_onback query_offback
    
    label $w.prompt -relief flat -anchor nw
    entry $w.query -relief flat -borderwidth 1
    button $w.yes -command "set query_done 1"
    button $w.no -command "set query_done 0"
    pack append $w \
	$w.prompt {top expand fill} \
	$w.query {left expand fill} \
	$w.yes {left fill} \
	$w.no {left fill}
    bind $w y "set query_done 1"
    bind $w n "set query_done 0"
    bind $w <Return> "set query_done 1"
    bind $w.query <Return> "set query_done 1"
    
    set query_onback($w) #aaffaa
    set query_offback($w) [lindex [$w configure -background] 4]
    set query_prev_focus($w) [focus]
    query_clear $w
}

proc query_message {w text} {
    set stuff [pack info $w]
    for {set i 0} {$i < [llength $stuff]} {incr i 2} {
	pack unpack [lindex $stuff $i]
    }
    $w.query delete 0 end    
    pack append $w $w.prompt {top frame nw} $w.query {top fill}
    $w.prompt configure -text $text -background "#aaffaa"
}

proc query_clear {w} {
    global query_prev_focus query_offback active
    
    $w.prompt configure -text "" -background $query_offback($w) 
    $w.query configure -width 0 -relief flat -background $query_offback($w) 
    $w.query delete 0 end
    $w.yes configure -state disabled -background $query_offback($w) -relief flat -text ""
    $w.no configure -state disabled -background $query_offback($w) -relief flat -text ""
    focus $query_prev_focus($w)
    set active 1
}

proc query_enable {w text usedefault default yes no} {
    global query_prev_focus query_onback query_waitvalue active
    
    catch {unset query_waitvalue}
    set query_prev_focus($w) [focus]
    $w.prompt configure -text $text
    $w.query insert 0 $default
    if {$yes != ""} {
	$w.yes configure -text $yes -state normal \
	    -background $query_onback($w) -relief raised
    }
    if {$no != ""} {
	$w.no configure -text $no -state normal \
	    -background $query_onback($w) -relief raised
    }
    if $usedefault {
	$w.query configure -relief sunken -background white -width 40
	pack after $w.prompt $w.query {left expand fill}
	focus $w.query
    } else {
	focus $w
	pack after $w.prompt $w.query {left}
    }
    set active 0
}

proc query_question {w text} {
    global query_done
    query_enable $w $text 0 "" Yes No
    tkwait variable query_done
    query_clear $w
    if $query_done {return *okay*} else {return *cancel*}
}

proc query_click {w text} {
    global query_done
    query_enable $w $text 0 "" Done ""
    tkwait variable query_done
    query_clear $w
}

proc query_enter {w text default} {
    global query_done
    query_enable $w $text 1 $default Okay Cancel
    tkwait variable query_done
    set val [$w.query get]
    query_clear $w
    if $query_done {return $val} else {return *cancel*}
}

proc query_path {w text default} {
    global query_done
    query_enable $w $text 1 $default Okay Cancel
    pathentry on $w.query
    tkwait variable query_done
    set val [$w.query get]
    pathentry off $w.query
    query_clear $w
    if $query_done {return $val} else {return *cancel*}
}

proc query_path_lc {w text default} {
    set res [query_path $w $text $default]
    if {[regexp \[A-Z\] $res] == 1} {
	infomsg "DDN path names must not contain capital letters!  Converting to lower case."
	set res [string tolower $res]
    }
    return $res
}

proc query_two_choices {w text choice1 choice2} {
    global query_done
    query_enable $w $text 0 "" $choice1 $choice2
    tkwait variable query_done
    query_clear $w
    if $query_done {return $choice1} else {return $choice2}
}

proc query_waitvar {w text} {
    global query_done query_waitvalue
    query_enable $w $text 0 "" "" Cancel
    tkwait variable query_done
    query_clear $w
    if [info exists query_waitvalue] {return $query_waitvalue} else {return *cancel*}
}

proc query_enter_or_waitvar {w text default} {
    global query_done query_waitvalue
    query_enable $w $text 1 $default Okay Cancel
    tkwait variable query_done
    set val [$w.query get]
    query_clear $w
    if [info exists query_waitvalue] {return $query_waitvalue}
    if $query_done {return $val} else {return *cancel*}
}

proc query_waitvar_done {w value} {
    global query_done query_waitvalue
    set query_waitvalue $value
    set query_done 1
}
