# lolist.tcl -- Ray R. Larson (Aug 92)
# Create a "listbox" style display box from the Inversion Large Objects
# for the current database. Takes two arguments.
#   The first arg is the pathname for the new window.
#   The second is a set of arguments for use in creating the message
#     to be displayed in the message box area of the window.


proc LOList {pathname } {

    set w .LO_LIST_[set pathname]
    catch {destroy $w}
    toplevel $w
    dpos $w
    global CURRENTDATABASE

    # Create three frames in the main window. The top frame will hold the
    # message and the bottom one will hold the <next> & <quit> buttons.
    # The middle frame holds the labels and data elements of the
    # instances of the retrieved tuples from the tuplearray. Arrange them
    # one above the other, with any extra vertical space split between
    # them.

    frame $w.top -relief raised -border 1
    frame $w.mid -relief raised -border 1
    frame $w.bot -relief raised -border 1

    pack append $w $w.top {top fill expand} $w.mid {top fill expand} $w.bot {bottom fill}

    # Create the message widget and arrange for it to be centered in the
    # top frame.

    eval message $w.top.msg -justify center -text \"Postgres Large Object List for $pathname \" -font -*-times-medium-r-normal--*-180* -aspect 300


    frame $w.top.lframe -borderwidth 1
    pack append $w.top.lframe \
         [label $w.top.lframe.tu -text "Current database: $CURRENTDATABASE" -relief raised] \
              {top padx 5 pady 4 expand}



    pack append $w.top $w.top.msg {top expand padx 5 pady 5 fillx} \
         $w.top.lframe {top expand padx 2 pady 2 fillx}


    #
    # Create a scrolling listbox in the middle frame
    #
    pack append $w.mid \
	[scrollbar $w.mid.scroll -relief sunken \
	    -command "$w.mid.list yview"] {right expand filly frame w} \
	[listbox $w.mid.list -geometry 34x10 -font -*-courier-*-r-*-120-* -yscroll "$w.mid.scroll set" -relief sunken] \
	     {left expand filly }


    bind $w.mid.list <Double-1> "LOSelect $pathname"

    #
    # Create the quit button in the bottom frame.
    #
    button $w.bot.ok -text QUIT \
        -command "destroy $w" 
    pack append $w.bot $w.bot.ok {bottom fill}


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

    #
    # Load the tuples into the listbox
    #
    $w.mid.list insert end "."
    $w.mid.list insert end ".."
    foreach i [lsort [PQlonames $pathname]] {
           set fmt [format "%-20s %8s %s" [lindex $i 0] [lindex $i 1] [lindex $i 2]]
           $w.mid.list insert end "$fmt"
    }


    destroy .waitwin
    $w config -cursor top_left_arrow
    update


}






proc LOSelect {pathname} {
   set pname $pathname
   set item [join [selection.get]]
   set loname [lindex $item 0]
   set lomode [lindex $item 2]
   set losize [lindex $item 1]
   if {[string compare $pname "/"]!=0} then {
       append pname /$loname
   } else {
       set pname /$loname
   }

   if {[string compare $lomode "<DIR>"]==0} then {
      LOList $pname
   } else {
      LObrowse "Text Document Browse" "[LOCopy $pname]"
   }
}

# LObrowse -- browse an inversion text file.
global LO_WIN_NUMBER
set LO_WIN_NUMBER 0

proc LObrowse {title Filename} {

        global LO_WIN_NUMBER
	set title2 "lo_browse_window$LO_WIN_NUMBER"
	incr LO_WIN_NUMBER

	catch {destroy .$title2}
	toplevel .$title2
	wm title .$title2 "$title"
        dpos .$title2

	frame .$title2.f1
	scrollbar .$title2.f1.scroll -command ".$title2.f1.list yview" -relief raised
	listbox .$title2.f1.list -yscroll ".$title2.f1.scroll set" \
			            -borderwidth 1 -relief raised -geometry 80x20
	pack append .$title2.f1 .$title2.f1.list {left expand fill}\
                                   .$title2.f1.scroll {left filly}


	frame .$title2.f2 -borderwidth {2}  -relief {raised}

	entry .$title2.f2.ent -relief {raised}  -width {50} 

	button .$title2.f2.srch -relief {raised}  -text "Scan for :" -padx 20 \
	   -command "dosearchlist $title2" 

	button .$title2.f2.ok -text "OK" -relief raised	 -padx 40 \
           -command "destroy .$title2" 

	pack append .$title2.f2   .$title2.f2.srch {left frame center} \
		 .$title2.f2.ent {left frame center} \
		 .$title2.f2.ok {left fill}


	pack append .$title2 .$title2.f1 {top}  .$title2.f2 {top fillx}


        .$title2 config -cursor watch
	update
        WaitWindow {retrieving text data from POSTGRES}
        update

	# read in the contents of the $Filename document file
	#-------------------------------------------------------------

	readFileIntoListBox $Filename .$title2.f1.list

        destroy .waitwin
        .$title2 config -cursor top_left_arrow
        update

}

proc LOCopy {invpath} {

    global TEMPFILENUM
    global TEMPFILES
    global TEMPFILEPATH

    set comp_ext ".Z"    

    if {[catch "set TEMPFILES($invpath)" tmpf]==0} {
	return $tmpf
    }
    if {[file exists $invpath]} {
	return $invpath
    }
    # no actual file with that name, so copy it out of inversion
    if {[string match ".Z" [file extension $invpath]]} {
	incr TEMPFILENUM
	set newfile $TEMPFILEPATH$TEMPFILENUM$comp_ext
    } else {
	incr TEMPFILENUM
	set newfile $TEMPFILEPATH$TEMPFILENUM
    }
    PQlocopy $invpath $newfile
    set TEMPFILES($invpath) $newfile
    return $newfile
   
}

proc LO_uncompress {compfile} {
    global TEMPFILES TEMPFILEPATH TEMPFILENUM

    if {[string match ".Z" [file extension $compfile]]} {
	# it's a compressed file, so move and uncompress it
	if {[regexp ^$TEMPFILEPATH\[0-9\]*  $compfile]} { #in temp area
	    exec uncompress $compfile
	    return [file rootname $compfile]
	} else { #not in temp area
	    incr TEMPFILENUM
	    set newfile $TEMPFILEPATH$TEMPFILENUM
	    exec zcat $compfile > $newfile
	    set TEMPFILES($compfile) $newfile
	    return $newfile
	}
    }
    #not compressed (or at least not .Z compressed)
    return $compfile
}

proc dosearchlist {win} {
	set pat [.$win.f2.ent get]
        set start [.$win.f1.list nearest 1]

#        puts stdout $pat
#        puts stdout $start

        .$win config -cursor watch
        update

        searchlist .$win.f1.list "$pat" $start

        .$win config -cursor top_left_arrow
        update


}
