-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmclistbox.tcl
executable file
·3567 lines (3091 loc) · 104 KB
/
mclistbox.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 1999, Bryan Oakley
# All Rights Reservered
#
# Bryan Oakley
#
# mclistbox v1.02 March 30, 1999
#
# a multicolumn listbox written in pure tcl
#
# this code is freely distributable without restriction, but is
# provided as-is with no waranty expressed or implied.
#
# basic usage:
#
# mclistbox::mclistbox .listbox
# .listbox column add col1 -label "Column 1"
# .listbox column add col2 -label "Column 2"
# .listbox insert end [list "some stuff" "some more stuff"]
# .listbox insert end [list "a second row of stuff" "blah blah blah"]
#
# see the documentation for more, uh, documentation.
#
# Something to think about: implement a "-optimize" option, with two
# values: speed and memory. If set to speed, keep a copy of the data
# in our hidden listbox so retrieval of data doesn't require us to
# do all the getting and splitting and so forth. If set to "memory",
# bag saving a duplicate copy of the data, which means data retrieval
# will be slower, but memory requirements will be reduced.
package require Tk 8.3
package provide mclistbox 1.02
namespace eval ::mclistbox {
variable bwidget 0
if { ![catch {package require BWidget}] } {
set bwidget 1
}
# this is the public interface
namespace export mclistbox
# these contain references to available options
variable widgetOptions
variable columnOptions
# these contain references to available commands and subcommands
variable widgetCommands
variable columnCommands
variable labelCommands
}
# ::mclistbox::Init --
#
# Initialize the global (well, namespace) variables. This should
# only be called once, immediately prior to creating the first
# instance of the widget
#
# Results:
#
# All state variables are set to their default values; all of
# the option database entries will exist.
#
# Returns:
#
# empty string
proc ::mclistbox::Init {} {
variable widgetOptions
variable columnOptions
variable widgetCommands
variable columnCommands
variable labelCommands
# here we match up command line options with option database names
# and classes. As it turns out, this is a handy reference of all of the
# available options. Note that if an item has a value with only one
# item (like -bd, for example) it is a synonym and the value is the
# actual item.
array set widgetOptions [list \
-background {background Background} \
-bd -borderwidth \
-bg -background \
-borderwidth {borderWidth BorderWidth} \
-columnbd -columnborderwidth \
-columnborderwidth {columnBorderWidth BorderWidth} \
-columnrelief {columnRelief Relief} \
-cursor {cursor Cursor} \
-exportselection {exportSelection ExportSelection} \
-fg -foreground \
-fillcolumn {fillColumn FillColumn} \
-font {font Font} \
-foreground {foreground Foreground} \
-height {height Height} \
-highlightbackground {highlightBackground HighlightBackground} \
-highlightcolor {highlightColor HighlightColor} \
-highlightthickness {highlightThickness HighlightThickness} \
-labelanchor {labelAnchor Anchor} \
-labelbackground {labelBackground Background} \
-labelbd -labelborderwidth \
-labelbg -labelbackground \
-labelborderwidth {labelBorderWidth BorderWidth} \
-labelfg -labelforeground \
-labelfont {labelFont Font} \
-labelforeground {labelForeground Foreground} \
-labelheight {labelHeight Height} \
-labelimage {labelImage Image} \
-labelrelief {labelRelief Relief} \
-labels {labels Labels} \
-relief {relief Relief} \
-resizablecolumns {resizableColumns ResizableColumns} \
-selectbackground {selectBackground Foreground} \
-selectborderwidth {selectBorderWidth BorderWidth} \
-selectcommand {selectCommand Command} \
-selectforeground {selectForeground Background} \
-selectmode {selectMode SelectMode} \
-setgrid {setGrid SetGrid} \
-state {state State} \
-takefocus {takeFocus TakeFocus} \
-width {width Width} \
-xscrollcommand {xScrollCommand ScrollCommand} \
-yscrollcommand {yScrollCommand ScrollCommand} \
-iseditableindexcommand \
{isEditableIndexCommand IsEditableIndexCommand} \
]
# If we have bwidget, add the bwidget drag-and-drop stuff.
if { $::mclistbox::bwidget } {
array set widgetOptions [list \
-dragendcmd {dragEndCmd DragEndCmd} \
-dragenabled {dragEnabled DragEnabled} \
-draginitcmd {dragInitCmd DragInitCmd} \
-dragtype {dragType DragType} \
-dropcmd {dropCmd DropCmd} \
-dropenabled {dropEnabled DropEnabled} \
-dropovercmd {dropOverCmd DropOverCmd} \
-droptypes {dropTypes DropTypes} \
-dropcursor {dropCursor DropCursor} \
]
}
# and likewise for column-specific stuff.
array set columnOptions [list \
-background {background Background} \
-bitmap {bitmap Bitmap} \
-font {font Font} \
-foreground {foreground Foreground} \
-image {image Image} \
-label {label Label} \
-position {position Position} \
-resizable {resizable Resizable} \
-visible {visible Visible} \
-width {width Width} \
-editable {editable Editable} \
-editcommand {editcommand Editcommand} \
-listvar {listvar Listvar} \
]
# this defines the valid widget commands. It's important to
# list them here; we use this list to validate commands and
# expand abbreviations.
set widgetCommands [list \
activate bbox cget column configure \
curselection delete edit editcombo get index \
insert label nearest scan see \
selection size xview yview
]
set columnCommands [list add cget configure delete names nearest x]
set labelCommands [list bind]
######################################################################
#- this initializes the option database. Kinda gross, but it works
#- (I think).
######################################################################
set packages [package names]
# why check for the Tk package? This lets us be sourced into
# an interpreter that doesn't have Tk loaded, such as the slave
# interpreter used by pkg_mkIndex. In theory it should have no
# side effects when run
if {[lsearch -exact [package names] "Tk"] != -1} {
# compute a widget name we can use to create a temporary widget
set tmpWidget ".__tmp__"
set count 0
while {[winfo exists $tmpWidget] == 1} {
set tmpWidget ".__tmp__$count"
incr count
}
# steal options from the listbox
# we want darn near all options, so we'll go ahead and do
# them all. No harm done in adding the one or two that we
# don't use.
listbox $tmpWidget
foreach foo [$tmpWidget configure] {
if {[llength $foo] == 5} {
set option [lindex $foo 1]
set value [lindex $foo 4]
option add *Mclistbox.$option $value widgetDefault
# these options also apply to the individual columns...
if {[string compare $option "foreground"] == 0 \
|| [string compare $option "background"] == 0 \
|| [string compare $option "font"] == 0} {
option add *Mclistbox*MclistboxColumn.$option $value \
widgetDefault
}
}
}
destroy $tmpWidget
# steal some options from label widgets; we only want a subset
# so we'll use a slightly different method. No harm in *not*
# adding in the one or two that we don't use... :-)
label $tmpWidget
foreach option [list Anchor Background Font \
Foreground Height Image ] {
set values [$tmpWidget configure -[string tolower $option]]
option add *Mclistbox.label$option [lindex $values 3]
}
destroy $tmpWidget
# these are unique to us...
option add *Mclistbox.columnBorderWidth 0 widgetDefault
option add *Mclistbox.columnRelief flat widgetDefault
option add *Mclistbox.labelBorderWidth 1 widgetDefault
option add *Mclistbox.labelRelief raised widgetDefault
option add *Mclistbox.labels 1 widgetDefault
option add *Mclistbox.resizableColumns 1 widgetDefault
option add *Mclistbox.selectcommand {} widgetDefault
option add *Mclistbox.fillcolumn {} widgetDefault
option add *Mclistbox.iseditableindexcommand {} widgetDefault
option add *Mclistbox.state normal widgetDefault
# Bwidget stuff
option add *Mclistbox.dragEndCmd {} widgetDefault
option add *Mclistbox.dragEnabled false widgetDefault
option add *Mclistbox.dragInitCmd {} widgetDefault
option add *Mclistbox.dragType {LISTBOX_ITEM} widgetDefault
option add *Mclistbox.dropCmd {} widgetDefault
option add *Mclistbox.dropEnabled false widgetDefault
option add *Mclistbox.dropOverCmd {} widgetDefault
option add *Mclistbox.dropTypes \
{LISTBOX_ITEM {copy {} move {}}} widgetDefault
option add *Mclistbox.dropCursor before widgetDefault
# column options
option add *Mclistbox*MclistboxColumn.visible 1 widgetDefault
option add *Mclistbox*MclistboxColumn.resizable 1 widgetDefault
option add *Mclistbox*MclistboxColumn.position end widgetDefault
option add *Mclistbox*MclistboxColumn.label "" widgetDefault
option add *Mclistbox*MclistboxColumn.width 0 widgetDefault
option add *Mclistbox*MclistboxColumn.bitmap "" widgetDefault
option add *Mclistbox*MclistboxColumn.image "" widgetDefault
option add *Mclistbox*MclistboxColumn.editable \
0 widgetDefault
option add *Mclistbox*MclistboxColumn.editcommand \
"" widgetDefault
}
######################################################################
# define the class bindings
######################################################################
SetClassBindings
}
# ::mclistbox::mclistbox --
#
# This is the command that gets exported. It creates a new
# mclistbox widget.
#
# Arguments:
#
# w path of new widget to create
# args additional option/value pairs (eg: -background white, etc.)
#
# Results:
#
# It creates the widget and sets up all of the default bindings
#
# Returns:
#
# The name of the newly create widget
proc ::mclistbox::mclistbox {args} {
variable widgetOptions
# perform a one time initialization
if {![info exists widgetOptions]} {
Init
}
# make sure we at least have a widget name
if {[llength $args] == 0} {
error "wrong # args: should be \"mclistbox pathName ?options?\""
}
# ... and make sure a widget doesn't already exist by that name
if {[winfo exists [lindex $args 0]]} {
error "window name \"[lindex $args 0]\" already exists"
}
# and check that all of the args are valid
foreach {name value} [lrange $args 1 end] {
Canonize [lindex $args 0] option $name
}
# build it...
set w [eval Build $args]
# set some bindings...
SetBindings $w
# and we are done!
return $w
}
# ::mclistbox::Build --
#
# This does all of the work necessary to create the basic
# mclistbox.
#
# Arguments:
#
# w widget name
# args additional option/value pairs
#
# Results:
#
# Creates a new widget with the given name. Also creates a new
# namespace patterened after the widget name, as a child namespace
# to ::mclistbox
#
# Returns:
#
# the name of the widget
proc ::mclistbox::Build {w args} {
variable widgetOptions
# create the namespace for this instance, and define a few
# variables
namespace eval ::mclistbox::$w {
variable options
variable widgets
variable misc
}
# this gives us access to the namespace variables within
# this proc
upvar ::mclistbox::${w}::widgets widgets
upvar ::mclistbox::${w}::options options
upvar ::mclistbox::${w}::misc misc
# initially we start out with no columns
set misc(columns) {}
# this is our widget -- a frame of class Mclistbox. Naturally,
# it will contain other widgets. We create it here because
# we need it to be able to set our default options.
set widgets(this) [frame $w -class Mclistbox -takefocus 1]
# this defines all of the default options. We get the
# values from the option database. Note that if an array
# value is a list of length one it is an alias to another
# option, so we just ignore it
foreach name [array names widgetOptions] {
if {[llength $widgetOptions($name)] == 1} continue
set optName [lindex $widgetOptions($name) 0]
set optClass [lindex $widgetOptions($name) 1]
set options($name) [option get $w $optName $optClass]
}
# now apply any of the options supplied on the command
# line. This may overwrite our defaults, which is OK
if {[llength $args] > 0} {
array set options $args
}
# the columns all go into a text widget since it has the
# ability to scroll.
set widgets(text) [text $w.text \
-width 0 \
-height 0 \
-padx 0 \
-pady 0 \
-wrap none \
-borderwidth 0 \
-highlightthickness 0 \
-takefocus 0 \
-cursor {} \
]
$widgets(text) configure -state disabled
# here's the tricky part (shhhh... don't tell anybody!)
# we are going to create column that completely fills
# the base frame. We will use it to control the sizing
# of the widget. The trick is, we'll pack it in the frame
# and then place the text widget over it so it is never
# seen.
set columnWidgets [NewColumn $w {__hidden__} true]
set widgets(hiddenFrame) [lindex $columnWidgets 0]
set widgets(hiddenListbox) [lindex $columnWidgets 1]
set widgets(hiddenLabel) [lindex $columnWidgets 2]
# by default geometry propagation is turned off, but for this
# super-secret widget we want it turned on. The idea is, we
# resize the listbox which resizes the frame which resizes the
# whole shibang.
pack propagate $widgets(hiddenFrame) on
pack $widgets(hiddenFrame) -side top -fill both -expand y
place $widgets(text) -x 0 -y 0 -relwidth 1.0 -relheight 1.0
raise $widgets(text)
# we will later rename the frame's widget proc to be our
# own custom widget proc. We need to keep track of this
# new name, so we'll define and store it here...
set widgets(frame) ::mclistbox::${w}::$w
# this moves the original frame widget proc into our
# namespace and gives it a handy name
rename ::$w $widgets(frame)
# now, create our widget proc. Obviously (?) it goes in
# the global namespace. All mclistbox widgets will actually
# share the same widget proc to cut down on the amount of
# bloat.
proc ::$w {command args} \
"eval ::mclistbox::WidgetProc {$w} \$command \$args"
# ok, the thing exists... let's do a bit more configuration.
if {[catch "Configure $widgets(this) [array get options]" res]} {
catch {destroy $w}
return -code error $res
}
# and be prepared to handle selections.. (this, for -exportselection
# support)
selection handle $w [list ::mclistbox::SelectionHandler $w get]
return $w
}
# ::mclistbox::SelectionHandler --
#
# handle reqests to set or retrieve the primary selection. This is
# the "guts" of the implementation of the -exportselection option.
# What a pain! Note that this command is *not* called as a result
# of the widget's "selection" command, but rather as a result of
# the global selection being set or cleared.
#
# If I read the ICCCM correctly (which is doubtful; who has time to
# read that thing thoroughly?), this should return each row as a tab
# separated list of values, and the whole as a newline separated
# list of rows.
#
# Arguments:
#
# w pathname of the widget
# type one of "own", "lose" or "get"
# offset only used if type is "get"; offset into the selection
# buffer where the returned data should begin
# length number of bytes to return
#
proc ::mclistbox::SelectionHandler {w type {offset ""} {length ""}} {
upvar ::mclistbox::${w}::options options
upvar ::mclistbox::${w}::misc misc
upvar ::mclistbox::${w}::widgets widgets
switch -exact $type {
own {
selection own \
-command [list ::mclistbox::SelectionHandler $w lose] \
-selection PRIMARY \
$w
}
lose {
if {$options(-exportselection)} {
foreach id $misc(columns) {
$widgets(listbox$id) selection clear 0 end
}
}
}
get {
set end [expr {$length + $offset - 1}]
set column [lindex $misc(columns) 0]
set curselection [$widgets(listbox$column) curselection]
# this is really, really slow (relatively speaking).
# but the only way I can think of to speed this up
# is to duplicate all the data in our hidden listbox,
# which I really don't want to do because of memory
# considerations.
set data ""
foreach index $curselection {
set rowdata [join [::mclistbox::WidgetProc-get $w $index] "\t"]
lappend data $rowdata
}
set data [join $data "\n"]
return [string range $data $offset $end]
}
}
}
# ::mclistbox::convert --
#
# public routine to convert %x, %y and %W binding substitutions.
# Given an x, y and or %W value relative to a given widget, this
# routine will convert the values to be relative to the mclistbox
# widget. For example, it could be used in a binding like this:
#
# bind .mclistbox <blah> {doSomething [::mclistbox::convert %W -x %x]}
#
# Note that this procedure is *not* exported, but is indented for
# public use. It is not exported because the name could easily
# clash with existing commands.
#
# Arguments:
#
# w a widget path; typically the actual result of a %W
# substitution in a binding. It should be either a
# mclistbox widget or one of its subwidgets
#
# args should one or more of the following arguments or
# pairs of arguments:
#
# -x <x> will convert the value <x>; typically <x> will
# be the result of a %x substitution
# -y <y> will convert the value <y>; typically <y> will
# be the result of a %y substitution
# -W (or -w) will return the name of the mclistbox widget
# which is the parent of $w
#
# Returns:
#
# a list of the requested values. For example, a single -w will
# result in a list of one items, the name of the mclistbox widget.
# Supplying "-x 10 -y 20 -W" (in any order) will return a list of
# three values: the converted x and y values, and the name of
# the mclistbox widget.
proc ::mclistbox::convert {w args} {
set result {}
if {![winfo exists $w]} {
error "window \"$w\" doesn't exist"
}
while {[llength $args] > 0} {
set option [lindex $args 0]
set args [lrange $args 1 end]
switch -exact -- $option {
-x {
set value [lindex $args 0]
set args [lrange $args 1 end]
set win $w
while {[winfo class $win] != "Mclistbox"} {
incr value [winfo x $win]
set win [winfo parent $win]
if {$win == "."} break
}
lappend result $value
}
-y {
set value [lindex $args 0]
set args [lrange $args 1 end]
set win $w
while {[winfo class $win] != "Mclistbox"} {
incr value [winfo y $win]
set win [winfo parent $win]
if {$win == "."} break
}
lappend result $value
}
-w -
-W {
set win $w
while {[winfo class $win] != "Mclistbox"} {
set win [winfo parent $win]
if {$win == "."} break;
}
lappend result $win
}
}
}
return $result
}
# ::mclistbox::SetBindings --
#
# Sets up the default bindings for the named widget
#
# Arguments:
#
# w the widget pathname for which the bindings should be assigned
#
# Results:
#
# The named widget will inheirit all of the default Mclistbox
# bindings.
proc ::mclistbox::SetBindings {w} {
upvar ::mclistbox::${w}::widgets widgets
upvar ::mclistbox::${w}::options options
upvar ::mclistbox::${w}::misc misc
# we must do this so that the columns fill the text widget in
# the y direction
bind $widgets(text) <Configure> \
[list ::mclistbox::AdjustColumns $w %h]
}
# ::mclistbox::SetClassBindings --
#
# Sets up the default bindings for the widget class
#
# Arguments:
#
# none
#
proc ::mclistbox::SetClassBindings {} {
# this allows us to clean up some things when we go away
bind Mclistbox <Destroy> [list ::mclistbox::DestroyHandler %W]
# steal all of the standard listbox bindings. Note that if a user
# clicks in a column, %W will return that column. This is bad,
# so we have to make a substitution in all of the bindings to
# compute the real widget name (ie: the name of the topmost
# frame)
foreach event [bind Listbox] {
set binding [bind Listbox $event]
regsub -all {%W} $binding {[::mclistbox::convert %W -W]} binding
regsub -all {%x} $binding {[::mclistbox::convert %W -x %x]} binding
regsub -all {%y} $binding {[::mclistbox::convert %W -y %y]} binding
bind Mclistbox $event $binding
}
# Add a button press binding to track selection for drag and drop
bind Mclistbox <ButtonPress-1> [list +::mclistbox::SetDragIndex %W %Y]
# these define bindings for the column labels for resizing. Note
# that we need both the name of this widget (calculated by $this)
# as well as the specific widget that the event occured over.
# Also note that $this is a constant string that gets evaluated
# when the binding fires.
# What a pain.
set this {[::mclistbox::convert %W -W]}
bind MclistboxMouseBindings <ButtonPress-1> \
"::mclistbox::ResizeEvent $this buttonpress %W %x %X %Y"
bind MclistboxMouseBindings <ButtonRelease-1> \
"::mclistbox::ResizeEvent $this buttonrelease %W %x %X %Y"
bind MclistboxMouseBindings <Enter> \
"::mclistbox::ResizeEvent $this motion %W %x %X %Y"
bind MclistboxMouseBindings <Motion> \
"::mclistbox::ResizeEvent $this motion %W %x %X %Y"
bind MclistboxMouseBindings <B1-Motion> \
"::mclistbox::ResizeEvent $this drag %W %x %X %Y"
}
# ::mclistbox::SetDragIndex --
#
# Store the drag index when the button is first clicked so that the right
# item is dragged and dropped.
#
# Arguments:
#
# path The path to the widget where the drag is started (%W from
# bind).
# Y The %Y from bind.
#
# Results:
#
# Calculates the index of the row that was clicked in case it the item is
# dragged.
#
proc ::mclistbox::SetDragIndex {path Y} {
set index [$path nearest [expr {$Y - [winfo rooty $path]}]]
set path [::mclistbox::convert $path -W]
upvar ::mclistbox::${path}::options options
set options(dragIndex) $index
return
}
# ::mclistbox::NewColumn --
#
# Adds a new column to the mclistbox widget
#
# Arguments:
#
# w the widget pathname
# id the id for the new column
# hidden boolean indicating if this is a hidden column (HACK! YUCK!)
#
# Results:
#
# Creates a set of widgets which defines the column. Adds
# appropriate entries to the global array widgets for the
# new column.
#
# Note that this column is not added to the listbox by
# this proc.
#
# Returns:
#
# A list of four elements: the path to the column frame,
# the path to the column listbox, the path to the column
# label, and the path to the editbutton, in that order.
proc ::mclistbox::NewColumn {w id {hidden false}} {
upvar ::mclistbox::${w}::widgets widgets
upvar ::mclistbox::${w}::options options
upvar ::mclistbox::${w}::misc misc
upvar ::mclistbox::${w}::columnID columnID
# the columns are all children of the text widget we created...
if { $hidden } {
set parent $w
} else {
set parent $w.text
}
set frame \
[frame $parent.frame$id \
-takefocus 0 \
-highlightthickness 0 \
-class MclistboxColumn \
-background $options(-background) \
]
set label \
[label $frame.label \
-takefocus 0 \
-relief raised \
-bd 1 \
-highlightthickness 0 \
-state $options(-state) \
]
set listbox \
[listbox $frame.listbox \
-takefocus 0 \
-bd 0 \
-setgrid $options(-setgrid) \
-exportselection false \
-selectmode $options(-selectmode) \
-highlightthickness 0 \
-yscrollcommand [list ::mclistbox::InvalidateScrollbars $w] \
]
set button \
[button $frame.editbutton \
-bd 1 \
-padx 0 \
-pady 0 \
-text "Edit" \
-command [list ::mclistbox::_editButtonCommand $w $id] \
-state disabled
]
# Pick up the font family from the default, but change the size to 8 point
# (so it's very small) and drop any bold/italic/whatever modifiers
foreach {fontFamily fontSize} [$button cget -font] break
set fontSize 8
$button configure -font [list $fontFamily $fontSize]
# define mappings from widgets to columns
set columnID($label) $id
set columnID($frame) $id
set columnID($listbox) $id
set columnID($button) $id
# we're going to associate a new bindtag for the label to
# handle our resize bindings. Why? We want the bindings to
# be specific to this widget but we don't want to use the
# widget name. If we use the widget name then the bindings
# could get mixed up with user-supplied bindigs (via the
# "label bind" command).
set tag MclistboxLabel
bindtags $label [list MclistboxMouseBindings $label]
# reconfigure the label based on global options
foreach option [list bd image height relief font anchor \
background foreground borderwidth] {
if {[info exists options(-label$option)] \
&& $options(-label$option) != ""} {
$label configure -$option $options(-label$option)
}
}
# reconfigure the column based on global options
foreach option [list borderwidth relief] {
if {[info exists options(-column$option)] \
&& $options(-column$option) != ""} {
$frame configure -$option $options(-column$option)
}
}
# geometry propagation must be off so we can control the size
# of the listbox by setting the size of the containing frame
pack propagate $frame off
pack $label -side top -fill x -expand n
pack $listbox -side top -fill both -expand y -pady 2
# any events that happen in the listbox gets handled by the class
# bindings. This has the unfortunate side effect
bindtags $listbox [list $w Mclistbox all]
# Make the listbox a bwidget drag and drop site, if we have bwidgets
if { $::mclistbox::bwidget } {
if { $options(-dragenabled) } {
DragSite::register $listbox \
-draginitcmd ::mclistbox::_init_drag_cmd \
-dragendcmd ::mclistbox::_drag_end_cmd \
-dragevent 1
}
if { $options(-dropenabled) } {
DropSite::register $listbox \
-droptypes $options(-droptypes) \
-dropovercmd ::mclistbox::_over_cmd \
-dropcmd ::mclistbox::_drop_cmd
}
}
# return a list of the widgets we created.
return [list $frame $listbox $label $button]
}
# ::mclistbox::Column-add --
#
# Implements the "column add" widget command
#
# Arguments:
#
# w the widget pathname
# args additional option/value pairs which define the column
#
# Results:
#
# A column gets created and added to the listbox
proc ::mclistbox::Column-add {w args} {
upvar ::mclistbox::${w}::widgets widgets
upvar ::mclistbox::${w}::options options
upvar ::mclistbox::${w}::misc misc
variable widgetOptions
set id "column-[llength $misc(columns)]" ;# a suitable default
# if the first argument doesn't have a "-" as the first
# character, it is an id to associate with this column
if {![string match {-*} [lindex $args 0]]} {
# the first arg must be an id.
set id [lindex $args 0]
set args [lrange $args 1 end]
if {[lsearch -exact $misc(columns) $id] != -1} {
error "column \"$id\" already exists"
}
}
# define some reasonable defaults, then add any specific
# values supplied by the user
set opts(-bitmap) {}
set opts(-image) {}
set opts(-visible) 1
set opts(-resizable) 1
set opts(-position) "end"
set opts(-width) 20
set opts(-background) $options(-background)
set opts(-foreground) $options(-foreground)
set opts(-font) $options(-font)
set opts(-label) $id
set opts(-editable) 0
set opts(-editcommand) ""
set opts(-listvar) ""
if {[expr {[llength $args]%2}] == 1} {
# hmmm. An odd number of elements in args
# if the last item is a valid option we'll give a different
# error than if its not
set option [::mclistbox::Canonize $w "column option" [lindex $args end]]
error "value for \"[lindex $args end]\" missing"
}
array set opts $args
# figure out if we have any data in the listbox yet; we'll need
# this information in a minute...
if {[llength $misc(columns)] > 0} {
set col0 [lindex $misc(columns) 0]
set existingRows [$widgets(listbox$col0) size]
} else {
set existingRows 0
}
# create the widget and assign the associated paths to our array
set widgetlist [NewColumn $w $id]
set widgets(frame$id) [lindex $widgetlist 0]
set widgets(listbox$id) [lindex $widgetlist 1]
set widgets(label$id) [lindex $widgetlist 2]
# ericm@scriptics
set widgets(editbutton$id) [lindex $widgetlist 3]
# ericm@scriptics
if {[string equal $options(-state) "disabled"]} {
set opts(-foreground) [$widgets(label$id) cget -disabledforeground]
}
# add this column to the list of known columns
lappend misc(columns) $id
# configure the options. As a side effect, it will be inserted
# in the text widget
eval ::mclistbox::Column-configure {$w} {$id} [array get opts]
# now, if there is any data already in the listbox, we need to
# add a corresponding number of blank items. At least, I *think*
# that's the right thing to do.
if {$existingRows > 0} {
set blanks {}
# The new listbox may already have data in it (because of the listvar)
# so start iterating from [listbox size] instead of from 0
for {set i [$widgets(listbox$id) size]} {$i < $existingRows} {incr i} {
lappend blanks {}
}
eval {$widgets(listbox$id)} insert end $blanks
}
InvalidateScrollbars $w
return $id
}
# ::mclistbox::Column-configure --
#
# Implements the "column configure" widget command
#
# Arguments:
#
# w widget pathname
# id column identifier
# args list of option/value pairs
proc ::mclistbox::Column-configure {w id args} {
variable widgetOptions
variable columnOptions
upvar ::mclistbox::${w}::widgets widgets
upvar ::mclistbox::${w}::options options
upvar ::mclistbox::${w}::misc misc
# bail if they gave us a bogus id
set index [CheckColumnID $w $id]
# define some shorthand
set listbox $widgets(listbox$id)
set frame $widgets(frame$id)
set label $widgets(label$id)
set editbutton $widgets(editbutton$id)
if {[llength $args] == 0} {
# hmmm. User must be wanting all configuration information
# note that if the value of an array element is of length
# one it is an alias, which needs to be handled slightly
# differently
set results {}
foreach opt [lsort [array names columnOptions]] {
if {[llength $columnOptions($opt)] == 1} {
set alias $columnOptions($opt)
set optName $columnOptions($alias)
lappend results [list $opt $optName]
} else {
set optName [lindex $columnOptions($opt) 0]
set optClass [lindex $columnOptions($opt) 1]
set default [option get $frame $optName $optClass]
lappend results [list $opt $optName $optClass \
$default $options($id:$opt)]
}
}