# Browse doc keywords
#  Ray R. Larson (mar 92)
#
proc browsedockeywords {} {

catch {destroy .key}

toplevel .key -relief {raised}
wm geometry .key +300+200
wm maxsize .key 1100 850
wm minsize .key 10 10
#wm positionfrom .key program
wm title .key "Keywords"

global BIGFONT

	frame .key.keytm -borderwidth {2}  -geometry {20x20} -relief raised

	button .key.keytm.butt8   \
               -text {Cancel Browse} -pady 10 -command {destroy .key}

	button .key.keytm.butt7   -text {Help} \
               -command "createHelpListBox Keywords_Help keywords.help"


	button .key.keytm.butt6  \
               -text {Document List}\
               -command {getkdoclist [selection.get]}

	pack append .key.keytm \
              .key.keytm.butt6 {left frame center expand fillx filly} \
              .key.keytm.butt7 {left frame center expand fillx filly} \
              .key.keytm.butt8 {left frame center expand fillx filly}


        frame .key.keytl

	scrollbar .key.keytl.scr -command {.key.keytl.list yview}  -relief {raised}

	listbox .key.keytl.list -geometry {40x24}  -relief {raised} \
                  -yscroll {.key.keytl.scr set}
        bind .key.keytl.list <Double-Button-1>  {getkdoclist [selection.get]}


	pack append .key.keytl \
              .key.keytl.list {left frame center expand fill} \
              .key.keytl.scr {left frame center expand filly}


	message .key.keytms -aspect {900}  -font $BIGFONT \
            -justify {center}  -pady 10  -relief {raised} \
            -text {Browse Keywords}


	pack append .key \
              .key.keytms {top frame center expand fillx filly} \
              .key.keytl {top frame center expand fillx filly} \
              .key.keytm {top frame center expand fillx filly}

        .key  config -cursor watch
        WaitWindow {retrieving from POSTGRES}
        update

   # run the retrieve command to get the names from Postgres
   #
   global KEYWD
   catch {unset KEYWD}



   # this uses new versions of the command
   set ids [lrange [retrieve unique (kw_term_doc_rel.termid)] 1 end]
   foreach termid $ids {
       lappend termlist [lrange [retrieve (wn_index.word) where wn_index.termid = $termid] 1 end]
   }

   #
   # Load the words into the listbox
   #
   foreach i [lsort $termlist] {
       .key.keytl.list insert end "$i"
   }
   destroy .waitwin
   .key config -cursor top_left_arrow
# end of browseauthornames
}

#getdoclist -- list the doc name for a particular author
proc getkdoclist {key} {

set key2 [join $key]

   set query [format "retrieve (bib.title)  where tkey.word = \"%s\"::text and tkey.accno = bib.accno" $key2]
puts stdout $query
   browsedocs .key $query "Browsing Documents with keyword:\n $key2 "
}
