# newlist -- Next-style listbox browser   Sep. 92  Ray R. Larson
# Create a "listbox" style display box from the Inversion Large Objects
# for the current database.
#   The first arg is the inversion pathname to browse
global MAXLISTBOXES
set MAXLISTBOXES 3


proc Newlist {pathname } {

    set w .lo_LIST
    catch {destroy $w}
    toplevel $w
    wm geometry $w +50+50

    global MAXLISTBOXES
    global CURRENTDATABASE
    global INVPATHNAME
    set INVPATHNAME $pathname
    global NLPATH
    set NumNewList 1

    # Create three frames in the main window. The top frame will hold the
    # message and the bottom one will hold the <quit> button.
    # The middle frame holds the list boxes showing inversion file
    # names.

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

    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 Browser\" -font -*-times-medium-r-normal--*-180*  -aspect 1000

    frame $w.top.lframe
    pack append $w.top.lframe \
         [label $w.top.lframe.lab1 -text "Current database:" -relief sunken] \
              {left padx 5 pady 4 expand frame e } \
         [label $w.top.lframe.lab2 -textvar CURRENTDATABASE -relief sunken] \
              {left padx 5 pady 4 expand frame w } \
         [label $w.top.lframe.lab3 -text "Inversion Path:" -relief sunken] \
              {left padx 5 pady 4 expand frame e } \
         [label $w.top.lframe.lab4 -textvar INVPATHNAME -relief sunken] \
              {left padx 5 pady 4 expand frame w }

    pack append $w.top $w.top.msg {top expand fillx} \
         $w.top.lframe {top fillx}


    #
    # Create a scrolling listbox in the middle frame
    #

    set NLPATH(root) $w
    for {set i 1} {$i <= $MAXLISTBOXES} { incr i } {
	    set NLPATH($i) [mknewlist $w.mid $i]
    }

    #
    # Create the quit button in the bottom frame.
    #
    button $w.bot.shiftright -text "    <<=== Move    " \
        -command "new_list_shift_right" 
    button $w.bot.ok -text QUIT  -pady 5 \
        -command "unset NLPATH; destroy $w" 
    button $w.bot.shiftleft -text "    Move ===>>     " \
        -command "new_list_shift_left" 
    pack append $w.bot $w.bot.shiftright {left filly} \
                $w.bot.ok {left expand fillx filly} \
                $w.bot.shiftleft {left filly}

    $w config -cursor watch

    loadNewlist $NLPATH(1)
    set NLPATH(IPATH1) $pathname
    set NLPATH(SEL1) $pathname
    set NLPATH(VIRT1) $pathname
    set NLPATH(VIRTFULL) 1
    set NLPATH(VISABLEROOT) 1 

    $w config -cursor top_left_arrow
    update


}

proc loadNewlist { w } {
    global INVPATHNAME
    #
    # Load the tuples into the listbox
    #
    $w delete 0.0 end
    set lineno 0

    foreach i [lsort [PQlonames $INVPATHNAME]] {
           if {[string compare [lindex $i 2] "<DIR>"]==0} then {
              set dirstring ">>"
           } else { set dirstring " "}
           set fmt [format "%-15s %s\n" [lindex $i 0] $dirstring]
           set newtag l[incr lineno]
           set start [$w index insert]
           $w insert insert "$fmt"
           $w tag add $newtag $start [$w index insert]
           $w tag bind $newtag <1> "LOSelect $w $lineno $newtag"
    }
    $w config -state disabled
}

