-
Notifications
You must be signed in to change notification settings - Fork 1
/
apavebase.tcl
executable file
·4073 lines (3798 loc) · 141 KB
/
apavebase.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: apavebase.tcl
# Author: Alex Plotnikov (aplsimple@gmail.com)
# Date: 12/09/2021
# Brief: Handles APaveBase class, sort of geometry manager.
# License: MIT.
###########################################################
# ________________________ TCLLIBPATH _________________________ #
# use TCLLIBPATH variable (some tclkits don't see it)
catch {
foreach _ [lreverse $::env(TCLLIBPATH)] {
set _ [file normalize $_]
if {[lsearch -exact $::auto_path $_]<0 && [file exists $_]} {
set ::auto_path [linsert $::auto_path 0 $_]
}
}
}
unset -nocomplain _
# _____ Remove installed subpackages of apave _____ #
foreach _ {baltip bartabs hl_tcl} {
catch {package forget $_}
catch {namespace delete ::$_}
}
unset -nocomplain _
# ________________________ apave NS _________________________ #
namespace eval ::apave {
variable SRCDIR [file normalize [file dirname [info script]]]
## ________________________ apave variables _________________________ ##
variable ISBALTIP yes
variable cursorwidth 1
variable apaveDir [file dirname [info script]]
variable _AP_ICO { none folder OpenFile SaveFile saveall print font color \
date help home misc terminal run tools file find replace other view \
categories actions config pin cut copy paste plus minus add delete \
change diagram box trash double more undo redo up down previous next \
previous2 next2 upload download tag tagoff tree lock light restricted \
attach share mail www map umbrella gulls sound heart clock people info \
err warn ques retry yes no ok cancel exit }
variable _AP_IMG; array set _AP_IMG [list]
variable _AP_VARS; array set _AP_VARS [list]
set _AP_VARS(APPICON) {}
set _AP_VARS(.,MODALS) 0
set _AP_VARS(WPAVE) {}
set _AP_VARS(TIMW) [list]
set _AP_VARS(LINKFONT) [list -underline 1]
set _AP_VARS(INDENT) " "
set _AP_VARS(KEY,F3) F3
set _AP_VARS(KEY,CtrlA) [list Control-A Control-a]
set _AP_VARS(KEY,CtrlD) [list Control-D Control-d]
set _AP_VARS(KEY,CtrlY) [list Control-Y Control-y]
set _AP_VARS(KEY,CtrlT) [list Control-T Control-t]
set _AP_VARS(KEY,CtrlB) [list Control-B Control-b]
set _AP_VARS(KEY,CtrlE) [list Control-E Control-e]
set _AP_VARS(KEY,AltQ) [list Alt-Q Alt-q]
set _AP_VARS(KEY,AltW) [list Alt-W Alt-w]
variable _AP_VISITED; array set _AP_VISITED [list]
set _AP_VISITED(ALL) [list]
variable UFF "\uFFFF"
variable MC_NS {} FOCUSED {}
## _ default options & attributes of widgets _ ##
variable _Defaults [dict create \
bts {{} {}} \
but {{} {}} \
buT {{} {-width -20 -pady 1}} \
btT {{} {-width -20 -pady 1 -relief flat -overrelief raised -highlightthickness 0 -takefocus 0}} \
can {{} {}} \
chb {{} {}} \
swi {{} {}} \
chB {{} {-relief sunken -padx 6 -pady 2}} \
cbx {{} {}} \
fco {{} {}} \
ent {{} {}} \
enT {{} {-insertwidth $::apave::cursorwidth -insertofftime 250 -insertontime 750}} \
fil {{} {}} \
fis {{} {}} \
dir {{} {}} \
fon {{} {}} \
clr {{} {}} \
dat {{} {}} \
fiL {{} {}} \
fiS {{} {}} \
diR {{} {}} \
foN {{} {}} \
clR {{} {}} \
daT {{} {}} \
sta {{} {}} \
too {{} {}} \
fra {{} {-takefocus 0}} \
frA {{} {-takefocus 0}} \
ftx {{} {}} \
gut {{} {-width 0 -highlightthickness 1}} \
lab {{-sticky w} {-takefocus 0}} \
laB {{-sticky w} {-takefocus 0}} \
lfr {{} {-takefocus 0}} \
lfR {{} {-relief groove -takefocus 0}} \
lbx {{} {-activestyle none -exportselection 0 -selectmode browse}} \
flb {{} {}} \
meb {{} {}} \
meB {{} {}} \
nbk {{} {}} \
opc {{} {}} \
pan {{} {}} \
pro {{} {}} \
rad {{} {}} \
raD {{} {-padx 6 -pady 2}} \
sca {{} {-orient horizontal -takefocus 0}} \
scA {{} {-orient horizontal -takefocus 0}} \
sbh {{-sticky ew} {-orient horizontal -takefocus 0}} \
sbH {{-sticky ew} {-orient horizontal -takefocus 0}} \
sbv {{-sticky ns} {-orient vertical -takefocus 0}} \
sbV {{-sticky ns} {-orient vertical -takefocus 0}} \
scf {{} {}} \
seh {{-sticky ew} {-orient horizontal -takefocus 0}} \
sev {{-sticky ns} {-orient vertical -takefocus 0}} \
siz {{} {}} \
spx {{} {}} \
spX {{} {}} \
tbl {{} {-selectborderwidth 1 -highlightthickness 2 \
-labelcommand tablelist::sortByColumn -stretch all \
-showseparators 1}} \
tex {{} {-undo 1 -maxundo 0 -highlightthickness 2 -insertofftime 250 -insertontime 750 -insertwidth $::apave::cursorwidth -wrap word -selborderwidth 1 -exportselection 0}} \
tre {{} {-selectmode browse}} \
h_ {{-sticky ew -csz 3 -padx 3} {}} \
v_ {{-sticky ns -rsz 3 -pady 3} {}}]
## _______________________ Helpers _____________________ ##
proc WindowStatus {w name {val ""} {defval ""}} {
# Sets/gets a status of window. The status is a value assigned to a name.
# w - window's path
# name - name of status
# val - if blank, to get a value of status; otherwise a value to set
# defval - default value (actual if the status not set beforehand)
# Returns a value of status.
# See also: IntStatus
variable _AP_VARS
if {$val eq {}} { ;# getting
if {[info exist _AP_VARS($w,$name)]} {
return $_AP_VARS($w,$name)
}
return $defval
}
set _AP_VARS($w,$name) $val ;# setting
}
#_______________________
proc mainWindowOfApp {{win ""}} {
# Sets/gets a main window of application.
# win - window's path
# This should be run at application start, before opening any window.
WindowStatus . MAIN_WINDOW_OF_APP $win
}
#_______________________
proc IntStatus {w {name "status"} {val ""}} {
# Sets/gets a status of window. The status is an integer assigned to a name.
# w - window's path
# name - name of status
# val - if blank, to get a value of status; otherwise a value to set
# Default value of status is 0.
# Returns an old value of status.
# See also: WindowStatus
set old [WindowStatus $w $name {} 0]
if {$val ne {}} {WindowStatus $w $name $val 1}
return $old
}
#_______________________
proc iconImage {{icon ""} {iconset "small"} {doit no}} {
# Gets a defined icon's image or list of icons.
# If *icon* equals to "-init", initializes apave's icon set.
# icon - icon's name
# iconset - one of small/middle/large
# doit - force the initialization
# Returns the icon's image or, if *icon* is blank, a list of icons
# available in *apave*.
variable _AP_IMG
variable _AP_ICO
if {$icon eq {}} {return $_AP_ICO}
; proc imagename {icon} { # Get a defined icon's image name
return _AP_IMG(img$icon)
}
variable apaveDir
if {![array size _AP_IMG] || $doit} {
# Make images of icons
source [file join $apaveDir apaveimg.tcl]
if {$iconset ne "small"} {
foreach ic $_AP_ICO { ;# small icons best fit for menus
set _AP_IMG($ic-small) [set _AP_IMG($ic)]
}
if {$iconset eq "middle"} {
source [file join $apaveDir apaveimg2.tcl]
} else {
source [file join $apaveDir apaveimg2.tcl] ;# TODO
}
}
foreach ic $_AP_ICO {
if {[catch {image create photo [imagename $ic] -data [set _AP_IMG($ic)]}]} {
# some png issues on old Tk
image create photo [imagename $ic] -data [set _AP_IMG(none)]
} elseif {$iconset ne "small"} {
image create photo [imagename $ic-small] -data [set _AP_IMG($ic-small)]
}
}
}
if {$icon eq "-init"} {return $_AP_ICO} ;# just to get to icons
if {$icon ni $_AP_ICO} {set icon [lindex $_AP_ICO 0]}
if {$iconset eq "small" && "_AP_IMG(img$icon-small)" in [image names]} {
set icon $icon-small
}
return [imagename $icon]
}
#_______________________
proc iconData {{icon "info"} {iconset ""}} {
# Gets an icon's data.
# icon - icon's name
# iconset - one of small/middle/large
# Returns data of the icon.
variable _AP_IMG
iconImage -init
if {$iconset ne {} && "_AP_IMG(img$icon-$iconset)" in [image names]} {
return [set _AP_IMG($icon-$iconset)]
}
set _AP_IMG($icon)
}
#_______________________
proc setAppIcon {win {winicon ""}} {
# Sets application's icon.
# win - path to a window of application
# winicon - data of icon
# The *winicon* may be a contents of variable (as supposed by default) or
# a file's name containing th image data.
# If it fails to find an image in either, no icon is set.
variable _AP_VARS
set _AP_VARS(APPICON) {}
if {$winicon ne {}} {
if {[catch {set _AP_VARS(APPICON) [image create photo -data $winicon]}]} {
catch {set _AP_VARS(APPICON) [image create photo -file $winicon]}
}
}
if {$_AP_VARS(APPICON) ne {}} {wm iconphoto $win -default $_AP_VARS(APPICON)}
}
#_______________________
proc getAppIcon {} {
# Gets application's icon.
variable _AP_VARS
return $_AP_VARS(APPICON)
}
#_______________________
proc precedeWidgetName {widname prename} {
# Adds a preceding name to a tail name of widget.
# widname - widget's full name
# prename - preceding name
# Useful at getting a entry/button name of chooser.
# Example:
# set wentry [::apave::precedeWidgetName [$pobj DirToChoose] ent]
# See also: APaveBase::Replace_chooser
set p [string last . $widname]
set res [string range $widname 0 $p]
append res $prename [string range $widname $p+1 end]
return $res
}
#_______________________
proc defaultAttrs {{type ""} {opts ""} {atrs ""} {widget ""}} {
# Sets, gets or registers default options and attributes for widget type.
# type - widget type
# opts - new default grid/pack options
# atrs - new default attributes
# widget - Tcl/Tk command for the new registered widget type
# See also: APaveBase::defaultATTRS
obj defaultATTRS $type $opts $atrs $widget
}
#_______________________
proc focusApp {{win ""}} {
# Saves (if *win* is set) or restores app's focus.
# win - focused window's path
# In some DE, if app loses focus, restoring it
# focuses main window, ignores modal toplevel, locks keyboard.
# focusApp tries to focus on last modal window.
variable FOCUSED
if {$win ne {}} {
setProperty FOCW_$win [focus]
return
}
catch {
if {[set foc [focus]] ne {}} {
set foc [winfo toplevel $foc]
set modal 0
foreach fw $FOCUSED {
if {$fw eq $foc} {set modal 1; break}
}
# if non-modal is currently focused, let it be so
if {!$modal} {
if {[focus] eq $foc} {catch {focus [getProperty FOCW_$foc]}}
return
}
}
# find and focus last open modal
for {set i [llength $FOCUSED]} {$i} {} {
set fw [lindex $FOCUSED [incr i -1]]
if {[winfo exists $fw]} {
if {$fw ne $foc} {
focus -force $fw
catch {focus [getProperty FOCW_$fw]}
}
break
}
set FOCUSED [lreplace $FOCUSED $i $i]
}
}
}
## _______________________ Text little procs _________________________ ##
proc eventOnText {w ev} {
# Generates an event on a text, saving its current index in hl_tcl.
# w - text widget's path
# ev - event
# The hl_tcl needs to call MemPos before any action changing the text.
catch {::hl_tcl::my::MemPos $w}
if {[catch {$w tag ranges sel} sels]} {set sels [list]}
switch -exact -- $ev {
<<Cut>> - <<Copy>> {
if {[set llen [expr {[llength $sels]-1}]] < 2} return
# multiple ranges of selection:
# first, copy all selections to clipboard
clipboard clear -displayof $w
foreach {pos1 pos2} $sels {
clipboard append -displayof $w [$w get $pos1 $pos2]
}
if {$ev eq {<<Cut>>}} {
# for Cut event: delete all selections
for {set i $llen} {$i>0} {incr i -2} {
set pos1 [lindex $sels $i-1]
set pos2 [lindex $sels $i]
$w delete $pos1 $pos2
}
}
return -code break
}
default {
event generate $w $ev
}
}
}
#_______________________
proc getTextHotkeys {key} {
# Gets upper & lower keys for a hot key.
# key - the hot key
variable _AP_VARS
if {![info exist _AP_VARS(KEY,$key)]} {return [list]}
set keys $_AP_VARS(KEY,$key)
if {[llength $keys]==1} {
if {[set i [string last - $keys]]>0} {
set lt [string range $keys $i+1 end]
if {[string length $lt]==1} { ;# for lower case of letters
set keys "[string range $keys 0 $i][string toupper $lt]"
lappend keys "[string range $keys 0 $i][string tolower $lt]"
}
}
}
return $keys
}
#_______________________
proc setTextHotkeys {key value} {
# Sets new key combinations for some operations on text widgets.
# key - ctrlD for "double selection", ctrlY for "delete line" operation
# value - list of new key combinations
variable _AP_VARS
set _AP_VARS(KEY,$key) $value
}
#_______________________
proc setTextIndent {len {padchar { }}} {
# Sets an indenting for text widgets.
# len - length of indenting
# padchar - indenting character
variable _AP_VARS
if {$padchar ne "\t"} {set padchar { }}
set _AP_VARS(INDENT) [string repeat $padchar $len]
}
## ________________________ EONS ::apave _________________________ ##
}
# ________________________ source *.tcl _________________________ #
# Let the *.tcl be sourced here just to ensure
# that apave's stuff available for them and vice versa.
source [file join $::apave::apaveDir obbit.tcl]
# ________________________ APaveBase oo::class _________________________ #
oo::class create ::apave::APaveBase {
mixin ::apave::ObjectTheming
variable PV Moveall Initialcolor Modalwin Fgbut Bgbut Fgtxt Bgtxt Prepost Widgetopts Edge
constructor {{cs -2} args} {
# Creates APaveBase object.
# cs - color scheme (CS)
# args - additional arguments
# If cs>-2, the appropriate CS is set for the created APaveBase object.
# Makes few procedures in the object's namespace to access from
# event handlers:
# - ListboxHandle
# - ListboxSelect
# - WinResize
# This trick with *proc* inside an object is discussed at
# [proc-in-tcl-ooclass](https://stackoverflow.com/questions/54804964/proc-in-tcl-ooclass)
# keep the 'important' data of Pave object in array
array set PV [list]
set Moveall 1
set Initialcolor {}
set Modalwin .
set Fgbut [ttk::style lookup TButton -foreground]; if {$Fgbut eq {}} {set Fgbut #000000}
set Bgbut [ttk::style lookup TButton -background]; if {$Bgbut eq {}} {set Bgbut #d9d9d9}
set Fgtxt [ttk::style lookup TEntry -foreground] ; if {$Fgtxt eq {}} {set Fgtxt #000000}
set Prepost [list]
set Widgetopts [list]
set Edge @@
if {$Fgtxt in {black #000000}} {
set Bgtxt white
} else {
set Bgtxt [ttk::style lookup TEntry -background]
}
# set/reset a color scheme if it is/was requested
if {$cs>=-1} {my csSet $cs} {my initTooltip}
# object's procedures
; proc ListboxHandle {W offset maxChars} {
set list {}
foreach index [$W curselection] { lappend list [$W get $index] }
set text [join $list \n]
return [string range $text $offset [expr {$offset+$maxChars-1}]]
}
; proc ListboxSelect {W} {
# This code had been taken from Tcl's wiki:
# https://wiki.tcl-lang.org/page/listbox+selection
selection clear -displayof $W
selection own -command {} $W
selection handle -type UTF8_STRING \
$W [list [namespace current]::ListboxHandle $W]
selection handle \
$W [list [namespace current]::ListboxHandle $W]
return
}
; proc WinResize {win} {
# Restricts the window's sizes (thus fixing Tk's issue with a menubar)
# win - path to a window to be of restricted sizes
if {[$win cget -menu] ne {}} {
lassign [::apave::splitGeometry [wm geometry $win]] w h
lassign [wm minsize $win] wmin hmin
if {$w<$wmin && $h<$hmin} {
set corrgeom ${wmin}x$hmin
} elseif {$w<$wmin} {
set corrgeom ${wmin}x$h
} elseif {$h<$hmin} {
set corrgeom ${w}x$hmin
} else {
return
}
wm geometry $win $corrgeom
}
return
}
# the end of APaveBase constructor
if {[llength [self next]]} { next {*}$args }
return
}
destructor {
# Clears variables used in the object.
array unset PV *
if {[llength [self next]]} next
}
## _______________________ Methods to be redefined ____________________ ##
method themePopup {mnu} {
# Applies a color scheme to a popup menu.
# mnu - name of popup menu
# The method is to be redefined in descendants/mixins.
}
method NonTtkTheme {win} {
# Applies a current color scheme for non-ttk widgets.
# win - path to a window to be colored.
# Method to be redefined in descendants/mixins.
}
method NonTtkStyle {typ {dsbl 0}} {
# Gets a style for non-ttk widgets.
# typ - the type of widget (in apave terms, i.e. but, buT etc.)
# dsbl - a mode to get style of disabled (1) or readonly (2) widgets
# See also: widgetType
# Method to be redefined in descendants/mixins.
}
## _______________________ Helpers for APaveBase ________________________ ##
method paveoptionValue {opt} {
# Gets an option's value.
# opt - option's name
# Returns a value for options like "Moveall".
if {$opt in [info object vars [self]]} {
variable $opt
return [set $opt]
}
return {}
}
#_______________________
method checkXY {w h x y} {
# Checks the coordinates of window (against the screen).
# w - width of window
# h - height of window
# x - window's X coordinate
# y - window's Y coordinate
# Returns new coordinates in +X+Y form.
# check for left/right edge of screen (accounting decors)
set scrw [expr {[winfo vrootwidth .] - 12}]
set scrh [expr {[winfo vrootheight .] - 36}]
if {($x + $w) > $scrw } {
set x [expr {$scrw - $w}]
}
if {($y + $h) > $scrh } {
set y [expr {$scrh - $h}]
}
if {![string match -* $x]} {set x +[string trimleft $x +]}
if {![string match -* $y]} {set y +[string trimleft $y +]}
return $x$y
}
#_______________________
method CenteredXY {rw rh rx ry w h} {
# Gets the coordinates of centered window (against its parent).
# rw - parent's width
# rh - parent's height
# rx - parent's X coordinate
# ry - parent's Y coordinate
# w - width of window to be centered
# h - height of window to be centered
# Returns centered coordinates in +X+Y form.
set x [expr {max(0, $rx + ($rw - $w) / 2)}]
set y [expr {max(0,$ry + ($rh - $h) / 2)}]
my checkXY $w $h $x $y
}
#_______________________
method ownWName {name} {
# Gets a tail (last part) of widget's name
# name - name (path) of the widget
lindex [split $name .] end
}
#_______________________
method parentWName {name} {
# Gets parent name of widget.
# name - name (path) of the widget
string range $name 0 [string last . $name]-1
}
#_______________________
method iconA {icon {iconset small} {cmpd left}} {
# Gets icon attributes for buttons, menus etc.
# icon - name of icon
# iconset - one of small/middle/large
# cmpd - value of -compound option
# The *iconset* is "small" for menus (recommended and default).
return "-image [::apave::iconImage $icon $iconset] -compound $cmpd"
}
#_______________________
method configure {args} {
# Configures the apave object (all of options may be changed).
# args - list of pairs name/value of options
# Example:
# pobj configure edge "@@"
foreach {optnam optval} $args {set $optnam $optval}
}
#_______________________
method ExpandOptions {options} {
# Expands shortened options.
set options [string map {
{ -st } { -sticky }
{ -com } { -command }
{ -t } { -text }
{ -w } { -width }
{ -h } { -height }
{ -var } { -variable }
{ -tvar } { -textvariable }
{ -lvar } { -listvariable }
{ -ro } { -readonly }
} " $options"]
return $options
}
#_______________________
method AddButtonIcon {w attrsName} {
# Gets the button's icon based on its text and name (e.g. butOK) and
# appends it to the attributes of button.
# w - button's name
# attrsName - name of variable containing attributes of the button
upvar 1 $attrsName attrs
set com [::apave::getOption -com {*}$attrs]
if {[string is integer -strict $com]} { ;# returned integer result
::apave::extractOptions attrs -com {}
append attrs " -com {[self] res {[my pavedPath]} $com}"
}
if {[::apave::getOption -image {*}$attrs] ne {}} return
set txt [::apave::getOption -t {*}$attrs]
if {$txt eq {}} { set txt [::apave::getOption -text {*}$attrs] }
set im {}
set icolist [list {exit abort} {exit close} \
{SaveFile save} {OpenFile open}]
# ok, yes, cancel, apply buttons should be at the end of list
# as their texts can be renamed (e.g. "Help" in e_menu's "About")
lappend icolist {*}[::apave::iconImage] {yes apply}
foreach icon $icolist {
lassign $icon ic1 ic2
# text of button is of highest priority at defining its icon
if {[string match -nocase $ic1 $txt] || \
[string match -nocase b*t$ic1 $w] || ($ic2 ne {} && ( \
[string match -nocase b*t$ic2 $w] || [string match -nocase $ic2 $txt]))} {
if {[string match -nocase btT* $w]} {
set cmpd none
} else {
set cmpd left
}
append attrs " [my iconA $ic1 small $cmpd]"
break
}
}
}
#_______________________
method ListboxesAttrs {w attrs} {
# Appends selection attributes to listboxes.
# Details:
# 1. https://wiki.tcl-lang.org/page/listbox+selection
# 2. https://stackoverflow.com, the question:
# the-tablelist-curselection-goes-at-calling-the-directory-dialog
if {{-exportselection} ni $attrs} {
append attrs " -ListboxSel $w -selectmode extended -exportselection 0"
}
return $attrs
}
#_______________________
method getWidChildren {wid treeName {init yes}} {
# Gets children of a widget.
# wid - widget's path
# treeName - name of variable to hold the result.
upvar $treeName tree
if {$init} {set tree [list]}
foreach ch [winfo children $wid] {
lappend tree $ch
my getWidChildren $ch $treeName no
}
}
#_______________________
method findWidPath {wid {mode exact} {visible yes}} {
# Searches a widget's path among the active widgets.
# w - widget name, set partially e.g. "wid" instead of ".win.wid"
# mode - if "exact", searches *.wid; if "globe", searches *wid*
# Returns the widget's full path or "" if the widget isn't active.
my getWidChildren . tree
if {$mode eq {exact}} {
set i [lsearch -glob $tree "*.$wid"]
} else {
set i [lsearch -glob $tree "*$wid*"]
}
if {$i>-1} {return [lindex $tree $i]}
return {}
}
## _______________________ File content widget _______________________ ##
method FCfieldAttrs {wnamefull attrs varopt} {
# Fills the non-standard attributes of file content widget.
# wnamefull - a widget name
# attrs - a list of all attributes
# varopt - a variable option
# The *varopt* refers to a variable part such as tvar, lvar:
# * -inpval option means an initial value of the field
# * -retpos option has p1:p2 format (e.g. 0:10) to cut a substring \
from a returned value
# Returns *attrs* without -inpval and -retpos options.
lassign [::apave::parseOptions $attrs $varopt {} -retpos {} -inpval {}] \
vn rp iv
if {[string first {-state disabled} $attrs]<0 && $vn ne {}} {
set all {}
if {$varopt eq {-lvar}} {
lassign [::apave::extractOptions attrs -values {} -ALL 0] iv a
if {[string is boolean -strict $a] && $a} {set all ALL}
lappend Widgetopts "-lbxname$all $wnamefull $vn"
}
if {$rp ne {}} {
if {$all ne {}} {set rp 0:end}
lappend Widgetopts "-retpos $wnamefull $vn $rp"
}
}
if {$iv ne {}} { set $vn $iv }
return [::apave::removeOptions $attrs -retpos -inpval]
}
#_______________________
method FCfieldValues {wnamefull attrs} {
# Fills the file content widget's values.
# wnamefull - name (path) of fco widget
# attrs - attributes of the widget
; proc readFCO {fname} {
# Reads a file's content.
# Returns a list of (non-empty) lines of the file.
if {$fname eq {}} {
set retval {{}}
} else {
set retval {}
foreach ln [::apave::textsplit [::apave::readTextFile $fname {} 1]] {
# probably, it's bad idea to have braces in the file of contents
set ln [string map [list \\ \\\\ \{ \\\{ \} \\\}] $ln]
if {$ln ne {}} {lappend retval $ln}
}
}
return $retval
}
; proc contFCO {fline opts edge args} {
# Given a file's line and options,
# cuts a substring from the line.
lassign [::apave::parseOptionsFile 1 $opts {*}$args] opts
lassign $opts - - - div1 - div2 - pos - len - RE - ret
set ldv1 [string length $div1]
set ldv2 [string length $div2]
set i1 [expr {[string first $div1 $fline]+$ldv1}]
set i2 [expr {[string first $div2 $fline]-1}]
set filterfile yes
if {$ldv1 && $ldv2} {
if {$i1<0 || $i2<0} {return $edge}
set retval [string range $fline $i1 $i2]
} elseif {$ldv1} {
if {$i1<0} {return $edge}
set retval [string range $fline $i1 end]
} elseif {$ldv2} {
if {$i2<0} {return $edge}
set retval [string range $fline 0 $i2]
} elseif {$pos ne {} && $len ne {}} {
set retval [string range $fline $pos $pos+[incr len -1]]
} elseif {$pos ne {}} {
set retval [string range $fline $pos end]
} elseif {$len ne {}} {
set retval [string range $fline 0 $len-1]
} elseif {$RE ne {}} {
set retval [regexp -inline $RE $fline]
if {[llength $retval]>1} {
foreach r [lrange $retval 1 end] {append retval_tmp $r}
set retval $retval_tmp
} else {
set retval [lindex $retval 0]
}
} else {
set retval $fline
set filterfile no
}
if {$retval eq {} && $filterfile} {return $edge}
set retval [string map [list "\}" "\\\}" "\{" "\\\{"] $retval]
return [list $retval $ret]
}
set edge $Edge
set ldv1 [string length $edge]
set filecontents {}
set optionlists {}
set tplvalues {}
set retpos {}
set values [::apave::getOption -values {*}$attrs]
if {[string first $edge $values]<0} { ;# if 1 file, edge
set values "$edge$values$edge" ;# may be omitted
}
# get: files' contents, files' options, template line
set lopts {-list {} -div1 {} -div2 {} -pos {} -len {} -RE {} -ret 0}
while {1} {
set i1 [string first $edge $values]
set i2 [string first $edge $values $i1+1]
if {$i1>=0 && $i2>=0} {
incr i1 $ldv1
append tplvalues [string range $values 0 $i1-1]
set fdata [string range $values $i1 $i2-1]
lassign [::apave::parseOptionsFile 1 $fdata {*}$lopts] fopts fname
lappend filecontents [readFCO $fname]
lappend optionlists $fopts
set values [string range $values $i2+$ldv1 end]
} else {
append tplvalues $values
break
}
}
# fill the combobox lines, using files' contents and options
if {[set leno [llength $optionlists]]} {
set newvalues {}
set ilin 0
lassign $filecontents firstFCO
foreach fline $firstFCO { ;# lines of first file for a base
set line {}
set tplline $tplvalues
for {set io 0} {$io<$leno} {incr io} {
set opts [lindex $optionlists $io]
if {$ilin==0} { ;# 1st cycle: add items from -list option
lassign $opts - list1 ;# -list option goes first
if {[llength $list1]} {
foreach l1 $list1 {append newvalues "\{$l1\} "}
lappend Widgetopts "-list $wnamefull [list $list1]"
}
}
set i1 [string first $edge $tplline]
if {$i1>=0} {
lassign [contFCO $fline $opts $edge {*}$lopts] retline ret
if {$ret ne "0" && $retline ne $edge && \
[string first $edge $line]<0} {
set p1 [expr {[string length $line]+$i1}]
if {$io<($leno-1)} {
set p2 [expr {$p1+[string length $retline]-1}]
} else {
set p2 end
}
set retpos "-retpos $p1:$p2"
}
append line [string range $tplline 0 $i1-1] $retline
set tplline [string range $tplline $i1+$ldv1 end]
} else {
break
}
set fline [lindex [lindex $filecontents $io+1] $ilin]
}
if {[string first $edge $line]<0} {
# put only valid lines into the list of values
append newvalues "\{$line$tplline\} "
}
incr ilin
}
# replace old 'values' attribute with the new 'values'
lassign [::apave::parseOptionsFile 2 $attrs -values \
[string trimright $newvalues]] attrs
}
return "$attrs $retpos"
}
## _______________________ Timeout button _______________________ ##
method timeoutButton {w tmo lbl {lbltext ""}} {
# Invokes a button's action after a timeout.
# w - button's path
# tmo - timeout in sec.
# lbl - label widget, where seconds to wait are displayed
# lbltext - original text of label
if {$tmo>0} {
catch {set lbl [my $lbl]}
if {[winfo exist $lbl]} {
if {$lbltext eq {}} {
set lbltext [$lbl cget -text]
lappend ::apave::_AP_VARS(TIMW) $w
}
$lbl configure -text "$lbltext $tmo sec. "
}
incr tmo -1
after 1000 [list if "\[info commands [self]\] ne {}" \
"[self] checkTimeoutButton $w $tmo $lbl {$lbltext}"]
return
}
if {[winfo exist $w]} {$w invoke}
}
#_______________________
method checkTimeoutButton {w tmo lbl {lbltext ""}} {
# Checks if the timeout button is alive & focused; if not, cancels the timeout.
# w - button's path
# tmo - timeout in sec.
# lbl - label widget, where seconds to wait are displayed
# lbltext - original text of label
if {[winfo exists $lbl]} {
if {[focus] in [list $w {}]} {
if {$w in $::apave::_AP_VARS(TIMW)} {
my timeoutButton $w $tmo $lbl $lbltext
}
} else {
$lbl configure -text $lbltext
}
}
}
## ________________________ Making widgets _________________________ ##
method widgetType {wnamefull options attrs} {
# Gets the widget type based on 3 initial letters of its name. Also
# fills the grid/pack options and attributes of the widget.
# wnamefull - path to the widget
# options - grid/pack options of the widget
# attrs - attribute of the widget
# Returns a list of items:
# widget - Tk/Ttk widget name
# options - grid/pack options of the widget
# attrs - attribute of the widget
# nam3 - 3 initial letters of widget's name
# disabled - flag of *disabled* state
set disabled [expr {[::apave::getOption -state {*}$attrs] eq {disabled}}]
set pack $options
set name [my ownWName $wnamefull]
if {[info exists ::apave::_AP_VARS(ProSplash,type)] && \
$::apave::_AP_VARS(ProSplash,type) eq {}} {
set val [my progress_Go [incr ::apave::_AP_VARS(ProSplash,curvalue)] {} $name]
}
set nam3 [string tolower [string index $name 0]][string range $name 1 2]
if {[string index $nam3 1] eq "_"} {set k [string range $nam3 0 1]} {set k $nam3}
lassign [my defaultATTRS $k] defopts defattrs newtype
set options "$defopts $options"
set attrs "$defattrs $attrs"
switch -glob -- $nam3 {
bts {
set widget ttk::frame
if {![namespace exists ::bartabs]} {
source [file join $::apave::SRCDIR bartabs bartabs.tcl]
}
set attrs "-bartabs {$attrs}"
}
but {
set widget ttk::button
my AddButtonIcon $name attrs
}
buT - btT {
set widget button
my AddButtonIcon $name attrs