
# 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

proc question {question {default ""}} {
    return [question_box .question_box Query $question $default]
}

proc question_box {w title question {default ""} {geo +400+400}} {
    global question_box_value
    global batch_mode toplevel
    
    if {[info exists batch_mode] && $batch_mode} {
	error "can't ask questions in batch mode"
    }
    
    catch {top_destroy $w}
    
    set question_box_value($w) $default
    top_window $w 1 $title
    label $w.l -text $question
    entry $w.e -borderwidth 2 -relief sunken -background white -width 20 \
	    -textvar question_box_value($w)
    button $w.done -text Done -width 5 -command "top_destroy $w"
    button $w.cancel -text Cancel -width 5 -command \
	    "set question_box_value($w) {}; top_destroy $w"
    
    pack $w.l $w.e -side top -fill both
    pack $w.done $w.cancel -side left -fill both -expand 1 -padx 10 -pady 10
    
    bind $w.e <Return> "top_destroy $w; break"
    
    $w.e selection from 0
    $w.e selection to end
    
    if {![info exists toplevel(single)] || !$toplevel(single)} {
	::tk::SetFocusGrab $w $w.e
    } else {
	form_auto_raise $w
	focus $w.e
    }
    
    top_geometry $w $geo
    
    set bb [catch {busy reset} ow]
    tkwait window $w
    if !$bb { if $ow { busy $ow } }
    
    return $question_box_value($w)
}

# Args are lists of {label default}.
# Return value is a list of the return values.

proc question_box_mult {w title question entrylist {but_labels {Done Cancel}} {rtype 0}} {
    global question_box_value
    global batch_mode
    
    if {[info exists batch_mode] && $batch_mode} {
	error "can't ask questions in batch mode"
    }
    
    catch {top_destroy $w}
    
    set nargs [llength $entrylist]
    top_window $w 1 $title
    if {$question != ""} {
	label $w.l -text $question -justify l
	blt_table $w $w.l 0,0 -columnspan 2
    }
    set c 1
    set nargs [llength $entrylist]
    for {set i 0} {$i < $nargs} {incr i} {
	set arg [lindex $entrylist $i]
	
	label $w.l$i -text [lindex $arg 0] -justify r
	if {[lindex $arg 3] == "text"} {
	    set tw $w.e$i
	    text $tw -borderwidth 2 -relief sunken -background white \
		-width 40 -height 10
	    $tw insert end [lindex $arg 1]
	    
	    blt_table $w $w.l$i $c,0 -columnspan 2 -fill x
	    incr c
	    blt_table $w $tw $c,0 -columnspan 2 -fill x
	    
	} else {
	    set question_box_value($w,$i) [lindex $arg 1]
	    entry $w.e$i -borderwidth 2 -relief sunken -background white \
		-width 20 -textvar question_box_value($w,$i)
	    bind $w.e$i <Return> "top_destroy $w; break"
	    
	    blt_table $w $w.l$i $c,0 -fill x
	    blt_table $w $w.e$i $c,1 -fill x
	}
	set but [lindex $arg 2]
	if {$but != ""} {
	    button $w.b$i -text Select -command \
		"set question_box_value($w,$i) \[$but\]"
	    blt_table $w $w.e$i $c,2 -fill x
	}
	incr c
    }
    
    frame $w.f
    blt_table $w $w.f $c,0 -columnspan 2
    
  # button $w.f.done -text [lindex $but_labels 0] \
  #     -command "set question_box_value($w,return) 1"
  # button $w.f.cancel -text [lindex $but_labels 1] \
  #     -command "set question_box_value($w,return) 0"
    
  # pack $w.f.done $w.f.cancel -side left -fill both -expand 1 \
  #     -padx 10 -pady 10
    
    set n [llength $but_labels]
    set question_box_value($w,return) $n
    set buts ""
    for {set i 0} {$i < $n} {incr i} {
       set j [expr $n - ($i + 1)]
       button $w.f.b$i -text [lindex $but_labels $i] \
            -command "set question_box_value($w,return) $j"
       lappend buts $w.f.b$i
    }
    eval pack $buts -side left -fill both -expand 1 -padx 10 -pady 10

    form_auto_raise $w
    
    putcenter $w
    #top_geometry $w +300+300
    
    if $nargs { focus $w.e1 }
    
    set bb [catch {busy reset} ow]
    tkwait variable question_box_value($w,return)
    if !$bb { if $ow { busy $ow } }
    
    set ret ""
    if $question_box_value($w,return) {
	for {set i 0} {$i < $nargs} {incr i} {
	    if {[lindex [lindex $entrylist $i] 3] == "text"} {
		lappend ret [$w.e$i get 1.0 end]
	    } else {
		lappend ret $question_box_value($w,$i)
	    }
	}
        if $rtype {lappend ret $question_box_value($w,return)}
    }
    top_destroy $w
    return $ret
}

proc lpopup {mess {quest 0} {title Message}} {
    global batch_mode
    
    if $batch_mode {
	puts $mess
	return 0
    }
    if $quest {
	return [expr ![tk_dialog .lpopup_message $title $mess "" -1 Yes No]]
    } else {
	tk_dialog .lpopup_message $title $mess "" -1 Ok
    }
}

proc question_large {question {default ""}} {
    global question_box_value
    global batch_mode
    
    set w .question_box
    set geo +400+400
    set title Question
    
    if {[info exists batch_mode] && $batch_mode} {
	error "can't ask questions in batch mode"
    }
    
    catch {top_destroy $w}
    
    set question_box_value($w) ""
    top_window $w 1 $title
    label $w.l -text $question
    text $w.e -borderwidth 2 -relief sunken -background white -width 40 \
	-height 16
    $w.e insert end $default
    button $w.done -text Done -width 5 -command \
	"set question_box_value($w) \[$w.e get 1.0 end\]; top_destroy $w"
    button $w.cancel -text Cancel -width 5 -command \
	"set question_box_value($w) {}; top_destroy $w"
    
    pack $w.l $w.e -side top -fill both
    pack $w.done $w.cancel -side left -fill both -expand 1 -padx 10 -pady 10
    
    catch {form_auto_raise $w}
    
    top_geometry $w $geo
    focus $w.e
    set bb [catch {busy reset} ow]
    tkwait window $w
    if !$bb { if $ow { busy $ow } }
    return $question_box_value($w)
}
