
# This code is shared by all the ICEM CFD GUI's.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# 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) [lindex [$w.yes configure -background] 4]
    set query_offback($w) [lindex [$w configure -background] 4]
    set query_prev_focus($w) ""
    query_clear $w
}

proc query_message {w text} {
    foreach s [pack slaves $w] {
	pack unpack $s
    }
    $w.query delete 0 end    
    pack append $w $w.prompt {top frame nw} $w.query {top fill}
    $w.prompt configure -text $text
}

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 ""
    if {$query_prev_focus($w) != ""} {
	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
	pack $w.yes -side left -fill none -expand 0
    }
    if {$no != ""} {
	$w.no configure -text $no -state normal \
	    -background $query_onback($w) -relief raised
	pack $w.no -side left -fill none -expand 0
    }
    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
    
    if ![winfo exists $w.prompt] {
	if [lpopup $text 1] {
	    return "*okay*"
	} else {
	    return "*cancel*"
	}
    }
    
    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
    
    if ![winfo exists $w.prompt] {
	set s [question $text $default]
	if {$s == ""} {
	    return "*cancel*"
	} else {
	    return $s
	}
    }
    
    
    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
    
    if ![winfo exists $w.prompt] {
	set f [file_selection -message $text -default $default -new_ok 1]
	if {$f == ""} {
	    return *cancel*
	} else {
	    return $f
	}
    }
    
    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
    
    if ![winfo exists $w.prompt] {
	if [tk_dialog .query_choices Query $text "" -1 $choice1 $choice2] {
	    return $choice2
	} else {
	    return $choice1
	}
    }
    
    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
}
