-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbartabs.tcl
executable file
·2300 lines (2058 loc) · 67.2 KB
/
bartabs.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
###########################################################
# Name: bartabs.tcl
# Author: Alex Plotnikov ([email protected])
# Date: 01/12/2023
# Brief: Handles the tab bar widget.
# License: MIT.
###########################################################
package provide bartabs 1.6.10
# ________________________ NS bartabs _________________________ #
namespace eval ::bartabs {
# IDs for new bars & tabs
variable NewBarID -1 NewTabID -1 NewTabNo -1
variable NewAfterID; array set NewAfterID [list]
# images made by base64
image create photo bts_ImgLeft \
-data {iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAElBMVEUAAABJSUmSkpJtbW22trbb
29vYK8X/AAAAAXRSTlMAQObYZgAAAEBJREFUCNdjAANGBigQhNKMjlCGEJTBqAplCIVCGIwqKopg
hrATjKGkZAiRMgIyIEJABlTIEGYDjMEoiGQp3BkAc58E+W1dC9QAAAAASUVORK5CYII=}
image create photo bts_ImgRight \
-data {iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAElBMVEUAAABJSUmSkpJtbW22trbb
29vYK8X/AAAAAXRSTlMAQObYZgAAAEBJREFUCNdjYGAQYIACQRhDRADGUIQxggSgjFCokJCTkwCU
oWIIZggrKcEYygIQBlAAwgAKQBiCMLsE0C2FCAAAa1IEzBjs2sUAAAAASUVORK5CYII=}
image create photo bts_ImgNone \
-data {iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEUAAACnej3aAAAAAXRSTlMA
QObYZgAAAAtJREFUCNdjIBEAAAAwAAFletZ8AAAAAElFTkSuQmCC}
image create photo bts_ImgClose \
-data {iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAALVBMVEUAAAAAACTb29u2trbt4+Ll
4eHw4eD/6N/y3Nv85N/94t3m29vn0dPjz8/dyssim+gAAAAAAXRSTlMAQObYZgAAAEdJREFUCNdj
wAvaFZiWgWgORSUlYQcgg11QSFFQAchgUhQUFLoAkjskKKgNVqwkKKjHAJMCqeGEKWYzBGoPAMk5
KTCp4rURAEWmB5A5tzUJAAAAAElFTkSuQmCC}
variable BarsList [list]
; proc drawAll {} {
# Draws all bars. Used at updating themes etc.
foreach bars $::bartabs::BarsList {$bars drawAll}
}
#_______________________
proc messageBox {type ttl msg args} {
# Runs Tk's or apave's ok/yes/no/cancel dialogue.
# type - ok, yesno or yesnocancel
# ttl - title
# ttl - message
# args - additional arguments of tk_messageBox
# Returns 1 if 'yes' chosen, 2 if 'no', 0 otherwise.
# try the apave package's dialogue
if {[catch {set res [::apave::obj $type ques $ttl $msg]}]} {
# or run the standard tk_messageBox
set res [tk_messageBox -title $ttl -message $msg -type $type \
-icon question {*}$args]
set res [expr {$res eq {yes} ? 1 : ($res eq {no} ? 2 : 0)}]
}
return $res
}
## ____________ EONS bartabs ____________ ##
}
# ____________ bartabs class hierarchy ____________ #
oo::class create ::bartabs::Tab {
}
oo::class create ::bartabs::Bar {
superclass ::bartabs::Tab
}
oo::class create ::bartabs::Bars {
superclass ::bartabs::Bar
}
# ________________________ Tab _______________________ #
## ____________ Private methods of Tab ____________ ##
oo::define ::bartabs::Tab {
method My {ID} {
# Creates a caller of method.
# ID - ID of caller
set t [string range $ID 0 2]
oo::objdefine [self] "method $ID {args} { \
set m \[lindex \$args 0\] ; \
if {\$m in {{} -1}} {return {}} ; \
if {\$m eq {create} && {$t} eq {bar} || \$m in {cget configure} && {$t} eq {tab}} { \
set args \[lreplace \$args 0 0 Tab_\$m\]} ; \
return \[my {*}\$args\]}"
}
#_______________________
method ID {} {
# Returns ID of caller.
lindex [uplevel 1 {self caller}] 2
}
#_______________________
method IDs {TID} {
# Returns a pair of TID and BID.
list $TID [my $TID cget -BID]
}
#_______________________
method Tab_Create {BID TID w text} {
# Creates a tab widget (frame, label, button).
# w - parent frame
# text - tab's label
# Returns a list of created widgets of the tab.
lassign [my $BID cget -relief -bd -padx -pady -BGMAIN] relief bd padx pady bgm
lassign [my $TID cget -wb -wb1 -wb2] wb wb1 wb2
if {!$bd} {set relief flat}
if {![my Tab_Is $wb]} {
if {$wb eq {}} {
set ::bartabs::NewTabNo [expr {($::bartabs::NewTabNo+1)%1000000}]
set wb $w.$TID[format %06d $::bartabs::NewTabNo]
set wb1 $wb.l
set wb2 $wb.b
}
my $TID configure -wb $wb -wb1 $wb1 -wb2 $wb2
ttk::frame $wb -borderwidth [expr {$bd? $bd : 2}]
ttk::label $wb1
if {[my TtkTheme]} {
ttk::button $wb2 -style ClButton$BID -image bts_ImgNone \
-command [list [self] $TID close yes -withicon yes] -takefocus 0
} else {
button $wb2 -relief flat -borderwidth 0 -highlightthickness 0 -image bts_ImgNone \
-command [list [self] $TID close yes -withicon yes] -takefocus 0 -background $bgm
}
}
$wb configure -relief $relief
$wb1 configure -relief flat -padding "$padx $pady $padx $pady" \
{*}[my Tab_Font $BID]
lassign [my Tab_TextEllipsed $BID $text] text ttip
if {[set tip [my $TID cget -tip]] ne {}} {
my $TID configure -tip $tip ;# run baltip after creating $wb1 & $wb2
}
$wb1 configure -text $text -background $bgm
if {[my Tab_Iconic $BID]} {
$wb2 configure -state normal
} else {
$wb2 configure -state disabled -image {}
}
list $wb $wb1 $wb2
}
#_______________________
method Tab_create {tabCom label} {
# Creates tab method and registers it. Defined by "My".
set BID [my ID]
if {[set TID [my $BID tabID $label]] eq {}} {
return -code error "No label {$label} in $BID"
}
; proc $tabCom {args} "return \[[self] $TID {*}\$args\]"
set lObj [my $BID cget -TABCOM]
my $BID configure -TABCOM [lappend lObj [list $TID $tabCom]]
}
#_______________________
method Tab_ExpandOption {BID expand} {
# Gets a real -expand option, counting that it may be set as a number>1
# meaning "starting from this number do expanding, otherwise not"
# expand - original value of -expand option
if {[string is digit $expand] && $expand>1} {
set tabs [my $BID cget -TABS]
set expand [expr {$expand<[llength $tabs]}]
}
return $expand
}
#_______________________
method Tab_cget {args} {
# Gets options of tab.
# args - list of options
# Returns a list of values or one value if args is one option.
variable btData
lassign [my Tab_BID [set TID [my ID]]] BID i tab
lassign $tab tID tdata
set res [list]
foreach opt $args {
switch -- $opt {
-BID {lappend res $BID}
-text - -wb - -wb1 - -wb2 - -pf {
if {[catch {lappend res [dict get $tdata $opt]}]} {
lappend res {}
}
}
-index {
if {$i<([dict get $btData $BID -LLEN]-1)} {
lappend res $i
} else {
lappend res end
}
}
-width { ;# width of tab widget
lassign [my Tab_DictItem $tab] tID text wb wb1 wb2
if {![my Tab_Is $wb]} {
lappend res 0
} else {
set b1 [ttk::style configure TLabel -borderwidth]
if {$b1 eq {}} {set b1 0}
lassign [my $BID cget -bd -expand -static] bd expand static
set bd [expr {$bd?2*$b1:0}]
set b2 [expr {[my Aux_WidgetWidth $wb2]-3}]
set expand [my Tab_ExpandOption $BID $expand]
set expand [expr {$expand||![my Tab_Iconic $BID]?2:0}]
lappend res [expr {[my Aux_WidgetWidth $wb1]+$b2+$bd+$expand}]
}
}
default { ;# user's options
if {[catch {lappend res [dict get $tdata $opt]}]} {lappend res {}}
}
}
}
if {[llength $args]==1} {return [lindex $res 0]}
return $res
}
#_______________________
method Tab_configure {args} {
# Sets values of options for a tab.
# args - list of pairs "option value"
lassign [my Tab_BID [set TID [my ID]]] BID i tab
lassign $tab tID data
foreach {opt val} $args {
dict set data $opt $val
if {$opt eq {-tip}} { ;# configure the tab's tip
lassign [my $TID cget -wb1 -wb2] wb1 wb2
if {$wb1 ne {}} {
catch {
baltip::tip $wb1 $val -under 3
baltip::tip $wb2 $val -under 3
}
}
}
}
set tab [list $TID $data]
my $BID configure -TABS [lreplace [my $BID cget -TABS] $i $i $tab]
}
#_______________________
method Tab_DictItem {TID {data ""}} {
# Gets item data from a tab item (ID + data).
# TID - tab ID or the tab item (ID + data).
# data - tab's data (list of option-value)
# If 'data' omitted, TID is a tab item (ID + data).
# If the tab's attribute is absent, it's meant to be "".
# Returns a list of values: ID, text, wb, wb1, wb2, pf.
if {$data eq {}} {lassign $TID TID data}
set res [list $TID]
foreach a {-text -wb -wb1 -wb2 -pf} {
if {[dict exists $data $a]} {
lappend res [dict get $data $a]
} else {
lappend res {}
}
}
return $res
}
#_______________________
method Tab_ItemDict {TID text {wb ""} {wb1 ""} {wb2 ""} {pf ""}} {
# Returns a tab item (ID + data) from item data.
# text - tab's text;
# wb - tab's frame widget
# wb1 - tab's label widget
# wb2 - tab's button widget
# pf - "p" for tab packed, "" for tab forgotten
list $TID [list -text $text -wb $wb -wb1 $wb1 -wb2 $wb2 -pf $pf]
}
#_______________________
method Tab_Data {BID text} {
# Creates data of new tab.
# text - new tab's label
# The bar is checked for a duplicate of 'text'.
# Returns a tab item or "" (if duplicated).
variable btData
if {[dict exists $btData $BID] && [my $BID tabID $text] ne {}} {return {}}
my My tab[incr ::bartabs::NewTabID]
my Tab_ItemDict tab$::bartabs::NewTabID $text
}
#_______________________
method Tab_BID {TID {act ""}} {
# Gets BID from TID.
# act - if "check", only checks the existance of TID
# If 'act' is "check" and a bar not found, -1 is returned, otherwise BID.
# Returns a list of 1. BID (or -1 if no bar found) 2. index of the tab in tab list 3. the tab data.
variable btData
set BID {}
dict for {bID bInfo} $btData {
set tabs [my $bID cget -TABS]
if {[set i [my Aux_IndexInList $TID $tabs]] > -1} {
set BID $bID
break
}
}
if {$act eq {check}} {return $BID}
if {$BID eq {}} {
return -code error "bartabs: tab ID $TID not found in the bars"
}
list $BID $i [lindex $tabs $i]
}
#_______________________
method Tab_Bindings {BID} {
# Sets bindings on events of tabs.
lassign [my $BID cget -static -FGOVER -BGOVER -WWID] static fgo bgo wwid
foreach tab [my $BID listTab] {
lassign $tab TID text wb wb1 wb2
if {[my Tab_Is $wb]} {
set bar "[self] $BID"
set tab "[self] $TID"
set ctrlBP "$tab OnCtrlClick ; break"
foreach w [list $wb $wb1 $wb2] {
bind $w <Enter> "$bar OnEnterTab $TID $wb1 $wb2 $fgo $bgo"
bind $w <Leave> "[self] $TID OnLeaveTab $wb1 $wb2"
bind $w <Button-3> "[self] $TID OnPopup %X %Y"
bind $w <Control-ButtonPress> $ctrlBP
}
bind $wb <Control-ButtonPress> $ctrlBP
bind $wb <ButtonPress> "[self] $BID OnButtonPress $TID $wb1 {}"
bind $wb1 <ButtonPress> "[self] $BID OnButtonPress $TID $wb1 %x"
bind $wb1 <ButtonRelease> "[self] $BID OnButtonRelease $wb1 %x"
bind $wb1 <Motion> "[self] $BID OnButtonMotion $wb $wb1 %x %y"
}
}
bind [lindex $wwid 0] <Button-3> "[self] $BID OnPopup %X %Y $BID"
}
#_______________________
method Tab_Font {BID} {
# Gets a font attributes for tab label.
set font [my $BID cget -font]
if {$font eq {}} {
if {[set font [ttk::style configure TLabel -font]] eq {}} {
set font TkDefaultFont
}
set font [font actual $font]
}
return "-font {$font}"
}
#_______________________
method Tab_MarkAttrs {BID TID {withbg yes} {wb2 ""}} {
# Gets image & mark attributes of marks.
# TID - ID of current tab
# withbg - if true, gets also background
# wb2 - tab's button
# Returns string of attributes if any.
lassign [my $BID cget \
-mark -imagemark -fgmark -bgmark -IMAGETABS -FGMAIN -BGMAIN -FGDSBL -BGDSBL] \
marktabs imagemark fgm bgm imagetabs fgmain bgmain fgdsbl bgdsbl
set res {}
if {[my Disabled $TID]} {
set imagemark {}
if {$wb2 ne {}} {$wb2 configure -state disabled}
set res " -foreground $fgdsbl"
if {$withbg} {append res " -background $bgdsbl"}
} elseif {[lsearch $marktabs $TID]>-1} {
if {$imagemark eq {}} {
if {$fgm eq {}} {set fgm $fgmain} ;# empty value - no markable tabs
set res " -foreground $fgm"
if {$withbg} {
if {$bgm eq {}} {set bgm $bgmain}
append res " -background $bgm"
}
if {$wb2 ne {}} {$wb2 configure -image bts_ImgNone}
}
} else {
set imagemark {}
if {[set i [lsearch -index 0 $imagetabs $TID]]>-1} {
set imagemark [lindex $imagetabs $i 1]
} elseif {$wb2 ne {}} {
$wb2 configure -image bts_ImgNone
}
}
if {$imagemark ne {}} {
set res " -image $imagemark"
if {$wb2 ne {}} {
$wb2 configure {*}$res
catch {$wb2 configure -style ClButton$BID}
}
}
return $res
}
#_______________________
method Tab_SelAttrs {fnt fgsel bgsel} {
# Gets font attributes of selected tab.
# fnt - original font attributes
# fgsel - foreground for selection
# bgsel - background for selection
# If both set, fgsel and bgsel mean colors
# If bgsel=="", fgsel!="", fgsel is a widget to get attributes
# If fgsel=="", 'selection' is 'underlining'
lassign $fnt opt val
if {$fgsel eq {}} {
dict set val -underline 1
} else {
if {$bgsel eq {}} {
set bgsel [ttk::style configure $fgsel -selectbackground]
set fgsel [ttk::style configure $fgsel -selectforeground]
}
set opt "-foreground $fgsel -background $bgsel $opt"
}
return "$opt {$val}"
}
#_______________________
method Tab_MarkBar {BID {TID "-1"}} {
# Marks the tabs of a bar .
# TID - ID of the current tab
lassign [my $BID cget -tabcurrent -fgsel -bgsel -select -FGMAIN -BGMAIN] \
tID fgs bgs fewsel fgm bgm
if {$TID in {{} {-1}}} {set TID $tID}
foreach tab [my $BID listTab] {
lassign $tab tID text wb wb1 wb2
if {[my Tab_Is $wb]} {
set font [my Tab_Font $BID]
set selected [expr {$tID == $TID || [lsearch $fewsel $tID]>-1}]
if {$selected} {set font [my Tab_SelAttrs $font $fgs $bgs]}
$wb1 configure {*}$font
set attrs [my Tab_MarkAttrs $BID $tID [expr {!$selected}] $wb2]
if {$attrs ne {} && {-image} ni $attrs } {
$wb1 configure {*}$attrs
} elseif {!$selected} {
$wb1 configure -foreground $fgm -background $bgm
}
}
}
my $BID configure -tabcurrent $TID
}
#_______________________
method Tab_MarkBars {{BID -1} {TID -1}} {
# Marks the tabs.
# BID - bar ID (if omitted, all bars are scanned)
# TID - ID of the current tab
variable btData
if {$BID == -1} {
dict for {BID barOpts} $btData {my Tab_MarkBar $BID}
} else {
my Tab_MarkBar $BID $TID
}
}
#_______________________
method Tab_TextEllipsed {BID text {lneed -1}} {
# Returns a tab's label and tip.
# text - label
# lneed - label length anyway
lassign [my $BID cget -lablen -ELLIPSE] lablen ellipse
if {$lneed ne -1} {set lablen $lneed}
if {$lablen && [string length $text]>$lablen} {
set ttip $text
set text [string range $text 0 $lablen-1]
append text $ellipse
} else {
set ttip {}
}
list $text $ttip
}
#_______________________
method Tab_Iconic {BID} {
# Gets a flag "tabs with icons".
# Returns "yes", if tabs are supplied with icons.
expr {![my $BID cget -static]}
}
#_______________________
method Tab_Pack {BID TID wb wb1 wb2} {
# Packs a tab widget.
# wb, wb1, wb2 - tab's widgets
lassign [my $BID cget -static -expand] static expand
if {[my Tab_Iconic $BID]} {
pack $wb1 -side left
pack $wb2 -side left
} else {
pack $wb1 -side left -fill x
pack forget $wb2
}
set expand [my Tab_ExpandOption $BID $expand]
if {$expand} {
pack $wb -side left -fill x -expand 1
} else {
pack $wb -side left
}
my $TID configure -pf "p"
}
#_______________________
method Tab_RemoveLinks {BID TID} {
# Removes a tab's links to lists.
foreach o {-IMAGETABS -TABCOM -mark -disable -select} {
set l [my $BID cget $o]
for {set i 0} {$i>-1} {} {
if {[set i [lsearch -index 0 $l $TID]]>-1} {
set l [lreplace $l $i $i]
my $BID configure $o $l
}
}
}
my Tab_MarkBars $BID
}
#_______________________
method Tab_Is {wb} {
# Checks if 'wb' is an existing tab widget.
# wb - path
expr {$wb ne {} && [winfo exists $wb]}
}
#_______________________
method Tab_CloseFew {{TID -1} {left no} args} {
# Closes tabs of bar.
# TID - ID of the current tab or -1 if to close all
# left - "yes" if to close all at left of TID, "no" if at right
# args - options (if contains -skipsel, selected tabs aren't closed)
set BID [my ID]
if {$TID ne {-1}} {lassign [my Tab_BID $TID] BID icur}
set tabs [my $BID listTab]
set skipsel [expr {[lsearch $args -skipsel]>-1}]
set seltabs [my $BID cget -select]
set doupdate no
set first 1
for {set i [llength $tabs]} {$i} {} {
incr i -1
set tID [lindex $tabs $i 0]
if {!$skipsel || $tID ni $seltabs} {
if {$TID eq {-1} || ($left && $i<$icur) || (!$left && $i>$icur)} {
if {![set res [my $tID close no -first $first]]} break
if {$res==1} {set doupdate yes}
set first 0 ;# -first option is "1" for the very first closed tab
}
}
}
if {$doupdate} {
my $BID clear
if {$TID eq {-1}} {
my $BID Refill 0 yes
} else {
my $BID $TID show yes
}
}
}
#_______________________
method PrepareCmd {TID BID opt args} {
# Prepares a command bound to an action on a tab.
# opt - command option (-csel, -cmov, -cdel)
# args - additional argumens of the command
# The commands can include wildcards: %b for bar ID, %t for tab ID, %l for tab label.
# Returns "" or the command if 'opt' exists in 'args'.
variable btData
if {[dict exists $btData $BID $opt]} {
set com [dict get $btData $BID $opt]
if {$TID>-1} {
set label [my $TID cget -text]
} else {
set label {}
}
set label [string map {\{ ( \} )} $label]
lappend com {*}$args
return [string map [list %b $BID %t $TID %l $label] $com]
}
return {}
}
#_______________________
method Tab_Cmd {opt args} {
# Executes a command bound to an action on a tab.
# opt - command option (-csel, -cmov, -cdel)
# args - additional argumens of the command
# The commands can include wildcards: %b for bar ID, %t for tab ID, %l for tab label.
# Returns 1, if no command set; otherwise: 1 for Yes, 0 for No, -1 for Cancel.
lassign [my IDs [my ID]] TID BID
if {[set com [my PrepareCmd $TID $BID $opt {*}$args]] ne {}} {
if {[catch {set res [{*}$com]}]} {set res yes}
if {$res eq {} || !$res} {return 0}
return $res
}
return 1
}
#_______________________
method Tab_BeCurrent {} {
# Makes the tab be currently visible.
if {[set TID [my ID]] in {{} {-1}} || [my Disabled $TID]} return
set BID [my $TID cget -BID]
my $TID Tab_Cmd -csel ;# command before the selection shown
my Tab_MarkBar $BID $TID
if {[set wb2 [my $TID cget -wb2]] ne {} && \
![string match *bartabs::* [$wb2 cget -image]] &&
$TID ni [my $BID listFlag "m"]} {
$wb2 configure -image bts_ImgNone
}
my $BID Bar_Cmd2 -csel2 $TID ;# command after the selection shown
}
#_______________________
method Disabled {TID} {
# Checks if the tab is disabled.
set dsbltabs [my [my $TID cget -BID] cget -disable]
expr {[lsearch $dsbltabs $TID]>-1}
}
## ____________ Event handlers ____________ ##
method DestroyMoveWindow {} {
# Destroys the moving window zombi.
set BID [my ID]
set movWin [lindex [my $BID cget -MOVWIN] 0]
catch {destroy $movWin}
my $BID configure -MOVX {} -wb1 {}
}
#_______________________
method OnEnterTab {TID wb1 wb2 fgo bgo} {
# Handles the mouse pointer entering a tab.
# wb1, wb2 - tab's widgets
# fgo, bgo - colors of "mouse over the tab"
if {[my Disabled $TID]} return
$wb1 configure -foreground $fgo -background $bgo
if {[my Tab_Iconic [my ID]]} {$wb2 configure -image bts_ImgClose}
}
#_______________________
method OnLeaveTab {wb1 wb2} {
# Handles the mouse pointer leaving a tab.
# wb1, wb2 - tab's widgets
lassign [my IDs [my ID]] TID BID
if {[my Disabled $TID]} return
if {![winfo exists $wb1]} return
lassign [my $BID cget -FGMAIN -BGMAIN] fgm bgm
$wb1 configure -foreground $fgm -background $bgm
my Tab_MarkBars $BID
if {"-image" ni [set attrs [my Tab_MarkAttrs $BID $TID 0 $wb2]] && \
[my Tab_Iconic $BID]} {
$wb2 configure -image bts_ImgNone
catch {$wb2 configure -style ClButton$BID}
}
}
#_______________________
method OnButtonPress {TID wb1 x} {
# Handles the mouse clicking a tab.
# wb1 - tab's label
# x - x position of the mouse pointer
if {[my Disabled $TID]} return
my [set BID [my ID]] configure -MOVX $x
if {$TID eq {}} {set TID [my $BID tabID [$wb1 cget -text]]}
my $TID Tab_BeCurrent
}
#_______________________
method OnButtonMotion {wb wb1 x y} {
# Handles the mouse moving over a tab.
# wb - tab's frame
# wb1 - tab's label
# x, y - positions of the mouse pointer
lassign [my [set BID [my ID]] cget \
-static -FGMAIN -FGOVER -BGOVER -MOVWIN -MOVX -MOVX0 -MOVX1 -MOVY0] \
static fgm fgo bgo movWin movX movx movx1 movY0
if {$movX eq {} || $static} return
# dragging the tab
if {![winfo exists $movWin]} {
# make the tab's replica to be dragged
toplevel $movWin
if {$::tcl_platform(platform) == "windows"} {
wm attributes $movWin -alpha 0.0
} else {
wm withdraw $movWin
}
if {[tk windowingsystem] eq "aqua"} {
::tk::unsupported::MacWindowStyle style $movWin help none
} else {
wm overrideredirect $movWin 1
}
set movx [set movx1 $x]
set movX [expr {[winfo pointerx .]-$x}]
set movY0 [expr {[winfo pointery .]-$y}]
label $movWin.label -text [$wb1 cget -text] -relief solid \
-foreground black -background #7eeeee {*}[my Tab_Font $BID]
pack $movWin.label -expand 1 -fill both -ipadx 1
wm minsize $movWin [winfo reqwidth $movWin.label] [winfo reqheight $wb1]
set againstLooseFocus "[self] $BID DestroyMoveWindow"
bind $movWin <Leave> $againstLooseFocus
bind $movWin <ButtonPress> $againstLooseFocus
$wb1 configure -foreground $fgm
my $BID configure -wb1 $wb1 -MOVX1 $movx1 -MOVY0 $movY0
}
if {abs([winfo pointery .]-$movY0)>$movY0*.5} {
my $BID DestroyMoveWindow ;# too vertical
return
}
lassign [my $BID cget -WWID] wframe wlarr
lassign [split [winfo geometry $wframe] x+] wflen
lassign [split [winfo geometry $wlarr] x+] walen
lassign [split [winfo geometry $wb] x+] wbl - wbx
if {abs($x-$movx)>1 && ($wflen-$wbx+$movx1+$walen)>$x && ($wbx+$wbl-$movx1+$x)>0} {
wm geometry $movWin +$movX+$movY0
if {$::tcl_platform(platform) == "windows"} {
if {[wm attributes $movWin -alpha] < 0.1} {wm attributes $movWin -alpha 1.0}
} else {
catch {wm deiconify $movWin ; raise $movWin}
}
}
my $BID configure -MOVX [expr {$movX+$x-$movx}] -MOVX0 $x
}
#_______________________
method OnButtonRelease {wb1o x} {
# Handles the mouse releasing a tab.
# wb1o - original tab's label
# x - x position of the mouse pointer
lassign [my [set BID [my ID]] cget \
-MOVWIN -MOVX -MOVX1 -MOVY0 -FGMAIN -wb1 -tleft -tright -wbar -static] \
movWin movX movx1 movY0 fgm wb1 tleft tright wbar static
my $BID DestroyMoveWindow
if {$movX eq {} || $wb1o ne $wb1 || $static} return
# dropping the tab - find a tab being dropped at
$wb1 configure -foreground $fgm
lassign [my Aux_InitDraw $BID no] bwidth vislen bd arrlen llen
set vislen1 $vislen
set vlist [list]
set i 0
set iw1 -1
set tabssav [set tabs [my $BID cget -TABS]]
foreach tab $tabs {
lassign [my Tab_DictItem $tab] tID text _wb _wb1 _wb2 _pf
if {$_pf ne {}} {
if {$_wb1 eq $wb1} {
set vislen0 $vislen
set tab1 $tab
set iw1 $i
set TID $tID
}
set wl [expr {[winfo reqwidth $_wb1]+[winfo reqwidth $_wb2]}]
lappend vlist [list $i $vislen $wl]
incr vislen $wl
}
incr i
}
if {$iw1==-1} return ;# for sure
if {[my $TID Tab_Cmd -cmov] ni {"1" "yes" "true"}} return ;# chosen to not move
set vislen2 [expr {$vislen0+$x-$movx1}]
foreach vl $vlist {
lassign $vl i vislen wl
set rightest [expr {$i==$tright && $vislen2>(10+$vislen)}]
if {$iw1==($i+1) && $x<0} {incr vislen2 $wl}
if {($vislen>$vislen2 || $rightest)} {
set tabs [lreplace $tabs $iw1 $iw1]
set i [expr {$rightest||$iw1>$i?$i:$i-1}]
if {$rightest && $i<($llen-1) && $i==$iw1} {incr i}
set tabs [linsert $tabs $i $tab1]
set left yes
if {$rightest} {
set left no
set tleft $i
} elseif {$i<$tleft} {
set tleft $i
}
break
}
}
if {$tabssav ne $tabs} {
my $BID configure -TABS $tabs
my $BID Refill $tleft $left
my $BID Bar_Cmd2 -cmov2 $TID ;# command after the action
}
}
#_______________________
method OnCtrlClick {} {
# Handles a selection of tabs with Ctrl+click.
lassign [my IDs [my ID]] TID BID
lassign [my $BID cget -static -select] static fewsel
if {$static} return
if {[set i [lsearch $fewsel $TID]]>-1} {
set fewsel [lreplace $fewsel $i $i]
} else {
lappend fewsel $TID
}
my $BID configure -select $fewsel
my Tab_MarkBar $BID
my $BID Bar_Cmd2 -csel3 $TID ;# command after the action
}
#_______________________
method OnPopup {X Y {BID "-1"} {TID "-1"} {textcur ""}} {
# Handles the mouse right-clicking on a tab.
# X, Y - positions of the mouse pointer
if {$BID eq "-1"} {
lassign [my IDs [my ID]] TID BID
set textcur [my $TID cget -text]
}
lassign [my $BID cget -wbar -menu -USERMNU -UMNU -TABS -static -hidearrows -WWID] \
wbar popup usermnu popup0 tabs static hidearr wwid
if {$static && $hidearr && !$usermnu} {
lassign $wwid wframe wlarr wrarr
if {[catch {pack info $wlarr}] && [catch {pack info $wrarr}]} {
return ;# static absolutely
}
}
set pop $wbar.popupMenu
if {[winfo exist $pop]} {destroy $pop}
my $BID configure -LOCKDRAW 1
menu $pop -tearoff 0
set ipops [set lpops [list]]
if {$TID eq "-1"} {
set popup [list [lindex $popup 0] s {*}$popup0] ;# let "List" be in
}
foreach p $popup {
lassign $p typ label comm menu dsbl tip var
if {$menu ne {}} {set popc $pop.$menu} {set popc $pop}
foreach opt {label comm menu dsbl} {
set $opt [string map [list %b $BID %t $TID %l $textcur] [set $opt]]
}
if {[info commands [lindex $dsbl 0]] ne {}} {
;# 0/1/2 image label hotkey
lassign [{*}$dsbl $BID $TID $label] dsbl comimg comlabel hotk
} else {
lassign $dsbl dsbl comimg comlabel hotk
if {$dsbl ne {}} {set dsbl [expr $dsbl]}
set dsbl [expr {([string is boolean $dsbl] && $dsbl ne {})?$dsbl:0}]
}
if {$dsbl eq {2}} continue ;# 2 - "hide"; 1 - "disable"; 0 - "normal"
if {$dsbl} {set dsbl {-state disabled}} {set dsbl {}}
if {$comimg ne {}} {set comimg "-image $comimg"}
if {$comlabel ne {}} {set label $comlabel}
if {$comimg eq {}} {set comimg {-image bts_ImgNone}}
if {$hotk ne {}} {set hotk "-accelerator $hotk"}
switch [string index $typ 0] {
s {$popc add separator}
c {
switch [string index $typ 1] {
o - {} { ;# command
$popc add command -label $label -command $comm \
{*}$dsbl -compound left {*}$comimg {*}$hotk
}
h { ;# checkbutton
if {$comm ne {}} {set comm [list -command $comm]}
$popc add checkbutton -label $label {*}$comm -variable $var
}
}
}
m {
if {$menu eq {bartabs_cascade} && !$usermnu && $static} {
set popc $pop ;# no user mnu & static: only list of tabs be shown
} else {
if {[winfo exist $popc]} {destroy $popc}
menu $popc -tearoff 0
set popm [string range $popc 0 [string last . $popc]-1]
$popm add cascade -label $label -menu $popc \
{*}$dsbl -compound left {*}$comimg {*}$hotk
}
if {[string match {bartabs_cascade*} $menu]} {
set popi $popc
lappend lpops $popi
set ipops [my $BID FillMenuList $BID $popi $TID $menu]
}
}
}
if {$tip ne {}} {
catch {baltip::tip $popc $tip -index [$popc index end]}
}
}
if {[llength $lpops]} {
catch {::apave::obj themePopup $pop}
my Bar_MenuList $BID $TID $pop ;# main menu
foreach popi $lpops {my Bar_MenuList $BID $TID $popi $ipops}
if {$TID ne {-1}} {
lassign [my $TID cget -wb1 -wb2] wb1 wb2
bind $pop <Unmap> [list [self] $TID OnLeaveTab $wb1 $wb2]
}
my $BID DestroyMoveWindow
tk_popup $pop $X $Y
} else {
my $BID popList $X $Y
}
my $BID configure -LOCKDRAW {}
}
## ____________ Public methods of Tab ____________ ##
method show {{refill no} {lifo yes}} {
# Shows a tab in a bar and sets it current.
# refill - if "yes", update the bar
# lifo - if "yes", allows moving a tab to 0th position
# When refill=no and lifo=no, just shows a tab in its current position.
lassign [my IDs [my ID]] TID BID
if {$refill} {my $BID clear}
set itab 0
foreach tab [my $BID listTab] {
lassign $tab tID text wb wb1 wb2 pf
if {$TID eq $tID} {
set refill [expr {$pf eq {}}] ;# check if visible
break
}
incr itab
}
if {$refill && $lifo && [my $BID cget -lifo] && (![my $TID visible] || \
[string is true -strict [my $BID cget -lifoest]])} {
my $BID moveTab $TID 0
set itab 0
}
if {$refill} {my $BID Refill $itab no yes}
my $TID Tab_BeCurrent
}
#_______________________
method close {{redraw yes} args} {
# Closes a tab and updates the bar.
# redraw - if "yes", update the bar and select the new tab
# args - additional argumens of the -cdel command
# Returns "1" if the deletion was successful, otherwise 0 (no) or -1 (cancel).
lassign [my Tab_BID [set TID [my ID]]] BID icurr tabcurr
if {[my Disabled $TID]} {
set ttl [msgcat::mc Closing]
set t [my $TID cget -text]
set msg [msgcat::mc "Can't close the disabled\n\"%t\"\n\nClose others?"]
set msg [string map [list %t $t] $msg]
return [expr {[::bartabs::messageBox yesno $ttl $msg -icon question]==1}]
}
set cdel [my $BID cget -cdel]
if {$cdel eq {}} {
set res 1
} else {
set cdel [my PrepareCmd $TID $BID -cdel {*}$args]
if {[catch {set res [{*}$cdel]}]} {
set res [my $TID Tab_Cmd -cdel {*}$args]
}
}
if {$res ni {1 yes true}} {return $res}
if {$redraw} {my $BID clear}
lassign [my $BID cget -TABS -tleft -tright -tabcurrent] tabs tleft tright tcurr
my Tab_RemoveLinks $BID $TID
destroy [my $TID cget -wb]
set tabs [lreplace $tabs $icurr $icurr]
oo::objdefine [self] [list deletemethod [lindex $tabcurr 0]]
my $BID configure -TABS $tabs
if {$redraw} {
if {$icurr>=$tleft && $icurr<[llength $tabs]} {
my $BID draw
my [lindex $tabs $icurr 0] Tab_BeCurrent
} else {
if {[set TID [lindex $tabs end 0]] ne {}} {
my $TID show yes ;# last tab deleted: show the new last if any
}
}
}
my $BID Bar_Cmd2 -cdel2 ;# command after the action