#!/usr/bin/wishx
#Test multiple listboxes for dbp implementation.  Delete this when mlb.tcl done
#TODO:
#X- fix crazy loop in yscroll
#- integrate into dbp
#- 

image create bitmap but -data "#define dot_width 7\n#define dot_height 7\nstatic unsigned char dot_bits[] = {\n0x08, 0x14, 0x2a, 0x55, 0x2a, 0x14, 0x08};"
#set is {a b c d e f g h i j k l m n o p q r s t u v w x y z}
set is {a b c d e f g h i j k l}

#Anytime the widget main frame is configured, reconfigure the inner component sizes
#--------------------------------
proc p_config {w h} {
#puts "P Configure w:$w h:$h width:[winfo width .p] height:[winfo height .p]"
    grid propagate .p no
    set pw [expr $w - [winfo width  .p.ys]]
    set ph [expr $h - [winfo height .p.xs]]

    set fw 0    
    foreach i $::is {incr fw [winfo width .p.c.f.c$i]
#FIXME: need to use cfig widths (unmapped frames read width of 1)
#puts "$i:[winfo width .p.c.f.c$i]"
    }
#puts "pw:$pw ph:$ph fw:$fw"
    set mw [max $pw $fw]
    set mwp [expr $mw + 50]

    .p.c.f configure -height $ph -width $mw
    .p.c configure -width $pw -height $ph -scrollregion "0 0 $mw $ph"

#    set pcfh [expr [winfo height .p.c.f] - [winfo height .p.c.f.ca.t] - [winfo height .p.c.f.ca.s] - [winfo height .p.c.f.ca.xs]]
#puts "pcfh:$pcfh"
    foreach i $::is {.p.c.f.c$i configure -height 100}
}

#When the sizer button is being dragged
#------------------------------------------
proc motion {w z pos} {
    variable val
#puts "motion:$w:$z:$pos:"

#puts "motion pos:$pos sizea:$val(sizea$w) sizeb:$val(sizeb$w)"
    set delta [expr $pos - $val(start$w)]
    set neww [expr $val(owid$w) + $delta]
    if {$neww < 0} return
#puts "$w configure -width $neww"
    $w configure -width $neww
}

#When the sizer button is pressed
#------------------------------------------
proc press {w z pos} {
    variable val
    set bw [winfo width $w]
#puts "press:$w:$z:$bw:$pos:"
    set val(start$w) $pos
    set val(owid$w) [winfo width $w]
    bind $z <Any-Motion> "motion $w $z %X"
    $z configure -relief sunken		;#press the button
}

#When the button is released
#------------------------------------------
proc release {w z} {
#puts release
    bind $z <Any-Motion> {}
    $z configure -relief raised		;#unpress the button
}

set master .p.c.f.c[lindex $::is 0].b

#When the scroll bar is operated, trigger all the listboxes
#--------------------------------
proc yview {args} {
#puts "yview args:$args"
    eval $::master yview $args
}

#When one of the listboxes is moved by hand, adjust the others and the scroll bar
#--------------------------------
proc yscroll {w j x1 x2} {
    global master
#puts "yscroll w:$w j:$j master:$master x1:$x1 x2:$x2"
    if {$w != $master} return
    .p.ys set $x1 $x2

    foreach x $::is {
        if {".p.c.f.c$x.b" != "$master"} {.p.c.f.c$x.b yview moveto $x1}
    }
}

frame .p 	;#-bg pink		;#holds the whole megawidget
pack .p -side top -fill both -exp 1
bind .p <Configure> "p_config %w %h"

canvas .p.c -bg white -xscrollc ".p.xs set" -width 400 -height 300 -scrollregion {0 0 1000 300}
scrollbar .p.xs -orient h -width 10 -command ".p.c xview"
scrollbar .p.ys -orient v -width 10 -command "yview"
button .p.b -image but
grid .p.c .p.ys -row 0 -sticky news
grid .p.xs .p.b -row 1 -sticky news

frame .p.c.f		;#-bg yellow
pack propagate .p.c.f no
#bind .p.c.f <Configure> "pcf_config"
.p.c create window 0 0 -window .p.c.f -anchor nw

foreach i $::is {
    frame .p.c.f.c$i -width 80
    frame .p.c.f.z$i -width 2 -bd 1 -relief raised -bg CadetBlue -cursor sb_h_double_arrow
    bind .p.c.f.z$i <Enter>		".p.c.f.z$i configure -bg DarkBlue"
    bind .p.c.f.z$i <Leave>		".p.c.f.z$i configure -bg CadetBlue"
    bind .p.c.f.z$i <Button-1>		"press .p.c.f.c$i .p.c.f.z$i %X"
    bind .p.c.f.z$i <ButtonRelease-1>	"release .p.c.f.c$i .p.c.f.z$i"
    bind .p.c.f.z$i <B1-Leave>		{}
    bind .p.c.f.z$i <Unmap>		"release .p.c.f.c$i .p.c.f.z$i"

    label .p.c.f.c$i.t -text "Title $i" -anchor w -bd 0 -relief raised
    listbox .p.c.f.c$i.b -font fixed -height 0 -bg grey94 -width 20 -bd 0 -highlightth 0 -relief sunken -xscrollc ".p.c.f.c$i.xs set" -yscrollc "yscroll .p.c.f.c$i.b $i"
    menubutton .p.c.f.c$i.s -text "Summary $i" -anchor w -relief sunken -bd 1 -menu .p.c.f.c$i.s.m
    menu .p.c.f.c$i.s.m
    scrollbar .p.c.f.c$i.xs -orient h -width 5 -bd 1 -command ".p.c.f.c$i.b xview"

    bind .p.c.f.c$i.b <Button-2>	"set ::master %W"
    bind .p.c.f.c$i.t <Button-2>	{.p.c scan mark [winfo pointerx .p.c] 0}
    bind .p.c.f.c$i.t <B2-Motion>	{.p.c scan dragto [winfo pointerx .p.c] 0}
    bind .p.c.f.c$i.s <Button-2>	{.p.c scan mark [winfo pointerx .p.c] 0}
    bind .p.c.f.c$i.s <B2-Motion>	{.p.c scan dragto [winfo pointerx .p.c] 0}

    pack .p.c.f.c$i.t -side top -fill x
    pack .p.c.f.c$i.xs -side bottom -fill x
    pack .p.c.f.c$i.s -side bottom -fill x
    pack .p.c.f.c$i.b -side top -exp 1 -fill both
    
    pack .p.c.f.c$i -side left -fill both -exp 0
    pack .p.c.f.z$i -side left -fill y
    pack propagate .p.c.f.c$i no
}

foreach ln [read_file "/usr/share/dict/words"] {
    set first [string range $ln 0 0]
    if {[lcontain $::is $first]} {
        .p.c.f.c$first.b insert end $ln
    }
#puts $ln
}

set max 0
foreach i $::is {
    set last($i) [.p.c.f.c$i.b size]
    if {$last($i) > $max} {set max $last($i)}
}

foreach i $::is {
    for {set j $last($i)} {$j < $max} {incr j} {
        .p.c.f.c$i.b insert end $j
    }
}

.p.c.f.ca.b itemconfigure 3 -bg red
.p.c.f.cb.b itemconfigure 3 -bg white
.p.c.f.cc.b itemconfigure 3 -bg blue
