# $Header: /home/cvsroot/tcldb/wisql/Tlib/setOrder,v 1.3 1997/08/28 05:24:21 de Exp $
# tcl procs saved on Tue Sep 05 09:33:12 PDT 1995

proc setOrder {w col flagsN orderN countN colsN scolsN} {
	

#	flags is either prints or sorts
#	order is either porder or sorder
#	count is either numprt or numsrt

	upvar #0 $colsN cols
	upvar #0 $flagsN flags
	upvar #0 $orderN order
	upvar #0 $countN count


		if {$flagsN == "EZRprints"} {
			set type "PRT"
			set frame "winL"
			lappend buts "pb"
			set eb "po"
		} else {
			set type "SRT"
			set frame "winR"
			lappend buts "a" "d"
			set eb "so"
		}
		

		set ct $count

#	why bother to check print/sort order when there are no columns ?
	if {$ct == 0} {
		return
	}


#	why bother to check print order for nonprinting columns?
	set isit $flags($col)
	if {$isit == 0} {
		return
	}


	set newpos [$w.data.pic.win.$frame.$col.$eb get]

#	Pathological or malicious input?

	if {![ctype digit $newpos]} {
#		Nuke it -- the user is fooling with us
		deleteElem $col $orderN $count
		set flags($col) 0
		incr count -1
		$w.data.pic.win.$frame.$col.$eb delete 0 end
		foreach b $buts {
		$w.data.pic.win.$frame.$col.$b deselect
		$w.data.pic.win.$frame.$col.$b configure -relief flat
		}
		return
	}

	if {$newpos >= $ct} {
		set newpos [expr {$ct - 1}]
		$w.data.pic.win.$frame.$col.$eb delete 0 end
		$w.data.pic.win.$frame.$col.$eb insert 0 $newpos
	}

	set oldpos X
	set rs [array size order]
	foreach p [array names order] {
		set val $order($p)
#		puts stdout "$val " nonewline
		if {$val == $col} {set oldpos $p}
	}
#	puts stdout "\n" nonewline
	if {$oldpos == "X"} {
		exit 1
	}

	if {$oldpos == $newpos} {
		return
	}

#	puts stderr "Call changeElem oldpos $oldpos newpos $newpos arr $orderN count $ct"
	changeElem $oldpos $newpos $orderN $ct

#	set rs [array size order]
	
# This is pretty simple really.  You have this list porder consisting
# of indices into the cols/prints/where matrix.  So you check on
# the contents of porder and refresh the text from that.

	loop p 0 $ct {
		set c $order($p)
		$w.data.pic.win.$frame.$c.$eb delete 0 end
		$w.data.pic.win.$frame.$c.$eb insert 0 $p
	}
}

