#!sqlwish -f
# -[Fri Oct  9 16:25:27 1992 by cxh]-

set auto_path "$tk_library/demos $auto_path"

# Set up the main window
wm title . "Ingres Browser:"
wm geometry . 300x400+10+10
wm minsize . 200 300

frame .menu -relief raised -borderwidth 1
frame .ent -borderwidth 10
frame .tables -borderwidth 1
frame .columns -borderwidth 1


#	[label .ent.l1 -relief sunken -borderwidth 1 \
#		-text "Database"] {top fillx} \

# Setup some entry widgets
pack append .ent \
	[button .ent.b1 -text "Open Database" \
		-command opendbButton ] {top fillx} \
	[entry .ent.e1 -relief sunken] {top fillx} \
	[scrollbar .ent.s1 -relief sunken -orient horiz -command \
		".ent.e1 view"] {top fillx} \
	[frame .ent.f1 -geometry 20x10] {top} \
	[button .ent.l2 -text "Command" \
		-command {commandButton "[eval .ent.e2 get]"} ] {top fillx} \
	[entry .ent.e2 -relief sunken] {top fillx} \
	[scrollbar .ent.s2 -relief sunken -orient horiz -command \
	    	".ent.e2 view"] {top fillx} \
	[frame .ent.f2 -geometry 20x10] {top} 


.ent.e1 config -scroll ".ent.s1 set"
.ent.e1 insert 0 "dbdict"
.ent.e2 config -scroll ".ent.s2 set"
.ent.e2 insert end "select * from tables"



# Select Button
menubutton .menu.select -text "select" -menu .menu.select.m
menu .menu.select.m
.menu.select.m add command -label "Select all tables" \
  -command {.txt.text delete 0.0 end; \
		.txt.text insert 0.0 [SQLselect * from tables]}
.menu.select.m add command -label "Select all columns" \
  -command {.txt.text delete 0.0 end; \
		.txt.text insert 0.0 [SQLselect * from columns]}
.menu.select.m add command -label "Fill lists" \
  -command {fill_lists}

# Database Button
#menubutton .menu.database -text "database" -menu .menu.database.m
#menu .menu.database.m
#.menu.database.m add command -label "open microlab" \
#  -command "SQLopendb microlab"
#.menu.database.m add command -label "open" \
#  -command {SQLopendb [eval .ent.e1 get]}
#.menu.database.m add command -label "close database" -command "SQLclosedb"

# FILE Button
#menubutton .menu.file -text File -menu .menu.file.m
#menu .menu.file.m
#.menu.file.m add command -label "Exit" -command "destroy ."
#.menu.file.m add command -label "Clear text" -command ".txt.text delete 0.0 end"

pack append .menu \
	[button .menu.b1 -text "Exit" \
		-command "destroy ." ] left \
	.menu.select left

# Deal with the text area

#frame .txt
#pack append . .txt {bottom expand fill}
#
#scrollbar .txt.vscrollbar -command ".txt.text yview" -relief sunken
#scrollbar .hscrollbar -relief sunken -orient horizontal \
# 	-command ".txt.text xview"
#
#text .txt.text -height 20 -width 16 \
#	-yscrollcommand ".txt.vscrollbar set" \
#	-xscrollcommand ".hscrollbar set" 
#
#pack append .txt .txt.vscrollbar {left filly} .txt.text {left expand fill}
#pack append . .hscrollbar {top fillx}

# Tables listbox
scrollbar .tables.xscroll -orient horizontal -command ".tables.list xview"
scrollbar .tables.yscroll -command ".tables.list yview"
listbox .tables.list \
	-xscroll ".tables.xscroll set" \
	-yscroll ".tables.yscroll set" \
	-relief raised -geometry 16x20

pack append .tables \
	[label .tables.l1 -relief sunken -borderwidth 1 \
		-text "Tables"] {top fillx} \
	.tables.xscroll {bottom fillx} \
	.tables.yscroll {right filly} \
	.tables.list {left expand fill}

bind .tables.list <Double-Button-1> {foreach i [selection get] {tableDef $i}}

# Columns listbox
scrollbar .columns.xscroll -orient horizontal -command ".columns.list xview"
scrollbar .columns.yscroll -command ".columns.list yview"

listbox .columns.list \
	-xscroll ".columns.xscroll set" \
	-yscroll ".columns.yscroll set" \
	-relief raised -geometry 16x20

pack append .columns \
	[label .columns.l1 -relief sunken -borderwidth 1 \
		-text "columns"] {top fillx} \
	.columns.xscroll {bottom fillx} \
	.columns.yscroll {right filly} \
	.columns.list {left expand fill}

bind .columns.list <Double-Button-1> {foreach i [selection get] {columnDef $i}}


# Pack it all up
pack append . .menu {top fillx } .ent {top fillx} \
	.tables {left  frame w} \
	.columns {right frame e}
focus .ent.e1

# Open the database
#ITopendb dbdict


# Fill the tables and columns lists
proc fill_lists {{tab .tables.list} {col .columns.list}} {

	
	SQLselect sel_tab table_name from tables

	set tab_search [array startsearch sel_tab]
	while {[array anymore sel_tab $tab_search]} {
		$tab insert end  $sel_tab([array nextelement sel_tab $tab_search])
	}
	SQLselect sel_col col_name from columns

	set col_search [array startsearch sel_col]
	while {[array anymore sel_col $col_search]} {
		$col insert end  $sel_col([array nextelement sel_col $col_search])
	}

}

proc opendbButton {} {
	SQLopendb [eval .ent.e1 get]
	.ent.b1 configure -text "Close Database" -command closedbButton
	fill_lists
}

proc closedbButton {} {
	SQLopendb [eval .ent.e1 get]
	.ent.b1 configure -text "Open Database" -command opendbButton
	fill_lists
}

# Get the definitions of tableDef, columnDef and commandButton.
source tbldef.tcl
