proc createMain {} {

  global dbpipe1 DB showText
  global sybmsg
  global currentFile
  global server base
  global mode
  global CmdText
  global ResClear Have_Table Have_BLT MsgW
  global Sheet CellValues CellFormulae TitleRow curVal curCell curRow
  global xess_connection look_for_xess AutoRecompute

# set in wisql launch code
  global bitmapdir

  wm geom    . 900x800+100+100
  wm minsize . 900 800
  wm iconname . "$server"
  wm iconbitmap . @$bitmapdir/wisql5.xbm

  # create a top level frame

  frame .m -relief flat -background antiquewhite

  pack .m -side top -fill both -expand true

  frame .m.mb -relief raised -borderwidth 2 -background antiquewhite
  menubutton .m.mb.file -text "Query..." -menu .m.mb.file.m -background lavender -activebackground lavender -activeforeground magenta2 -border 2 -relief raised
  menu .m.mb.file.m -background lavender
  .m.mb.file.m add command -label "New" -command doNew -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.file.m add command -label "Open..."  -command "fileBox .open * \"\" \"\" tryOpen" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.file.m add command -label "Save" -command doSave  -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.file.m add command -label "Save as..."  -command "fileBox .save * \"\" \"\" doSaveAs" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.file.m add command -label "Detach" -command detachCmd -background lavender -activebackground lavender -activeforeground magenta2
# .m.mb.file.m add separator
# .m.mb.file.m add command -label "Exit"  -command confirmExit  -background lavender -activebackground lavender -activeforeground red

  menubutton .m.mb.out -text "Results..." -menu .m.mb.out.m -background lavender -activebackground lavender -activeforeground magenta2 -border 2 -relief raised
  menu .m.mb.out.m -background lavender
  .m.mb.out.m add radiobutton -label "Append results" -variable ResClear  -value 0  -command "set ResClear 0" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m add radiobutton -label "Clear results" -variable ResClear  -value 1  -command "set ResClear 1" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m add command -label "Data formats" -command "userCustom" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m add separator -background mistyrose
  .m.mb.out.m add command -label "Detach" -command detachRes -activebackground lavender -activeforeground magenta2 -background lavender
  .m.mb.out.m add command -label "Clear" -command "clearoutput 1" -activebackground lavender -activeforeground magenta2 -background lavender
  .m.mb.out.m add command -label "Save as..." -command "fileBox .save_Results * \"\" \"\" doSaveOut" -activebackground lavender -activeforeground magenta2 
  .m.mb.out.m add command -label "Mail To..." -command doMailResultsTo -activebackground lavender -activeforeground magenta2 -background lavender
  .m.mb.out.m add command -label "Print" -command doPrint -activebackground lavender -activeforeground magenta2 -background lavender
  if {$Have_Table} {
  .m.mb.out.m add command -label "Spreadsheet" -command "newSheet ; set Sheet 1" -activebackground lavender -activeforeground magenta2 -background lavender
  }
  if {$Have_BLT} {
  .m.mb.out.m add command -label "Plot..." -command plotControl -activebackground lavender -activeforeground magenta2 -background lavender
  }
  if {$DB == "SYB"} {
	.m.mb.out.m add check -label "ShowText" -variable showText -onvalue 1 -offvalue 0 -background lavender -activebackground lavender -activeforeground magenta2 -selectcolor magenta2
  }
	

#	Sheet is now detached, not embedded
#  if {$Sheet} {
#  .m.mb.out.m add command -label "Recompute" -command "compSheet .m.o.out"
#  .m.mb.out.m add command -label "Export TSV..." -command "fileBox .save_Results * \"\" \"\" \"\\\{doSaveSheet $SheetT TSV\\\}\""
#  .m.mb.out.m add command -label "Save w/Calc..." -command "fileBox .save_Results * \"\" \"\" \"\\\{doSaveSheet $SheetT FOR\\\}\""
#  .m.mb.out.m add command -label "Import TSV..." -command "fileBox .save_Results * \"\" \"\" \"\\\{doLoadSheet $SheetT\\\}\""
#  }

  .m.mb.out.m add command -label "Server Msgs" -command dbMsgs -activebackground lavender -activeforeground magenta2 
  .m.mb.out.m add cascade -label "Font Size  -->" -menu .m.mb.out.m.f -background lavender -activebackground lavender -activeforeground magenta2
# I changed courier 17 to courier 18, a more commonly available size
  menu .m.mb.out.m.f -background lavender
  .m.mb.out.m.f add radiobutton -variable fontSize -value  8 -label " 8"  -command ".m.o.out configure -font -*-courier-*-r-*-*-8-*-*-*-*-*-*-*" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m.f add radiobutton -variable fontSize -value 10 -label "10"  -command ".m.o.out configure -font -*-courier-*-r-*-*-10-*-*-*-*-*-*-*" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m.f add radiobutton -variable fontSize -value 12 -label "12"  -command ".m.o.out configure -font -*-courier-*-r-*-*-12-*-*-*-*-*-*-*" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m.f add radiobutton -variable fontSize -value 14 -label "14"  -command ".m.o.out configure -font -*-courier-*-r-*-*-14-*-*-*-*-*-*-*" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m.f add radiobutton -variable fontSize -value 18 -label "18"  -command ".m.o.out configure -font -*-courier-*-r-*-*-18-*-*-*-*-*-*-*" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m.f add radiobutton -variable fontSize -value 20 -label "20"  -command ".m.o.out configure -font -*-courier-*-r-*-*-20-*-*-*-*-*-*-*" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.out.m.f add radiobutton -variable fontSize -value 24 -label "24"  -command ".m.o.out configure -font -*-courier-*-r-*-*-24-*-*-*-*-*-*-*" -background lavender -activebackground lavender -activeforeground magenta2

  menubutton .m.mb.db -text "Databases..." -menu .m.mb.db.m -background lavender -activebackground lavender -activeforeground magenta2 -border 2 -relief raised
  menu .m.mb.db.m -background lavender
  set dblist [${DB}getDB]
  foreach db $dblist {
	.m.mb.db.m add command -label $db -command "${DB}useDB $db" -background lavender -activebackground lavender -activeforeground magenta2
  }

  button .m.mb.tb -text "Choose Table" -relief flat -border 0  -command ${DB}showTables  -padx 0 -pady 0 -background mistyrose -activebackground mistyrose -activeforeground magenta2 -border 2 -relief raised
  button .m.mb.tf -text "Show Fields" -relief flat -border 0  -command ${DB}showFields -padx 0 -pady 0 -background mistyrose -activebackground mistyrose -activeforeground magenta2 -border 2 -relief raised

  if {$mode} {
  menubutton .m.mb.ob -text "Definitions..." -menu .m.mb.ob.m -background lavender -activebackground lavender -activeforeground magenta2 -border 2 -relief raised
  menu .m.mb.ob.m -background lavender
  .m.mb.ob.m add command  -label "Views"    -command "${DB}showObjs Views V" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.ob.m add command  -label "Procs"    -command "${DB}showObjs Procedures P" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.ob.m add command  -label "Triggers" -command "${DB}showObjs Triggers TR" -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.ob.m add command  -label "Rules"    -command "${DB}showObjs Rules R" -background lavender -activebackground lavender -activeforeground magenta2

  bind .m.mb.ob <Control-1> "ezHelp wisqlDef; break"

  }

#  execCmd is normally "doSql", except while in doSql, then it is Cancel

  button .m.mb.exec  -text "Execute" -command {eval $execCmd} -relief raised -border 2 -padx 0 -pady 0 -background lemonchiffon -activebackground gold 


  button .m.mb.mkCount -text "RecordCt" -relief raised  -command showCount -padx 0 -pady 0 -background mistyrose -activebackground mistyrose -activeforeground magenta2

  menubutton .m.mb.ez -text "EZ..." -background lavender -menu .m.mb.ez.m -border 2 -relief raised -activebackground lavender -activeforeground magenta2
  menu .m.mb.ez.m  -background lavender
  .m.mb.ez.m add command -label "EZrpt" -command makeEZrpt -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.ez.m add command -label "EZedit" -command makeEZedit -background lavender -activebackground lavender -activeforeground magenta2
  .m.mb.ez.m add command -label "EZhide" -command makeEZEhide -background lavender -activebackground lavender -activeforeground magenta2

  menubutton .m.mb.help -text "Help..." -menu .m.mb.help.m -background lavender -activebackground lavender -activeforeground magenta2 -border 2 -relief raised
  menu .m.mb.help.m -background lavender
  .m.mb.help.m add command -label "About WISQL"   -command aboutWisql -background lavender -activebackground lavender -activeforeground magenta2 
  .m.mb.help.m add command -label "General" -command "ezHelp wisql" -background lavender -activebackground lavender -activeforeground magenta2 
  .m.mb.help.m add command -label "FastKeys" -command "ezHelp wisqlKeys" -background lavender -activebackground lavender -activeforeground magenta2 
  .m.mb.help.m add command -label "HowTo" -command "ezHelp wisqlHow" -background lavender -activebackground lavender -activeforeground magenta2 
  .m.mb.help.m add command -label "Data" -command "dataDict" -background lavender -activebackground lavender -activeforeground magenta2 
#  .m.mb.help.m add cascade -label "Menus  -->"   -menu .m.mb.help.m.m

  button .m.mb.quit -text "QUIT" -border 1 -command confirmExit -padx 0 -pady 0 -background white -activebackground black -foreground red -activeforeground yellow

  pack .m.mb -side top -fill x

#	Control click help bindings

	bind .m.mb.file <Control-1> "ezHelp wisqlFile; break"
	bind .m.mb.out <Control-1> "ezHelp wisqlResults; break"
	bind .m.mb.db <Control-1> "ezHelp wisqlDB; break"
	bind .m.mb.tb <Control-1> "ezHelp wisqlChooT; break"
	bind .m.mb.tf <Control-1> "ezHelp wisqlShowF; break"
	bind .m.mb.ez <Control-1> "ezHelp wisqlEZstuff; break"
#	bind .m.mb.mkRpt <Control-1> "ezHelp wisqlEZR; break"
#	bind .m.mb.mkEdit <Control-1> "ezHelp wisqlEZE; break"
#	bind .m.mb.mkHide <Control-1> "ezHelp wisqlEZEhide; break"
	bind .m.mb.mkCount <Control-1> "ezHelp wisqlEZcount; break"
	bind .m.mb.exec <Control-1> "ezHelp wisqlExe; break"
	bind .m.mb.help <Control-1> "ezHelp wisqlHlp; break"

	bind .m.mb.exec <Shift-Control-3> "getItAll; break"

  if {$mode} {
  pack .m.mb.file -side left -ipadx 0 -padx 2
  pack .m.mb.out -side left -ipadx 0 -padx 2
  pack .m.mb.db -side left  -ipadx 0 -padx 2
  pack .m.mb.tb -side left  -ipadx 0 -padx 2
  pack .m.mb.tf -side left  -ipadx 0 -padx 2
  pack .m.mb.mkCount -side left  -ipadx 0 -padx 2
  pack .m.mb.ob -side left  -ipadx 0 -padx 2
  pack .m.mb.ez -side left  -ipadx 0 -padx 2
#  pack .m.mb.mkRpt -side left  -ipadx 0 -padx 2
#  pack .m.mb.mkEdit -side left  -ipadx 0 -padx 2
#  pack .m.mb.mkHide -side left  -ipadx 0 -padx 2
  pack .m.mb.exec -side left  -ipadx 0 -padx 2
  pack .m.mb.quit -side right -ipadx 0 -padx 2
  pack .m.mb.help -side right  -ipadx 0 -padx 2
  } else {
  pack .m.mb.file -side left -ipadx 0 -padx 2
  pack .m.mb.out -side left -ipadx 0 -padx 2
  pack .m.mb.db -side left  -ipadx 0 -padx 2
  pack .m.mb.tb -side left  -ipadx 0 -padx 2
  pack .m.mb.tf -side left  -ipadx 0 -padx 2
  pack .m.mb.mkCount -side left  -ipadx 0 -padx 2
  pack .m.mb.ez -side left  -ipadx 0 -padx 2
#  pack .m.mb.mkRpt -side left  -ipadx 0 -padx 2
#  pack .m.mb.mkEdit -side left  -ipadx 0 -padx 2
#  pack .m.mb.mkHide -side left  -ipadx 0 -padx 2
  pack .m.mb.exec -side left  -ipadx 0 -padx 2
  pack .m.mb.quit -side right -ipadx 0 -padx 2
  pack .m.mb.help -side right  -ipadx 0 -padx 2
  }

  tk_bindForTraversal .m.mb
  tk_menuBar .m.mb .m.mb.file .m.mb.out .m.mb.db  .m.mb.ob .m.mb.mkRpt .m.mb.exec .m.mb.help

  # create a top title

##--	DC Mar 24 93  change window subtitle structure

	set font [getFont {} banner]
	frame .m.title -relief raised -border 1 -background antiquewhite
	label .m.title.sl -text "server:"  -font $font -background antiquewhite
	label .m.title.bl -text "database:"  -font $font -background antiquewhite
	label .m.title.tl -text "table:"  -font $font -background antiquewhite
	entry .m.title.cs -font $font   -width 15 -background ivory
	entry .m.title.cb -font $font  -width 15 -background ivory
	entry .m.title.ct -font $font  -width 15 -background ivory

# This feature was removed in favour of using tkinspect, which does almost
# everything I want and better than tlibDoc ever did.
#	if {$mode} {
#		set font [getFont {} helvetica]
#		bind .m.title.ad <Control-1> "ezHelp AutoDoc; break"
#	}

	bind .m.title.cs <Control-1> "ezHelp Wserver; break"
	bind .m.title.cb <Control-1> "ezHelp Wdatabase; break"
	bind .m.title.ct <Control-1> "ezHelp Wtable; break"

	if {$mode} {
	pack .m.title.sl .m.title.cs .m.title.bl .m.title.cb .m.title.tl .m.title.ct -side left 
	} else { 
	pack .m.title.sl .m.title.cs .m.title.bl .m.title.cb .m.title.tl .m.title.ct -side left
	}



	.m.title.cs insert 0 "$server"
	.m.title.cs configure -state disabled
	.m.title.cb configure -state disabled
	.m.title.ct configure -state disabled

		

  pack .m.title -side top -fill x -ipadx 10 -anchor center

  # create a frame listing sql code

  frame .m.s -relief raised -borderwidth 2 -background antiquewhite
  pack .m.s -side top -fill both

  label .m.s.l -text "SQL (noname)"  -background antiquewhite
  scrollbar .m.s.vert -relief sunken -command ".m.s.sql yview"  -orient vertical \
	-troughcolor bisque -background ivory -activebackground mistyrose
  bind .m.s.vert <Control-1> "ezHelp Scrollbars; break"
  set font [getFont {} mediumcou]
  text .m.s.sql -font $font -relief sunken -height 8 -width 80  -yscroll ".m.s.vert set"  -wrap word -borderwidth 2 -background ivory
  set CmdText .m.s.sql

# Balloon help and quick-key execute
  bind .m.s.sql <Control-1> "ezHelp wisqlSQL; break"
  bind .m.s.sql <Control-Return> ".m.mb.exec invoke; break"
  bind .m.s.sql <Shift-Return>   ".m.mb.exec invoke; break"
# Implement command Ring Buffer
  bind .m.s.sql <Control-p>   "prevSql -1; break"
  bind .m.s.sql <Control-n>   "prevSql  1; break"
# And a couple of other handy bindings
  bind .m.s.sql <Enter> "focus .m.s.sql; break"
  bind .m.s.sql <Control-x> "clearsql; break"
# Eventually I would like to get some real bindings in here for editing,
# to detect whether there is a selection area and delete the selection area
# on DEL event, and replace selection area on user keystrokes, etc.

  pack .m.s.l -side top -fill x
  pack .m.s.vert -side right -fill y
  pack .m.s.sql -side left -fill both -expand true

  # create a frame listing sql output

  frame .m.o -relief raised -background antiquewhite
  pack .m.o -side top -fill both -expand true

  label .m.o.l -text "Results" -background antiquewhite
  scrollbar .m.o.vert -relief sunken -command ".m.o.out yview"  -orient vertical \
	-troughcolor bisque -background ivory -activebackground mistyrose
  scrollbar .m.o.horz -relief sunken -command ".m.o.out xview"  -orient horizontal \
	-troughcolor bisque -background ivory -activebackground mistyrose
  bind .m.o.vert <Control-1> "ezHelp Scrollbars; break"
  bind .m.o.horz <Control-1> "ezHelp Scrollbars; break"

 
  set font [getFont {} mediumcou]
  set tfont [getFont {} mediumhelb]
#
#  WAS if {$Sheet} ...
#  frame .m.o.c
#  label .m.o.c.clab -relief sunken -background ivory -font $tfont \
#	-text "Edit :" -width 20
#  set SheetEdLab .m.o.c.clab
#  entry .m.o.c.cell -relief sunken -background white -foreground red \
#	-textvariable curVal -border 2 
#  button .m.o.c.adds -background lavender -text "Add2Formula" \
#	-command "addCell2Form A $SheetT"
#  set SheetAddB .m.o.c.adds
#  bind .m.o.c.cell <Return> "cellCommit $SheetT"
#  table .m.o.out -relief sunken -background ivory \
#	 -yscroll ".m.o.vert set" -xscroll ".m.o.horz set" \
#	-cols 10 -rows 25 -variable CellValues -font $font \
#	-browsecommand "cellSelect %W" -anchor w -titlerows 1 \
#	-titlecols 1 -exportselection 1 -selectmode extended  
#	validate works on every keystroke, not what we need for checking
#	formula entry.
#	-vcmd "cellCommit $SheetT" -validate 1
#
#  .m.o.out tag config separator -bg gray
#  .m.o.out tag config active -fg red -bg yellow
#  .m.o.out tag config sel -bg yellow
#  .m.o.out tag config formula -bg seashell
#  .m.o.out tag config error -bg mistyrose
#  .m.o.out tag config title -bg papayawhip -font $tfont -fg black \
#	-anchor center
#  .m.o.out tag config heads -bg mistyrose -font $tfont 
#  .m.o.out width 0 6
#  set curRow 1
#  keySheet .m.o.out
#  set SheetM .m.msg

#
#	significant change:  listbox becomes textbox
#	this should make cut/paste work better
#	with a little more effort we could have shaded columns a la electrolog (wow)
#

  set font [getFont {} mediumcou]
  text .m.o.out -relief sunken  -font $font   -yscroll ".m.o.vert set" -xscroll ".m.o.horz set" -background ivory -wrap none

#  selection handle .m.o.out "handleList .m.o.out 0 end"

  bind .m.o.out <Control-1> "ezHelp wisqlOUT; break"
  bind .m.o.out <Control-u> "clearoutput 1; break"
#  if {$Sheet} {
#  pack .m.o.c.clab -side left
#  pack .m.o.c.adds -side right 
#  pack .m.o.c.cell -side left -fill x -expand true
#  pack .m.o.c -side top -fill x -expand true
#  }
  pack .m.o.l -side top -fill x
  pack .m.o.vert -side right -fill y
  pack .m.o.horz -side bottom -fill x
  pack .m.o.out -side top -fill both -expand true
  bind .m.o.out <Control-x> "clearoutput 1; break"

  # create a message at the bottom

  #label .m.msg -text "" -width 40 -relief sunken 

  set font [getFont {} banneri]
  set MsgW .m.msg
  message .m.msg -text "" -justify center -aspect 2000  -width 700 -relief sunken -font $font -background ivory
  bind .m.msg <Control-1> "ezHelp Messages; break"
    
  pack .m.msg -side bottom -fill x

  focus .m.s.sql

  ${DB}useDB $base

  if {$look_for_xess} {
  set xess_connection 0
  set isxess [info commands xess]
  if {$isxess != ""} {
	echo "Tcl-Xess is present on this host..."
	set xess_running [xess scan]
	if {$xess_running != ""} {
		set err [catch {xess connect xs}]
		if {$err} {
			echo "Unable to connect to running Xess spreadsheet ID $xess_running...."
		} else {
			.m.msg configure -text "Xess connection enabled"
			.m.mb.out.m add separator
			.m.mb.out.m add radiobutton -label "Output to Xess"  -variable outxess -value 1  -command "set outxess 1; .m.msg configure -text \"Data will be sent to Xess instead of Results Area\""
			.m.mb.out.m add radiobutton -label "Output to Wisql"  -variable outxess  -value 0  -command "set outxess 0; .m.msg configure -text \"Data will be sent to Results Area not to Xess\""
			set xess_connection 1
		}
	} else {
		echo "No Xess spreadsheet is running at this time..."
	}
  } 
  sleep 5
  }

  .m.msg configure -text "At your service....."
  update

}
