-
Notifications
You must be signed in to change notification settings - Fork 14
/
listbox.tcl
1762 lines (1533 loc) · 56 KB
/
listbox.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
# ----------------------------------------------------------------------------
# listbox.tcl
# This file is part of Unifix BWidget Toolkit
# $Id: listbox.tcl,v 1.33 2010/05/12 08:28:56 oehhar Exp $
# ----------------------------------------------------------------------------
# Index of commands:
# - ListBox::create
# - ListBox::configure
# - ListBox::cget
# - ListBox::insert
# - ListBox::itemconfigure
# - ListBox::itemcget
# - ListBox::bindText
# - ListBox::bindImage
# - ListBox::delete
# - ListBox::move
# - ListBox::reorder
# - ListBox::selection
# - ListBox::exists
# - ListBox::index
# - ListBox::item - deprecated
# - ListBox::items
# - ListBox::see
# - ListBox::edit
# - ListBox::xview
# - ListBox::yview
# - ListBox::_update_edit_size
# - ListBox::_destroy
# - ListBox::_see
# - ListBox::_update_scrollregion
# - ListBox::_draw_item
# - ListBox::_redraw_items
# - ListBox::_redraw_selection
# - ListBox::_redraw_listbox
# - ListBox::_redraw_idle
# - ListBox::_resize
# - ListBox::_init_drag_cmd
# - ListBox::_drop_cmd
# - ListBox::_over_cmd
# - ListBox::_auto_scroll
# - ListBox::_scroll
# - ListBox::_themechanged
# ----------------------------------------------------------------------------
namespace eval ListBox {
Widget::define ListBox listbox DragSite DropSite DynamicHelp
namespace eval Item {
Widget::declare ListBox::Item {
{-indent Int 0 0 "%d >= 0"}
{-text String "" 0}
{-font String "" 0}
{-foreground Color "SystemWindowText" 0}
{-image TkResource "" 0 label}
{-window String "" 0}
{-data String "" 0}
{-fill Synonym -foreground}
{-fg Synonym -foreground}
}
}
DynamicHelp::include ListBox::Item balloon
Widget::tkinclude ListBox canvas .c \
remove {
-insertwidth -insertbackground -insertborderwidth -insertofftime
-insertontime -selectborderwidth -closeenough -confine -scrollregion
-xscrollincrement -yscrollincrement -width -height
} \
initialize {
-relief sunken -borderwidth 2 -takefocus 1
-highlightthickness 1 -width 200
}
DragSite::include ListBox "LISTBOX_ITEM" 1
DropSite::include ListBox {
LISTBOX_ITEM {copy {} move {}}
}
Widget::declare ListBox {
{-deltax Int 10 0 "%d >= 0"}
{-deltay Int 15 0 "%d >= 0"}
{-padx Int 20 0 "%d >= 0"}
{-foreground Color "SystemWindowText" 0}
{-background Color "SystemWindow" 0}
{-selectbackground Color "SystemHighlight" 0}
{-selectforeground Color "SystemHighlightText" 0}
{-font String "TkTextFont" 0}
{-width TkResource "" 0 listbox}
{-height TkResource "" 0 listbox}
{-redraw Boolean 1 0}
{-multicolumn Boolean 0 0}
{-dropovermode Flag "wpi" 0 "wpi"}
{-selectmode Enum none 0 {none single multiple}}
{-fg Synonym -foreground}
{-bg Synonym -background}
{-dropcmd String "ListBox::_drag_and_drop" 0}
{-autofocus Boolean 1 1}
{-selectfill Boolean 0 1}
}
Widget::addmap ListBox "" .c {-deltay -yscrollincrement}
bind ListBox <FocusIn> [list after idle {BWidget::refocus %W %W.c}]
bind ListBox <Destroy> [list ListBox::_destroy %W]
bind ListBox <Configure> [list ListBox::_resize %W]
bind ListBoxFocus <1> [list focus %W]
bind ListBox <Key-Up> [list ListBox::_keyboard_navigation %W -1]
bind ListBox <Key-Down> [list ListBox::_keyboard_navigation %W 1]
if {[lsearch [bindtags .] ListBoxThemeChanged] < 0} {
bindtags . [linsert [bindtags .] 1 ListBoxThemeChanged]
}
variable _edit
}
# ----------------------------------------------------------------------------
# Command ListBox::create
# ----------------------------------------------------------------------------
proc ListBox::create { path args } {
Widget::init ListBox $path $args
variable $path
upvar 0 $path data
frame $path -class ListBox -bd 0 -highlightthickness 0 -relief flat \
-takefocus 0
# For 8.4+ we don't want to inherit the padding
catch {$path configure -padx 0 -pady 0}
# widget informations
set data(nrows) -1
# items informations
set data(items) {}
set data(selitems) {}
# update informations
set data(upd,level) 0
set data(upd,afterid) ""
set data(upd,level) 0
set data(upd,delete) {}
# drag and drop informations
set data(dnd,scroll) ""
set data(dnd,afterid) ""
set data(dnd,item) ""
eval [list canvas $path.c] [Widget::subcget $path .c] \
[list -xscrollincrement 8]
pack $path.c -expand yes -fill both
DragSite::setdrag $path $path.c ListBox::_init_drag_cmd \
[Widget::cget $path -dragendcmd] 1
DropSite::setdrop $path $path.c ListBox::_over_cmd ListBox::_drop_cmd 1
Widget::create ListBox $path
set w [Widget::cget $path -width]
set h [Widget::cget $path -height]
set dy [Widget::cget $path -deltay]
$path.c configure -width [expr {$w*8}] -height [expr {$h*$dy}]
# Insert $path into the canvas bindings, so that anyone binding
# directly onto the widget will see their bindings activated when
# the canvas has focus.
set bindtags [bindtags $path.c]
set bindtags [linsert $bindtags 1 $path]
# Let any click within the canvas focus on the canvas so that
# MouseWheel scroll events will be properly handled by the canvas.
if {[Widget::cget $path -autofocus]} {
lappend bindtags ListBoxFocus
BWidget::bindMouseWheel $path.c
BWidget::bindMiddleMouseMovement $path.c
}
bindtags $path.c $bindtags
# Add slightly modified up/down bindings to the canvas, in case
# it gets the focus (like with -autofocus).
bind $path.c <Key-Up> {ListBox::_keyboard_navigation [winfo parent %W] -1}
bind $path.c <Key-Down> {ListBox::_keyboard_navigation [winfo parent %W] 1}
bind ListBoxThemeChanged <<ThemeChanged>> \
"+ [namespace current]::_themechanged $path"
_configureSelectmode $path [Widget::getoption $path -selectmode]
return $path
}
# ----------------------------------------------------------------------------
# Command ListBox::_configureSelectmode
# ----------------------------------------------------------------------------
# Configure the selectmode
proc ListBox::_configureSelectmode { path selectmode {previous none} } {
# clear current binding
switch -exact -- $previous {
single {
$path _bindText <Button-1> ""
$path _bindImage <Button-1> ""
}
multiple {
$path _bindText <ButtonRelease-1> ""
$path _bindText <Shift-ButtonRelease-1> ""
$path _bindText <Control-ButtonRelease-1> ""
$path _bindImage <ButtonRelease-1> ""
$path _bindImage <Shift-ButtonRelease-1> ""
$path _bindImage <Control-ButtonRelease-1> ""
}
}
# set new bindings
switch -exact -- $selectmode {
single {
$path _bindText <Button-1> [list ListBox::_mouse_select $path set]
$path _bindImage <Button-1> [list ListBox::_mouse_select $path set]
if {1 < [llength [ListBox::selection $path get]]} {
ListBox::selection $path clear
}
}
multiple {
set cmd ListBox::_multiple_select
$path _bindText <ButtonRelease-1> [list $cmd $path n %x %y]
$path _bindText <Shift-ButtonRelease-1> [list $cmd $path s %x %y]
$path _bindText <Control-ButtonRelease-1> [list $cmd $path c %x %y]
$path _bindImage <ButtonRelease-1> [list $cmd $path n %x %y]
$path _bindImage <Shift-ButtonRelease-1> [list $cmd $path s %x %y]
$path _bindImage <Control-ButtonRelease-1> [list $cmd $path c %x %y]
}
default {
if {0 < [llength [ListBox::selection $path get]]} {
ListBox::selection $path clear
}
}
}
}
# ----------------------------------------------------------------------------
# Command ListBox::configure
# ----------------------------------------------------------------------------
proc ListBox::configure { path args } {
set selectmodePrevious [Widget::getoption $path -selectmode]
set res [Widget::configure $path $args]
if { [Widget::hasChanged $path -selectmode selectmode] } {
_configureSelectmode $path $selectmode $selectmodePrevious
}
set ch1 [expr {[Widget::hasChanged $path -deltay dy] |
[Widget::hasChanged $path -padx val] |
[Widget::hasChanged $path -multicolumn val]}]
set ch2 [expr {[Widget::hasChanged $path -selectbackground val] |
[Widget::hasChanged $path -selectforeground val]}]
set redraw 0
if { [Widget::hasChanged $path -height h] } {
$path.c configure -height [expr {$h*$dy}]
set redraw 1
}
if { [Widget::hasChanged $path -width w] } {
$path.c configure -width [expr {$w*8}]
set redraw 1
}
if { [Widget::hasChanged $path -background bg] } {
$path.c itemconfigure box -fill $bg
}
if { !$redraw } {
if { $ch1 } {
_redraw_idle $path 2
} elseif { $ch2 } {
_redraw_idle $path 1
}
}
if { [Widget::hasChanged $path -redraw bool] && $bool } {
variable $path
upvar 0 $path data
set lvl $data(upd,level)
set data(upd,level) 0
_redraw_idle $path $lvl
}
set force [Widget::hasChanged $path -dragendcmd dragend]
DragSite::setdrag $path $path.c ListBox::_init_drag_cmd $dragend $force
DropSite::setdrop $path $path.c ListBox::_over_cmd ListBox::_drop_cmd
return $res
}
# ----------------------------------------------------------------------------
# Command ListBox::cget
# ----------------------------------------------------------------------------
proc ListBox::cget { path option } {
return [Widget::cget $path $option]
}
# ----------------------------------------------------------------------------
# Command ListBox::insert
# ----------------------------------------------------------------------------
proc ListBox::insert { path index item args } {
variable $path
upvar 0 $path data
set item [Widget::nextIndex $path $item]
if {[info exists data(exists,$item)]} {
return -code error "item \"$item\" already exists"
}
Widget::init ListBox::Item $path.$item $args
set data(items) [linsert $data(items) $index $item]
set data(exists,$item) 1
set data(upd,create,$item) $item
_redraw_idle $path 2
return $item
}
# Bastien Chevreux ([email protected])
# The multipleinsert command performs inserts several items at once into
# the list. It is faster than calling insert multiple times as it uses the
# Widget::copyinit command for initializing all items after the 1st. The
# speedup factor is between 2 and 3 for typical usage, but could be higher
# for inserts with many options.
#
# Syntax: path and index are as in the insert command
# args is a list of even numbered elements where the 1st of each pair
# corresponds to the item of 'insert' and the second to args of 'insert'.
# ----------------------------------------------------------------------------
# Command ListBox::multipleinsert
# ----------------------------------------------------------------------------
proc ListBox::multipleinsert { path index args } {
variable $path
upvar 0 $path data
# If we got only one list as arg, take the first element as args
# This enables callers to use
# $list multipleinsert index $thelist
# instead of
# eval $list multipleinsert index $thelist
if {[llength $args] == 1} {
set args [lindex $args 0]
}
set count 0
foreach {item iargs} $args {
if {[info exists data(exists,$item)]} {
return -code error "item \"$item\" already exists"
}
if {$count==0} {
Widget::init ListBox::Item $path.$item $iargs
set firstpath $path.$item
} else {
Widget::copyinit ListBox::Item $firstpath $path.$item $iargs
}
set data(items) [linsert $data(items) $index $item]
set data(exists,$item) 1
set data(upd,create,$item) $item
incr count
}
_redraw_idle $path 2
return $item
}
# ----------------------------------------------------------------------------
# Command ListBox::itemconfigure
# ----------------------------------------------------------------------------
proc ListBox::itemconfigure { path item args } {
variable $path
upvar 0 $path data
if { [lsearch -exact $data(items) $item] == -1 } {
return -code error "item \"$item\" does not exist"
}
set oldind [Widget::getoption $path.$item -indent]
set res [Widget::configure $path.$item $args]
set chind [Widget::hasChanged $path.$item -indent indent]
set chw [Widget::hasChanged $path.$item -window win]
set chi [Widget::hasChanged $path.$item -image img]
set cht [Widget::hasChanged $path.$item -text txt]
set chf [Widget::hasChanged $path.$item -font fnt]
set chfg [Widget::hasChanged $path.$item -foreground fg]
set idn [$path.c find withtag n:$item]
_set_help $path $item
if { $idn == "" } {
# item is not drawn yet
_redraw_idle $path 2
return $res
}
set oldb [$path.c bbox $idn]
set coords [$path.c coords $idn]
set padx [Widget::getoption $path -padx]
set x0 [expr {[lindex $coords 0]-$padx-$oldind+$indent}]
set y0 [lindex $coords 1]
if { $chw || $chi } {
# -window or -image modified
set idi [$path.c find withtag i:$item]
set type [lindex [$path.c gettags $idi] 0]
if { [string length $win] } {
if { [string equal $type "win"] } {
$path.c itemconfigure $idi -window $win
} else {
$path.c delete $idi
$path.c create window $x0 $y0 -window $win -anchor w \
-tags [list win i:$item]
}
} elseif { [string length $img] } {
if { [string equal $type "img"] } {
$path.c itemconfigure $idi -image $img
} else {
$path.c delete $idi
$path.c create image $x0 $y0 -image $img -anchor w \
-tags [list img imgbind i:$item]
}
} else {
$path.c delete $idi
}
}
if { $cht || $chf || $chfg } {
# -text or -font modified, or -foreground modified
set fnt [_getoption $path $item -font]
set fg [_getoption $path $item -foreground]
$path.c itemconfigure $idn -text $txt -font $fnt -fill $fg
_redraw_idle $path 1
}
if { $chind } {
# -indent modified
$path.c coords $idn [expr {$x0+$padx}] $y0
$path.c coords i:$item $x0 $y0
_redraw_idle $path 1
}
if { [Widget::getoption $path -multicolumn] && ($cht || $chf || $chind) } {
set bbox [$path.c bbox $idn]
if { [lindex $bbox 2] > [lindex $oldb 2] } {
_redraw_idle $path 2
}
}
return $res
}
# ----------------------------------------------------------------------------
# Command ListBox::itemcget
# ----------------------------------------------------------------------------
proc ListBox::itemcget { path item option } {
return [Widget::cget $path.$item $option]
}
# ----------------------------------------------------------------------------
# Command ListBox::_bindText
# ----------------------------------------------------------------------------
proc ListBox::_bindText { path event script {tag click} } {
if { $script != "" } {
set map [list %W $path]
set script [string map $map $script]
append script " \[ListBox::_get_current [list $path]\]"
}
$path.c bind $tag $event $script
}
# ----------------------------------------------------------------------------
# Command ListBox::bindText
# ----------------------------------------------------------------------------
proc ListBox::bindText { path event script } {
_bindText $path $event $script clickbind
}
# ----------------------------------------------------------------------------
# Command ListBox::_bindImage
# ----------------------------------------------------------------------------
proc ListBox::_bindImage { path event script {tag img} } {
if { $script != "" } {
set map [list %W $path]
set script [string map $map $script]
append script " \[ListBox::_get_current [list $path]\]"
}
$path.c bind $tag $event $script
}
# ----------------------------------------------------------------------------
# Command ListBox::bindImage
# ----------------------------------------------------------------------------
proc ListBox::bindImage { path event script } {
_bindImage $path $event $script imgbind
}
# ----------------------------------------------------------------------------
# Command ListBox::delete
# ----------------------------------------------------------------------------
proc ListBox::delete { path args } {
variable $path
upvar 0 $path data
Widget::getVariable $path help
foreach litems $args {
foreach item $litems {
set idx [lsearch -exact $data(items) $item]
if { $idx != -1 } {
set data(items) [lreplace $data(items) $idx $idx]
array unset help $item
Widget::destroy $path.$item
if { [info exists data(exists,$item)] } {
unset data(exists,$item)
}
if { [info exists data(upd,create,$item)] } {
unset data(upd,create,$item)
} else {
lappend data(upd,delete) $item
}
}
}
}
set sel $data(selitems)
set data(selitems) {}
eval [list selection $path set] $sel
_redraw_idle $path 2
}
# ----------------------------------------------------------------------------
# Command ListBox::move
# ----------------------------------------------------------------------------
proc ListBox::move { path item index } {
variable $path
upvar 0 $path data
if { [set idx [lsearch -exact $data(items) $item]] == -1 } {
return -code error "item \"$item\" does not exist"
}
set data(items) [linsert [lreplace $data(items) $idx $idx] $index $item]
_redraw_idle $path 2
}
# ----------------------------------------------------------------------------
# Command ListBox::reorder
# ----------------------------------------------------------------------------
proc ListBox::reorder { path neworder } {
variable $path
upvar 0 $path data
set data(items) [BWidget::lreorder $data(items) $neworder]
_redraw_idle $path 2
}
# ----------------------------------------------------------------------------
# Command ListBox::selection
# ----------------------------------------------------------------------------
proc ListBox::selection { path cmd args } {
variable $path
upvar 0 $path data
switch -- $cmd {
set {
set data(selitems) {}
foreach item $args {
if { [lsearch -exact $data(selitems) $item] == -1 } {
if { [lsearch -exact $data(items) $item] != -1 } {
lappend data(selitems) $item
}
}
}
}
add {
foreach item $args {
if { [lsearch -exact $data(selitems) $item] == -1 } {
if { [lsearch -exact $data(items) $item] != -1 } {
lappend data(selitems) $item
}
}
}
}
remove {
foreach item $args {
if { [set idx [lsearch -exact $data(selitems) $item]] != -1 } {
set data(selitems) [lreplace $data(selitems) $idx $idx]
}
}
}
clear {
set data(selitems) {}
}
get {
return $data(selitems)
}
includes {
return [expr {[lsearch -exact $data(selitems) $args] != -1}]
}
default {
return
}
}
_redraw_idle $path 1
}
# ----------------------------------------------------------------------------
# Command ListBox::exists
# ----------------------------------------------------------------------------
proc ListBox::exists { path item } {
variable $path
upvar 0 $path data
return [expr {[lsearch -exact $data(items) $item] != -1}]
}
# ----------------------------------------------------------------------------
# Command ListBox::index
# ----------------------------------------------------------------------------
proc ListBox::index { path item } {
variable $path
upvar 0 $path data
if {[string equal $item "active"]} { return [$path selection get] }
return [lsearch -exact $data(items) $item]
}
# ----------------------------------------------------------------------------
# ListBox::find
# Returns the item given a position.
# findInfo @x,y ?confine?
# lineNumber
# ----------------------------------------------------------------------------
proc ListBox::find {path findInfo {confine ""}} {
variable $path
upvar 0 $path widgetData
if {[regexp -- {^@([0-9]+),([0-9]+)$} $findInfo match x y]} {
set x [$path.c canvasx $x]
set y [$path.c canvasy $y]
} elseif {[regexp -- {^[0-9]+$} $findInfo lineNumber]} {
set dy [Widget::getoption $path -deltay]
set y [expr {$dy*($lineNumber+0.5)}]
set confine ""
} else {
return -code error "invalid find spec \"$findInfo\""
}
set found 0
set xi 0
foreach xs $widgetData(xlist) {
if {$x <= $xs} {
foreach id [$path.c find overlapping $xi $y $xs $y] {
set ltags [$path.c gettags $id]
set item [lindex $ltags 0]
if { [string equal $item "item"] ||
[string equal $item "img"] ||
[string equal $item "win"] } {
# item is the label or image/window of the node
set item [string range [lindex $ltags 1] 2 end]
set found 1
break
}
}
break
}
set xi $xs
}
if {$found} {
if {[string equal $confine "confine"]} {
# test if x stand inside node bbox
set xi [expr {[lindex [$path.c coords n:$item] 0]-[Widget::getoption $path -padx]}]
set xs [lindex [$path.c bbox n:$item] 2]
if {$x >= $xi && $x <= $xs} {
return $item
}
} else {
return $item
}
}
return ""
}
# ----------------------------------------------------------------------------
# Command ListBox::item - deprecated
# ----------------------------------------------------------------------------
proc ListBox::item { path first {last ""} } {
variable $path
upvar 0 $path data
if { ![string length $last] } {
return [lindex $data(items) $first]
} else {
return [lrange $data(items) $first $last]
}
}
# ----------------------------------------------------------------------------
# Command ListBox::items
# ----------------------------------------------------------------------------
proc ListBox::items { path {first ""} {last ""}} {
variable $path
upvar 0 $path data
if { ![string length $first] } {
return $data(items)
}
if { ![string length $last] } {
return [lindex $data(items) $first]
} else {
return [lrange $data(items) $first $last]
}
}
# ----------------------------------------------------------------------------
# Command ListBox::see
# ----------------------------------------------------------------------------
proc ListBox::see { path item } {
variable $path
upvar 0 $path data
if { [Widget::getoption $path -redraw] && $data(upd,afterid) != "" } {
after cancel $data(upd,afterid)
_redraw_listbox $path
}
set idn [$path.c find withtag n:$item]
if { $idn != "" } {
set idi [$path.c find withtag i:$item]
if { $idi == "" } { set idi $idn }
ListBox::_see $path $idn right
ListBox::_see $path $idi left
}
}
# ----------------------------------------------------------------------------
# Command ListBox::edit
# ----------------------------------------------------------------------------
proc ListBox::edit { path item text {verifycmd ""} {clickres 0} {select 1}} {
variable _edit
variable $path
upvar 0 $path data
if { [Widget::getoption $path -redraw] && $data(upd,afterid) != "" } {
after cancel $data(upd,afterid)
_redraw_listbox $path
}
set idn [$path.c find withtag n:$item]
if { $idn != "" } {
ListBox::_see $path $idn right
ListBox::_see $path $idn left
set oldfg [$path.c itemcget $idn -fill]
set sbg [Widget::getoption $path -selectbackground]
set coords [$path.c coords $idn]
set x [lindex $coords 0]
set y [lindex $coords 1]
set bd [expr {[$path.c cget -borderwidth]+[$path.c cget -highlightthickness]}]
set w [expr {[winfo width $path] - 2*$bd}]
set wmax [expr {[$path.c canvasx $w]-$x}]
$path.c itemconfigure $idn -fill [Widget::getoption $path -background]
$path.c itemconfigure s:$item -fill {} -outline {}
set _edit(text) $text
set _edit(wait) 0
set frame [frame $path.edit \
-relief flat -borderwidth 0 -highlightthickness 0 \
-background [Widget::getoption $path -background]]
set ent [entry $frame.edit \
-width 0 \
-relief solid \
-borderwidth 1 \
-highlightthickness 0 \
-foreground [_getoption $path $item -foreground] \
-background [Widget::getoption $path -background] \
-selectforeground [Widget::getoption $path -selectforeground] \
-selectbackground $sbg \
-font [_getoption $path $item -font] \
-textvariable ListBox::_edit(text)]
pack $ent -ipadx 8 -anchor w
set idw [$path.c create window $x $y -window $frame -anchor w]
trace variable ListBox::_edit(text) w [list ListBox::_update_edit_size $path $ent $idw $wmax]
tkwait visibility $ent
grab $frame
BWidget::focus set $ent
_update_edit_size $path $ent $idw $wmax
update
if { $select } {
$ent selection range 0 end
$ent icursor end
$ent xview end
}
bindtags $ent [list $ent Entry]
bind $ent <Escape> {set ListBox::_edit(wait) 0}
bind $ent <Return> {set ListBox::_edit(wait) 1}
if { $clickres == 0 || $clickres == 1 } {
bind $frame <Button> [list set ListBox::_edit(wait) $clickres]
}
set ok 0
while { !$ok } {
tkwait variable ListBox::_edit(wait)
if { !$_edit(wait) || [llength $verifycmd]==0 ||
[uplevel \#0 $verifycmd [list $_edit(text)]] } {
set ok 1
}
}
trace vdelete ListBox::_edit(text) w [list ListBox::_update_edit_size $path $ent $idw $wmax]
grab release $frame
BWidget::focus release $ent
destroy $frame
$path.c delete $idw
$path.c itemconfigure $idn -fill $oldfg
$path.c itemconfigure s:$item -fill $sbg -outline $sbg
if { $_edit(wait) } {
return $_edit(text)
}
}
return ""
}
# ----------------------------------------------------------------------------
# Command ListBox::xview
# ----------------------------------------------------------------------------
proc ListBox::xview { path args } {
return [eval [linsert $args 0 $path.c xview]]
}
# ----------------------------------------------------------------------------
# Command ListBox::yview
# ----------------------------------------------------------------------------
proc ListBox::yview { path args } {
return [eval [linsert $args 0 $path.c yview]]
}
proc ListBox::getcanvas { path } {
return $path.c
}
proc ListBox::curselection { path } {
return [$path selection get]
}
# ----------------------------------------------------------------------------
# Command ListBox::_update_edit_size
# ----------------------------------------------------------------------------
proc ListBox::_update_edit_size { path entry idw wmax args } {
set entw [winfo reqwidth $entry]
if { $entw >= $wmax } {
$path.c itemconfigure $idw -width $wmax
} else {
$path.c itemconfigure $idw -width 0
}
}
# ----------------------------------------------------------------------------
# Command ListBox::_getoption
# Returns the value of option for node. If empty, returned value is those
# of the ListBox.
# ----------------------------------------------------------------------------
proc ListBox::_getoption { path item option } {
set value [Widget::getoption $path.$item $option]
if {![string length $value]} {
set value [Widget::getoption $path $option]
}
return $value
}
# ----------------------------------------------------------------------------
# Command ListBox::_destroy
# ----------------------------------------------------------------------------
proc ListBox::_destroy { path } {
variable $path
upvar 0 $path data
if { $data(upd,afterid) != "" } {
after cancel $data(upd,afterid)
}
if { $data(dnd,afterid) != "" } {
after cancel $data(dnd,afterid)
}
foreach item $data(items) {
Widget::destroy $path.$item
}
Widget::destroy $path
unset data
}
# ----------------------------------------------------------------------------
# Command ListBox::_see
# ----------------------------------------------------------------------------
proc ListBox::_see { path idn side } {
set bbox [$path.c bbox $idn]
set scrl [$path.c cget -scrollregion]
set ymax [lindex $scrl 3]
set dy [$path.c cget -yscrollincrement]
set yv [$path.c yview]
set yv0 [expr {round([lindex $yv 0]*$ymax/$dy)}]
set yv1 [expr {round([lindex $yv 1]*$ymax/$dy)}]
set y [expr {int([lindex [$path.c coords $idn] 1]/$dy)}]
if { $y < $yv0 } {
$path.c yview scroll [expr {$y-$yv0}] units
} elseif { $y >= $yv1 } {
$path.c yview scroll [expr {$y-$yv1+1}] units
}
set xmax [lindex $scrl 2]
set dx [$path.c cget -xscrollincrement]
set xv [$path.c xview]
if { [string equal $side "right"] } {
set xv1 [expr {round([lindex $xv 1]*$xmax/$dx)}]
set x1 [expr {int([lindex $bbox 2]/$dx)}]
if { $x1 >= $xv1 } {
$path.c xview scroll [expr {$x1-$xv1+1}] units
}
} else {
set xv0 [expr {round([lindex $xv 0]*$xmax/$dx)}]
set x0 [expr {int([lindex $bbox 0]/$dx)}]
if { $x0 < $xv0 } {
$path.c xview scroll [expr {$x0-$xv0}] units
}
}
}
# ----------------------------------------------------------------------------
# Command ListBox::_update_scrollregion
# ----------------------------------------------------------------------------
proc ListBox::_update_scrollregion { path } {
set bd [$path.c cget -borderwidth]
set ht [$path.c cget -highlightthickness]
set bd [expr {2*($bd + $ht)}]
set w [expr {[winfo width $path] - $bd}]
set h [expr {[winfo height $path] - $bd}]
set xinc [$path.c cget -xscrollincrement]
set yinc [$path.c cget -yscrollincrement]
set bbox [$path.c bbox item win img]
if { [llength $bbox] } {
set xs [lindex $bbox 2]
set ys [lindex $bbox 3]
if { $w < $xs } {
set w [expr {int($xs)}]
if { [set r [expr {$w % $xinc}]] } {
set w [expr {$w+$xinc-$r}]
}
}
if { $h < $ys } {
set h [expr {int($ys)}]
if { [set r [expr {$h % $yinc}]] } {
set h [expr {$h+$yinc-$r}]
}
}
}
$path.c configure -scrollregion [list 0 0 $w $h]
}