proc LOSelect {lbox lineno atag} {

   global INVPATHNAME
   global MAXLISTBOXES
   global NLPATH

   catch {$lbox tag delete lselect}
   $lbox tag config lselect -relief raised -borderwidth 2 -background LightSkyBlue1 -foreground black
   $lbox tag add lselect $lineno.0 [expr {$lineno + 1}].0

   # put this change up before going out for postgres stuff
   update 

   set item [join [$lbox get $atag.first $atag.last]]
   set loname [lindex $item 0]
   set lomode [lindex $item 1]

   set boxnum $NLPATH($lbox)
   set pname $NLPATH(IPATH$boxnum)
   incr boxnum

   if {[string compare $pname "/"]!=0} then {
       append pname "/"
       append pname $loname
   } else {
       set pname "/$loname"
   }

   if {[string compare $lomode ">>"]==0} then {
      if {$boxnum <= $MAXLISTBOXES} {
        if {$boxnum <= $MAXLISTBOXES} {
           # this is a new directory, so eliminate any previous stuff
           for {set i $boxnum} {$i <= $MAXLISTBOXES} {incr i} {
		    $NLPATH($i) config -state normal
		    $NLPATH($i) delete 1.0 end
                    set NLPATH(SEL$i) "No Selection"
           }
        }
        set INVPATHNAME $pname
        loadNewlist $NLPATH($boxnum)
        set NLPATH(SEL$boxnum) $loname
        set NLPATH(IPATH$boxnum) $INVPATHNAME
        set virtboxnum [expr {$NLPATH(VISABLEROOT) + $boxnum - 1}]
        set NLPATH(VIRT$virtboxnum) $INVPATHNAME
        set NLPATH(VIRTFULL) $virtboxnum

      } else {
	# shift them over
	for {set i 1} {$i <= $MAXLISTBOXES} {incr i} {
		$NLPATH($i) config -state normal
		$NLPATH($i) delete 1.0 end
	}

	set virtboxnum [incr NLPATH(VIRTFULL)]
	set vroot [incr NLPATH(VISABLEROOT)]
        set NLPATH(VIRT$virtboxnum) $pname

	for {set i 1} {$i < $MAXLISTBOXES} {incr i} {
		set n [expr {$i + 1}]
                set INVPATHNAME $NLPATH(IPATH$n)
		loadNewlist $NLPATH($i)
                set NLPATH(SEL$i) $NLPATH(SEL$n)
		set NLPATH(IPATH$i) $INVPATHNAME
	}
#	fill up the new box 
        set INVPATHNAME $pname
        loadNewlist $NLPATH($n)
        set NLPATH(SEL$n) $loname
        set NLPATH(IPATH$n) $INVPATHNAME

      }
   } else {
      # assume a text document, not a directory, and browse it
      LObrowse "Text Document Browse" "[LOCopy $pname]"
   }
}

 proc mknewlist {w num } {
    global NLPATH
    set fr $w.box$num
    # Create a set of new frames in the w window. The top frame will hold a
    # message and the bottom one will hold a text widget and scrollbar

    frame $fr

    # Create the message widget and arrange for it to be centered in the
    # top frame.
    set NLPATH(SEL$num) "No Selection"

    frame $fr.ftop 
    eval message $fr.ftop.sname -justify center -textvar NLPATH(SEL$num) -font -Adobe-Helvetica-Bold-R-Normal-*-120*  -aspect 1000 -relief raised -border 1

     pack append $fr.ftop  $fr.ftop.sname {top expand fillx} 

    
    frame $fr.bot

    #
    # Create a scrolling listbox in the middle frame
    #
    pack append $fr.bot \
	[scrollbar $fr.bot.scroll -relief sunken \
	    -command "$fr.bot.list yview"] {right expand filly frame w} \
	[text $fr.bot.list -height 20 -width 25 -font -*-courier-*-r-*-120-* -yscroll "$fr.bot.scroll set" -relief sunken -wrap none ] \
	     {left expand filly } 
    pack append $fr $fr.ftop {top expand fillx} $fr.bot {top expand fill}

    # add the new frame to the parent
    pack append $w $fr  {left expand filly } 

    # remove bindings for text widgets. $tk_priv(selectMode) holds one of
    # "char", "word", or "line" to indicate which selection mode is active.
    # ----------------------------------------------------------------------

    bind $fr.bot.list <1> {}
    bind $fr.bot.list <Double-1> {}    
    bind $fr.bot.list <Triple-1> {}
    bind $fr.bot.list <B1-Motion> {}
    bind $fr.bot.list <2> {%W scan mark %y}
    bind $fr.bot.list <B2-Motion> {%W scan dragto %y}
    bind $fr.bot.list <Any-KeyPress> {}

    set NLPATH($fr.bot.list) $num
    return $fr.bot.list
}


proc new_list_shift_right {} {
	# contents of listboxes to the right
	global NLPATH INVPATHNAME MAXLISTBOXES

	set vroot $NLPATH(VISABLEROOT)
	# are we able to shift right?
	if {$vroot == 1} return

	for {set i 1} {$i <= $MAXLISTBOXES} {incr i} {
		$NLPATH($i) config -state normal
		$NLPATH($i) delete 1.0 end
	}

        set savepathname $INVPATHNAME
	set virtboxfull $NLPATH(VIRTFULL)

	for {set i $MAXLISTBOXES} {$i > 1} {incr i -1} {
		set n [expr {$i - 1}]
                set INVPATHNAME $NLPATH(IPATH$n)
		loadNewlist $NLPATH($i)
                set NLPATH(SEL$i) $NLPATH(SEL$n)
		set NLPATH(IPATH$i) $INVPATHNAME
	}
#	fill up the new box
	set vroot [expr {$vroot - 1}] 
        set INVPATHNAME $NLPATH(VIRT$vroot)
        loadNewlist $NLPATH(1)
        if {[string compare $INVPATHNAME "/"]==0} then {
		set loname "/"
	} else {
	        set loname [string range $INVPATHNAME [expr {[string last "/" $INVPATHNAME] + 1}] end]
	}
        set NLPATH(SEL1) $loname
        set NLPATH(IPATH1) $INVPATHNAME
        set INVPATHNAME $savepathname 
	set NLPATH(VISABLEROOT) $vroot
}

proc new_list_shift_left {} {
	# contents of listboxes to the right
	global NLPATH INVPATHNAME MAXLISTBOXES

	set vroot $NLPATH(VISABLEROOT)
	# any need to shift left?
	set virtboxfull $NLPATH(VIRTFULL)
	set diff [expr {$virtboxfull - $vroot + 1}]
	if {$diff <= $MAXLISTBOXES } return

	for {set i 1} {$i <= $MAXLISTBOXES} {incr i} {
		$NLPATH($i) config -state normal
		$NLPATH($i) delete 1.0 end
	}

        set savepathname $INVPATHNAME

	for {set i 1} {$i < $MAXLISTBOXES} {incr i} {
		set n [expr {$i + 1}]
                set INVPATHNAME $NLPATH(IPATH$n)
		loadNewlist $NLPATH($i)
                set NLPATH(SEL$i) $NLPATH(SEL$n)
		set NLPATH(IPATH$i) $INVPATHNAME
	}
#	fill up the new box
	set NLPATH(VISABLEROOT) [incr vroot]
	set n  [expr {$vroot + $MAXLISTBOXES - 1}]
        set INVPATHNAME $NLPATH(VIRT$n)
        loadNewlist $NLPATH($MAXLISTBOXES)
        set loname [string range $INVPATHNAME [expr {[string last "/" $INVPATHNAME] + 1}] end]
        set NLPATH(SEL$MAXLISTBOXES) $loname
        set NLPATH(IPATH$MAXLISTBOXES) $INVPATHNAME
        set INVPATHNAME $savepathname 
}





