-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreeviewlib.tcl
More file actions
executable file
·1883 lines (1661 loc) · 46.1 KB
/
treeviewlib.tcl
File metadata and controls
executable file
·1883 lines (1661 loc) · 46.1 KB
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
#
# Name: TreeView Library Functions
# Version: 1.0.3
# Date: Oct 10, 2024
# Author: Brian O'Hagan
# Email: brian199@comcast.net
# Legal Notice: (c) Copyright 2020 by Brian O'Hagan
# Released under the BSD license. I would appreciate a copy of any modifications
# made to this package for possible incorporation in a future release.
# Description: Provides convenience functions for the treeview widget.
#
# Package dependencies
package require Tcl 8.6-
package require Tk 8.6-
catch {package require autoscroll}; # Tklib
# Library module
package provide treeviewlib 1.0.3
#
# Create namespace for package
#
namespace eval ::treeviewlib {
variable column ""
variable cursor ""
variable moveColumn ""
variable moveItem ""
variable widgets
variable fakeId 0
# Movable column bindings
bind Treeview <ButtonPress-1> +[list ::treeviewlib::MoveColumnPress %W %x %y]
bind Treeview <B1-Motion> +[list ::treeviewlib::MoveColumnDrag %W %x %y]
bind Treeview <ButtonRelease-1> +[list ::treeviewlib::MoveColumnRelease %W %x %y]
# Font size bindings
bind Treeview <<TkWorldChanged>> [list ::treeviewlib::FontChanged %W %d]
variable fonts
# Sort direction arrows from Tk demos
# 3d arrows
image create photo ::treeviewlib::arrowUp3d -data {
R0lGODlhDgAOAJEAANnZ2YCAgPz8/P///yH5BAEAAAAALAAAAAAOAA4AAAImhI+
py+1LIsJHiBAh+BgmiEAJQITgW6DgUQIAECH4JN8IPqYuNxUAOw==}
image create photo ::treeviewlib::arrowDown3d -data {
R0lGODlhDgAOAJEAANnZ2YCAgPz8/P///yH5BAEAAAAALAAAAAAOAA4AAAInhI+
py+1I4ocQ/IgDEYIPgYJICUCE4F+YIBolEoKPEJKZmVJK6ZACADs=}
image create photo ::treeviewlib::arrowBlank3d -height 14 -width 14
# Solid triangle icons
image create bitmap ::treeviewlib::arrowUp -data {
#define arrowUp_width 9
#define arrowUp_height 5
static char arrowUp_bits[] = {
0x10, 0x00, 0x38, 0x00, 0x7c, 0x00, 0xfe, 0x00, 0xff, 0x01
};
}
image create bitmap ::treeviewlib::arrowDown -data {
#define arrowDown_width 9
#define arrowDown_height 5
static char arrowDown_bits[] = {
0xff, 0x01, 0xfe, 0x00, 0x7c, 0x00, 0x38, 0x00, 0x10, 0x00
};
}
image create bitmap ::treeviewlib::arrowBlank -data {
#define arrowBlank_width 9
#define arrowBlank_height 5
static char arrowBlank_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
}
# Caret icons
image create bitmap ::treeviewlib::caretUp -data {
#define caretUp_width 9
#define caretUp_height 5
static char caretUp_bits[] = {
0x10, 0x00, 0x28, 0x00, 0x44, 0x00, 0x82, 0x00, 0x01, 0x01
};
}
image create bitmap ::treeviewlib::caretDown -data {
#define caretDown_width 9
#define caretDown_height 5
static char caretDown_bits[] = {
0x01, 0x01, 0x82, 0x00, 0x44, 0x00, 0x28, 0x00, 0x10, 0x00
};
}
# Outline triangle icons
image create bitmap ::treeviewlib::triangleUp -data {
#define triangleUp_width 9
#define triangleUp_height 5
static char triangleUp_bits[] = {
0x10, 0x00, 0x28, 0x00, 0x44, 0x00, 0x82, 0x00, 0xff, 0x01
};
}
image create bitmap ::treeviewlib::triangleDown -data {
#define triangleDown_width 9
#define triangleDown_height 5
static char triangleDown_bits[] = {
0xff, 0x01, 0x82, 0x00, 0x44, 0x00, 0x28, 0x00, 0x10, 0x00
};
}
# Undo change in Tk 8.6.9 that prevents use of tag colors
if {[info patchlevel] eq "8.6.9"} {
ttk::style map Treeview -background [list disabled SystemButtonFace selected SystemHighlight] \
-foreground [list disabled SystemGrayText selected SystemHighlightText]
}
}
###############################################################################
# Ttk Overrides
###############################################################################
if {[package vcompare [package require Tk] 8.6.9] < 0} {
#
# ActivateHeading -- track active heading element
#
# Args: w = Widget
# heading = Heading column number
# Returns: nothing
#
proc ttk::treeview::ActivateHeading {w heading} {
variable State
if {$w != $State(activeWidget) || $heading != $State(activeHeading)} {
if {[winfo exists $State(activeWidget)] && $State(activeHeading) != {}} {
$State(activeWidget) heading $State(activeHeading) state !active
}
if {$heading != {}} {
$w heading $heading state active
}
set State(activeHeading) $heading
set State(activeWidget) $w
}
}
}
###############################################################################
# Widget Setup
###############################################################################
#
# Create treeview widget with scrollbars
#
# Args: w = Window frame containing treeview to create
# id = Context id for this widget
# titles = Column titles in width, title, alignment pairs
# ids = Field or column ids for each column
# is_tree = Show tree column boolean
# Returns: Treeview widget name
#
proc ::treeviewlib::create {w id {titles {}} {ids {}} {is_tree 0}} {
variable widgets
# Create frame
set w [::ttk::frame $w -padding 1 -relief sunken -borderwidth 0]
# Create widget and scrollbars
set tree [ttk::treeview $w.tree -selectmode extended -takefocus 1 \
-xscrollcommand [list $w.sx set] -yscrollcommand [list $w.sy set]]
ttk::scrollbar $w.sy -orient vertical -command [list $tree yview]
ttk::scrollbar $w.sx -orient horizontal -command [list $tree xview]
grid $w.tree $w.sy -sticky nsew
grid $w.sx -sticky nsew
grid columnconfigure $w 0 -weight 1
grid rowconfigure $w 0 -weight 1
set widgets($id) $tree
# Only display scrollbars when needed
catch {::autoscroll::autoscroll $w.sx}
catch {::autoscroll::autoscroll $w.sy}
# Configure tree
reconfigure $tree $titles $ids $is_tree
# Selection bindings
if {[tk windowingsystem] ne "aqua"} {set key "Control"} else {set key "Command"}
foreach {seq fn} [list <$key-a> all <$key-i> invert <$key-u> unselect \
<$key-space> toggle <<SelectAll>> all <<SelectNone>> clear] {
bind $tree $seq +[list ::treeviewlib::select %W $fn]
}
# Edit bindings
foreach {seq fn} [list <$key-c> Copy <<Copy>> Copy] {
bind $tree $seq +[list ::treeviewlib::$fn %W]
}
# Stripe tag
$tree tag configure stripe -background #f4f1e8
# Create pop-up menu
set menu [menu $tree.column_menu]
set proc ::treeviewlib::menu_callback
$menu add command -label "Hide Column" -command [list $proc $tree hide]
$menu add command -label "Show Column" -command [list $proc $tree show]
$menu add separator
$menu add command -label "Move Column Left" -command [list $proc $tree move_left]
$menu add command -label "Move Column Right" -command [list $proc $tree move_right]
$menu add separator
$menu add command -label "Size Column to Fit Data" -command [list $proc $tree autosize]
$menu add command -label "Size All Columns to Fit" -command [list $proc $tree autosize_all]
# Add binding for popup menu
bind $tree <<ContextMenu>> [list ::treeviewlib::post_menu $tree $menu %X %Y %x %y]
return $tree
}
#
# Update treeview columns
#
# Args: w = Treeview widget
# titles = Column titles in width, title, alignment pairs
# ids = Field or column ids for each column
# is_tree = Show tree column boolean
# Returns: Treeview widget name
#
proc ::treeviewlib::reconfigure {w {titles {}} {ids {}} {is_tree 0}} {
variable fonts
# Get column ids
set i [expr {!$is_tree + 0}]
set columns [list]
foreach id $ids {width title align mode} $titles {
if {$i > 0} {
lappend columns [expr {$id ne "" ? $id : $title}]
}
incr i
}
# Show treeview components
set show [list]
foreach {name boolean} [list tree $is_tree headings [llength $titles]] {
if {$boolean} {
lappend show $name
}
}
# Set column ids
$w configure -columns $columns -show $show
# Cell font
#set font [ttk::style configure Treeview -font]
set font [ttk::style lookup Treeview -font]
if {$font eq ""} {set font TkDefaultFont}
set cwidth [font measure $font -displayof $w "n"]
set fonts($w-$font) $cwidth
# Set column titles
set i [expr {!$is_tree + 0}]
foreach id $ids {width title align mode} $titles {
set column [expr {$i == 0 ? "#0" : ($id ne "" ? $id : $title)}]
set stretch [expr {$width == 0}]
set anchor [string map [list left w right e] [string tolower $align]]
set width [expr {$width * $cwidth}]
$w heading $column -anchor center -text $title -image ::treeviewlib::arrowBlank \
-command [list ::treeviewlib::SortByColumn $w {} $column increasing $mode] \
state [list !selected !alternate !user1]
$w column $column -anchor $anchor -minwidth 25 -stretch $stretch -width $width
incr i
}
}
#
# Resize columns and rows after font change
#
# Args: w = Treeview widget
# detail = Trigger for event
# Returns: nothing
#
proc ::treeviewlib::FontChanged {w detail} {
set top [winfo toplevel $w]
variable fonts
# Only valid for font updates
if {$detail ne "FontChanged"} {
return
}
# Get class font
#set font [ttk::style configure Treeview -font]
set font [ttk::style lookup Treeview -font]
if {$font eq ""} {set font TkDefaultFont}
set new [font measure $font -displayof $top "n"]
# Get previous size
set old $fonts($w-$font)
set fonts($w-$font) $new
# Resize columns
foreach column [concat [list #0] [$w cget -columns]] {
$w column $column -width [expr {int([$w column $column -width] / $old) * $new}]
}
# Resize tags
foreach tag [$w tag names] {
# Get font
set font [$w tag configure $tag -font]
if {$font eq ""} continue
set cwidth [font measure $font -displayof $top "n"]
# Resize rows
set h [font metrics $font -displayof $top -linespace]
}
}
###############################################################################
# Widget Functions
###############################################################################
#
# Select treeview functions handler for keyboard and menu events
#
# Args: id = Widget id
# fn = Function to perform
# args = Arguments to fn
# Returns: Function dependent
#
proc ::treeviewlib::handler {id fn args} {
variable widgets
perform_fn $widgets($id) $fn {*}$args
}
#
# Perform select treeview functions
#
# Args: w = Treeview widget
# fn = Function to perform
# args = Tree item ids or function dependent
# Returns: Function dependent
#
proc ::treeviewlib::perform_fn {w fn args} {
# Default to selected item(s), if not specified
if {[llength $args] == 0 && $fn in [list collapse copy cut delete detach expand get lower raise]} {
set args [list [$w selection]]
}
switch -exact -- $fn {
"append" {
# Append rows
Insert $w {} end {*}$args
}
"attach" - "paste" {
# Reattach items
set index [$w focus]
if {$index eq ""} {
set index [lindex [$w selection] 0]
}
if {$index eq ""} {
set index "end"
}
MoveRow $w {} $index {*}$args
}
"autosizecolumns" {
# Auto size columns
ColumnAutoSize $w $args
}
"clear" {
# Delete all tree items
FilterClear $w
$w delete [$w children {}]
}
"collapse" - "expand" {
# Close or open items
ExpandCollapse $w [string match "expand" $fn] {*}$args
}
"collapse_all" - "expand_all" {
# Close or expand all items
ExpandCollapse $w [string match "expand*" $fn] [$w children {}]
}
"copy" {
# Copy items
Copy $w {*}$args
}
"cut" - "detach" {
# Cut items, but don't delete
$w detach {*}$args
return $args
}
"delete" {
# Delete items
if {![catch {$w delete {*}$args}]} {
Restripe $w {}
upvar #0 ::treeviewlib::data$w cache
if {[info exists cache]} {
foreach item $args {
if {[set index [lsearch $cache $item]] > -1} {
set cache [lreplace $cache $index $index]
}
}
}
}
}
"get" {
# Get item
return [Get $w {*}$args]
}
"hide" {
# Hide column
ColumnHide $w {*}$args
}
"insert" {
# Insert rows
Insert $w {} {*}$args
}
"lower" - "raise" {
# Lower (move down) or raise (move up) items
RaiseLower $w $fn {*}$args
}
"move" {
# MoveRow item under parent at position index
MoveRow $w {*}$args
}
"movecolumn" {
# Move column
MoveColumn $w {*}$args
}
"set" - "update" {
# Update items
Update $w {*}$args
}
"show" - "unhide" {
# Show column
ColumnShow $w {*}$args
}
"size" {
# Total number items
return [Size $w]
}
"sort" {
# Sort using current sort column and direction
Sort $w
}
"sortbycolumn" {
# Sort by column
SortByColumn $w {} {*}$args
}
"sortcolumn" {
# Return current sort column
return [lindex [SortInfo $w] 0]
}
"sortinfo" {
# Return current sort column, order, and mode
return [SortInfo $w]
}
"sortorder" {
# Return current sort order
return [lindex [SortInfo $w] 1]
}
}
}
#
# Copy treeview items to Clipboard
#
# Args: w = Treeview widget
# items = Items to copy to clipboard or {} to use current selection
# Returns: nothing
#
proc ::treeviewlib::Copy {w {items {}}} {
set data ""
if {[llength $items] == 0} {
set items [$w selection]
}
if {"tree" in [$w cget -show]} {
foreach item $items {
append data [join [concat [list [$w item $item -text]] [$w item $item -values]] \t] \n
}
} else {
foreach item $items {
append data [join [$w item $item -values] \t] \n
}
}
clipboard clear -displayof $w
clipboard append -displayof $w -- $data
}
#
# Collapse/expand treeview items
#
# Args: w = Treeview widget
# boolean = Open state boolean
# items = Items to open/close
# depth = Depth to recursively open/close. Default is 1.
# Returns: nothing
#
proc ::treeviewlib::ExpandCollapse {w boolean items {depth 1}} {
incr depth -1
foreach item $items {
$w item $item -open $boolean
if {$depth > 0} {
set children [$w children $item]
if {[llength $children] > 0} {
ExpandCollapse $w $boolean $children $depth
}
}
}
}
#
# Get treeview items
#
# Args: w = Treeview widget
# items = Items to retrieve
# display = Use display column order boolean. Default is false.
# Returns: Data from widget in list of lists format
#
proc ::treeviewlib::Get {w items {display 0}} {
set is_tree [expr {"tree" in [$w cget -show]}]
set result [list]
# If data and display column order is the same, use faster method
if {$display} {
set columns [$w cget -displaycolumns]
if {$columns eq "#all"} {
set display 0
}
}
# Get data
if {!$display} {
# Stored order
if {!$is_tree} {
foreach item $items {
lappend result [$w item $item -values]
}
} else {
foreach item $items {
lappend result [concat [list [$w item $item -text]] [$w item $item -values]]
}
}
} else {
# Display order
foreach item $items {
set list [list]
if {$is_tree} {
lappend list [$w item $item -text]
}
foreach column $columns {
lappend list [$w set $item $column]
}
lappend result $list
}
}
if {[llength $items] > 1} {
return $result
} else {
return [lindex $result 0]
}
}
#
# Insert items into treeview widget
#
# Args: w = Treeview widget
# parent = Parent item
# index = Index to insert at
# data = Data to insert in repeating id and data elements format
# Returns: nothing
#
proc ::treeviewlib::Insert {w parent index data} {
set is_tree [expr {"tree" in [$w cget -show]}]
lassign [$w yview] start end
upvar #0 ::treeviewlib::data$w cache
set add [info exists cache]
variable fakeId
# Insert data
foreach {id record} $data {
if {[$w exists $id]} {
set id [format "fakeId%05d" [incr fakeId]]
}
if {!$is_tree} {
$w insert $parent $index -id $id -values $record
} else {
$w insert $parent $index -id $id -text [lindex $record 0] -values [lrange $record 1 end]
}
if {$add} {
lappend cache $id
}
if {[string is integer $index]} {
incr index
}
}
# Restripe
if {$parent eq ""} {
Restripe $w $parent [lindex $data 0]
}
# Scroll to end
if {$index eq "end" && $start > 0.0 && $end == 1.0} {
$w yview moveto 1.0
}
}
#
# Insert items into treeview widget without record ids
#
# Args: w = Treeview widget
# parent = Parent item
# index = Index to insert at
# data = Data to insert in list of lists format
# Returns: list of item ids for inserted data
#
proc ::treeviewlib::Insert_No_Id {w parent index data} {
set result [list]
set is_tree [expr {"tree" in [$w cget -show]}]
lassign [$w yview] start end
upvar #0 ::treeviewlib::data$w cache
set add [info exists cache]
# Insert data
foreach {record} $data {
if {!$is_tree} {
lappend result [$w insert $parent $index -values $record]
} else {
lappend result [$w insert $parent $index -text [lindex $record 0] -values [lrange $record 1 end]]
}
if {$add} {
lappend cache [lindex $result end]
}
if {[string is integer $index]} {
incr index
}
}
# Restripe
if {$parent eq ""} {
Restripe $w $parent [lindex $result 0]
}
# Scroll to end
if {$index eq "end" && $start > 0.0 && $end == 1.0} {
$w yview moveto 1.0
}
return $result
}
#
# Lower (move down) or raise (move up) items in treeview widget
#
# Args: w = Treeview widget
# fn = Function to perform: raise or lower
# items = Items to raise or lower
# Returns: nothing
#
proc ::treeviewlib::RaiseLower {w fn items} {
set incr [expr {$fn eq "lower" ? 1 : -1}]
set list [list]
foreach item $items {
set index [expr {[$w index $item] + $incr}]
if {$index < 0} return
lappend list $item [$w parent $item] $index
}
$w detach $items
foreach {item parent index} $list {
$w move $item $parent $index
}
}
#
# Treeview size (count of items)
#
# Args: w = Treeview widget
# parent = Parent item
# recursive = Count child elements boolean
# only_open = Only count children of open elements boolean
# Returns: Count of widget items
#
proc ::treeviewlib::Size {w {parent {}} {recursive 1} {only_open 1}} {
set items [$w children $parent]
set count [llength $items]
if {$recursive} {
foreach item $items {
if {!$only_open || ($only_open && [$w item $item -open])} {
incr count [Size $w $item $recursive $only_open]
}
}
}
return $count
}
#
# Update/insert items in treeview widget
#
# Args: w = Treeview widget
# data = Data to update in repeating id and data elements format
# Returns: nothing
#
proc ::treeviewlib::Update {w data} {
set is_tree [expr {"tree" in [$w cget -show]}]
set do_sort 0
upvar #0 ::treeviewlib::data$w cache
set add [info exists cache]
foreach {id record} $data {
if {[$w exists $id]} {
# Update item
if {!$is_tree} {
$w item $id -values $record
} else {
$w item $id -text [lindex $record 0] -values [lrange $record 1 end]
}
} else {
# Insert item
if {!$is_tree} {
$w insert {} end -id $id -values $record
} else {
$w insert {} end -id $id -text [lindex $record 0] -values [lrange $record 1 end]
}
if {$add} {
lappend cache $id
}
set do_sort 1
}
}
# Do sort
if {$do_sort} {
SortUpdate $w {} {*}[SortInfo $w]
}
}
###############################################################################
# Selection Functions
###############################################################################
#
# Selection functions handler for menu & keyboard events
#
# Args: id = Widget id
# fn = Function to perform
# args = Arguments to fn
# Returns: Nothing
#
proc ::treeviewlib::select_handler {id fn args} {
variable widgets
select $widgets($id) $fn {*}$args
}
#
# Perform selection functions
#
# Args: w = Treeview widget
# fn = Function to perform
# args = Tree item ids or function dependent
# Returns: Nothing except get where data is returned
# Effects: Updated tree
#
proc ::treeviewlib::select {w fn args} {
switch -exact -- $fn {
"all" {
# Select all items
#SelectAllRecursive $w {}
$w selection set [$w children {}]
}
"clear" {
# Unselect all items
$w selection set {}
}
"goto" - "see" {
# See items, use current selection as default
if {[llength $args] == 0} {
set args [$w selection]
}
foreach item $args {
$w see $item
break
}
}
"invert" {
# Invert selection
set sel [$w selection]
#SelectAllRecursive $w {}
$w selection set [$w children {}]
$w selection remove $sel
}
"select" {
# Select current active item or a list of items
if {[llength $args] == 0 && [$w focus] ne ""} {
set args [list [$w focus]]
}
if {[llength $args] > 0} {
$w selection [expr {[$w cget -selectmode] eq "browse" ? "set" : "add"}] $args
$w see [lindex $args 0]
}
}
"size" {
# Number of selected items
return [llength [$w selection]]
}
"toggle" {
# Toggle selection state of current item
if {[llength $args] == 0 && [$w focus] ne ""} {
set args [list [$w focus]]
}
$w selection toggle $args
$w see [lindex $args 0]
}
"unselect" {
# Unselect current active item or list of items
if {[llength $args] == 0 && [$w focus] ne ""} {
set args [list [$w focus]]
}
if {[llength $args] > 0} {
$w selection remove $args
$w see [lindex $args 0]
}
}
}
}
#
# Select all tree items
#
# Args: w = Treeview widget
# parent = Parent of items to recursively select
# Returns: nothing
#
proc ::treeviewlib::SelectAllRecursive {w parent} {
set items [$w children $parent]
$w selection add $items
foreach item $items {
set sublist [$w children $item]
if {[llength $sublist] > 0} {
SelectAllRecursive $w $item
}
}
}
###############################################################################
# Column Hide Functions
###############################################################################
#
# Hide columns
#
# Args: w = Treeview widget
# args = Column ids to hide
# Returns: nothing
#
proc ::treeviewlib::ColumnHide {w args} {
# Get current columns
set columns [$w cget -displaycolumns]
if {[lindex $columns 0] eq "#all"} {
set columns [$w cget -columns]
}
# Remove from displayed columns list
foreach column $args {
if {$column eq "#0"} continue
if {[set index [lsearch $columns [$w column $column -id]]] > -1} {
set columns [lreplace $columns $index $index]
}
}
$w configure -displaycolumns $columns
}
#
# Show columns
#
# Args: w = Treeview widget
# args = Column ids to show
# Returns: nothing
#
proc ::treeviewlib::ColumnShow {w args} {
# Get current columns
set columns [$w cget -columns]
set displaycolumns [$w cget -displaycolumns]
if {[lindex $displaycolumns 0] eq "#all"} {
return
}
# Add to displayed columns list
foreach column $args {
if {$column eq "#0"} continue
set column [$w column $column -id]
if {$column in $columns && $column ni $displaycolumns} {
set index end
foreach temp [lrange $columns [expr {[lsearch $columns $column] + 1}] end] {
if {$temp in $displaycolumns} {
set index [lsearch $displaycolumns $temp]
break
}
}
set displaycolumns [linsert $displaycolumns $index $column]
}
}
$w configure -displaycolumns $displaycolumns
}
###############################################################################
# Column Menu Functions
###############################################################################
#
# Handler for pop-up menus to store selected column
#
# Args: w = Treeview widget
# m = Menu widget
# X = Root window x coord for popup menu position
# Y = Root window y coord for popup menu position
# x = X coord of mouse pointer in widget
# y = Y coord of mouse pointer in widget
# Returns: nothing
#
proc ::treeviewlib::post_menu {w m X Y x y} {
if {[$w identify region $x $y] ne "heading"} return
variable column [$w column [$w identify column $x $y] -id]
# Post menu
tk_popup $m $X $Y
return -code break
}
#
# Callback for pop-up menu items
#
# Args: w = Treeview widget
# fn = Function to perform
# Returns: nothing
#
proc ::treeviewlib::menu_callback {w fn} {
variable column
switch -- $fn {
"autosize" {
# Auto size column to fit data
ColumnAutoSize $w [list $column]
}
"autosize_all" {
# Auto size all columns to fit data
ColumnAutoSize $w
}
"hide" {
# Hide current column
ColumnHide $w $column
}
"move_left" - "move_right" {
# Move column left or right
set incr [expr {$fn eq "move_left" ? -1 : 1}]
set columns [$w cget -displaycolumns]
if {$columns eq "#all"} {
set columns [$w cget -columns]
}
set index [lsearch $columns $column]
set new [lindex $columns [incr index $incr]]
if {$new ne "" && $column ne $new} {
MoveColumn $w $column $new
}
}
"show" {
# Show hidden column
# Get list of hidden columns
set display [$w cget -displaycolumns]
if {$display eq "#all"} {
return
}
# Create menu
set m $w.column_show