-
Notifications
You must be signed in to change notification settings - Fork 3
/
direct-code.red
3131 lines (2881 loc) · 123 KB
/
direct-code.red
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
Red [
Title: "Direct Code"
Author: "Mike Yaunish / Nenad Rakocevic / Didier Cadieu /"
File: %direct-code.red
Version: 4.0.0
Needs: 'View
Requires: 10-Oct-2023/9:48:47-06:00
Usage: {
Continuation of sample livecode-enhanced.red script. from Nenad and Didier.
Type VID code in the bottom left area, you will see the resulting GUI components
rendered live on the right side and fully functional (events/actors/reactors working live).
The top left area lets you define Red's values to be used in your VID code, even functions or anything.
}
History: [
1.0.0 "01-09-2016" "First version (Nenad)."
1.1.0 "09-09-2016" "Addition of red code predefinitions area and window resizing (Didier)."
1.2.0 "21-09-2016" "Addition of vertical and horizontal spliters (Didier)."
1.2.1 "06-10-2016" "Correction of vertical spliter resize (Didier)."
1.3.0 "06-12-2017" {Added: Live update check, manual update buttons, Save/Open/New of script as
well as Update check and panel sizes.
Autosave of file when it interprets correctly. Control+Tilde or middle mouse
to see details of the object that you are over (Mike)}
1.4.0 "13-12-2017" {Start adding direct maninpulation features and rename project 'direct-code' to
reflect the impetus to create a larger scope for the project(Mike)}
2.0.0 "03-01-2021" {Full rewrite of code generation engine. (Mike)}
2.0.1 "15-02-2021" {Correctly parse blocks imbedded in vid code}
3.0.0 "19-03-2022" {Full rewrite of the code generation engine (again) using transcode.
Rebuild the VID Object Editor to enable changing every facet of an object.}
4.0.0 "02-06-2024" {Added: Insert Tool, Style Object Editor, Scenario and setup-style code blocks}
]
Tabs: 4
]
if system/build/date < 10-Oct-2023/15:49:07 [
version-test: ask rejoin [ {The version of Red you are using needs to be} newline
{at least at: 10-Oct-2023/15:49:07 or this program } newline
{will not work properly.} newline {Do you want to continue anyway? y = "yes", n = "no"}
]
if version-test <> "y" [ exit ]
]
#include %support-scripts/direct-code-includes.red
lprint: function [ s /no-newline /force ] [
if any [
global-logging
force
][
lines: either no-newline [ false ] [ true ]
write/append/:lines rejoin [ ( first split-path current-file ) %direct-code.log ] form reduce s
]
]
bprint: function [s] [
lprint s
print form reduce s
]
dc-ctx: context [
set 'root-path copy what-dir
#include rejoin [ root-path %support-scripts/select-text-block.red ]
#include rejoin [ root-path %support-scripts/direct-code-utilities.red ]
set 'dc-alter-facet-object-name
set 'dc-code-version 0
set 'dc-voe-layout-template-file rejoin [ root-path %support-scripts/voe-layout-template.red ]
set 'dc-voe-layout-template-data read dc-voe-layout-template-file
set 'dc-style-template rejoin [ root-path %support-scripts/style-template.red ]
set 'active-voe-windows copy []
set 'orphaned-voe-windows copy []
set 'internal-source-change-flag? false
set 'dc-last-setup-code-error copy []
set 'dc-last-vid-code-error copy []
--over-face: none
--obj-selected: none
set '--dc-mainwin-offset 0x0
set '--dc-mainwin-edge 0x0
set '--voe-window make object! [size: 0x0 offset: 0x0]
set 'dc-red-executable copy ""
set 'dc-all-active-styles copy []
set 'dc-style-catalog-path rejoin [ root-path %style-catalog/ ]
set 'dc-scenario-catalog-path rejoin [ root-path %scenario-catalog/ ]
set 'current-file copy ""
set 'gray-green 157.178.145
set 'yellow-green 205.194.79
dc-reactor: make reactor! [
current-file: rejoin [ system/options/path %help/welcome-to-direct-code.red ]
active-object: ""
]
set 'rooted function [ file [file!]][
return rejoin [ root-path file ]
]
set 'evo-after-insert true
current-path: does [ first split-path dc-reactor/current-file ]
setup-size: 752x200
vid-size: 752x232
output-panel-size: 987x526
live-update?: ""
insert-tool-pinned?: false
set 'dc-voe-selected-tab 1 ;-- This is global because it needs to be read by VOE
set 'dc-voe-size "regular"
set 'dc-insert-tool-tab 1
reload-on-change?: false
set 'global-logging false
red-executable: none
set 'get-catalog-filenames function [ ;-- get-catalog-filenames:
/scenario
][
post-fix: %-style.red
post-len: -10
files: either scenario [
post-fix: %-scenario.red
post-len: -13
read dc-scenario-catalog-path
][
read dc-style-catalog-path
]
collected: collect [
foreach file files [
if (copy/part tail file post-len) = post-fix [
keep file
]
]
]
]
set 'dc-external-editor none
set 'dc-external-editor-commands [
needs-shell?: none
plain-open: [ editor-executable " " filename ]
open-to-line: none
open-to-column: none
open-with-find: none
]
set 'dc-default-action-list does [ ;-- dc-default-action-list:
return collect [
foreach i (keys-of system/view/vid/styles) [
keep i
keep to-safe-string system/view/vid/styles/:i/default-actor
]
]
]
set 'dc-actor-list sort collect [
foreach [ a1 a2 ] to-block system/view/evt-names [
keep to-string a2
]
]
direct-code-settings: rejoin [ root-path %settings/direct-code-settings.data ]
set 'back-out-vid-changes func [ ;-- back-out-vid-changes:
change-index [integer!]
][
if vid-code-undoer/action-index = change-index [ exit ]
new-vid-code: vid-code-undoer/back-out-changes change-index
vid-code/text: copy new-vid-code
run-and-save "back-out-changes"
]
set 'dc-undo-redo func [
/vid
/setup
/undo
/redo
/local new-setup-code new-vid-code
][
undoer-refine: either undo [
'undo
][
'redo
]
if all [
setup
(new-setup-code: setup-code-undoer/:undoer-refine )
][
setup-code/text: copy new-setup-code
]
if all [
vid
(new-vid-code: vid-code-undoer/:undoer-refine )
][
vid-code/text: copy new-vid-code
]
if any [ new-setup-code new-vid-code ][
run-and-save "undo-redo"
]
]
set 'get-catalog-entry-names function[ ;-- get-catalog-entry-names2
/scenario
/code
][
either code [
read rooted %code-catalog/
][
catalog-files: get-catalog-filenames/:scenario
post-len: either scenario [ 13 ][ 10 ]
collect [
foreach filename catalog-files [
keep copy/part (to-string filename) (( length? filename) - post-len )
]
]
]
]
set 'dc-catalog-styles get-catalog-entry-names
set 'dc-scenarios get-catalog-entry-names/scenario
set 'dc-code-catalog get-catalog-entry-names/code
set 'is-scenario-file? function [
filename
][
return either find to-string (second split-path filename ) "-scenario.red" [
true
][
false
]
]
set 'vid-code-marker {;Direct Code VID Code source marker - DO NOT MODIFY THIS LINE OR THE NEXT LINE!} ;-- vid-code-marker:
set 'show-window-code-marker {;Direct Code Show Window source marker - DO NOT MODIFY THIS LINE OR THE NEXT LINE!} ;-- vid-code-marker:
get-list-of-styles: function [] [
results: collect [
foreach [style-name x ] (get-styles to-block vid-code/text) [
keep to-string style-name
]
]
return results
]
set 'validate-object-name function [ ;-- validate-object-name:
object-name [string!]
][
if value? (to-word object-name) [
object-exists?: true
while [ object-exists? ] [
either req-res: prompt/text rejoin ["The word: " mold object-name " is already in use. Please provide a different name."]
[
object-exists?: value? (to-word object-name: trim req-res)
][
return false
]
]
return object-name
]
return object-name
]
set 'get-list-of-named-objects function [] [ ;-- get-list-of-named-objects:
res-blk: copy []
foreach-face output-panel [
obj-name: get-object-name face
if obj-name <> "*unusable-no-name*" [
append res-blk obj-name
]
]
return res-blk
]
set-red-executable: function [
/none-set
/extern red-executable dc-red-executable
][
full-msg: "Where is the Red executable file?"
if none-set [
red-executable: system/options/boot
dc-red-executable: system/options/boot
save-settings
return ""
]
refines: copy []
if red-ex: do refine-function/args request-a-file refines reduce [
any [ (if (red-executable <> 'none) [red-executable]) "" ]
full-msg
"Red Executable File:"
][
red-executable: red-ex
dc-red-executable: red-ex
save-settings
]
]
set-external-editor: function [
/none-set
/extern dc-external-editor
][
full-msg: "Where is your External Text Editor?"
either none-set [
full-msg: copy rejoin ["The current 'External Text Editor' Setting is missing or incomplete ^/" full-msg ]
refines: copy [ skip-button ]
][
refines: copy []
]
if external-ed: do refine-function/args request-a-file refines reduce [
any [ (if (dc-external-editor <> 'none) [ dc-external-editor ]) "" ]
full-msg
"External Text Editor:"
][
dc-external-editor: external-ed
save-settings
]
]
set 'run-script function [ ;-- run-script:
{Run a Red script while protecting the current directory}
filename {The full path and filename to the script}
/args arg
]
[
cur-dir: system/options/path
either args [
do/args filename arg
][
do filename
]
change-dir cur-dir
]
set 'run-user-script does [
run-script rejoin [ root-path %user-script/user-script.red ]
]
edit-user-script: does [
load-and-run rejoin [ root-path %user-script/user-script.red ]
]
edit-evo: does [
load-and-run rejoin [ root-path %experiments/edit-vid-object/edit-vid-object.red ]
]
run-program-separately: does [
either (exists? to-file red-executable) [
call/shell rejoin [ to-local-file red-executable " " to-local-file dc-reactor/current-file ]
][
set-red-executable/none-set
]
]
do-current-file: does [
do dc-reactor/current-file
]
set 'monitor-file-change function [ ;-- monitor-file-change:
status
/extern reload-on-change?
][
either status [
the-rate: 00:00:00.250
the-color: 0.200.0
reload-on-change?: true
][
the-rate: 999:00:00
the-color: blue
reload-on-change?: false
]
save-settings
check-for-file-change/rate: the-rate
check-for-file-change/color: the-color
]
set 'find-unused-object-name function [ ;-- find-unused-object-name:
obj-prefix [ string!]
/excluding exclude-names [string!]
][
obj-midfix: pick [ "-" "" ] (all-to-logic find ["h1" "h2" "h3" "h4" "h5"] obj-prefix )
ndx: 1
obj-search: rejoin [ obj-prefix obj-midfix ndx ":" ]
if not excluding [ exclude-names: "" ]
while [
if any [
find setup-code/text obj-search
find vid-code/text obj-search
find exclude-names obj-search
][ true ]
][
ndx: ndx + 1
obj-search: rejoin [ obj-prefix obj-midfix ndx ":"]
]
return rejoin [ obj-prefix obj-midfix ndx ]
]
set 'insert-vid-object function [ ;-- insert-vid-object:
{Inserts a given object type into the current layout}
obj-type [string! word!] {Object type that determines object naming prefix as well}
/with-on-click on-click-code [block!]
/with-text text-string [string!]
/with-offset offset-pos [pair!]
/position pos [integer!]
/style style-name [string!]
/named named-object [string!] {Custom object name prefix}
/pre-selected pre-insert-object-selected
/catalog
/no-setup {exclude running run-setup-style}
][
before-change-index: vid-code-undoer/action-index
selected-object: either pre-selected [
either pre-insert-object-selected [
obj-src-pos: get-object-source-position vid-code/text pre-insert-object-selected
find-vid-object/location vid-code/text obj-src-pos
][
none
]
][
either vid-code/selected = none [
none
][
find-vid-object/location vid-code/text vid-code/selected
]
]
if selected-object [
if find (get-styles to-block vid-code/text) (to-set-word first selected-object) [
style-end-pos: tail-position-of-styles vid-code/text
selected-object/2/x: (style-end-pos/y + 2 )
]
if not position [
position: true
pos: selected-object/2/x
]
vid-code/selected: none
]
if obj-type = none [
return none
]
object-type: either ((copy/part to-string obj-type 4) = "ins-") [ ;-- This is to deal with the menu selection stuff
object-type: copy skip to-string obj-type 4
][
object-type: copy to-string obj-type
]
obj-template: [
base [ (obj-set-word) (to-word object-type) (obj-name) font-color 255.255.255]
box [ (obj-set-word) (to-word object-type) (obj-name)]
text [ (obj-set-word) (to-word object-type) (obj-name)]
button [ (obj-set-word) (to-word object-type) (obj-name)]
check [ (obj-set-word) (to-word object-type) (obj-name)]
radio [ (obj-set-word) (to-word object-type) (obj-name)]
toggle [ (obj-set-word) (to-word object-type) (obj-name)]
field [ (obj-set-word) (to-word object-type) (obj-name)]
area [ (obj-set-word) (to-word object-type) (obj-name)]
image [ (obj-set-word) (to-word object-type) ]
text-list [ (obj-set-word) (to-word object-type) data ["one" "two" "three" "four"] select 2 ]
drop-list [ (obj-set-word) (to-word object-type) data ["one" "two" "three" "four"] select 2 ]
drop-down [ (obj-set-word) (to-word object-type) data ["one" "two" "three" "four"] select 2 ]
calendar [ (obj-set-word) (to-word object-type) ]
progress [ (obj-set-word) (to-word object-type) 25% ]
slider [ (obj-set-word) (to-word object-type) ]
scroller [ (obj-set-word) (to-word object-type) ]
camera [ (obj-set-word) (to-word object-type) 330x250 on-create [ (to-set-path rejoin [to-string obj-set-word "/selected" ]) 1 ] ]
panel [ (obj-set-word) (to-word object-type) (obj-name) 250.250.250 [ panel-button1: button "panel-button1"] ]
tab-panel [ (obj-set-word) (to-word object-type) (obj-name) [ "Tab-A" [ tab-a-btn1: button "tab-A-btn1"] "Tab-B" [ tab-b-btn1: button "tab-B-btn1" ] ]]
;window [ (obj-set-word) (to-word object-type) (obj-name)]
screen [ (obj-set-word) (to-word object-type) (obj-name)]
group-box [ (obj-set-word) (to-word object-type) (obj-name) [ group-box-button1: button "group-box-button1"] ]
h1 [ (obj-set-word) (to-word object-type) (obj-name)]
h2 [ (obj-set-word) (to-word object-type) (obj-name)]
h3 [ (obj-set-word) (to-word object-type) (obj-name)]
h4 [ (obj-set-word) (to-word object-type) (obj-name)]
h5 [ (obj-set-word) (to-word object-type) (obj-name)]
rich-text [ (obj-set-word) (to-word object-type) "Hello Red World" data [1x17 0.0.255 italic 7x3 255.0.0 bold 24 underline] ]
timer [ (obj-set-word) (to-word object-type) (obj-name) 210.210.210]
iso-info [ (obj-set-word) (to-word object-type) ]
iso-question [ (obj-set-word) (to-word object-type) ]
iso-warning [ (obj-set-word) (to-word object-type) ]
iso-action-required [ (obj-set-word) (to-word object-type) ]
iso-prohibit [ (obj-set-word) (to-word object-type) ]
]
obj-name: either named [
find-unused-object-name named-object
][
find-unused-object-name object-type
]
obj-set-word: to-set-word obj-name
the-template: copy either style [
orig-object-type: object-type
object-type: style-name
show-insert-tool/refresh
[ (obj-set-word) (to-word style-name) ]
][
copy select obj-template (to-lit-word object-type)
]
if with-on-click [
append/only the-template 'on-click
append/only the-template [ (on-click-code) ]
]
if with-offset [
insert the-template reduce ['at offset-pos ]
]
if with-text [
orig-obj-name: copy obj-name
obj-name: copy/part text-string 40
]
either position [
prev-newline: char-index?/back vid-code/text pos #"^/"
code-to-insert: rejoin [ tab (mold/only compose/deep the-template) newline ]
insert-pos: either prev-newline > 0 [
prev-newline
][
pos - 1
]
insert (skip vid-code/text insert-pos ) code-to-insert
][
either vid-code/selected [
][
new-line-amt: either all [ (vid-code/text <> "") ((last vid-code/text) <> #"^/" ) ] [
newline
][
""
]
indent-amt: "^-"
append vid-code/text rejoin [ new-line-amt indent-amt ]
append vid-code/text mold/only compose/deep the-template
]
]
if with-text [ obj-name: copy orig-obj-name ]
run-and-save "insert-vid-object"
if no-setup [
return to-string obj-set-word
]
setup-result: either style [
run-setup-style/:catalog obj-name "" ""
][
true
]
if setup-result = 'no-target-from-source [
if req-res: request-yes-no rejoin [ "Error inserting: '" object-type "'. If this style requires a parent style, try inserting the parent style first. ^/Do you want to roll back the changes that have been made?" ] [
back-out-vid-changes before-change-index
request-message "While attempting to 'insert-vid-object' there was a problem locating the 'setup-style'. Any changes made have been reversed."
return none
]
]
if setup-result = false [
back-out-vid-changes before-change-index
request-message "While attempting to 'insert-vid-object' the 'setup-style' did not complete. Any changes made have been reversed."
return none
]
if evo-after-insert? [
obj-name: either all-to-logic setup-result [
either setup-result = true [
to-string obj-set-word
][
setup-result
]
][
to-string obj-set-word
]
edit-vid-object obj-name "vid-code"
]
]
get-function-source: function [
filename [file!]
function-name [string!]
][
src: load filename
if fnd: select src to-lit-word function-name [
return mold/only fnd
]
return false
]
indent-string: function [
val [string!]
indent [string!]
][
replace/all val "^/" rejoin [ "^/" indent ]
insert val indent
return val
]
set 'insert-catalog-code function [
catalog-code-name [file!]
][
if file-data: read rejoin [ root-path %code-catalog/ catalog-code-name ] [
header: select-text-block file-data "Red "
replace file-data rejoin ["Red " header] ""
trim-newlines/head file-data
either last-prt: last-printable setup-code/text [
indent: get-indent-chars setup-code/text last-prt
][
indent: "^-"
]
indent-string file-data indent
if last-prt [ append setup-code/text rejoin [ newline ] ]
append setup-code/text file-data
run-and-save "insert-function-code"
]
]
set 'insert-function-code function [
function-name [string!]
][
code-catalog: load rooted %code-catalog/code-catalog-index.data
if catalog-entry: select code-catalog function-name [
filename: rooted rejoin [ %code-catalog/ (select catalog-entry 'filename) ]
if source-code: get-function-source filename (select catalog-entry 'function-name ) [
either last-prt: last-printable setup-code/text [
indent: get-indent-chars setup-code/text last-prt
][
indent: ""
]
indent-string source-code indent
if last-prt [ append setup-code/text rejoin [ newline newline ] ]
append setup-code/text source-code
run-and-save "insert-function-code"
]
]
return ""
]
set 'extract-setup-code function [ filename ] [ ;-- extract-setup-code:
the-code: read filename
either (find the-code vid-code-marker ) [
clue: rejoin [ vid-code-marker "^/view"]
][
clue: "^/view"
]
a: first split the-code clue
un-block-string any [ (second split a "do setup:") "" ]
]
set 'extract-vid-code function [ filename ] [ ;-- extract-vid-code:
i: read filename
either (find i vid-code-marker ) [
clue: rejoin [ vid-code-marker "^/view"]
][
clue: "^/view"
]
b: second split i clue
c: find b "["
un-block-string c
]
set 'extract-from-source function [ ;-- extract-from-source:
series [string!]
left-delim [string!]
right-delim [string!]
][
extracted: delim-extract/first series left-delim right-delim
return un-block-string extracted
]
set 'get-dc-code-version function [
file-data
/extern show-window-code-marker
][
return either find file-data show-window-code-marker [ 2 ][ 1 ]
]
set 'get-setup-code function [
file-data [string!]
/extern vid-code-marker
][
extract-from-source file-data rejoin [ "^/do setup:" ] vid-code-marker
]
set 'get-vid-code function [
file-data [string!]
code-version [integer!]
/extern vid-code-marker
][
head-marker: either code-version = 1 [
rejoin [ vid-code-marker "^/view " ]
][
vid-code-marker
]
content: find file-data head-marker
content: find/tail content ": "
if code-version = 2 [
content: delim-extract/use-head/first content "" show-window-code-marker
]
return un-block-string content
]
set 'edit-show-window-code function [
code
][
results: request-multiline-text/size/preload/custom/modal
"Change window setting by clicking on the 'Window Configuration' button or just type changes below."
800x100
code
[ ;-- custom block
"Window Configuration" [
if req-res: request-view-code --multiline-area/text [
? req-res
--multiline-area/text: req-res
]
]
]
if results [
either error? err: try/all [
test-code: load results
true ;-- makes try happy dev-init
][
print "********** show-window CODE ERROR **************************************************************"
print err
print "*********************************************************************************"
][ ;-- Good run condition
show-window-code/text: copy results
run-and-save "show-window-code"
]
]
]
set 'get-show-window-code function [
file-data [string!]
/extern show-window-code-marker
][
either content: find/tail file-data rejoin [ show-window-code-marker {^/do show-window: }][
return un-block-string content
][
return false
]
]
set 'get-file-base-name function [
filename [file!]
][
return to-string first split (second (split-path filename)) "."
]
set 'dc-load-direct-code load-direct-code: function [
filename [ file! ]
/extern dc-code-version
][
filename: clean-path filename
if (not exists? filename)[
req-res: request-message/size rejoin [ "Trying to open filename:^/" to-string filename "^/This file does not exist"] 600x300
return false
]
file-header: read/part filename 200
either all [
(exists? filename)
(find file-header {Comment: "Generated with Direct Code"} )
][
if (value? 'dc-initialized) [
recent-menu/add-item dc-reactor/current-file
]
if ((to-string filename) <> dc-reactor/current-file ) [
monitor-file-change false
]
if filename <> dc-reactor/current-file [
close-object-editor/all-open ""
]
file-data: read filename
dc-code-version: get-dc-code-version file-data
if dc-code-version = 1 [
setup-code/text: orig-setup-code: extract-setup-code filename
vid-code/text: orig-vid-code: extract-vid-code filename
show-window-code/text: copy "" ;-- just zero it out
]
if dc-code-version = 2 [
setup-code/text: new-setup-code: get-setup-code file-data
vid-code/text: new-vid-code: get-vid-code file-data dc-code-version
show-window-code/text: get-show-window-code file-data
red-header-code/text: select-text-block/only/trim-newline file-data "Red "
]
dc-reactor/current-file: filename
change-dir first split-path filename
return true
][
request-message/size rejoin [{Unable to load } filename {.^/Either the file doesn't exist or it isn't a 'Direct Code' program.}] 750x200
return false
]
]
save-direct-code: function [
filename [ file!]
/version version-num [integer!]
][
direct-code-version: 2 ;-- This is the new default version
temp-file: copy ""
just-filename: to-string second split-path filename
if version [
direct-code-version: version-num
]
if direct-code-version = 1 [
write filename append temp-file reduce [
{Red [^/^-Title: "} just-filename {"}
{^/^-Needs: View}
{^/^-Comment: "Generated with Direct Code"} ; "
{^/]^/}
{^/}
{do setup: }
string-to-block/no-indent any [ setup-code/text "" ]
{^/}
vid-code-marker
{^/}
"view " rejoin [ first split (to-string just-filename) "." "-layout: "]
string-to-block/no-indent any [ vid-code/text "" ]
]
]
if direct-code-version = 2 [
if any [
(red-header-code/text = "")
(red-header-code/text = none)
][
red-header-code/text: rejoin [
{^/^-Title: "} just-filename {"}
{^/^-Needs: View}
{^/^-Comment: "Generated with Direct Code"} ; "
{^/}
]
]
base-name: first split (to-string just-filename) "."
show-win-code: copy either any [
(show-window-code/text = "")
(show-window-code/text = none)
][
rejoin [ "^-view " base-name "-layout" ]
][
show-window-code/text
]
write filename append temp-file reduce [
{Red }
string-to-block/no-indent red-header-code/text
{^/^/}
{do setup: }
string-to-block/no-indent any [ setup-code/text "" ]
{^/^/}
vid-code-marker
{^/}
rejoin [ base-name "-layout: "]
string-to-block/no-indent any [ vid-code/text "" ]
{^/^/}
show-window-code-marker
{^/}
{do show-window: }
string-to-block/no-indent show-win-code
]
]
save-settings
change-dir first split-path filename
set 'dc-filename filename
]
set 'recent-menu closure [ ;-- recent-menu:
recent-file-list: [] [block!]
max-entries: 11
][
/add-item filename [file!]
/get-item item-num [number!]
/get-all
/set-all value [ block! ]
/local push-into-menu ndx file-fnd
][
push-into-menu: func [ value ][
insert recent-file-list value
recent-menu-location: ((index? find mainwin/menu/2 "Recent Files") + 1)
if ((length? recent-file-list) > max-entries) [
recent-file-list: copy/part recent-file-list max-entries
]
ndx: 1
remove/part mainwin/menu/2/:recent-menu-location (length? mainwin/menu/2/:recent-menu-location )
foreach i recent-file-list [
append mainwin/menu/2/:recent-menu-location reduce [ (to-string second split-path i) to-word rejoin [ "recent-" ndx ] ]
ndx: ndx + 1
]
]
case [
add-item [
if (file-fnd: find recent-file-list filename ) [
remove file-fnd
]
push-into-menu filename
]
get-item [
return pick recent-file-list item-num
]
get-all [
prin ""
return recent-file-list
]
set-all [
foreach i value [
push-into-menu i
]
]
]
]
save-settings: does [
recent-files: recent-menu/get-all
if value? 'setup-code [
save rejoin [ root-path %/settings/direct-code-settings.data ]
new-line/skip/all reduce [
'filename dc-reactor/current-file
'setup-size setup-code/size
'vid-size vid-code/size
'output-panel-size output-panel/size
'live-update? live-update?
'evo-after-insert? evo-after-insert?
'insert-tool-pinned? insert-tool-pinned?
'red-executable red-executable
'external-editor dc-external-editor
'logging global-logging
'recent-files recent-files
'reload-on-change? reload-on-change?
'dc-voe-selected-tab dc-voe-selected-tab
'dc-voe-size dc-voe-size
'dc-insert-tool-tab dc-insert-tool-tab
'insert-tool-open? insert-tool-open?
] true 2
]
]
set 'refresh-open-voes does [ ;-- refresh-open-voes:
foreach window active-voe-windows [
object-name: copy skip window 13
win-obj: (get to-path reduce [to-word window 'extra])
object-type: get to-path reduce [ (to-word object-name) 'type ]
clear-voe-fields win-obj/target-object-name
source-to-view-fields/id/refresh object-name object-type vid-code/text win-obj/target-object-name
do to-path reduce [ to-word (rejoin [ "highlight-styled-fields" win-obj/target-object-name ]) 'refresh ]
]
]
set 'run-and-save function [ ;-- run-and-save:
id
/no-save
/version version-num
/extern active-voe-windows internal-source-change-flag? dc-last-vid-code-error dc-last-vid-code-error dc-code-version
][
;-- save only happens if the run is successful
set 'project-path copy system/options/path
set 'current-file copy dc-reactor/current-file
setup-good?: vid-good?: true
either exists? dc-reactor/current-file [
either dc-code-version = 1 [
original-setup-text: extract-setup-code dc-reactor/current-file
original-vid-text: extract-vid-code dc-reactor/current-file
][
file-data: read dc-reactor/current-file
original-setup-text: get-setup-code file-data
original-vid-text: get-vid-code file-data dc-code-version
]
][
original-setup-text: copy ""
original-vid-text: copy ""
]
either error? err: try/all [
if setup-code/text [
do load setup-code/text
]
true ;-- makes try happy dev-init
][
dc-last-setup-code-error: err
print "*** SETUP CODE ERROR / VID CODE IGNORED! ****************************************"
print err
print "*********************************************************************************"
active-filename/color: yellow
setup-code/color: yellow
setup-good?: false
][ ;-- setup code ran clean