-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapsforge-for-QMapShack.tcl
More file actions
3056 lines (2570 loc) · 91.8 KB
/
Mapsforge-for-QMapShack.tcl
File metadata and controls
3056 lines (2570 loc) · 91.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# GUI to make Mapsforge maps and themes available to QMapShack
# ============================================================
# Important:
# - Only new "tasks" server type supported!
# - At least Java version 11 required!
# Notes:
# - Additional user settings file is mandatory!
# Name of file = this script's full path
# where file extension "tcl" is replaced by "ini"
# - At least one additional localized resource file is mandatory!
# Name of file = this script's full path
# where file extension "tcl" is replaced by
# 2 lowercase letters ISO 639-1 code, e.g. "en"
# Force file encoding "utf-8"
# Usually required for Tcl/Tk version < 9.0 on Windows!
if {[encoding system] != "utf-8"} {
encoding system utf-8
exit [source $argv0]
}
package require Tk
wm withdraw .
set version "2026-04-22"
set script [file normalize [info script]]
set title [file tail $script]
# Workaround running script by "Open with" on Windows
# Fatal Qt6WebEngineCore issue!
if {[pwd] == "C:/Windows/System32"} {cd [file dirname $script]}
set cwd [pwd]
# Required packages
foreach item {Thread msgcat tooltip http md5 zipfile::decode} {
if {[catch "package require $item"]} {
::tk::MessageBox -title $title -icon error \
-message "Could not load required Tcl package '$item'" \
-detail "Please install missing $tcl_platform(os) package!"
exit
}
}
# Procedure aliases
interp alias {} ::mc {} ::msgcat::mc
interp alias {} ::messagebox {} ::tk::MessageBox
interp alias {} ::tooltip {} ::tooltip::tooltip
interp alias {} ::style {} ::ttk::style
interp alias {} ::button {} ::ttk::button
interp alias {} ::checkbutton {} ::ttk::checkbutton
interp alias {} ::combobox {} ::ttk::combobox
interp alias {} ::radiobutton {} ::ttk::radiobutton
interp alias {} ::scrollbar {} ::ttk::scrollbar
interp alias {} ::md5 {} ::md5::md5
# Define color palette
foreach {item value} {
Background #f0f0f0
ButtonHighlight #ffffff
Border #a0a0a0
ButtonText #000000
DisabledText #6d6d6d
Focus #e0e0e0
Highlight #0078d7
HighlightText #ffffff
InfoBackground #ffffe1
InfoText #000000
Trough #c8c8c8
Window #ffffff
WindowFrame #646464
WindowText #000000
} {set color$item $value}
# Global widget options
foreach {item value} {
background Background
foreground ButtonText
activeBackground Background
activeForeground ButtonText
disabledBackground Background
disabledForeground DisabledText
highlightBackground Background
highlightColor WindowFrame
readonlyBackground Background
selectBackground Highlight
selectForeground HighlightText
selectColor Window
troughColor Trough
Entry.background Window
Entry.foreground WindowText
Entry.insertBackground WindowText
Entry.highlightColor WindowFrame
Listbox.background Window
Listbox.highlightColor WindowFrame
Tooltip*Label.background InfoBackground
Tooltip*Label.foreground InfoText
} {option add *$item [set color$value]}
set dialog.wrapLength [expr [winfo screenwidth .]/2]
foreach {item value} {
Dialog.msg.wrapLength ${dialog.wrapLength}
Dialog.dtl.wrapLength ${dialog.wrapLength}
Dialog.msg.font TkDefaultFont
Dialog.dtl.font TkDefaultFont
Entry.borderWidth 1
Entry.highlightThickness 1
Label.borderWidth 1
Label.padX 0
Label.padY 0
Labelframe.borderWidth 0
Listbox.exportSelection 0
Listbox.relief sunken
Listbox.takeFocus 1
Scale.highlightThickness 1
Scale.showValue 0
Scale.takeFocus 1
Tooltip*Label.padX 2
Tooltip*Label.padY 2
} {eval option add *$item $value}
# Global ttk widget options
style theme use clam
if {$tcl_version > 8.6} {
switch $tcl_platform(os) {
"Windows NT" {lassign {23 41 101 69 120} ry ul ll cy ht}
"Linux" -
"Darwin" {lassign { 3 21 81 49 100} ry ul ll cy ht}
}
set CheckOff "
<rect width='94' height='94' x='3' y='$ry'
style='fill:white;stroke-width:3;stroke:black'/>
"
set CheckOn "
<rect width='94' height='94' x='3' y='$ry'
style='fill:white;stroke-width:3;stroke:black'/>
<path d='M20 $ll L80 $ul M20 $ul L80 $ll'
style='fill:none;stroke:black;stroke-width:14;stroke-linecap:round'/>
"
set RadioOff "
<circle cx='49' cy='$cy' r='47'
fill='white' stroke='black' stroke-width='3'/>
"
set RadioOn "
<circle cx='49' cy='$cy' r='37'
fill='black' stroke='white' stroke-width='20'/>
<circle cx='49' cy='$cy' r='47'
fill='none' stroke='black' stroke-width='3'/>
"
foreach item {CheckOff CheckOn RadioOff RadioOn} \
{image create photo $item \
-data "<svg width='125' height='$ht'>[set $item]</svg>"}
foreach item {Check Radio} {
style element create ${item}button.sindicator image \
[list ${item}Off selected ${item}On]
style layout T${item}button \
[regsub indicator [style layout T${item}button] sindicator]
}
}
switch $tcl_platform(os) {
"Windows NT" {lassign {1 1} yb yc}
"Linux" -
"Darwin" {lassign {0 2} yb yc}
}
foreach {item option value} {
. background $colorBackground
. bordercolor $colorBorder
. focuscolor $colorFocus
. darkcolor $colorWindowFrame
. lightcolor $colorWindow
. troughcolor $colorTrough
. selectbackground $colorHighlight
. selectforeground $colorHighlightText
TButton borderwidth 2
TButton padding "{0 -2 0 $yb}"
TCombobox arrowsize 15
TCombobox padding 0
TCheckbutton padding "{0 $yc}"
TRadiobutton padding "{0 $yc}"
} {eval style configure $item -$option [eval set . \"$value\"]}
foreach {item option value} {
TButton darkcolor {pressed $colorWindow}
TButton lightcolor {pressed $colorWindowFrame}
TButton background {focus $colorFocus pressed $colorFocus}
TCombobox background {focus $colorFocus pressed $colorFocus}
TCombobox bordercolor {focus $colorWindowFrame}
TCombobox selectbackground {!focus $colorWindow}
TCombobox selectforeground {!focus $colorWindowText}
TCheckbutton background {focus $colorFocus}
TRadiobutton background {focus $colorFocus}
Arrow.TButton bordercolor {focus $colorWindowFrame}
} {style map $item -$option [eval list {*}$value]}
# Global widget bindings
foreach item {TButton TCheckbutton TRadiobutton} \
{bind $item <Return> {%W invoke}}
bind TCombobox <Return> {event generate %W <Button-1>}
proc scale_updown {w d} {$w set [expr [$w get]+$d*[$w cget -resolution]]}
bind Scale <MouseWheel> {scale_updown %W [expr %D>0?+1:-1]}
bind Scale <Button-4> {scale_updown %W -1}
bind Scale <Button-5> {scale_updown %W +1}
bind Scale <Button-1> {+focus %W}
# Bitmap arrow down
image create bitmap ArrowDown -data {
#define x_width 9
#define x_height 7
static char x_bits[] = {
0x00,0xfe,0x00,0xfe,0xff,0xff,0xfe,0xfe,0x7c,0xfe,0x38,0xfe,0x10,0xfe
};
}
# Recursively find children
proc find_children {widget} {
set list [winfo children $widget]
foreach item $list {lappend list {*}[find_children $item]}
return $list
}
# Set "tk busy" state
if {$tcl_platform(os) != "Darwin"} {
# Use built-in "tk busy" where applicable
interp alias {} ::tk_busy {} ::tk busy
} else {
# Emulate "tk busy" where required
proc tk_busy {state widget args} {
set classes {TButton TCheckbutton TRadiobutton TCombobox \
Entry Scale Listbox}
lappend list $widget {*}[find_children $widget]
global $widget.busy
switch $state {
"hold" {
array set $widget.busy {}
foreach item $list {
if {[winfo class $item] in $classes} {
set $widget.busy($item) [$item cget -state]
$item configure -state disabled
}
}
}
"forget" {
foreach item $list {
if {[winfo class $item] in $classes} {
set val [set $widget.busy($item)]
$item configure -state $val
}
}
array unset $widget.busy
}
}
}
}
# Try using system locale for script
# If corresponding localized file does not exist, try locale "en" (English)
# Localized filename = script's filename where file extension "tcl"
# is replaced by 2 lowercase letters ISO 639-1 code
set locale [regsub {(.*)[-_]+(.*)} [::msgcat::mclocale] {\1}]
if {$locale == "c"} {set locale en}
set prefix [file rootname $script]
set list [list $locale en]
foreach item [glob -nocomplain -tails -path $prefix. -type f ??] \
{lappend list [lindex [split $item .] end]}
unset locale
foreach item $list {
set file $prefix.$item
if {![file exists $file]} continue
if {[catch {source $file} result]} {
messagebox -title $title -icon error \
-message "Error reading locale file '[file tail $file]':\n$result"
exit
}
set locale $item
::msgcat::mclocale $locale
break
}
if {![info exists locale]} {
messagebox -title $title -icon error \
-message "No locale file '[file tail $file]' found"
exit
}
# Read user settings from file
# Filename = script's filename where file extension "tcl" is replaced by "ini"
set file [file rootname $script].ini
if {![file exist $file]} {
messagebox -title $title -icon error \
-message "[mc i01 [file tail $file]]"
exit
} elseif {[catch {source $file} result]} {
messagebox -title $title -icon error \
-message "[mc i00 [file tail $file]]:\n$result"
exit
}
# Process user settings:
# replace commands resolved by current search path
# replace relative paths by absolute paths
# - commands
set cmds {java_cmd qms_cmd}
# - commands + folders + files
set list [concat $cmds ini_folder maps_folder themes_folder server_jar]
set drive [regsub {((^.:)|(^//[^/]*)||(?:))(?:.*$)} $cwd {\1}]
switch $tcl_platform(os) {
"Windows NT" {cd $env(SystemDrive)/}
"Linux" -
"Darwin" {cd /}
}
foreach item $list {
if {![info exists $item]} continue
set value [set $item]
if {$value == ""} continue
if {$tcl_version >= 9.0} {set value [file tildeexpand $value]}
if {$item in $cmds} {
set exec [auto_execok $value]
if {$exec == ""} {
messagebox -title $title -icon error -message [mc e04 $value $item]
exit
}
set value [lindex $exec 0]
}
switch [file pathtype $value] {
absolute {set $item [file normalize $value]}
relative {set $item [file normalize $cwd/$value]}
volumerelative {set $item [file normalize $drive/$value]}
}
}
cd $cwd
# Check operating system
if {$tcl_platform(os) == "Windows NT"} {
package require registry
if {![info exists env(TMP)]} {set env(TMP) $env(HOME)]}
append env(TMP) \\[format "TMS%8.8x" [pid]]
set tmpdir [file normalize $env(TMP)]
set nprocs $env(NUMBER_OF_PROCESSORS)
} elseif {$tcl_platform(os) == "Linux"} {
if {![info exists env(TMPDIR)]} {set env(TMPDIR) /tmp}
append env(TMPDIR) /[format "TMS%8.8x" [pid]]
set tmpdir $env(TMPDIR)
set nprocs [exec /usr/bin/nproc]
} elseif {$tcl_platform(os) == "Darwin"} {
if {![info exists env(TMPDIR)]} {set env(TMPDIR) /tmp}
append env(TMPDIR) /[format "TMS%8.8x" [pid]]
set tmpdir $env(TMPDIR)
set nprocs [exec sysctl -n hw.ncpu]
} else {
messagebox -title $title -icon error -message [mc e03 $tcl_platform(os)]
exit
}
# Create temporary files folder and delete on exit
file mkdir $tmpdir
rename ::exit ::quit
proc exit {args} {catch {file delete -force $::tmpdir}; eval quit $args}
# Trying to force Qt application QMapShack to use XCB instead of Wayland
# to be able to handle X11 events, in particular WM_DELETE_WINDOW
if {[tk windowingsystem] == "x11"} {set env(QT_QPA_PLATFORM) "xcb;wayland"}
# Restore saved settings from folder ini_folder
if {![info exists ini_folder]} {set ini_folder $env(HOME)/.Mapsforge}
file mkdir $ini_folder
set maps.selection {}
set maps.world 0
set maps.contrast 0
set maps.gamma 1.00
set user.scale 1.00
set text.scale 1.00
set symbol.scale 1.00
set line.scale 1.00
set font.size [font configure TkDefaultFont -size]
set console.show 0
set console.geometry ""
set console.font.size 8
set dem.folder ""
set shading.onoff 0
set shading.layer onmap
set shading.magnitude 1.
set shading.algorithm simple
set shading.simple.linearity 0.1
set shading.simple.scale 0.666
set shading.diffuselight.angle 50.
set shading.asy.values [list 0.5 0 80 [expr max(1,$nprocs/3)] $nprocs true]
array set shading.asy.array {}
set shading.zoom.min.apply false
set shading.zoom.min.value 9
set shading.zoom.max.apply false
set shading.zoom.max.value 17
set tcp.port $tcp_port
set tcp.interface $interface
set tcp.maxconn 1024
set log.requests 0
set min_zoom_level [expr max( 4,$min_zoom_level)]
set max_zoom_level [expr min(20,$max_zoom_level)]
set qms.conf 0
set qms.file ""
set qms.args ""
set qms.default {splash 0 style Fusion fontsize "" fontfamily "" scale 1.000 debug 0 highdpi 1}
lmap {i v} ${qms.default} {set qms.$i $v}
# Save/restore settings
proc save_settings {file args} {
array set save {}
set fd [open $file a+]
seek $fd 0
while {[gets $fd line] != -1} {
regexp {^(.*?)=(.*)$} $line "" name value
set save($name) $value
}
foreach name $args {set save($name) [set ::$name]}
seek $fd 0
chan truncate $fd
foreach name [lsort [array names save]] {puts $fd $name=$save($name)}
close $fd
}
proc restore_settings {file} {
if {![file exists $file]} return
set fd [open $file r]
while {[gets $fd line] != -1} {
regexp {^(.*?)=(.*)$} $line "" name value
set ::$name $value
}
close $fd
}
# Restore saved settings
foreach item {global hillshading qmapshack} \
{restore_settings $ini_folder/$item.ini}
set i 0
lmap v ${shading.asy.values} {set shading.asy.array($i) $v; incr i}
# Restore saved font sizes
foreach item {TkDefaultFont TkTextFont TkFixedFont TkTooltipFont} \
{font configure $item -size ${font.size}}
# Configure main window
set title [mc l01]
wm title . $title
wm protocol . WM_DELETE_WINDOW "set action 0"
wm resizable . 0 0
. configure -bd 5 -bg $colorBackground
# Output console window (".console" issue!)
set console 0; # Valid values: 0=hide, 1=show
toplevel .konsole
wm withdraw .konsole
wm title .konsole "$title - [mc l99]"
set family [font configure TkFixedFont -family]
foreach item {Consolas "Ubuntu Mono" "Noto Mono" "Liberation Mono" "SF Mono"} {
if {$item ni [font families]} continue
set family $item
break
}
font create console_font -family $family -size ${console.font.size}
text .konsole.txt -font console_font -wrap none -setgrid 1 \
-state disabled -undo 0 -bg white \
-width 120 -xscrollcommand {.konsole.sbx set} \
-height 24 -yscrollcommand {.konsole.sby set}
scrollbar .konsole.sbx -orient horizontal -command {.konsole.txt xview}
scrollbar .konsole.sby -orient vertical -command {.konsole.txt yview}
grid .konsole.txt -row 1 -column 1 -sticky nswe
grid .konsole.sby -row 1 -column 2 -sticky ns
grid .konsole.sbx -row 2 -column 1 -sticky we
grid columnconfigure .konsole 1 -weight 1
grid rowconfigure .konsole 1 -weight 1
update idletasks
if {${console.geometry} != ""} {
lassign ${console.geometry} x y cols rows
if {$x > [expr [winfo vrootx .konsole]+[winfo vrootwidth .konsole]] ||
$x < [winfo vrootx .konsole]} {set x [winfo vrootx .konsole]}
wm positionfrom .konsole program
catch "wm geometry .konsole ${cols}x${rows}+$x+$y"
}
bind .konsole.txt <Control-a> {%W tag add sel 1.0 end;break}
bind .konsole.txt <Control-c> {tk_textCopy %W;break}
bind .konsole <Control-plus> {console_font_size_incr +1}
bind .konsole <Control-minus> {console_font_size_incr -1}
bind .konsole <Control-KP_Add> {console_font_size_incr +1}
bind .konsole <Control-KP_Subtract> {console_font_size_incr -1}
bind .konsole <Configure> {
if {"%W" != ".konsole"} continue
scan [wm geometry %W] "%%dx%%d+%%d+%%d" cols rows x y
set console.geometry "$x $y $cols $rows"
}
proc console_font_size_incr {incr} {
set px [.konsole.txt xview]
set py [.konsole.txt yview]
set size [font configure console_font -size]
incr size $incr
if {$size < 5 || $size > 20} return
font configure console_font -size $size
set ::console.font.size $size
update idletasks
.konsole.txt xview moveto [lindex $px 0]
.konsole.txt yview moveto [lindex $py 0]
}
proc console_write {text} {
.konsole.txt configure -state normal
foreach item [split $text \n] {
if {[string index $item 0] == "\r"} {
set item [string range $item 1 end]
.konsole.txt delete end-2l end-1l
}
if {[string index $item end] == "\b"} {
set item [string range $item 0 end-1]
} else {
append item \n
}
.konsole.txt insert end $item
}
.konsole.txt configure -state disabled
if {[winfo ismapped .konsole]} {.konsole.txt see end}
}
proc console_show_hide {show} {
if {$show} {
.konsole.txt see end
wm attributes .konsole -topmost 1
wm deiconify .konsole
wm attributes .konsole -topmost 0
} else {
wm withdraw .konsole
}
}
if {$console == 1} {console_show_hide 1}
# Write to console
proc cputs {text} {console_write $text}
proc cputi {text} {cputs "\[---\] $text"}
proc cputw {text} {cputs "\[+++\] $text"}
cputw [mc m51 [pid] [file tail [info nameofexecutable]]]
cputw "Tcl/Tk version $tcl_patchLevel"
cputw "Script '[file tail $script]' version $version"
# Show error message
proc error_message {message exit_return} {
messagebox -title $::title -icon error -message $message
eval $exit_return
}
# Get shell command from exec command
proc get_shell_command {command} {
return [join [lmap item $command {regsub {^(.* +.*|())$} $item {"\1"}}]]
}
# Check commands, files & folders
foreach item {qms_cmd java_cmd} {
set value [set $item]
if {$value == ""} {error_message [mc e04 $value $item] exit}
}
foreach item {server_jar} {
set value [set $item]
if {![file isfile $value]} {error_message [mc e05 $value $item] exit}
}
foreach item {maps_folder themes_folder} {
set value [set $item]
if {![file isdirectory $value]} {error_message [mc e05 $value $item] exit}
}
# Work around Oracle's Java wrapper "java.exe" issue:
# Wrapper requires running within real Windows console,
# therefore not working within Tcl script called by "wish"!
# -> Try getting Java's real path from Windows registry
if {$tcl_platform(os) == "Windows NT" &&
([regexp -nocase {^.*/Program Files.*/Common Files/Oracle/Java/.*/java.exe$} $java_cmd]
|| [regexp -nocase {^.*/ProgramData/Oracle/Java/.*/java.exe$} $java_cmd])} {
set exec ""
foreach item {HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft \
HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\JavaSoft} {
foreach key {JRE "Java Runtime Environment" JDK "Java Development Kit"} {
if {[catch {registry get $item\\$key CurrentVersion} value]} continue
if {[catch {registry get $item\\$key\\$value JavaHome} value]} continue
set exec [auto_execok "[file normalize $value]/bin/java.exe"]
if {$exec != ""} break
}
if {$exec == ""} continue
set java_cmd [lindex $exec 0]
break
}
}
# Get major Java version
set java_version 0
set java_string unknown
set command [list $java_cmd -version]
set rc [catch "exec $command 2>@1" result]
if {!$rc} {
set line [lindex [split $result \n] 0]
regsub -nocase {^.* version "(.*)".*$} $line {\1} data
set java_string $data
if {[regsub {^1\.([1-9]+)\.[0-9]+.*$} $java_string {\1} data] > 0} {
set java_version $data; # Oracle Java version <= 8
} elseif {[regsub {^([1-9][0-9]*)((\.0)*\.[1-9][0-9]*)*([+-].*)?$} \
$java_string {\1} data] > 0} {
set java_version $data; # Other Java versions >= 9
}
}
if {$rc || $java_version == 0} \
{error_message [mc e08 Java [get_shell_command $command] $result] exit}
# Check minimum required Java version
set java_version_min 11
if {$java_version < $java_version_min} \
{error_message [mc e07 Java $java_string $java_version_min] exit}
# Prepend Java executable's path to PATH environment variable
# to force same Java executable for nested Java calls
set path [file dirname $java_cmd]
switch $tcl_platform(os) {
"Windows NT" {set env(PATH) "[file nativename $path]\;$env(PATH)"}
"Linux" -
"Darwin" {set env(PATH) "$path:$env(PATH)"}
}
# Evaluate numeric Mapsforge server version
# from output line ending with version string " version: x.y.z.c"
set server_version 0
set server_string unknown
set command [list $java_cmd -jar $server_jar -help]
set rc [catch "exec $command 2>@1" result]
foreach line [split $result \n] {
if {![regexp -nocase {^(?:.* version: )([0-9.]+)$} $line "" data]} continue
set server_string $data
set data [split $data .]
if {[llength $data] != 4} \
{error_message [mc e07 "Mapsforge Server" $server_string 0.22.0.0] exit}
foreach item $data {set server_version [expr 100*$server_version+$item]}
break
}
if {$rc || $server_version == 0} \
{error_message [mc e08 Server [get_shell_command $command] $result] exit}
if {$server_version < 220000} \
{error_message [mc e07 "Mapsforge Server" $server_string 0.22.0.0] exit}
# Get QMapShack version
set qms_version 0
set qms_string unknown
set command [list $qms_cmd --version]
set rc [catch "exec $command 2>@1" result]
if {!$rc} {
set line [lindex [split $result \n] 0]
regsub -nocase {^.* (.*)$} $line {\1} data
set qms_string $data
if {[regexp {^[0-9.]+$} $qms_string]} {
set data [split $data .]
foreach item $data {set qms_version [expr 100*$qms_version+$item]}
}
}
# Recursively find files
proc find_files {folder pattern} {
set list [glob -nocomplain -directory $folder -type f $pattern]
foreach subfolder [glob -nocomplain -directory $folder -type d *] \
{lappend list {*}[find_files $subfolder $pattern]}
return $list
}
# Get list of available Mapsforge maps
cd $maps_folder
set maps [find_files "" "*.map"]
cd $cwd
set maps [lsort -dictionary $maps]
if {[llength $maps] == 0} {error_message [mc e06] exit}
# Get list of available Mapsforge themes
# and add Mapsforge server's built-in themes
cd $themes_folder
set themes [find_files "" "*.xml"]
cd $cwd
zipfile::decode::open $server_jar
set dict [zipfile::decode::archive]
set list [zipfile::decode::files $dict]
foreach item [lsearch -inline -all $list "assets/mapsforge/*.xml"] {
zipfile::decode::copyfile $dict $item $tmpdir/$item
set item ([string toupper [file rootname [file tail $item]]])
if {$item != "(DEFAULT)" && $item != "(HILLSHADING)"} {lappend themes $item}
}
zipfile::decode::close
set themes [lsort -dictionary $themes]
set themes [linsert $themes 0 (DEFAULT)]
# --- Begin of main window
# Title
font create title_font {*}[font configure TkDefaultFont] \
-underline 1 -weight bold
label .title -text $title -font title_font -fg blue
pack .title -expand 1 -fill x
set github https://github.com/JFritzle/Mapsforge-for-QMapShack
tooltip .title $github
switch $tcl_platform(os) {
"Windows NT" {set exec "exec cmd.exe /C START {} $github"}
"Linux" {set exec "exec nohup xdg-open $github >/dev/null"}
"Darwin" {set exec "exec nohup open $github >/dev/null"}
}
bind .title <Button-1> "catch {$exec}"
# Menu column
frame .f
pack .f
# Server task(s)
set task.pattern "^\[0-9A-Za-z\]+(\[_.+-\]?\[0-9A-Za-z\]+)*$"
set task.active ""
set task.name ""
lappend task.set ""
foreach task [glob -nocomplain -path $ini_folder/ \
-type f -tails task.*.ini] {
set task [regsub {^task.(.*).ini$} $task {\1}]
if {![regexp ${task.pattern} $task]} continue
lappend task.set $task
}
set task.set [lsort -unique ${task.set}]
lappend task.use ""
set task.use [lmap task ${task.set} \
{if {"$task" ni ${task.use}} continue;set task}]
labelframe .task -labelanchor w -text "[mc l02]: " -bd 0
entry .task.name -width 28 -textvariable task.name \
-takefocus 1 -highlightthickness 0
bind .task.name <Return> task_item_add
button .task.post -image ArrowDown -command task_list_post
pack .task.post -side right -fill y
pack .task.name -side right -fill x -expand 1
pack .task -in .f -expand 1 -fill x -pady {8 0}
foreach item {.task .task.name} {tooltip $item [mc l02t]}
proc task_updown {d} {
set l [llength ${::task.set}]
if {$l == 1} {return}
set v ${::task.active}
save_task_settings $v
set i [lsearch ${::task.set} $v]
incr i $d
if {$i == $l} {set i 0}
if {$i == -1} {incr i $l}
set v [lindex ${::task.set} $i]
set ::task.name $v
.task.name icursor end
set ::task.active $v
catch ".task_list.listbox activate $i"
restore_task_settings $v
}
bind .task.name <MouseWheel> {task_updown [expr %D>0?-1:+1]}
foreach item {Down Button-4} {bind .task.name <$item> {task_updown +1}}
foreach item {Up Button-5} {bind .task.name <$item> {task_updown -1}}
proc task_list_post {} {
if {![task_item_add]} return
set tl .task_list
set lb $tl.listbox
set sb $tl.scrollbar
if {[winfo exists $lb]} {
task_list_unpost
return
}
set tn .task.name
set x [winfo rootx $tn]
set y [winfo rooty $tn]
scan [winfo geometry $tn] "%dx%d" w h
incr w [winfo width .task.post]
incr y $h
toplevel $tl -relief flat -bd 0
wm withdraw $tl
switch [tk windowingsystem] {
win32 {
wm overrideredirect $tl true
wm attributes $tl -topmost 1
}
x11 -
aqua {
wm attributes $tl -type combo
wm overrideredirect $tl true
}
}
wm geometry $tl +$x+$y
wm minsize $tl $w 0
scrollbar $sb -command "$lb yview"
set len [llength ${::task.set}]
listbox $lb -selectmode multiple -activestyle underline -bd 0 \
-height [expr min($len,5)]
if {$len > 5} {
pack $sb -side right -fill y
$lb configure -yscrollcommand "$sb set"
}
pack $lb -side left -fill x -expand 1
tooltip $lb [mc l03t]
$lb insert 0 {*}${::task.set}
$lb activate 0
set i 0
foreach v ${::task.set} {
if {$v in ${::task.use}} {$lb selection set $i}
if {$v == ${::task.name}} {$lb activate $i}
incr i
}
foreach v {Map Enter} \
{bind $lb <$v> {focus -force %W}}
bind $lb <Delete> task_item_delete
bind $lb <Tab> task_item_toggle
bind $lb <Key-space> {task_item_toggle;break}
bind $lb <Button-1> \
{%W activate @%x,%y;task_name_update;task_item_toggle;break}
foreach v {<PrevLine> <NextLine>} \
{bind $lb <$v> "[bind Listbox <$v>];task_name_update;break"}
foreach v {ButtonRelease-3 Escape FocusOut} \
{bind $lb <$v> {task_list_unpost;break}}
bind $tl <Button> \
{if {"[winfo containing %X %Y]" != "%W"} {task_list_unpost;break}}
wm transient $tl .
wm attribute $tl -topmost 1
update idletasks
wm deiconify $tl
raise $tl
grab -global $tl
}
proc task_list_unpost {} {
focus -force .task.name
set tl .task_list
set lb $tl.listbox
set ::task.use {}
foreach i [$lb curselection] {lappend ::task.use [$lb get $i]}
destroy $tl
}
proc task_name_update {} {
set lb .task_list.listbox
set i [$lb index active]
set v [$lb get $i]
if {$v == ${::task.active}} return
save_task_settings ${::task.active}
set ::task.name $v
set ::task.active $v
restore_task_settings ${::task.active}
if {[process_running srv]} {srv_task_create $v}
}
proc task_item_toggle {} {
set lb .task_list.listbox
set i [$lb index active]
set v [$lb get $i]
if {$v == ""} return
if {$i in [$lb curselection]} {
$lb selection clear $i
if {[process_running srv]} {srv_task_delete $v}
} else {
$lb selection set $i
if {[process_running srv]} {srv_task_create $v}
}
}
proc task_item_delete {} {
set lb .task_list.listbox
set sb .task_list.scrollbar
set i [$lb index active]
set v [$lb get $i]
if {$v == ""} return
$lb delete $i
set ::task.set [lreplace ${::task.set} $i $i]
set len [llength ${::task.set}]
$lb configure -height [expr min($len,5)]
if {$len <= 5} {pack forget $sb}
task_name_update
set file $::ini_folder/task.$v.ini
file delete $file
set ::task.active [$lb get active]
restore_task_settings ${::task.active}
if {[process_running srv]} {srv_task_delete $v}
}
proc task_item_add {} {
set tn .task.name
set v [$tn get]
set i [lsearch ${::task.set} $v]
if {$i != -1} {
if {$v == ${::task.active}} {return 1}
save_task_settings ${::task.active}
set ::task.active $v
restore_task_settings ${::task.active}
} elseif {[regexp ${::task.pattern} $v]} {
save_task_settings ${::task.active}
set ::task.active $v
set ::task.set [lsort [lappend ::task.set $v]]
set ::task.use [lsort [lappend ::task.use $v]]
} else {
error_message [mc l02e $v] return
set ::task.name ${::task.active}
return 0
}
save_task_settings ${::task.active}
if {[process_running srv]} {srv_task_create $v}
return 1
}
# Save active task settings
proc save_task_settings {task} {
lmap {i v} [array get ::shading.asy.array] {lset ::shading.asy.values $i $v}
set file $::ini_folder/task.$task.ini
file delete $file
save_settings $file \
maps.language maps.selection maps.world maps.contrast maps.gamma \
theme.selection user.scale text.scale symbol.scale line.scale \
shading.layer shading.onoff shading.algorithm \
shading.simple.linearity shading.simple.scale \
shading.diffuselight.angle shading.asy.values \
shading.magnitude dem.folder \
shading.zoom.min.apply shading.zoom.min.value \
shading.zoom.max.apply shading.zoom.max.value
lassign [get_selected_style_overlays] style.id overlay.ids
if {${style.id} != ""} {
set fd [open $file a+]
puts $fd style.id=${style.id}\noverlay.ids=${overlay.ids}
close $fd
}
}