-
Notifications
You must be signed in to change notification settings - Fork 1
/
apavedialog.tcl
executable file
·1444 lines (1337 loc) · 49.3 KB
/
apavedialog.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: apavedialog.tcl
# Author: Alex Plotnikov (aplsimple@gmail.com)
# Date: 12/09/2021
# Brief: Handles standard dialogs with advanced features.
# License: MIT.
###########################################################
source [file join [file dirname [info script]] apavebase.tcl]
# ________________________ apave NS _________________________ #
namespace eval ::apave {
variable querydlg {}
variable msgarray; array set msgarray [list]
set msgarray(savenot) {Don't save}
set msgarray(savetext) {Save text}
set msgarray(saveask) {Save changes made to the text?}
set msgarray(find) {Find: }
proc dlgPath {} {
# Gets a current dialogue's path.
# In fact, it does the same as [my dlgPath], but it can be
# called outside of apave dialogue object (useful sometimes).
return $::apave::querydlg
}
#_______________________
proc msgcatDialogs {} {
# Prepares localized messages used in dialogues.
variable msgarray
foreach n [array names msgarray] {
set msgarray($n) [msgcat::mc $msgarray($n)]
}
}
## ________________________ EONS apave _________________________ ##
}
# ________________________ APaveDialog class _________________________ #
oo::class create ::apave::APaveDialog {
superclass ::apave::APaveBase
variable HLstring Winpath CheckNomore Foundstr Dlgpath Defb1 Defb2 Indexdlg
#_______________________
constructor {{win ""} args} {
# Creates APaveDialog object.
# win - window's name (path)
# args - additional arguments
set Winpath $win ;# dialogs are bound to $win, default "" means .
set Dlgpath {} ;# current dialog's path
set Foundstr {} ;# current found string
set HLstring {} ;# current selected string
# Actions on closing the editor
; proc exitEditor {w resExit} {
upvar $resExit res
set wtxt [my TexM]
if {[my askForSave $wtxt] && [$wtxt edit modified]} {
set pdlg [::apave::APaveDialog new $w]
set r [$pdlg misc warn $::apave::msgarray(savetext) \
"\n $::apave::msgarray(saveask) \n" \
[list Save 1 $::apave::msgarray(savenot) Close Cancel 0] \
1 -focusback [my TexM] -centerme $w]
if {$r==1} {
set res 1
} elseif {$r eq "Close"} {
set res 0
}
$pdlg destroy
} else {
set res 0
}
return
}
# end of APaveDialog constructor
if {[llength [self next]]} { next {*}$args }
}
#_______________________
destructor {
# Clears variables used in the object.
if {[llength [self next]]} next
}
## ________________________ Standard dialogs _________________________ ##
# ok - dialog with button OK
# okcancel - dialog with buttons OK, Cancel
# yesno - dialog with buttons YES, NO
# yesnocancel - dialog with buttons YES, NO, CANCEL
# retrycancel - dialog with buttons RETRY, CANCEL
# abortretrycancel - dialog with buttons ABORT, RETRY, CANCEL
# misc - dialog with miscellaneous buttons
#
# Called as:
# dialog icon ttl msg ?defb? ?args?
#
# Mandatory arguments of dialogs:
# icon - icon name (info, warn, err, ques)
# ttl - title
# msg - message
# Optional arguments:
# defb - default button (OK, YES, NO, CANCEL, RETRY, ABORT)
# args - options for Query
#_______________________
method PrepArgs {args} {
# Prepares a list of arguments.
# Returns the list (wrapped in list) and a command for OK button.
lassign [::apave::parseOptions $args -modal {} -ch {} -comOK {} -onclose {}] \
modal ch comOK onclose
if {[string is true -strict $modal]} {
set com 1
} elseif {$ch ne {}} {
# some options are incompatible with -ch
if {[string match *destroy* $onclose]} {set onclose {}}
lappend args -modal 1 -onclose $onclose
set com 1
} elseif {$comOK eq {}} {
set com destroy ;# non-modal without -ch option
} else {
set com $comOK
}
list [list $args] $com
}
#_______________________
method ok {icon ttl msg args} {
# Shows the *OK* dialog.
# icon - icon
# ttl - title
# msg - message
# args - options
lassign [my PrepArgs {*}$args] args comOK
my Query $icon $ttl $msg "ButOK OK $comOK" ButOK {} $args
}
#_______________________
method okcancel {icon ttl msg {defb OK} args} {
# Shows the *OKCANCEL* dialog.
# icon - icon
# ttl - title
# msg - message
# defb - button to be selected
# args - options
lassign [my PrepArgs {*}$args] args
my Query $icon $ttl $msg \
{ButOK OK 1 ButCANCEL Cancel 0} But$defb {} $args
}
#_______________________
method yesno {icon ttl msg {defb YES} args} {
# Shows the *YESNO* dialog.
# icon - icon
# ttl - title
# msg - message
# defb - button to be selected
# args - options
lassign [my PrepArgs {*}$args] args
my Query $icon $ttl $msg \
{ButYES Yes 1 ButNO No 0} But$defb {} $args
}
#_______________________
method yesnocancel {icon ttl msg {defb YES} args} {
# Shows the *YESNOCANCEL* dialog.
# icon - icon
# ttl - title
# msg - message
# defb - button to be selected
# args - options
lassign [my PrepArgs {*}$args] args
my Query $icon $ttl $msg \
{ButYES Yes 1 ButNO No 2 ButCANCEL Cancel 0} But$defb {} $args
}
#_______________________
method retrycancel {icon ttl msg {defb RETRY} args} {
# Shows the *RETRYCANCEL* dialog.
# icon - icon
# ttl - title
# msg - message
# defb - button to be selected
# args - options
lassign [my PrepArgs {*}$args] args
my Query $icon $ttl $msg \
{ButRETRY Retry 1 ButCANCEL Cancel 0} But$defb {} $args
}
#_______________________
method abortretrycancel {icon ttl msg {defb RETRY} args} {
# Shows the *ABORTRETRYCANCEL* dialog.
# icon - icon
# ttl - title
# msg - message
# defb - button to be selected
# args - options
lassign [my PrepArgs {*}$args] args
my Query $icon $ttl $msg \
{ButABORT Abort 1 ButRETRY Retry 2 ButCANCEL \
Cancel 0} But$defb {} $args
}
#_______________________
method misc {icon ttl msg butts {defb ""} args} {
# Shows the *MISCELLANEOUS* dialog.
# icon - icon
# ttl - title
# msg - message
# butts - list of buttons
# defb - button to be selected
# args - options
# The *butts* is a list of pairs "title of button" "number/ID of button"
foreach {nam num} $butts {
set but But[namespace tail $num] ;# for "num" set as a command
lappend apave_msc_bttns $but "$nam" $num
if {$defb eq {}} {
set defb $num
}
}
lassign [my PrepArgs {*}$args] args
my Query $icon $ttl $msg $apave_msc_bttns But$defb {} $args
}
## ________________________ Progress for splash _________________________ ##
method progress_Begin {type wprn ttl msg1 msg2 maxvalue args} {
# Creates and shows a progress window. Fit for splash screens.
# type - any word(s)
# wprn - parent window
# ttl - title message
# msg1 - top message
# msg2 - bottom message
# maxvalue - maximum value
# args - additional attributes of the progress bar
# If type={}, widgetType method participates too in progress_Go, and also
# progress_End puts out a little statistics.
# See also: APaveBase::widgetType, progress_Go, progress_End
set ::apave::_AP_VARS(win) .proSplashScreen
set qdlg $::apave::_AP_VARS(win)
set atr1 "-maximum 100 -value 0 -mode determinate -length 300 -orient horizontal"
set widlist [list \
"fra - - - - pack {-h 10}" \
".Lab1SplashScreen - - - - pack {-t {$msg1}}" \
".ProgSplashScreen - - - - pack {$atr1 $args}" \
".Lab2SplashScreen - - - - {pack -anchor w} {-t {$msg2}}" \
]
set win [my makeWindow $qdlg.fra $ttl]
set widlist [my paveWindow $qdlg.fra $widlist]
::tk::PlaceWindow $win widget $wprn
my showWindow $win 0 1
update
set ::apave::_AP_VARS(ProSplash,type) $type
set ::apave::_AP_VARS(ProSplash,win) $win
set ::apave::_AP_VARS(ProSplash,wid1) [my Lab1SplashScreen]
set ::apave::_AP_VARS(ProSplash,wid2) [my ProgSplashScreen]
set ::apave::_AP_VARS(ProSplash,wid3) [my Lab2SplashScreen]
set ::apave::_AP_VARS(ProSplash,val1) 0
set ::apave::_AP_VARS(ProSplash,val2) 0
set ::apave::_AP_VARS(ProSplash,value) 0
set ::apave::_AP_VARS(ProSplash,curvalue) 0
set ::apave::_AP_VARS(ProSplash,maxvalue) $maxvalue
set ::apave::_AP_VARS(ProSplash,after) [list]
# 'after' should be postponed, as 'update' messes it up
rename ::after ::ProSplash_after
; proc ::after {args} {
lappend ::apave::_AP_VARS(ProSplash,after) $args
}
}
#_______________________
method progress_Go {value {msg1 ""} {msg2 ""}} {
# Updates a progress window.
# value - current value of the progress bar
# msg1 - top message
# msg2 - bottom message
# Returns current percents (value) of progress.
# If it reaches 100, the progress_Go may continue from 0.
# See also: progress_Begin
set ::apave::_AP_VARS(ProSplash,val1) $value
incr ::apave::_AP_VARS(ProSplash,val2)
set val [expr {min(100,int(100*$value/$::apave::_AP_VARS(ProSplash,maxvalue)))}]
if {$val!=$::apave::_AP_VARS(ProSplash,value)} {
set ::apave::_AP_VARS(ProSplash,value) $val
catch { ;# there might be no splash widgets, then let it run dry
$::apave::_AP_VARS(ProSplash,wid2) configure -value $val
if {$msg1 ne {}} {
$::apave::_AP_VARS(ProSplash,wid1) configure -text $msg1
}
if {$msg2 ne {}} {
$::apave::_AP_VARS(ProSplash,wid3) configure -text $msg2
}
update
}
}
return $val
}
#_______________________
method progress_End {} {
# Destroys a progress window.
# See also: progress_Begin
variable ::apave::_AP_VARS
catch {
destroy $::apave::_AP_VARS(ProSplash,win)
rename ::after {}
rename ::ProSplash_after ::after
foreach aftargs $::apave::_AP_VARS(ProSplash,after) {
after {*}$aftargs
}
if {$::apave::_AP_VARS(ProSplash,type) eq {}} {
puts "Splash statistics: \
\n \"maxvalue\": $::apave::_AP_VARS(ProSplash,maxvalue) \
\n curr.value: $::apave::_AP_VARS(ProSplash,val1) \
\n steps made: $::apave::_AP_VARS(ProSplash,val2)"
}
unset ::apave::_AP_VARS(ProSplash,type)
unset ::apave::_AP_VARS(ProSplash,win)
unset ::apave::_AP_VARS(ProSplash,wid1)
unset ::apave::_AP_VARS(ProSplash,wid2)
unset ::apave::_AP_VARS(ProSplash,wid3)
unset ::apave::_AP_VARS(ProSplash,val1)
unset ::apave::_AP_VARS(ProSplash,val2)
unset ::apave::_AP_VARS(ProSplash,value)
unset ::apave::_AP_VARS(ProSplash,curvalue)
unset ::apave::_AP_VARS(ProSplash,maxvalue)
unset ::apave::_AP_VARS(ProSplash,after)
}
}
## ________________________ Text utilities _________________________ ##
method pasteText {txt} {
# Removes a selection at pasting.
# txt - text's path
# The absence of this feature is very perpendicular of Tk's paste.
set err [catch {$txt tag ranges sel} sel]
if {!$err && [llength $sel]==2} {
lassign $sel pos1 pos2
set pos [$txt index insert]
if {[$txt compare $pos >= $pos1] && [$txt compare $pos <= $pos2]} {
$txt delete $pos1 $pos2
}
}
}
#_______________________
method doubleText {txt {dobreak 1}} {
# Doubles a current line or a selection of text widget.
# txt - text's path
# dobreak - if true, means "return -code break"
# The *dobreak=true* allows to break the Tk processing of keypresses
# such as Ctrl+D.
# If not set, the text widget is identified as `my TexM`.
if {$txt eq {}} {set txt [my TexM]}
set err [catch {$txt tag ranges sel} sel]
if {!$err && [llength $sel]==2} {
lassign $sel pos pos2
set pos3 insert ;# single selection
} else {
lassign [my GetLinePosition $txt insert] pos pos2 ;# current line
set pos3 $pos2
}
set duptext [$txt get $pos $pos2]
if {$pos3 ne {insert} && $pos2==[$txt index end]} {
# current line is the last one: duplicate it properly
set duptext \n[string range $duptext 0 end-1]
}
$txt insert $pos3 $duptext
if {$dobreak} {return -code break}
}
#_______________________
method deleteLine {txt {dobreak 1}} {
# Deletes a current line of text widget.
# txt - text's path
# dobreak - if true, means "return -code break"
# The *dobreak=true* allows to break the Tk processing of keypresses
# such as Ctrl+Y.
# If not set, the text widget is identified as `my TexM`.
if {$txt eq {}} {set txt [my TexM]}
lassign [my GetLinePosition $txt insert] linestart lineend
$txt delete $linestart $lineend
if {$dobreak} {return -code break}
}
#_______________________
method linesMove {txt to {dobreak 1}} {
# Moves a current line or lines of selection up/down.
# txt - text's path
# to - direction (-1 means "up", +1 means "down")
# dobreak - if true, means "return -code break"
# The *dobreak=true* allows to break the Tk processing of keypresses
# such as Ctrl+Y.
# If not set, the text widget is identified as `my TexM`.
; proc NewRow {ind rn} {
set i [string first . $ind]
set row [string range $ind 0 $i-1]
return [incr row $rn][string range $ind $i end]
}
if {$txt eq {}} {set txt [my TexM]}
set err [catch {$txt tag ranges sel} sel]
lassign [$txt index insert] pos ;# position of caret
if {[set issel [expr {!$err && [llength $sel]==2}]]} {
lassign $sel pos1 pos2 ;# selection's start & end
set l1 [expr {int($pos1)}]
set l2 [expr {int($pos2)}]
set pos21 [$txt index "$pos2 linestart"]
if {[$txt get $pos21 $pos2] eq {}} {incr l2 -1}
set lfrom [expr {$to>0 ? $l2+1 : $l1-1}]
set lto [expr {$to>0 ? $l1-1 : $l2-1}]
} else {
set lcurr [expr {int($pos)}]
set lfrom [expr {$to>0 ? $lcurr+1 : $lcurr-1}]
set lto [expr {$to>0 ? $lcurr-1 : $lcurr-1}]
}
set lend [expr {int([$txt index end])}]
if {$lfrom>0 && $lfrom<$lend} {
incr lto
lassign [my GetLinePosition $txt $lfrom.0] linestart lineend
set duptext [$txt get $linestart $lineend]
::apave::undoIn $txt
$txt delete $linestart $lineend
$txt insert $lto.0 $duptext
::tk::TextSetCursor $txt [NewRow $pos $to]
if {$issel} {
$txt tag add sel [NewRow $pos1 $to] [NewRow $pos2 $to]
}
if {[lsearch -glob [$txt tag names] tagCOM*]>-1} {
catch {::hl_tcl::my::Modified $txt insert $lto.0 $lto.end}
}
::apave::undoOut $txt
if {$dobreak} {return -code break}
}
}
#_______________________
method selectedWordText {txt} {
# Returns a word under the cursor or a selected text.
# txt - the text's path
set seltxt {}
if {![catch {$txt tag ranges sel} seltxt]} {
if {[set forword [expr {$seltxt eq {}}]]} {
set pos [$txt index "insert wordstart"]
set pos2 [$txt index "insert wordend"]
set seltxt [string trim [$txt get $pos $pos2]]
if {![string is wordchar -strict $seltxt]} {
# when cursor just at the right of word: take the word at the left
set pos [$txt index "insert -1 char wordstart"]
set pos2 [$txt index "insert -1 char wordend"]
}
} else {
lassign $seltxt pos pos2
}
catch {
set seltxt [$txt get $pos $pos2]
if {[set sttrim [string trim $seltxt]] ne {}} {
if {$forword} {set seltxt $sttrim}
}
}
}
return $seltxt
}
#_______________________
method InitFindInText { {ctrlf 0} {txt {}} } {
# Initializes the search in the text.
# ctrlf - "1" means that the method is called by Ctrl+F
# txt - path to the text widget
if {$txt eq {}} {set txt [my TexM]}
if {$ctrlf} { ;# Ctrl+F moves cursor 1 char ahead
::tk::TextSetCursor $txt [$txt index "insert -1 char"]
}
if {[set seltxt [my selectedWordText $txt]] ne {}} {
set Foundstr $seltxt
}
}
#_______________________
method findInText {{donext 0} {txt ""} {varFind ""} {dobell yes}} {
# Finds a string in text widget.
# donext - "1" means 'from a current position'
# txt - path to the text widget
# varFind - variable
# dobell - if yes, bells
# Returns yes, if found (or nothing to find), otherwise returns "no";
# also, if there was a real search, the search string is added.
if {$txt eq {}} {
set txt [my TexM]
set sel $Foundstr
} elseif {$donext && [set sel [my get_HighlightedString]] ne {}} {
# find a string got with alt+left/right
} elseif {$varFind eq {}} {
set sel $Foundstr
} else {
set sel [set $varFind]
}
if {$donext} {
set pos [$txt index insert]
if {{sel} in [$txt tag names $pos]} {
set pos [$txt index "$pos + 1 chars"]
}
set pos [$txt search -- $sel $pos end]
} else {
set pos {}
my set_HighlightedString {}
}
if {![string length "$pos"]} {
set pos [$txt search -- $sel 1.0 end]
}
if {[string length "$pos"]} {
::tk::TextSetCursor $txt $pos
$txt tag add sel $pos [$txt index "$pos + [string length $sel] chars"]
focus $txt
set res yes
} else {
if {$dobell} bell
set res no
}
list $res $sel
}
#_______________________
method GetLinkLab {m} {
# Gets a link for label.
# m - label with possible link (between <link> and </link>)
# Returns: list of "pure" message and link for label.
if {[set i1 [string first "<link>" $m]]<0} {
return [list $m]
}
set i2 [string first "</link>" $m]
set link [string range $m $i1+6 $i2-1]
set m [string range $m 0 $i1-1][string range $m $i2+7 end]
list $m [list -link $link]
}
#_______________________
method popupFindCommands {pop {txt {}} {com1 ""} {com2 ""}} {
# Returns find commands for a popup menu on a text.
# pop - path to the menu
# txt - path to the text
# com1 - user's command "find first"
# com2 - user's command "find next"
set accF3 [::apave::KeyAccelerator [::apave::getTextHotkeys F3]]
if {$com1 eq {}} {set com1 "[self] InitFindInText 0 $txt; focus \[[self] Entfind\]"}
if {$com2 eq {}} {set com2 "[self] findInText 1 $txt"}
return "\$pop add separator
\$pop add command [my iconA find] -accelerator Ctrl+F -label \"Find First\" \\
-command {$com1}
\$pop add command [my iconA none] -accelerator $accF3 -label \"Find Next\" \\
-command {$com2}"
}
#_______________________
method popupBlockCommands {pop {txt {}}} {
# Returns block commands for a popup menu on a text.
# pop - path to the menu
# txt - path to the text
set accD [::apave::KeyAccelerator [::apave::getTextHotkeys CtrlD]]
set accY [::apave::KeyAccelerator [::apave::getTextHotkeys CtrlY]]
return "\$pop add separator
\$pop add command [my iconA add] -accelerator $accD -label \"Double Selection\" \\
-command \"[self] doubleText {$txt} 0\"
\$pop add command [my iconA delete] -accelerator $accY -label \"Delete Line\" \\
-command \"[self] deleteLine {$txt} 0\"
\$pop add command [my iconA up] -accelerator Alt+Up -label \"Line(s) Up\" \\
-command \"[self] linesMove {$txt} -1 0\"
\$pop add command [my iconA down] -accelerator Alt+Down -label \"Line(s) Down\" \\
-command \"[self] linesMove {$txt} +1 0\""
}
#_______________________
method askForSave {wtxt {doask ""}} {
# For a text, sets/gets "ask for save changes" flag.
# wtxt - text's path
# doask - flag
# If the flag argument omitted, returns the flag else sets it.
# See also: constructor
set prop _AskForSave_$wtxt
if {$doask eq {}} {
set res [::apave::getProperty $prop]
if {![string is false -strict $res]} {set res 1}
} else {
set res [::apave::setProperty $prop $doask]
}
return $res
}
## ________________________ Highlighting _________________________ ##
method popupHighlightCommands {{pop ""} {txt ""}} {
# Returns highlighting commands for a popup menu on a text.
# pop - path to the menu
# txt - path to the text
set accQ [::apave::KeyAccelerator [::apave::getTextHotkeys AltQ]]
set accW [::apave::KeyAccelerator [::apave::getTextHotkeys AltW]]
set res "\$pop add separator
\$pop add command [my iconA upload] -accelerator $accQ \\
-label \"Highlight First\" -command \"[self] seek_highlight %w 2\"
\$pop add command [my iconA download] -accelerator $accW \\
-label \"Highlight Last\" -command \"[self] seek_highlight %w 3\"
\$pop add command [my iconA previous] -accelerator Alt+Left \\
-label \"Highlight Previous\" -command \"[self] seek_highlight %w 0\"
\$pop add command [my iconA next] -accelerator Alt+Right \\
-label \"Highlight Next\" -command \"[self] seek_highlight %w 1\"
\$pop add command [my iconA none] -accelerator Dbl.Click \\
-label \"Highlight All\" -command \"[self] highlight_matches %w\""
if {$txt ne {}} {set res [string map [list %w $txt] $res]}
return $res
}
#_______________________
method set_HighlightedString {sel} {
# Saves a string got from highlighting by Alt+left/right/q/w.
# sel - the string to be saved
set HLstring $sel
if {$sel ne {}} {set Foundstr $sel}
}
#_______________________
method get_HighlightedString {} {
# Returns a string got from highlighting by Alt+left/right/q/w.
if {[info exists HLstring]} {
return $HLstring
}
return {}
}
#_______________________
method set_highlight_matches {w} {
# Creates bindings to highlight matches in a text.
# w - path to the text
if {![winfo exists $w]} return
$w tag configure hilited -foreground #1f0000 -background #ffa073
$w tag configure hilited2 -foreground #1f0000 -background #ff6b85
$w tag lower hilited sel
bind $w <Double-ButtonPress-1> [list [self] highlight_matches $w]
::apave::bindToEvent $w <KeyRelease> [self] unhighlight_matches $w
bind $w <Alt-Left> "[self] seek_highlight $w 0 ; break"
bind $w <Alt-Right> "[self] seek_highlight $w 1 ; break"
foreach k [::apave::getTextHotkeys AltQ] {
bind $w <$k> [list [self] seek_highlight $w 2]
}
foreach k [::apave::getTextHotkeys AltW] {
bind $w <$k> [list [self] seek_highlight $w 3]
}
}
#_______________________
method get_highlighted {txt} {
# Gets a selected word after double-clicking on a text.
# w - path to the text
set err [catch {$txt tag ranges sel} sel]
lassign $sel pos pos2
if {!$err && [llength $sel]==2} {
set sel [$txt get $pos $pos2] ;# single selection
} else {
if {$err || [string trim $sel] eq {}} {
set pos [$txt index "insert wordstart"]
set pos2 [$txt index "insert wordend"]
set sel [string trim [$txt get $pos $pos2]]
if {![string is wordchar -strict $sel]} {
# when cursor just at the right of word: take the word at the left
# e.g. if "_" stands for cursor then "word_" means selecting "word"
set pos [$txt index "insert -1 char wordstart"]
set pos2 [$txt index "insert -1 char wordend"]
set sel [string trim [$txt get $pos $pos2]]
}
set slen [string length $sel]
if {!$slen} {incr slen; set pos2 [$txt index "$pos2 +1c"]}
set pos [$txt index "$pos2 -$slen char"]
set sel [string trim [$txt get $pos $pos2]]
}
}
list $sel $pos $pos2
}
#_______________________
method highlight_matches {txt} {
# Highlights matches of selected word in a text.
# txt - path to the text
lassign [my get_highlighted $txt] sel pos pos2
if {$sel eq {}} return
after idle "[self] highlight_matches_real $txt $pos $pos2"
my set_HighlightedString $sel
set lenList {}
set posList [$txt search -all -count lenList -- "$sel" 1.0 end]
foreach pos2 $posList len $lenList {
if {$len eq {}} {set len [string length $sel]}
set pos3 [$txt index "$pos2 + $len chars"]
if {$pos2 == $pos} {
lappend matches2 $pos2 $pos3
} else {
lappend matches1 $pos2 $pos3
}
}
catch {
$txt tag remove hilited 1.0 end
$txt tag remove hilited2 1.0 end
$txt tag add hilited {*}$matches1
$txt tag add hilited2 {*}$matches2
}
set ::apave::_AP_VARS(HILI,$txt) yes
}
#_______________________
method unhighlight_matches {txt} {
# Unhighlights matches of selected word in a text.
# w - path to the text
if {[info exists ::apave::_AP_VARS(HILI,$txt)] && $::apave::_AP_VARS(HILI,$txt)} {
$txt tag remove hilited 1.0 end
$txt tag remove hilited2 1.0 end
set ::apave::_AP_VARS(HILI,$txt) no
}
}
#_______________________
method seek_highlight {txt mode} {
# Seeks the selected word forward/backward/to first/to last in a text.
# w - path to the text
# mode - 0 (search backward), 1 (forward), 2 (first), 3 (last)
my unhighlight_matches $txt
lassign [my get_highlighted $txt] sel pos pos2
if {$sel eq {}} return
my set_HighlightedString $sel
switch $mode {
0 { ;# backward
set nc [expr {[string length $sel] - 1}]
set pos [$txt index "$pos - $nc chars"]
set pos [$txt search -backwards -- $sel $pos 1.0]
}
1 { ;# forward
set pos [$txt search -- $sel $pos2 end]
}
2 { ;# to first
set pos [$txt search -- $sel 1.0 end]
}
3 { ;# to last
set pos [$txt search -backwards -- $sel end 1.0]
}
}
if {[string length "$pos"]} {
::tk::TextSetCursor $txt $pos
$txt tag add sel $pos [$txt index "$pos + [string length $sel] chars"]
}
}
#_______________________
method highlight_matches_real {txt pos1 pos2} {
# Highlights a selected word in a text, esp. fow Windows.
# Windows thinks a word is edged by spaces only: not in real case.
# txt - path to the text
# pos1 - starting position of real selection
# pos2 - ending position of real selection
$txt tag remove sel 1.0 end
if {[$txt get $pos1] eq "\n"} {
# if a word at line start, Windows select an empty line above
lassign [split $pos1 .] l c
set pos1 [incr l].$c
}
catch {::tk::TextSetCursor $txt $pos1}
$txt tag add sel $pos1 $pos2
}
## ________________________ Query's auxiliaries _________________________ ##
method varName {wname} {
# Gets a variable name associated with a widget's name of "input" dialogue.
# wname - widget's name
return [namespace current]::var$wname
}
#_______________________
method FieldName {name} {
# Gets a field name.
return fraM.fra$name.$name
}
#_______________________
method GetVarsValues {lwidgets} {
# Gets values of entries passed (or set) in -tvar.
# lwidgets - list of widget items
set res [set vars [list]]
foreach wl $lwidgets {
set ownname [my ownWName [lindex $wl 0]]
set vv [my varName $ownname]
set attrs [lindex $wl 6]
if {[string match "ra*" $ownname]} {
# only for widgets with a common variable (e.g. radiobuttons):
foreach t {-var -tvar} {
if {[set v [::apave::getOption $t {*}$attrs]] ne {}} {
array set a $attrs
set vv $v
}
}
}
if {[info exist $vv] && [lsearch $vars $vv]==-1} {
lappend res [set $vv]
lappend vars $vv
}
}
return $res
}
#_______________________
method SetGetTexts {oper w iopts lwidgets} {
# Sets/gets contents of text fields.
# oper - "set" to set, "get" to get contents of text field
# w - window's name
# iopts - equals to "" if no operation
# lwidgets - list of widget items
if {$iopts eq {}} return
foreach widg $lwidgets {
set wname [lindex $widg 0]
set name [my ownWName $wname]
if {[string range $name 0 1] eq "te"} {
set vv [my varName $name]
if {$oper eq "set"} {
my displayText $w.$wname [set $vv]
} else {
set $vv [string trimright [$w.$wname get 1.0 end]]
}
}
}
}
#_______________________
method GetLinePosition {txt ind} {
# Gets a line's position.
# txt - text widget
# ind - index of the line
# Returns a list containing a line start and a line end.
set linestart [$txt index "$ind linestart"]
set lineend [expr {$linestart + 1.0}]
list $linestart $lineend
}
#_______________________
method AppendButtons {widlistName buttons neighbor pos defb timeout win modal ONCLOSE} {
# Adds buttons to the widget list from a position of neighbor widget.
# widlistName - variable name for widget list
# buttons - buttons to add
# neighbor - neighbor widget
# pos - position of neighbor widget
# defb - default button
# timeout - timeout (to count down seconds and invoke a button)
# win - dialogue's path
# modal - yes if the window is modal
# ONCLOSE - command to run at closing the dialog
# Returns list of "Help" button's name and command.
upvar $widlistName widlist
set Defb1 [set Defb2 [set bhlist {}]]
foreach {but txt res} $buttons {
set com "[self] res $Dlgpath"
if {[info commands $res] eq {}} {
set com "$com $res"
} else {
if {$res eq {destroy}} {
# for compatibility with old modal windows
if {$modal} {set res "$com 0"} {set res "destroy $win"}
}
set com $res ;# "res" is set as a command
}
if {$but eq {butHELP}} {
# Help button contains the command in "res"
set com [string map "%w $win" $res]
set bhlist [list $but $com]
} elseif {$Defb1 eq {}} {
set Defb1 $but
} elseif {$Defb2 eq {}} {
set Defb2 $but
}
if {$ONCLOSE ne {}} {append com " ; $ONCLOSE"}
if {[set _ [string first "::" $txt]]>-1} {
set tt " -tip {[string range $txt $_+2 end]}"
set txt [string range $txt 0 $_-1]
} else {
set tt {}
}
if {$timeout ne {} && ($defb eq $but || $defb eq {})} {
set tmo "-timeout {$timeout}"
} else {
set tmo {}
}
if {$but eq {butHELP}} {
set neighbor [lindex $widlist end 1]
set widlist [lreplace $widlist end end]
lappend widlist [list $but $neighbor T 1 1 {-st w} \
"-t \"$txt\" -com \"$com\"$tt $tmo -tip F1"]
set h h_Help
lappend widlist [list $h $but L 1 94 {-st we}]
set neighbor $h
} else {
lappend widlist [list $but $neighbor $pos 1 1 {-st we} \
"-t \"$txt\" -com \"$com\"$tt $tmo"]
set neighbor $but
}
set pos L
}
lassign [my LowercaseWidgetName $Dlgpath.fra.$Defb1] Defb1
lassign [my LowercaseWidgetName $Dlgpath.fra.$Defb2] Defb2
return $bhlist
}
## ________________________ Query the terrible _________________________ ##
method Query {icon ttl msg buttons defb inopts argdia {precom ""} args} {
# Makes a query (or a message) and gets the user's response.
# icon - icon name (info, warn, ques, err)
# ttl - title
# msg - message
# buttons - list of triples "button name, text, ID"
# defb - default button (OK, YES, NO, CANCEL, RETRY, ABORT)
# inopts - options for input dialog
# argdia - list of dialog's options
# precom - command(s) performed before showing the dialog
# args - additional options (message's font etc.)
# The *argdia* may contain additional options of the query, like these:
# -checkbox text (-ch text) - makes the checkbox's text visible
# -geometry +x+y (-g +x+y) - sets the geometry of dialog
# -color cval (-c cval) - sets the color of message
# If "-geometry" option is set (even equaling "") the Query procedure
# returns a list with chosen button's ID and a new geometry.
# Otherwise it returns only the chosen button's ID.
# See also:
# [aplsimple.github.io](https://aplsimple.github.io/en/tcl/pave/index.html)
set wdia $Winpath.dia
append wdia [lindex [split [self] :] end] ;# be unique per apave object
set qdlg [set Dlgpath $wdia[incr Indexdlg]]
# remember the focus (to restore it after closing the dialog)
set focusback [focus]
set focusmatch {}
# options of dialog
lassign {} chmsg geometry optsLabel optsMisc optsFont optsFontM optsHead \
root rotext head hsz binds postcom onclose ONCLOSE timeout tab2 \
tags cc themecolors optsGrid addpopup minsize savetext
set wasgeo [set textmode [set stay [set waitvar 0]]]
set readonly [set hidefind [set scroll [set modal 1]]]
set curpos {1.0}
set CheckNomore 0
foreach {opt val} {*}$argdia {
if {$opt in {-c -color -fg -bg -fgS -bgS -cc -hfg -hbg}} {
# take colors by their variables
if {[info exist $val]} {set val [set $val]}
}
switch -- $opt {
-H - -head {
set head [string map {$ \$ \" \'\' \{ ( \} )} $val]
}
-help {
set buttons "butHELP Help {$val} $buttons"