
# 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 scontrol_setup {w height titles} {
    global scontrol_height scontrol_width scontrol_type scontrol_num
    global scontrol_rowheight scontrol_totalwidth scontrol_extras
    
    frame $w.titles
    canvas $w.c -yscrollcommand "$w.scroll set"
    scrollbar $w.scroll -command "$w.c yview" -relief sunken -borderwidth 2 \
	    -orient v
    frame $w.all
    frame $w.none
    pack $w.scroll -side right -fill both
    pack $w.titles -side top -fill both
    pack $w.none -side bottom -fill both
    pack $w.all -side bottom -fill both
    pack $w.c -side left -fill both -expand 1
    
    set i 0
    foreach t $titles {
	set type [lindex $t 0]
	set title [lindex $t 1]
	set width [lindex $t 2]
	
	label $w.titles.$i -text $title
	pack $w.titles.$i -side left -fill both
	
	if {$type == "checkbutton"} {
	    button $w.all.$i -text All -command "scontrol_all $w $i 1"
	    button $w.none.$i -text None -command "scontrol_all $w $i 0"
	    set extras ""
	} elseif {$type == "radiobutton"} {
	    label $w.all.$i -relief flat -borderwidth 2 -pady 0
	    button $w.none.$i -text None -command "scontrol_none $w $i"
	    set extras ""
	} elseif {$type == "label"} {
	    label $w.all.$i -relief flat -borderwidth 2 -pady 0
	    label $w.none.$i -relief flat -borderwidth 2 -pady 0
	    set extras "-anchor e"
	} else {
	    label $w.all.$i -relief flat -borderwidth 2 -pady 0
	    label $w.none.$i -relief flat -borderwidth 2 -pady 0
	    set extras ""
	}
	pack $w.all.$i -side left -fill both
	pack $w.none.$i -side left -fill both
	
	set scontrol_type($w,$i) $type
	set scontrol_extras($w,$i) $extras
	set scwidth($i) $width
	
	incr i
    }

    set scontrol_totalwidth($w) 0
    set i 0
    foreach t $titles {
	if {$scwidth($i) != ""} {
	    set width $scwidth($i)
	} else {
	    set width [expr {[string length [lindex $t 1]] + 1}]
	    if {$width < 5} {set width 5}
	}
	$w.titles.$i configure -width $width
	$w.all.$i configure -width $width
	$w.none.$i configure -width $width
	set scontrol_width($w,$i) [winfo reqwidth $w.titles.$i]
	incr scontrol_totalwidth($w) $scontrol_width($w,$i)
	incr i
    }
    set wheight [winfo reqheight $w.titles.0]
    set theight [expr {$height * $wheight}]

    frame $w.c.win
    $w.c create window 0 0 -window $w.c.win -anchor nw
    $w.c configure -height $theight \
	-scrollregion "0 0 $scontrol_totalwidth($w) $theight"

    set i 0
    set cumx 0
    foreach t $titles {
	set width $scontrol_width($w,$i)
	frame $w.c.win.$i -relief flat -borderwidth 0 -width $width
	#place $w.c.win.$i -x $cumx
	pack $w.c.win.$i -side left
	
	incr i
	incr cumx $width
    }
    
    set scontrol_rowheight($w) $wheight
    set scontrol_height($w) $height
    set scontrol_num($w) 0
    
    update idletasks
    set nwidth [winfo width $w.c]
    $w.c.win configure -width $nwidth
}

proc scontrol_add {w stuff {tag ""}} {
    global scontrol_height scontrol_width scontrol_type scontrol_num
    global scontrol_rowheight scontrol_width scontrol_totalwidth
    global scontrol_extras scontrol_tag
    
    set ypos [expr {$scontrol_rowheight($w) * $scontrol_num($w)}]
    set totalh [expr {$scontrol_rowheight($w) * ($scontrol_num($w) + 1)}]
    
    set i 0
    set n $scontrol_num($w)
    set created {}
    foreach s $stuff {
	set wid $w.c.win.$i.$n
	eval $scontrol_type($w,$i) $wid -relief raised -borderwidth 2 -pady 0 \
	    $scontrol_extras($w,$i) $s
	place $wid -x 0 -y $ypos -relwidth 1
	$w.c.win.$i configure -height $totalh
	incr i
	lappend created $wid
    }
    
    if {$tag != ""} { set scontrol_tag($w,$tag) $n }
    
    incr scontrol_num($w)
    
    $w.c configure -scrollregion "0 0 $scontrol_totalwidth($w) $totalh"
    $w.c configure -width $scontrol_totalwidth($w)
    
    return $created
}

proc scontrol_all {w col on} {
    global scontrol_type scontrol_num
    
    for {set i 0} {$i < $scontrol_num($w)} {incr i} {
	set wid $w.c.win.$col.$i
	set var [lindex [$wid configure -variable] 4]
	uplevel #0 set $var $on
	set cmd [lindex [$wid configure -command] 4]
	if {$cmd != ""} { uplevel #0 eval $cmd }
    }
}

proc scontrol_none {w col} {
    global scontrol_num
    if {$scontrol_num($w) > 0} {
	set var [lindex [$w.c.win.$col.0 configure -variable] 4]
	uplevel #0 set $var \{\}
	set cmd [lindex [$w.c.win.$col.0 configure -command] 4]
	if {$cmd != ""} { uplevel #0 eval $cmd }
    }
}

proc scontrol_get_widget {w tag j} {
    global scontrol_tag
    
    return $w.c.win.$scontrol_tag($w,$tag).$j
}
