
# 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 scroll_bbox {w name message options variable} {
    global $variable
    
    catch {top_destroy $w}
    
    set nopts [llength $options]
    if {!$nopts} {puts stderr "Empty options string!!"; return 0}
    
    set sb [expr {$nopts > 10}]
    
    top_window $w
    top_title $w $name
    message $w.mess -text $message -aspect 400 -relief raised -borderwidth 2
    canvas $w.c
    frame $w.buts -relief raised -borderwidth 2
    
    frame $w.c.win
    
    # Add all the checkbuttons.
    for {set i 0} {$i < $nopts} {incr i} {
	set opt [lindex $options $i]
	checkbutton $w.c.win.$i -text $opt -variable ${variable}($opt) -anchor nw
	pack append $w.c.win $w.c.win.$i {top fill expand}
    }

    top_withdraw $w
    update idletasks
    
    set width [winfo reqwidth $w.c.win]
    if $sb {
	set height [expr {[winfo reqheight $w.c.win.0] * 10}]
    } else {
	set height [winfo reqheight $w.c.win]
    }
    
    set fitem [$w.c create window 0 0 -window $w.c.win -anchor nw]
    $w.c configure -width $width -height $height \
	-scrollregion "0 0 [winfo reqwidth $w.c.win] [winfo reqheight $w.c.win]"
    
    set donevar ${variable}_scroll_bbox_done
    global $donevar
    
    # Add the buttons.
    button $w.buts.done -text Done -command "set $donevar 1"
    button $w.buts.cancel -text Cancel -command "set $donevar 0"
    button $w.buts.all -text All -command \
	"foreach j \{[array names $variable]\} \{set [format %s(\$j) $variable] 1\}"
    button $w.buts.none -text None -command \
	"foreach j \{[array names $variable]\} \{set [format %s(\$j) $variable] 0\}"
    pack append $w.buts \
	$w.buts.done {left fill expand} \
	$w.buts.cancel {left fill expand} \
	$w.buts.all {left fill expand} \
	$w.buts.none {left fill expand}
	
    if $sb {
	scrollbar $w.scroll -command "$w.c yview"  -relief raised -borderwidth 2 \
	    -orient v
	$w.c configure -yscrollcommand "$w.scroll set"
	pack append $w \
	    $w.mess {top fill} \
	    $w.buts {bottom fill} \
	    $w.c {left fill expand} \
	    $w.scroll {right fill}
    } else {
	pack append $w \
	    $w.mess {top fill} \
	    $w.buts {bottom fill} \
	    $w.c {left fill expand}
    }

    #putcenter $w
    update idletasks
    top_geometry $w +300+300
    update idletasks
    top_deiconify $w
    
    tkwait variable $donevar

    top_destroy $w
    
    return [set $donevar]
}

proc scroll_bbox_labels {w name message options labels extras colorcommands \
    variable centercommand} {
    global $variable
    
    catch {top_destroy $w}
    
    set nopts [llength $options]
    if {!$nopts} {puts stderr "Empty options string!!"; return 0}
    
    set sb [expr {$nopts > 12}]
    
    top_window $w 1 $name
    message $w.mess -text $message -aspect 400 -relief raised -borderwidth 2
    canvas $w.c
    frame $w.buts -relief raised -borderwidth 2
    
    frame $w.c.win
    frame $w.c.win.ch
    pack append $w.c.win $w.c.win.ch {left fill}
    
    # Add all the checkbuttons.
    for {set i 0} {$i < $nopts} {incr i} {
	set opt [lindex $options $i]
	set lab [lindex $labels $i]
	set ext [lindex $extras $i]
	eval checkbutton $w.c.win.ch.$i -text \"$lab\" \
	    -variable \"${variable}($opt)\" -anchor nw $ext
	
	pack append $w.c.win.ch $w.c.win.ch.$i {top fill expand}
    }
    
    # Add all the color commands.
    if {$colorcommands != ""} {
	frame $w.c.win.co
	pack append $w.c.win $w.c.win.co {left fill}
	for {set i 0} {$i < $nopts} {incr i} {
	    set com [lindex $colorcommands $i]
	    eval button $w.c.win.co.$i $com
	    
	    pack append $w.c.win.co $w.c.win.co.$i {top fill expand}
	}    
    }
    
    top_withdraw $w
    update idletasks
    
    set width [winfo reqwidth $w.c.win]
    if $sb {
	set height [expr {[winfo reqheight $w.c.win.ch.0] * 10}]
    } else {
	set height [winfo reqheight $w.c.win]
    }
    
    set fitem [$w.c create window 0 0 -window $w.c.win -anchor nw]
    $w.c configure -width $width -height $height \
	-scrollregion "0 0 [winfo reqwidth $w.c.win] [winfo reqheight $w.c.win]"
    
    set donevar ${variable}_scroll_bbox_done
    global $donevar
    
    # Add the buttons.
    button $w.buts.done -text Done -command "set $donevar 1"
    button $w.buts.cancel -text Cancel -command "set $donevar 0"
    button $w.buts.all -text All -command \
	"foreach j \{[array names $variable]\} \{set [format %s(\$j) $variable] 1\}"
    button $w.buts.none -text None -command \
	"foreach j \{[array names $variable]\} \{set [format %s(\$j) $variable] 0\}"
    pack append $w.buts \
	$w.buts.done {left fill expand} \
	$w.buts.cancel {left fill expand} \
	$w.buts.all {left fill expand} \
	$w.buts.none {left fill expand}
	
    if $sb {
	scrollbar $w.scroll -command "$w.c yview"  -relief raised -borderwidth 2 \
		-orient v
	$w.c configure -yscrollcommand "$w.scroll set"
	pack append $w \
	    $w.mess {top fill} \
	    $w.buts {bottom fill} \
	    $w.c {left fill expand} \
	    $w.scroll {right fill}
    } else {
	pack append $w \
	    $w.mess {top fill} \
	    $w.buts {bottom fill} \
	    $w.c {left fill expand}
    }

    eval $centercommand
    update idletasks
    top_deiconify $w
    
    tkwait variable $donevar

    top_destroy $w
    
    return [set $donevar]
}

