# browsedocs.tcl -- for S2K browser
#   Ray R. Larson (mar 92)
#
proc browsedocs {{win .docs} {query "none"} {messag "Browse Results"}} {

if {[string compare "$query" "none"] == 0} \
   {error "browsedocs requires an PostQuel Query"} {}

global WINCOUNT
incr WINCOUNT
set w "$win$WINCOUNT"

catch {destroy $w}

toplevel $w -relief {raised}
wm geometry $w +404+178
wm maxsize $w 1100 850
wm minsize $w 10 10
#wm positionfrom $w program
wm title $w "Browse Documents"

global BIGFONT

frame $w.fm13 -borderwidth {2}  -geometry {20x20}  -relief {raised}

frame $w.fm13.fm14

entry $w.fm13.fm14.ent -relief {raised}  -width {65}
bind $w.fm13.fm14.ent <Return> {filterlist %W "[%W get]" "BROWSEDOCSAVE"}

label $w.fm13.fm14.lab -relief {raised}  -text " Filter by: "

pack append $w.fm13.fm14 \
              $w.fm13.fm14.lab {left frame center expand fillx filly} \
              $w.fm13.fm14.ent {right frame center expand fillx filly}

pack append $w.fm13 \
              $w.fm13.fm14 {top frame center}

frame $w.fm7 -borderwidth {2}  -geometry {20x20} -relief raised

button $w.fm7.butt12  -text {Help} -pady 10\
                -command "createHelpListBox Browse_Results_Help pqresults.help"


button $w.fm7.butt11   -command "destroy $w"\
       -text {Cancel Browse}

button $w.fm7.butt10   -command {findsimilar}\
       -text {Find Similar}

button $w.fm7.butt8   -command {viewdoc [selection.get]}  -text {View Document}

pack append $w.fm7  \
            $w.fm7.butt8 {left frame center expand fillx filly}  \
            $w.fm7.butt10 {left frame center expand fillx filly} \
            $w.fm7.butt12 {left frame center expand fillx filly} \
            $w.fm7.butt11 {left frame center expand fillx filly} 

frame $w.fm5 -borderwidth {2}  -geometry {20x20}  -relief {raised}

frame $w.fm5.fm6

scrollbar $w.fm5.fm6.scr  -relief {raised}


listbox $w.fm5.fm6.list -geometry {75x24}  -relief {sunken}
bind $w.fm5.fm6.list <Double-Button-1>  {%W config -cursor watch ; update; viewdoc [selection.get] ; %W config -cursor top_left_arrow}

$w.fm5.fm6.scr config -command "$w.fm5.fm6.list yview"
$w.fm5.fm6.list config -yscroll "$w.fm5.fm6.scr set"

pack append $w.fm5.fm6 \
            $w.fm5.fm6.list {left frame center expand fillx} \
            $w.fm5.fm6.scr {left frame center expand filly}

pack append $w.fm5  $w.fm5.fm6 {top frame center fillx}

frame $w.fm3 -borderwidth {2}  -geometry {20x20}  -relief {raised}

message $w.fm3.mess4 -aspect {900}  \
    -font $BIGFONT \
    -justify {center}  -padx {0}  -pady {10}  -relief {raised} \
    -text "$messag"

pack append $w.fm3 \
            $w.fm3.mess4 {left frame center expand fillx filly}

pack append $w \
            $w.fm3 {top frame center fillx filly} \
            $w.fm5 {top frame center fillx filly} \
            $w.fm7 {top frame center fillx filly}  \
            $w.fm13 {left frame center fillx filly}


$w config -cursor watch
WaitWindow {retrieving from POSTGRES}
update

# run the retrieve command to get the names from Postgres
#

global DOCS1
catch {unset DOCS1}

# this uses cookie for testing
PQexec $query
PQtuplearray blank DOCS1

	#
	# Load the tuples into the listbox
	#
	set fldnames $DOCS1(FIELDS.0)
   	foreach i [lsort [array names DOCS1]] {
                #  if it's not a data element of the array skip it
   		if {[string match "\[A-Z\]*" $i ] == 1} break

		#  put in a "record number"
   		$w.fm5.fm6.list insert end "Record id : $i"

  	 	# create lines for each attribute of the tuple having a
   		# label for the attribute name, a colon, and the
   		# value of the attribute
   	 	foreach f $fldnames {
        		set n [lsearch $fldnames $f]
   			set fldval [concat [lindex $DOCS1($i) $n]]
                        set tval [format "%s: %s" $f $fldval]
   			$w.fm5.fm6.list insert end "$tval"
        	}
   		$w.fm5.fm6.list insert end " "
       }

 catch { destroy .waitwin }
 $w config -cursor top_left_arrow

#end browsedocs
}



