 proc put_popup_column {ename fld} {
	 global lbox_ans_$ename

	 set ret [set lbox_ans_$ename]
	 if {$ret == ""} return
		 set oldval [ebox_getval item_create $fld]
	 foreach val $ret {
	 	set ret1 [string trimright [lindex $val 0]]
		 set ret2 [string trimright [lindex $val 1]]
	 if {$ret2 != "" && $fld == "col1"} {
		 append ret1 ".$ret2"
	 } elseif {$ret2 != ""} { set ret1 "$ret2" }
		 append oldval $ret1
	 }
	 ebox_putval item_create $fld "$oldval"
 }
proc update_EZrpt {c} {
	  global table
	  upvar #0 EZRcols  cols
	  upvar #0 EZRtypes types
	  upvar #0 EZRsizes sizes
	  upvar #0 EZRwhere where
	  upvar #0 EZRsorts sorts
	  upvar #0 EZRprints prints
	  upvar #0 EZRwidths widths
	  upvar #0 EZRnumsrt numsrt
	  upvar #0 EZRnumprt numprt
	  upvar #0 EZRnumcol numcol
	  upvar #0 EZRporder porder
	  upvar #0 EZRsorder sorder
	  upvar #0 EZRop op
	  upvar #0 EZRcol col
	  upvar #0 EZRind ind
	  upvar #0 EZRcomp1 comp1
	  upvar #0 EZRcomp2 comp2
	  upvar #0 EZRsqlcmd sqlcmd
	  upvar #0 EZRdist dist
	  upvar #0 EZRrform rf
	  set i [llength $cols]
	  lappend cols $c
	  set w .ezrpt
	 frame $w.data.pic.win.winL.$c -relief flat -borderwidth 0
	 frame $w.data.pic.win.winR.$c -relief flat -borderwidth 0

	 checkbutton $w.data.pic.win.winL.$c.pb -text "PR" \
	 -variable EZRprints($i) -relief flat -border 0
	 entry $w.data.pic.win.winL.$c.po -relief raised -width 3
	 radiobutton $w.data.pic.win.winL.$c.cc -text "$c" -relief flat \
	 -variable EZRcol -value "$c"  -border 0
	 entry $w.data.pic.win.winR.$c.st -relief raised -width 35

# this button was going to turn the RSE on and off, but why bother.
# if I was saving query text from here it might be useful, but the
# query gets saved from the main wisql window, so forget this
# checkbutton $w.data.pic.win.winR.$c.se -relief flat -variable EZRwhere($i,yn)

	 radiobutton $w.data.pic.win.winR.$c.a -text "A" -relief flat \
	 -variable EZRsorts($i) -value "a" -border 0
	 radiobutton $w.data.pic.win.winR.$c.d -text "D" -relief flat \
	 -variable EZRsorts($i) -value "d" -border 0
	 entry $w.data.pic.win.winR.$c.so -relief raised -width 3
	 bind $w.data.pic.win.winL.$c.pb <Control-1> "ezHelp EZRpr"
	 bind $w.data.pic.win.winL.$c.po <Control-1> "ezHelp EZRpr"
	 bind $w.data.pic.win.winL.$c.cc <Control-1> "ezHelp EZRfield"
	 bind $w.data.pic.win.winL.$c.cc <Control-2> "dataHelp $table $c"
	 bind $w.data.pic.win.winR.$c.st <Control-1> "ezHelp EZRreq"
	 bind $w.data.pic.win.winR.$c.a <Control-1> "ezHelp EZRsort"
	 bind $w.data.pic.win.winR.$c.d <Control-1> "ezHelp EZRsort"
	 bind $w.data.pic.win.winR.$c.so <Control-1> "ezHelp EZRsort"

	 bind $w.data.pic.win.winL.$c.cc <ButtonRelease-1> \
		 "colCurrent $w $c"
	 bind $w.data.pic.win.winL.$c.pb <ButtonRelease-1> \
		 "$w.data.pic.win.winL.$c.pb deactivate; setPrint $w $c"
	 bind $w.data.pic.win.winL.$c.po <Leave> \
		 "setOrder $w $c EZRprints EZRporder EZRnumprt EZRcols"
	 bind $w.data.pic.win.winR.$c.st <Leave> \
		 "chkEZRsel $w $c"
	 bind $w.data.pic.win.winR.$c.a <ButtonRelease-1> \
		 "$w.data.pic.win.winR.$c.a deactivate; setSort $w $c a"
	 bind $w.data.pic.win.winR.$c.d <ButtonRelease-1> \
		 "$w.data.pic.win.winR.$c.d deactivate; setSort $w $c d"
	 bind $w.data.pic.win.winR.$c.so <Leave> \
		 "setOrder $w $c EZRsorts EZRsorder EZRnumsrt EZRcols"
    pack append $w.data.pic.win.winL.$c \
		$w.data.pic.win.winL.$c.pb {left fillx frame w} \
		$w.data.pic.win.winL.$c.po {left fillx frame w} \
		$w.data.pic.win.winL.$c.cc {left fillx frame w}
	pack append $w.data.pic.win.winR.$c \
		$w.data.pic.win.winR.$c.st {left fillx frame w} \
		$w.data.pic.win.winR.$c.a {left fillx frame w} \
		$w.data.pic.win.winR.$c.d {left fillx frame w} \
		$w.data.pic.win.winR.$c.so {left fillx frame w}

	append Lpack "$w.data.pic.win.winL.$c \{top frame nw\} "
	append Rpack "$w.data.pic.win.winR.$c \{top frame nw\} "
	eval pack append $w.data.pic.win.winL $Lpack
	eval pack append $w.data.pic.win.winR $Rpack
    set prints($i) 0
	set sorts($i) 0
	set numcol $i
	set widths($i) 0
	incr i 1
	set numcol $i
destroy $w.data.sbv
scrollbar $w.data.sbv -width 15 -orient vertical \
	-command "$w.data.pic yview" -relief sunken
bind $w.data.sbv <Control-1> "ezHelp Scrollbars"
	 
eval pack append $w.data.pic.win.winL $Lpack 
eval pack append $w.data.pic.win.winR $Rpack 

pack append $w.data.pic.win $w.data.pic.win.winL \
	left $w.data.pic.win.winR left


pack append $w.data $w.data.spec {left filly} \
	$w.data.pic {left expand fillx filly} \
	$w.data.sbv {left filly} 

pack append $w $w.spacer {bot} \
	$w.control {top fillx pady 10}  \
	$w.messages {top fillx pady 10} \
	$w.data {top expand fill pady 100} 

#	used to follow sbv
#	$w.sbh {bot fillx pady 2} 

	set datah [winfo height $w.data.pic.win ]
	set dataw [winfo width $w.data.pic.win ]
     incr datah 1
#	example of setting limits to canvas scrolling
#	canvas $c -scrollregion {-10c -10c 50c 20c}
	eval $w.data.pic configure -scrollregion \{-10 -10 200 [expr {$datah + 10}] \}

colCurrent $w [lindex $cols 0] 
# .pic.win.winL.[lindex $cols 0].pb invoke


	update
}
proc list_predefined {proc toplev cur ent} {
      global item_line table tabid
   set sqlcmd "select colname,collength from syscolumns where tabid = $tabid"
   set cursor [sql open $sqlcmd]
   while {[set linex [sql fetch $cursor 1]] !=""} {
		   lappend plist [format "%-22.22s" [lindex $linex 0]]
									  }
      set name item_create
	 lbox $name -data [join $plist ","]\
		 -restrict -header "Field Names" -cols 50 -lines 20\
			 -ok "put_popup_column $name input_name" -multi 1 

} 
proc set_options { proc ans} {
	  global derived 
     set derived_name  [ebox_getval item_create  derived_name]
     set input_name [ebox_getval item_create  input_name]
   if {$derived_name  == ""} {
       show_dialog .nodb "InValid Fields"\
	      "When Adding A Derived Field The You Must Have A Name"
          return 0
      } 

   if {$input_name  == ""} {
       show_dialog .nodb "InValid Fields"\
	      "When Adding A Derived Field The You Must Have A Origin"
          return 0
      } 
     set derived($derived_name) $input_name
	 update_EZrpt $derived_name
#	 makeEZrpt
   return 1
}
proc mkDIRIVED {argv} {
	 set input_name ""
	 set derived_name ""
   ebox item_create "Derived Item Name" "
	 {derived_name {Derived Item Name:} {} {$derived_name} {} {}
	       {Enter The Derived Item Name} }
     {input_name {Enter  Origin :} {} {$input_name} {list_predefined create} 
		   {} {Enter The Calculation} {} {} }
               " -ok "set_options create"

	   
}

# $Header: /ni/rcsfiles/smart/guif/perfex/wisql.tlib,v 1.2 1995/02/08 18:23:01 allen Exp allen $
# tcl procs saved on Wed Oct 06 18:50:59 PDT 1993
proc process_derived {sqlcmd} {
	global derived

#	derived.def is the definition file for all derived fields
	set drvfile "/tmp/derived.def"
    set f 0

	foreach i [array names derived] {
		if {$f==0} {
  			set openrc [catch {set f [open $drvfile w]}]
  			if {$openrc==1} {
   				setMsg "Error: $drvfile could not be opened"
   				return
  			}
		}
		puts $f [format "#define %s (%s)\n" $i $derived($i)]
	}

	close $f

#	derived.def is the definition file for all derived fields
	set inc {#include "/tmp/derived.def"}

#	Pass the desired variable thru the m4 preprocessor
	foreach i [array names derived] {
		set status [catch {set output [exec echo "$inc\n$i" | /usr/ccs/lib/cpp -P]}]
        set output [string trimright $output]
        set output [string trimleft $output] 
        append sqlcmd [format " %s %s," $output $i]
	}
	return $sqlcmd
}