proc scroll_radiobuttons {w name message options labels extras colorcommands \
    variable centercommand} {
    global $variable
    
    catch {top_destroy $w}
    
    set nopts [llength $options]
    if {!$nopts} {puts stderr "Empty options string!!"; return 0}
    
    set sb [expr {$nopts > 12}]
    
    top_window $w 1 $name
    message $w.mess -text $message -aspect 400 -relief raised -borderwidth 2
    canvas $w.c
    frame $w.buts -relief raised -borderwidth 2
    
    frame $w.c.win
    frame $w.c.win.ch
    pack append $w.c.win $w.c.win.ch {left fill}
    
    # Add all the checkbuttons.
    for {set i 0} {$i < $nopts} {incr i} {
	set opt [lindex $options $i]
	set lab [lindex $labels $i]
	set ext [lindex $extras $i]
	eval radiobutton $w.c.win.ch.$i -text \"$lab\" \
	    -variable $variable -value $opt -anchor nw $ext
	
	pack append $w.c.win.ch $w.c.win.ch.$i {top fill expand}
    }
    
    # Add all the color commands.
    if {$colorcommands != ""} {
	frame $w.c.win.co
	pack append $w.c.win $w.c.win.co {left fill}
	for {set i 0} {$i < $nopts} {incr i} {
	    set com [lindex $colorcommands $i]
	    if {$com != ""} {
		eval button $w.c.win.co.$i $com
	    }
	    
	    pack append $w.c.win.co $w.c.win.co.$i {top fill expand}
	}    
    }
    
    top_withdraw $w
    update idletasks
    
    set width [winfo reqwidth $w.c.win]
    if $sb {
	set height [expr {[winfo reqheight $w.c.win.ch.0] * 10}]
    } else {
	set height [winfo reqheight $w.c.win]
    }
    
    set fitem [$w.c create window 0 0 -window $w.c.win -anchor nw]
    $w.c configure -width $width -height $height \
	-scrollregion "0 0 [winfo reqwidth $w.c.win] [winfo reqheight $w.c.win]" 
    
    set donevar ${variable}_scroll_bbox_done
    global $donevar
    
    # Add the buttons.
    button $w.buts.done -text Done -command "set $donevar 1"
    button $w.buts.cancel -text Cancel -command "set $donevar 0"
    pack append $w.buts \
	$w.buts.done {left fill expand} \
	$w.buts.cancel {left fill expand}
	
    if $sb {
	scrollbar $w.scroll -command "$w.c yview"  -relief raised -borderwidth 2 \
	    -orient v
	$w.c configure -yscrollcommand "$w.scroll set"
	pack append $w \
	    $w.mess {top fill} \
	    $w.buts {bottom fill} \
	    $w.c {left fill expand} \
	    $w.scroll {right fill}
    } else {
	pack append $w \
	    $w.mess {top fill} \
	    $w.buts {bottom fill} \
	    $w.c {left fill expand}
    }

    eval $centercommand
    update idletasks
    top_deiconify $w
    
    tkwait variable $donevar

    top_destroy $w
    
    if [set $donevar] {
	return [set $variable]
    } else {
	return ""
    }
}
