# $Header: /home/cvsroot/tcldb/wisql/Tlib/makeEZedit,v 1.7 1998/03/22 21:26:02 de Exp $
# tcl procs saved on Tue Sep 05 09:33:04 PDT 1995

proc makeEZedit {} {

global dbpipe1 sybmsg server uname base table 
global bitmapdir DB
  global MsgW

upvar #0 EZEsort sort
upvar #0 EZEsqlcmd sqlcmd
upvar #0 EZEcolatts colatts
upvar #0 EZEcols cols
upvar #0 EZEshows shows
upvar #0 EZEkeys keys
upvar #0 EZEcurx curx
upvar #0 EZEcury cury
upvar #0 EZEcurl curl
upvar #0 EZEnrec nrec
upvar #0 EZEcci cci
upvar #0 EZEforf forf
upvar #0 EZEvform vf
upvar #0 EZEhform hf
upvar #0 EZEselsql selsql
upvar #0 EZEprivs privs

#	NUKE all memory of previous invocations
	clearEZedit

	set sybmsg(nullvalue) "NULL"


#       Don't even paint the window until you know you have a database and
#       table selected.

	set cci 0

        if {($base == "") || ($table == "")} {
                .m.mb.ez flash
                catch {setMsg $MsgW "EZedit cannot be used until you select a database and table"}
                return
        }


	set sel [${DB}chkPriv $table sel]
	if {[lindex $sel 0] == "ERROR"} {
		catch {.m.mb.mkRpt flash}
                catch {setMsg $MsgW "Serious problem: $sel"}
		return
	}
	if {$sel == "NO"} {
		catch {.m.mb.mkRpt flash}
		catch {setMsg $MsgW "EZrpt cannot be run on table $table in database $base because you ($uname) do not have privilege to read that table"}
		return
	}

	if {[${DB}chkPriv $table upd] != "NO"} {append privs "U"}
	if {[${DB}chkPriv $table ins] != "NO"} {append privs "I"}
	if {[${DB}chkPriv $table del] != "NO"} {append privs "D"}

	set hf .ezehoriz
	set vf .ezeverti

	set w $hf
	catch {destroy $w}
        toplevel $w -class Dialog
        wm title $w "EZedit"
        wm iconname $w "EZedit!"
  	wm iconbitmap $w @$bitmapdir/ezedit.xbm

        set width 850
        set height 500
        wm geometry $w ${width}x${height}
	wm minsize $w ${width} ${height}

#	This sets EZEcols, types, nulls, sizes, and keys
	${DB}getEZcols E

	set temp [split $table .]
	set usr [lindex $temp 0]
	set tbl [lindex $temp 1]

#	set a global var with forbidden fields if any
	set forf [${DB}forBidden $tbl]
	if {[lindex $forf 0] == "Error:"} {
		echo "Can't read forbidden field table:\n$forf"
		set forf ""
	}

        set TableName "$table"

#	the data frame will hold the canvas with all its little boxes,
#	the scroll bar, and the message widget
#	 -- all the "horizontal" stuff is in these 2 boxes.
	frame $w.data -relief groove -border 1  -background antiquewhite

#	There isn't a v scroll bar any more.
#		-yscroll "$w.data.sbv set" 
#	We make a canvas so that we can scroll a whole area
	canvas $w.data.pic  -relief raised -border 0  -xscrollcommand "$w.data.sbh set"  -width [expr {$width - 105}] -height [expr {$height - 50}] -background antiquewhite

#	And we don't set our height explicitly here any more.
#		-height [expr {$height -10}] 

#	This is the frame that will hold all the subframes of data
#	inside the canvas
	frame $w.data.pic.win -relief groove -border 2 -background antiquewhite

#	and this will be the message area
	set font [getFont $w messagehel]
  	message $w.data.msg -text "" -justify center -aspect 3000  -relief sunken  -font $font -background bisque
	bind $w.data.msg <Control-1> "ezHelp Messages; break"

	$w.data.msg configure -text "Data Editor"

#	stuck over at the left side of the toplevel are the control buttons
	frame $w.ctrl -width 100 -relief groove -border 1 -background bisque
	
#	Try sticking them on each entry box (sigh)
#	set font [getFont $w smallhel]
#	label $w.ctrl.vmatch -text "match values:"  -relief flat -border 0 -font $font 
#	label $w.ctrl.vnew -text "new values:" -relief flat  -border 0 -font $font

	set font [getFont $w mediumhel]
	label $w.ctrl.table -text "$table" -relief sunken -border 2  -font $font -background antiquewhite
        label $w.ctrl.base -text "$base"  -relief sunken -border 2  -font $font -background antiquewhite

	bind $w.ctrl.table <Control-1> "ezHelp Wtable; break"
	bind $w.ctrl.base <Control-1> "ezHelp Wdatabase; break"

	button $w.ctrl.find -text   "FIND" -relief  raised -border 1  -command "findData {}" -padx 0 -pady 0 -background lemonchiffon -activeforeground magenta2 -activebackground lemonchiffon
	button $w.ctrl.del -text    "DELETE" -relief  raised -border 1  -command "changeData del $w {}" -padx 0 -pady 0 -background lemonchiffon -activeforeground magenta2 -activebackground lemonchiffon
	button $w.ctrl.verti -text  "V-FORM" -relief  raised -border 1  -command "verticalForm" -padx 0 -pady 0 -background lavender -activebackground lavender -activeforeground magenta2 -activebackground lemonchiffon
	button $w.ctrl.change -text "CHANGE" -relief  raised -border 1  -command "changeData upd $w {}" -padx 0 -pady 0 -background lemonchiffon -activeforeground magenta2 -activebackground lemonchiffon
	button $w.ctrl.chall -text "CH.ALL" -relief  raised -border 1  -command "changeData upd $w all" -padx 0 -pady 0 -background lemonchiffon -activeforeground magenta2 -activebackground lemonchiffon
	button $w.ctrl.ins -text    "INSERT" -relief  raised -border 1  -command "changeData ins $w {}" -padx 0 -pady 0 -background lemonchiffon -activeforeground magenta2 -activebackground lemonchiffon
#
	menubutton $w.ctrl.help -text "Help..." -menu $w.ctrl.help.m  -relief raised -border 1 -padx 0 -pady 0 -background lavender -activebackground lavender -activeforeground magenta2
	menu $w.ctrl.help.m -background lavender
	$w.ctrl.help.m add command -label "General" -command "ezHelp EZEgen" -background lavender -activebackground lavender -activeforeground magenta2
	$w.ctrl.help.m add command -label "FastKeys" -command "ezHelp EZEkeys" -background lavender -activebackground lavender -activeforeground magenta2
	$w.ctrl.help.m add command -label "HowTo" -command "ezHelp EZEhow" -background lavender -activebackground lavender -activeforeground magenta2
#
	button $w.ctrl.done -text "QUIT" -relief raised -border 1  -command "ezEkill" -padx 0 -pady 0 -background white -foreground red -activebackground black -activeforeground yellow

	if {[string first U $privs] < 0} {
	$w.ctrl.change configure -state disabled
	$w.ctrl.chall configure -state disabled
	}
	if {[string first I $privs] < 0} {
	$w.ctrl.ins configure -state disabled
	}
	if {[string first D $privs] < 0} {
	$w.ctrl.del configure -state disabled
	}

	set gu_prompt "Please enter a SQL record selection expression for this table, starting with 'where'"
	bind $w.ctrl.find <Shift-Control-3> "getUserInput EZE_SQLtext EZEselsql \"$gu_prompt\" where"

	bind $w.ctrl.find <Control-3> "findData show"
	bind $w.ctrl.del <Control-3> "changeData del $w show"
	bind $w.ctrl.change <Control-3> "changeData upd $w show"
	bind $w.ctrl.chall <Control-3> "changeData upd $w show_all"
	bind $w.ctrl.ins <Control-3> "changeData ins $w show"

	bind $w.ctrl.find <Control-1> "ezHelp EZEfind; break"
	bind $w.ctrl.del <Control-1> "ezHelp EZEdel; break"
	bind $w.ctrl.change <Control-1> "ezHelp EZEchg; break"
	bind $w.ctrl.chall <Control-1> "ezHelp EZEchgall; break"
	bind $w.ctrl.ins <Control-1> "ezHelp EZEins; break"
	bind $w.ctrl.verti <Control-1> "ezHelp EZEvform; break"
	bind $w.ctrl.help <Control-1> "ezHelp ToolHlp; break"
	bind $w.ctrl.done <Control-1> "ezHelp Quit; break"

#	Pull these out to make more room
#		$w.ctrl.tableL {top pady 10}  
#		$w.ctrl.baseL {top pady 5}  


	pack append $w.ctrl $w.ctrl.base {top pady 20}  $w.ctrl.table {top pady 20}  $w.ctrl.help {top pady 13}  $w.ctrl.find {top pady 13}  $w.ctrl.change {top pady 13}  $w.ctrl.chall {top pady 13} $w.ctrl.del {top pady 13}  $w.ctrl.ins {top pady 13}  $w.ctrl.verti {top pady 13} $w.ctrl.done {bottom}

	set noinsert 0
	set i 0
	set showem {}
	set showcol 0

	set smfont [getFont $w smallhel]

	foreach c $cols {

	set cn [keylget colatts($c) name]

	if {$shows($c)} {

	set trunc 0

#       Hack the type back from generic char(12) format to char
#       by splitting it at the paren and taking the first chunk

	set t [keylget colatts($c) type]
        set t [lindex [split $t (] 0]
	
	set s [keylget colatts($c) size]

	case $t in {
	{char varchar} {
		set ts [format "%s(%s)" $t $s]
	} {default} {
		set ts $t
	}
	}
	case $t in {
	{int} {
		set fw 11
	}
	{float real money} {
		set fw 15
	}
	{smallint} {
		set fw 6
	}
	{smallmoney} {
		set fw 10
	}
	{datetime} {
		set fw 20
	}
	{smalldatetime} {
		set fw 12
	}
	{char varchar} {
		set fw [expr {$s + 1}]
		if {$fw > 16} {
			set trunc 1
			set fw 16
		}
	}
	{default} {
		set fw 15
	}
	}

#	echo "WINFO CHILDREN:"
#	winfo children $w.data.pic.win
	frame $w.data.pic.win.$c -relief groove -border 2 -background antiquewhite
	label $w.data.pic.win.$c.n -text "$c" -background bisque
	label $w.data.pic.win.$c.t -text "$ts" -background antiquewhite
	label $w.data.pic.win.$c.mv -text "match val:"  -relief flat -border 0 -font $smfont -anchor w -background antiquewhite
	label $w.data.pic.win.$c.nv -text "new val:"  -relief flat -border 0 -font $smfont -anchor w -background antiquewhite

	bind $w.data.pic.win.$c.n <Control-1> "ezHelp Wfieldname; break"
	bind $w.data.pic.win.$c.n <Control-2> "dataHelp $table $c; break"
	bind $w.data.pic.win.$c.t <Control-1> "ezHelp Wdatatype; break"

	set font [getFont $w mediumcou]
	entry $w.data.pic.win.$c.f -relief sunken -border 2  -width $fw -font $font -background ivory

	bind $w.data.pic.win.$c.f <Control-1> "ezHelp Wcomparison; break"
	bind $w.data.pic.win.$c.f <Control-Return> "$w.ctrl.find invoke"

	frame $w.data.pic.win.$c.s

	set bfont [getFont $w helvetica]
	radiobutton $w.data.pic.win.$c.s.s -relief flat -border 0  -font $bfont -text "SORT" -variable EZEsort  -value $cn -padx 0 -pady 0 -background bisque -selectcolor gray75 -activebackground bisque -activeforeground magenta2

	bind $w.data.pic.win.$c.s.s <Control-1> "ezHelp EZEsort; break"

	pack append $w.data.pic.win.$c.s $w.data.pic.win.$c.s.s {frame center}

	listbox $w.data.pic.win.$c.d -relief sunken -border 2  -font $font -width ${fw} -height 15 -background antiquewhite
	bind $w.data.pic.win.$c.d <Control-1> "ezHelp EZElistbox; break"

	entry $w.data.pic.win.$c.e -relief sunken -border 2 -width $fw  -font $font -background ivory
#	This was too dangerous.
#	bind $w.data.pic.win.$c.e <Return> "$w.ctrl.change invoke"
	bind $w.data.pic.win.$c.e <Control-Return> "$w.ctrl.change invoke"
	bind $w.data.pic.win.$c.e <Control-1> "ezHelp Wnew; break"

	pack append $w.data.pic.win.$c  $w.data.pic.win.$c.n {top pady 5}   $w.data.pic.win.$c.t {top pady 2}  $w.data.pic.win.$c.mv {top pady 5 expand fillx}  $w.data.pic.win.$c.f {top}   $w.data.pic.win.$c.s {top pady 2}   $w.data.pic.win.$c.d {top pady 5}   $w.data.pic.win.$c.nv {top pady 5 expand fillx} $w.data.pic.win.$c.e {top} 

	pack append $w.data.pic.win $w.data.pic.win.$c {left}

	loop j 1 11 {
		$w.data.pic.win.$c.d insert end "--"
	}
#	there are not any real records yet so set count 0
	set nrec 0

	set curx($c) 0
	set curl 0
	set cury 0

	bind $w.data.pic.win.$c.d <Enter> "focus $w.data.pic.win.$c.d"

#	eval bind $w.data.pic.win.$c.d <2> \{ezEscroll $c mark %x %y \}
#	eval bind $w.data.pic.win.$c.d <B2-Motion> \{ezEscroll $c dragto %x %y\}
#
	eval bind $w.data.pic.win.$c.d <u> \{ezEscroll $c up %x %y\}
	eval bind $w.data.pic.win.$c.d <Up> \{ezEscroll $c up %x %y\}
	eval bind $w.data.pic.win.$c.d <Shift-Return> \{ezEscroll $c up %x %y\}
	eval bind $w.data.pic.win.$c.d <d>  \{ezEscroll $c down %x %y\}
	eval bind $w.data.pic.win.$c.d <Down>  \{ezEscroll $c down %x %y\}
	eval bind $w.data.pic.win.$c.d <Return>  \{ezEscroll $c down %x %y\}
	eval bind $w.data.pic.win.$c.d <U> \{ezEscroll $c Up %x %y\}
	eval bind $w.data.pic.win.$c.d <Shift-Up> \{ezEscroll $c Up %x %y\}
	eval bind $w.data.pic.win.$c.d <Shift-Tab> \{ezEscroll $c Up %x %y\}
	eval bind $w.data.pic.win.$c.d <D> \{ezEscroll $c Down %x %y\}
	eval bind $w.data.pic.win.$c.d <Shift-Down> \{ezEscroll $c Down %x %y\}
	eval bind $w.data.pic.win.$c.d <Tab> \{ezEscroll $c Down %x %y\}
#
set oldbut1 [bind Listbox <1>]
	     bind $w.data.pic.win.$c.d <1> "$oldbut1; ezEselect $c mouse %y"
#	eval bind $w.data.pic.win.$c.d <B1-Motion> \{ezEselect $c mouse %y \}
#	eval bind $w.data.pic.win.$c.d <Shift-1> \{ezEselect $c mouse %y \}
#	eval bind $w.data.pic.win.$c.d <Shift-B1-Motion> \{ezEselect $c mouse %y \}
#
	set cx($c) 0

	lappend showem $c

	} else {
		set noinsert 1
	}

	incr i
	}

#	set the default sort order
	set sort [keylget colatts([lindex $cols 0]) name]

#	You can't insert if you are hiding columns.  Not permitted.

	if {$noinsert} {
		$w.ctrl.ins configure -state disabled
	}

	set numcol $i

	set showcol [llength $showem]
	foreach c $showem {

		set this $c
		set j [lsearch $showem $c]

		if {$j < [expr {$showcol -1}]} {
		set next [lindex $showem [expr {$j + 1}]]
		} else {
		set next [lindex $showem 0]
		}

		if {$j > 0} {
		set prev [lindex $showem [expr {$j - 1}]]
		} else {
		set prev [lindex $showem [expr {$showcol - 1}]]
		}

		bind $w.data.pic.win.$this.e <Return> "focus $w.data.pic.win.$next.e; break"
		bind $w.data.pic.win.$this.e <Tab> "focus $w.data.pic.win.$next.e; break"
		bind $w.data.pic.win.$this.e <Shift-Return> "focus $w.data.pic.win.$prev.e; break"
		bind $w.data.pic.win.$this.e <Shift-Tab> "focus $w.data.pic.win.$prev.e; break"
		bind $w.data.pic.win.$this.f <Return> "focus $w.data.pic.win.$next.f; break"
		bind $w.data.pic.win.$this.f <Tab> "focus $w.data.pic.win.$next.f; break"
		bind $w.data.pic.win.$this.f <Shift-Return> "focus $w.data.pic.win.$prev.f; break"
		bind $w.data.pic.win.$this.f <Shift-Tab> "focus $w.data.pic.win.$prev.f; break"
	}

	scrollbar $w.data.sbh -width 15 -orient horiz  -command "$w.data.pic xview" -relief sunken -background ivory -troughcolor bisque -activebackground mistyrose
	bind $w.data.sbh <Control-1> "ezHelp Scrollbars; break"

	$w.data.pic create window 2 2 -anchor nw -window $w.data.pic.win

	pack $w.data.pic -side top -expand true -fill both

	pack $w.data.sbh -side bottom -expand true -fill x
	pack $w.data.msg -side bottom -expand true -fill x

	pack $w.ctrl -side left -expand true -fill both
	pack $w.data -side left -expand true -fill both 

	$w.data.msg configure -text "Please FIND a set of records, then select a record of interest.  Control-Click on the screen for help."
	update
	set datah [winfo height $w.data.pic.win ]
	set dataw [winfo width $w.data.pic.win ]
#	example of setting limits to canvas scrolling
#	canvas $c -scrollregion {-10c -10c 50c 20c}
	eval $w.data.pic configure -scrollregion \{-10 -10 [expr {$dataw + 10}] 200\}

	focus $w
}
