# Browse Author names
#  Ray R. Larson (mar 92)
#
proc browseauthornames {} {

    catch {destroy .au}

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

    global BIGFONT

    frame .au.autm -borderwidth {2}  -geometry {20x20} -relief raised

    button .au.autm.butt8   \
	-text {Cancel Browse} -command {destroy .au}

    button .au.autm.butt7   -text {Help} \
	-command "createHelpListBox Authors_Help authornames.help"


    button .au.autm.butt6  \
	-text {Document List} -pady 10 \
	-command {
	    .au config -cursor watch
	    update
	    getdoclist [selection.get]
	    .au config -cursor top_left_arrow
	}

    pack append .au.autm \
	.au.autm.butt6 {left frame center expand fillx filly} \
	.au.autm.butt7 {left frame center expand fillx filly} \
	.au.autm.butt8 {left frame center expand fillx filly}


    frame .au.autl

    scrollbar .au.autl.scr -command {.au.autl.list yview}  -relief {raised}

    listbox .au.autl.list -geometry {40x24}  -relief {raised} \
	-yscroll {.au.autl.scr set}

    bind .au.autl.list <Double-Button-1>   {
	.au config -cursor watch
	update
	getdoclist [selection.get]
	.au config -cursor top_left_arrow
    }

    pack append .au.autl \
	.au.autl.list {left frame center expand fillx} \
	.au.autl.scr {left frame center expand filly}


    message .au.autms -aspect {900}  -font $BIGFONT \
	-justify {center}  -padx {20}  -pady {10}  -relief {raised} \
	-text {Browse Author Names}


    pack append .au \
	.au.autms {top frame center expand fillx filly} \
	.au.autl {top frame center expand fillx filly} \
	.au.autm {top frame center expand fillx filly}


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

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


    PQexec "begin"
    # this uses cookie for testing
    PQexec "retrieve portal nameport (AUTHOR.name,AUTHOR.oid) sort by name using < "
    PQexec "fetch all in nameport"
    PQtuplearray nameport AUTNAMES
    PQexec "close nameport"
    PQexec "end"
    set lasttval ""

    #
    # Load the tuples into the listbox
    #
    foreach i [lsort [array names AUTNAMES]] {
	if {[string match "\[A-Z\]*" $i ] == 1} break
	set tval [concat [lindex "$AUTNAMES($i)" 0]]
	if {[string match $tval $lasttval ] == 1} continue
	set lasttval $tval
	.au.autl.list insert end "$tval"
    }

    catch {destroy .waitwin}
    .au config -cursor top_left_arrow

    # end of browseauthornames
}

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

    set author2 [join $author]
    #puts stdout "in getdoclist!!"
    set query [format "retrieve (t.oid, t.title, t.pages) \
        from t in TECH_REPORT_BIB \
        where AUTHOR.name = \"%s\" and AUTHOR.oid = AUTHOR_LINK.author and \
        AUTHOR_LINK.reference = t.oid " $author2]
    browsedocs .au $query "Browsing Documents by: \n $author2"
}

