Tcl Source Code

Check-in [5262e46c8d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:* added initial NaTk support - won't yet run without symlink lib/ to Wub/ and symlink to tcllib/
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ferrieux-nacl
Files: files | file ages | folders
SHA1: 5262e46c8dcf8848e451cf2ecce00e36b3e90d45
User & Date: colin 2011-05-05 07:53:50
Context
2011-05-06
01:30
* Allowed for emulation of <script> ... now runTclScripts will callback to eval check-in: 5ee1aa409f user: colin tags: ferrieux-nacl
2011-05-05
22:17
Update doc and tools to Colin's new layout Closed-Leaf check-in: 2d21680323 user: ferrieux tags: ferrieux-nacl
07:53
* added initial NaTk support - won't yet run without symlink lib/ to Wub/ and symlink to tcllib/ check-in: 5262e46c8d user: colin tags: ferrieux-nacl
07:51
* mods for NaTk check-in: d3a2b330e6 user: colin tags: ferrieux-nacl
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added nacl/demo/NaTk/NaTk.html.

























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="../loader.js"></script>
    <script type="text/javascript" src="NaTk.js"></script>
  </head>
  <body>

   <embed
      name='nacl_module'
      id='tcl'
      width=0 height=0
      nacl='tcl.nmf'
      type='application/x-nacl'
      onload='moduleDidLoad();'
      verbose=0
      source='NaTk.tcl'
      script='script'
      toplevel='toplevel'/>

   <div id="toplevel">Loading ...</div>

   <script id='script' type="text/tcl">
     button .b -text "Hello, Tk!" -command {alert HELLO} -grid {0 0}
   </script>

  </body>
</html>

Added nacl/demo/NaTk/NaTk.js.































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
function variableTk () {
   tcl("::oo::" + $(this).attr("id"), 'changed', $(this).val());
}

function buttonTk () {
   tcl("::oo::" + $(this).attr("id"), 'changed');
}

function rbuttonTk () {
   tcl("::oo::" + $(this).attr("id"), 'changed', $(this).val());
}

function cbuttonTk () {
   var val = this.value;
   if(!$(this).is(":checked")) {
      val = 0;
   }
   tcl("::oo::" + $(this).attr("id"), 'changed', val);
}

function sliderTk (e, id, ui) {
   if(e.originalEvent!=undefined) {
      tcl("::oo::" + id, 'changed', ui.value);
   }
}

function autocompleteTk (event,entry) {
   if(event.originalEvent!=undefined) {
        tcl("::oo::" + $(entry).attr("id"), 'changed', $(entry).val());
   }
}

Added nacl/demo/NaTk/NaTk.tcl.





































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
# NaTk - Tk emulation under NaTcl
#
# Colin McCormack
proc ::puts {args} {
    if {[llength $args] == 1} {
        printf "STDOUT: [lindex $args 0]"
    } else {
        printf "[string toupper [lindex $args 0]]: [lindex $args 1]"
    }
}

set _packages {mime base64}
proc package {cmd args} {
    printf "PKG: package $cmd $args"
    switch -- $cmd {
        require {
            lassign $args what
            if {$what in {Tcl Tk}} return
            if {$what in $::_packages} return
            source lib/Utilities/[lindex $args 0].tcl
        }
        provide {
            lassign $args what
            lappend ::_packages $what
        }
        vsatisfies {
            return 1
        }
    }
}
source tcllib/textutil/string.tcl
source tcllib/textutil/repeat.tcl
source tcllib/textutil/adjust.tcl
source tcllib/textutil/split.tcl
source tcllib/textutil/tabify.tcl
source tcllib/textutil/trim.tcl
source tcllib/textutil/textutil.tcl
source lib/extensions/extend-1.0.tm
source lib/extensions/file-1.0.tm
source lib/extensions/memoize-1.0.tm
source lib/extensions/know-1.0.tm

package require Form

namespace import oo::*
package provide NaTk 1.0

# radiobutton - textvariable command variable value
# entry - textvariable validatecommand
# scale - command variable
# checkbutton - textvariable command variable
# button - textvariable command

# TextVariable: radiobutton entry checkbutton button
# Command: radiobutton scale checkbutton button +validate:entry
# Variable: radiobutton scale checkbutton

# NaTk - class connecting an interp to an NaTcl window
class create ::NaTk {
    superclass FormClass

    method toplevel {args} {
        variable widgets
        if {[llength $args]} {
            {*}[dict get $widgets .] {*}$args
        } else {
            return [dict get $widgets .]
        }
    }

    method widget {widget} {
        variable widgets; return [dict get $widgets $widget]
    }

    method widgets {} {
        variable widgets; return $widgets
    }

    method mkwidget {type widget args} {
        variable ns
        variable interp
        variable widgets

        if {$widget ne "."} {
            set parent [join [lrange [split $widget .] 0 end-1] .]
            if {$parent eq ""} {
                set parent .
            }
            if {![dict exists $widgets $parent]} {
                error "Invalid Widget Name - no parent widget '$parent' exists for '$widget'"
            }
            set p [list -parent [dict get $widgets $parent]]	;# parent object
        } elseif {[dict exists $widgets .]} {
            error "Invalid Widget - Toplevel . already exists"
        } else {
            set p {-parent ""}
        }

        # construct a widget of given type
        Debug.widget {::NaTk::$type new -widget $widget $args -interp $interp $p -connection [self]}
        set cmd [::NaTk::$type new -widget $widget {*}$args -interp $interp {*}$p -connection [self]]

        return $widget
    }

    # addwidget - to both Interp and mapping
    method addwidget {widget cmd} {
        Interp alias $widget $cmd ;# install widget alias in Interp

        # remember name<->command mapping
        variable widgets
        dict set widgets $widget $cmd
        dict set widgets $cmd $widget
    }

    # delwidget - from both Interp and mapping
    method delwidget {obj} {
        Interp alias $widget {}

        # forget name<->command mapping
        variable widgets
        set widget [dict get $widgets $obj]
        dict unset widgets $widget
        dict unset widgets $obj
    }

    method grid {args} {
        if {[string index [lindex $args 0]] in {x . ^}} {
            set args [linsert $args 0 configure]
        }
        set args [lassign $args command]
        switch -- $command {
            anchor -
            bbox -
            columnconfigure -
            info -
            location -
            propagate -
            rowconfigure -
            size -
            slaves {
                set anchor {}
                set args [lassign $args widget]
                tailcall [dict get $widgets $widget] grid $command $widget {*}$args
            }

            configure -
            forget -
            remove -
            {
                set slaves {}
                while {[string index [lindex $args 0] 0] in {x . ^}} {
                    set args [lassign $args slave]
                    lappend slaves $slave
                }
                variable widgets
                set result {}
                foreach slave $slaves {
                    lappend result $slave
                    lappend result [[dict get $widgets $slave] grid $command $slave {*}$args]
                }
                return $result
            }
        }
    }

    method interp {script} {
        Debug.interp "INTERP: $script"
        return [Interp eval $script]
    }

    method start {script} {
        variable verbose
        set ::nacl::verbose $verbose

        Debug.interp "INTERP: $script"
        Interp eval $script
        my toplevel render
    }

    constructor {args} {
        Debug.widgets {[self] Creating ::NaTk $args}
        variable safe 0
        variable verbose 1
        variable {*}$args
        variable widgets {}
        variable ns [info object namespace [self]]

        # create an interpreter for NaTk program
        variable interp [::interp create {*}[expr {$safe?"-safe":""}] ${ns}::Interp]
        Interp alias bgerror [self] bgerror
        Interp alias alert ::nacl alert

        my mkwidget toplevel .

        # install each widget creator as alias in the interp
        foreach n {button entry label
            checkbutton radiobutton scale
            select combobox frame
        } {
            Interp alias $n [self] mkwidget $n
        }
        Interp alias $n grid [self] grid

        ::dom get [dict get $::argv script] [self] start
    }
}

class create ::NaTk::Widget {
    variable id props widget interp parent connection
    method js {} {return ""}

    method element {} {
        #return "document.getElementById('$id')"
        return "\$('#$id')"	;# if jQuery
    }

    # catch unknown methods and palm them off to the parent
    method unknown {cmd args} {
        Debug.widgets {unknown $cmd $args}
        if {[string match <*> $cmd]} {
            return [{*}$parent $cmd {*}$args]
        } elseif {[string match .* $cmd]} {
            return [$cmd {*}$args]
        }
        error "Unknown method '$cmd' in [self] of class [info object class [self]]"
    }

    method connection {args} {
        return [{*}$parent {*}$args]
    }

    # grid - percolates up to the parent containing a grid
    method grid {args} {
        tailcall $parent grid {*}$args
    }

    # gridded - record grid's values
    method gridded {args} {
        variable grid
        if {[llength $args]} {
            set grid [dict merge $grid $args]
        }
        return $grid
    }

    # Grid - interpret -grid options to configure as grid commands
    method Grid {args} {
        # this widget needs to be gridded
        set rs 1; set cs 1; set r 0; set c 0
        Debug.widgets {config gridding: '$args'}
        set pargs [lsearch -glob $args -*]
        if {$pargs > -1} {
            # we've got extra args
            set gargs [lrange $args $pargs end]
            set args [lrange $args 0 $pargs-1]
        } else {
            set gargs {}
        }
        lassign $args r c rs cs

        set ga {}
        foreach {v1 v2} {r row c column rs rowspan cs columnspan} {
            if {[info exists $v1] && [set $v1] ne ""} {
                if {$v1 in {rs cs}} {
                    if {[set $v1] <= 0} {
                        set $v1 1
                    }
                } else {
                    if {[set $v1] < 0} {
                        set $v1 0
                    }
                }
                lappend ga -$v2 [set $v1]
            }
        }

        Debug.widgets {option -grid: 'grid configure $widget $ga $gargs'}
        return [my grid configure $widget {*}$ga {*}$gargs]
    }

    # style - construct an HTML style form
    method style {gridding} {
        set attrs {}
        foreach {css tk} {
            background-color background
            color foreground
            text-align justify
            vertical-align valign
            border borderwidth
            border-color bordercolor
            width width
        } {
            set tkval [dict get? $props $tk]
            if {$tkval eq ""} continue

            if {$tk eq "background"} {
                lappend attrs background "none $tkval !important"
                if {![dict exists $props bordercolor]} {
                    dict set attrs border-color $tkval
                }
                # TODO: background images, URLs
            } else {
                dict set attrs $css $tkval
            }
        }

        if {0} {
            # process -sticky gridding
            set sticky [dict gridding.sticky?]
            if {$sticky ne ""} {
                # we have to use float and width CSS to emulate sticky
                set sticky [string trim [string tolower $sticky]]
                set sticky [string map {n "" s ""} $sticky];# forget NS
                if {[string first e $sticky] > -1} {
                    dict set attrs float "left"
                } elseif {[string first w $sticky] > -1} {
                    dict set attrs float "right"
                }

                if {$sticky in {"ew" "we"}} {
                    # this is the usual case 'stretch me'
                    dict set attrs width "100%"
                }
            }
        }

        # todo - padding
        set result ""
        dict for {n v} $attrs {
            append result "$n: $v;"
        }
        append result [dict gridding.style?]

        if {$result ne ""} {
            set result [list style $result]
        }

        Debug.widgets {style attrs:($attrs), style:($result)}

        if {[dict exists $props class]} {
            lappend result class [dict get $props class]
        }

        if {[dict exists $props state]
            && [dict get $props state] ne "normal"
        } {
            lappend result disabled 1
        }

        return $result
    }

    method compound {text} {
        set text [armour $text]

        set image [my cget? image]
        if {$image ne ""} {
            set image [$image render]
        }

        switch -- [my cget? compound] {
            left {
                return $image$text
            }
            right {
                return $text$image
            }
            center -
            top {
                return "$image[my <br>]$text"
            }
            bottom {
                return "$text[my <br>]$image"
            }
            none -
            default {
                # image instead of text
                if {$image ne ""} {
                    return "$image"
                } else {
                    return "$text"
                }
            }
        }
    }

    method getvalue {} {
        if {[dict exists $props textvariable]} {
            Debug.widgets {[self] getvalue from textvariable:'[my cget -textvariable]' value:'[my iget [my cget -textvariable]]'}
            set val [my iget [my cget -textvariable]]
            set result $val
        } elseif {[dict exists $props exists text]} {
            Debug.widgets {[self] getvalue from text '$text'}
            set result $text
        } else {
            Debug.widgets {[self] getvalue default to ""}
            set result ""
        }
        return $result
    }

    # access interp variables
    method iset {n v} {
        Debug.interp {iset '$n' <- '$v' traces:([{*}$interp eval [list trace info variable $n]]) ([lrange [info level -1] 0 1])}
        return [{*}$interp eval [list set $n $v]]
    }

    method iget {n} {
        try {
            set result [{*}$interp eval [list set $n]]
        } on error {e eo} {
            set result ""
        }

        Debug.interp {iget '$n' -> '$result' ([lrange [info level -1] 0 1])}
        return $result
    }

    method iexists {n} {
        set result [{*}$interp eval [list info exists $n]]
        Debug.interp {iexists '$n' -> $result}
        return $result
    }

    method cget {opt} {
        return [dict get $props [string trimleft $opt -]]
    }

    method cget? {opt} {
        set opt [string trim $opt -]
        if {[dict exists $props $opt]} {
            return [dict get $props $opt]
        } else {
            return ""
        }
    }

    method cexists {opt} {
        return [dict exists $props $opt]
    }

    # itrace - trace an Interp variable
    method itrace {what args} {
        variable trace	;# set of active traces for this widget
        if {[llength $args]} {
            # add a trace
            dict set trace $what $args
            {*}$interp eval [list trace add variable $what write $args]
            Debug.interp {itrace add $what $args: ([{*}$interp eval [list trace info variable $what]])}
        } elseif {[dict exists $trace $what]} {
            {*}$interp eval [list trace remove variable $what write [dict get $trace $what]]
            Debug.interp {itrace removed $what $args ([dict get $trace $what]) leaving ([{*}$interp eval [list trace info variable $what]])}
            dict unset trace $what
        }
    }

    # Configure - reflect configuration in $props dict
    # give trace-effect to -textvariable and -variable opts
    method Configure {args} {
        # set configuration opts
        Debug.widgets {[info coroutine] Configure [self] ($args)/[llength $args]}
        if {[llength $args]%2} {
            error "Configure requires even number of args"
        }

        dict for {n v} $args {
            set n [string trim $n -]

            switch -- $n {
                variable -
                textvariable {
                    # remove any old traces first
                    if {[dict get? $props $n] ne ""} {
                        my itrace [dict get $props $n]
                    }

                    dict set props $n $v

                    if {$v ne ""} {
                        # (re-)establish variable trace
                        Debug.widgets {tracking -$n '$v' changes}
                        if {![my iexists $v]} {
                            # create variable if necessary
                            {*}$interp eval [list ::variable ::$v]
                        }

                        # track changes to textvariable
                        my itrace [dict get $props $n] $widget $n
                    }
                }

                grid {
                    my Grid {*}$v
                }

                default {
                    # normal old configuration variable
                    dict set props $n $v
                }
            }
        }
    }

    method configure {args} {
        switch -- [llength $args] {
            0 {
                # get the names of all configuration opts
                set _result {};
                foreach v $props {
                    lappend result -$v [dict get $props $v]
                }
                return $result
            }

            1 {
                # get the value of a single configuration opt
                set v [string trimleft [llength $args 0] -]
                return [dict get $props $v]
            }

            default {
                # handle configure with sensitivity to -command, -variable, etc
                tailcall my Configure {*}$args
            }
        }
    }

    destructor {
        variable connection
        $connection delwidget [self]	;# remove full widget name
    }

    method id {} {
        return $id
    }

    constructor {args} {
        variable grid {}
        set id [namespace tail [self]]

        # get structural vars from NaTk mkwidget
        foreach v {widget interp parent connection} {
            set $v [dict get $args -$v]
            dict unset args -$v
        }

        # add widget to Interp and mapping
        $connection addwidget $widget [self]

        set props {}		;# no properties yet
        variable trace {}	;# no traces yet
        my Configure {*}$args	;# fill up props
    }
}

class create ::NaTk::button {
    superclass ::NaTk::Widget
    variable id props widget interp

    # button - textvariable command

    # textvariable - interp's textvariable has changed
    method textvariable {} {
        set val [my iget [dict get $props textvariable]]
        ::nacl js [my element].whatever
    }

    method changed {} {
        if {[dict get? $props command] ne ""} {
            {*}$interp eval [dict get $props command]
        }
    }

    method render {args} {
        if {[dict exists $props textvariable]} {
            set text [my iget [dict get $props textvariable]]
        } else {
            set text [dict get? $props text]
        }
        return [my <button> $id id $id onclick "tcl(\"[self]\",\"changed\");" {*}[my style $args] [my compound $text]]
    }

    constructor {args} {
        variable on 0
        next justify left {*}$args
    }
}

class create ::NaTk::entry {
    superclass ::NaTk::Widget
    variable id props

    # entry - textvariable validatecommand

    # textvariable - interp's textvariable has changed
    # reflect change onto browser widget
    method textvariable {} {
        set val [my iget [dict get $props textvariable]]
        ::nacl js [my element].whatever
    }

    method changed {value} {
        # the entry value has changed
        # TODO call validation cmd
        my iset [dict get $props textvariable] $value
    }

    method js {} {
        set result ""
        if {[my cexists type]} {
            switch -- [my cget type] {
                date {
                    append result [my element].datepicker();
                }
            }
        }

        append result [my element].change(variableTk)
        return $result
    }

    method render {args} {
        Debug.widgets {[info coroutine] rendering Entry [self]}

        switch -- [my cget? type] {
            password {
                set tag <password>
            }
            date -
            default {
                set tag <text>
            }
        }

        set class {class variable}
        set result [my $tag $id id $id {*}$class {*}[my style $args] size [my cget -width] [tclarmour [my getvalue]]]
        return $result
    }

    constructor {args} {
        next {*}[dict merge {
            justify left
            state normal width 16
        } $args]
        if {[my cexists -show] && ![my cexists -type]} {
            my configure -type password
        }
    }
}


class create ::NaTk::checkbutton {
    superclass ::NaTk::Widget
    variable id props widget

    # checkbutton - textvariable command variable

    method textvariable {} {
        set val [my iget [dict get $props textvariable]]
        ::nacl js [my element].whatever
    }

    # changed - button state in browser - reflect to var/command
    method changed {value} {
        variable ignore; incr ignore
        my iset [dict get $props variable] $value
        incr ignore -1
        if {[dict exists $props command]} {
            {*}$interp eval [dict get $props command]
        }
    }

    method js {} {
        return "\$('#$id'>:checkbox').change(cbuttonTk);"
    }

    method render {args} {
        Debug.widgets {checkbutton render: getting '[my cget variable]' == [my iget [my cget variable]]}

        set val [my iget [my cget variable]]
        if {$val ne "" && $val} {
            set checked 1
        } else {
            set checked 0
        }

        Debug.widgets {[self] checkbox render: checked:$checked}
        return [my <checkbox> $id id $id {*}[my style $args] checked $checked [tclarmour [my compound [my getvalue]]]]
    }

    constructor {args} {
        variable ignore 0
        next justify left {*}$args
        if {![dict exists $props variable]} {
            my Configure variable [lindex [split $widget .] end]
        }
    }
}

# rbC - pseudo-widget which handles change events for a set of
# radiobuttons sharing the same variable.
class create ::NaTk::rb {
    superclass ::NaTk::Widget
    variable id props

    method render {args} {
        error "Can't render an rbC"
    }

    constructor {args} {
        next {*}$args	;# set up traces etc
    }
}

class create ::NaTk::radiobutton {
    superclass ::NaTk::Widget
    variable id props

    # radiobutton - textvariable command variable value

    method textvariable {} {
        set val [my iget [dict get $props textvariable]]
        ::nacl js [my element].whatever
    }

    method variable {} {
        set val [my iget [dict get $props textvariable]]
        ::nacl js [my element].whatever
    }

    # changed - button state in browser - reflect to var/command
    method changed {value} {
        variable ignore; incr ignore
        my iset [dict get $props variable] $value
        incr ignore -1
        if {[dict exists $props command]} {
            {*}$interp eval [dict get $props command]
        }
    }

    method js {} {
        return "[my element].click(rbuttonTk);"
    }

    method update {args} {
        Debug.widgets {radiobutton render: getting '[my cget variable]' == [my iget [my cget variable]]}

        set checked 0
        set var [my cget variable]
        if {[my iexists $var]} {
            set val [my iget $var]
            if {$val eq [my cget value]} {
                set checked 1
            }
        }

        Debug.widgets {[self] radiobox render: checked:$checked}
        set result [my <radio> [[my connection rbvar $var] widget] id $id class rbutton {*}[my style $args] checked $checked value [my cget value] [dict args.label?]]
        Debug.widgets {RADIO html: $result}
        return $result
    }

    method render {args} {
        set label [tclarmour [my compound [my getvalue]]]
        return [my update {*}$args label $label]
    }

    constructor {args} {
        variable ignore 0
        next justify left {*}$args

        if {![dict exists $props variable]} {
            my Configure variable selectedButton
        }
    }
}

class create ::NaTk::label {
    superclass ::NaTk::Widget
    variable id props

    # label - textvariable text

    method textvariable {} {
        set val [my iget [dict get $props textvariable]]
        ::nacl js [my element].whatever
    }

    method render {args} {
        return [my <div> id $id class label {*}[my style $args] [my compound [my getvalue]]]
    }

    constructor {args} {
        next justify left {*}$args
    }
}

class create ::NaTk::scale {
    superclass ::NaTk::Widget
    variable id props

    # scale - command variable

    # scale value has changed in browser
    method changed {value} {
        variable ignore; incr ignore
        my iset [dict get $props variable] $value
        incr ignore -1
        if {[dict exists $props command]} {
            {*}$interp eval [dict get $props command]
        }
    }

    # variable - interp var has changed - reflect to browser widget
    method variable {} {
        variable ignore; if {$ignore} return
        set val [my iget [dict get $props variable]]
        ::nacl js [my element].whatever
    }

    # js - javascript to track changes to the browser widget
    method js {} {
        # need to generate the slider interaction
        lappend opts orientation '[my cget orient]'
        lappend opts min [my cget from]
        lappend opts max [my cget to]
        lappend opts value [my iget [my cget -variable]]
        lappend opts change [string map [list %ID% $id] {
            function (event,ui) {
                sliderTk(event, '%ID%', ui);
            }
        }]

        return "[my element].slider([::nacl opts {*}$opts]); [my element].change(sliderTk);"
    }

    method render {args} {
        Debug.widgets {scale $id render $args}
        set result ""
        if {[my cget? label] ne ""} {
            set result [my <label> [my cget label]]
        }

        append result [my <div> id $id class slider {*}[my style $args] {}]
        Debug.widgets {scale $id rendered '$result'}

        return $result
    }

    constructor {args} {
        variable ignore 0
        set var [lindex [split [dict get $args -name] .] end]
        next {*}[dict merge [list -variable $var] {
            justify left state active
            label "" command "" -length 100 -width 10 -showvalue 0
            -from 0 -to 100 -tickinterval 0
            -orient horizontal
        } $args]
    }
}

class create ::NaTk::select {
    superclass ::NaTk::Widget
    variable id props

    method textvariable {} {
        set val [my iget [dict get $props textvariable]]
        ::nacl js [my element].whatever
    }

    # variable - interp var has changed - reflect to browser widget
    method variable {} {
        variable ignore; if {$ignore} return
        set val [my iget [dict get $props variable]]
        ::nacl js [my element].whatever
    }

    method js {} {
        #::nacl js [my element].click(buttonTk)
    }

    method render {args} {
        set var [my cget textvariable]
        if {[my iexists $var]} {
            set val [my iget $var]
        }

        set values [my cget -values]
        Debug.widgets {val=$val values=$values}
        foreach opt $values {
            lappend opts [my <option> value [tclarmour $opt]]
        }
        set opts [join $opts \n]

        set class {variable ui-widget ui-state-default ui-corner-all}
        if {[my cexists combobox]
            && [my cget combobox]
        } {
            lappend class combobox
        }
        return [my <select> $id id $id class [join $class] {*}[my style $args] $opts]
    }

    constructor {args} {
        variable ignore 0
        next justify left {*}$args
    }
}

class create ::NaTk::combobox {
    superclass ::NaTk::select
    variable id props

    method js {} {
        Debug.widgets {combobox js}
        return "[next?]; [my element].combobox();"
    }

    constructor {args} {
        next {*}$args -combobox 1
    }
}

# grid store grid info in an x/y array gridLayout(column.row)
class create ::NaTk::grid {
    # traverse grid looking for changes.
    method changes {r} {
        if {[dict exists $r -repaint]} {
            # repaint short-circuits change search
            #Debug.widgets {Grid '[namespace tail [self]]' repainting}
            return [list $r {}]
        }

        variable grid;variable oldgrid
        #Debug.widgets {Grid detecting changes: '[namespace tail [self]]'}

        # look for modified grid entries, these will cause a repaint
        dict for {row rval} $grid {
            dict for {col val} $rval {
                if {$val ne [dict oldgrid.$row.$col?]} {
                    # grid has changed ... force reload
                    Debug.widgets {[namespace tail [self]] repainting}
                    dict set r -repaint 1
                    return [list $r {}]	;# return the dict of changes by id
                }
            }
        }

        if {[dict size [dict ni $oldgrid [dict keys $grid]]]} {
            # a grid element has been deleted
            Debug.widgets {Grid repainting: '[namespace tail [self]]'}
            dict set r -repaint 1
            return [list $r {}]	;# return the dict of changes by id
        }

        set changes {}
        dict for {row rval} $grid {
            dict for {col val} $rval {
                dict with val {
                    set type [uplevel 1 [list $widget type]]
                    set changed {}
                    switch -- $type {
                        accordion -
                        notebook -
                        frame {
                            # propagate change request to geometry managing widgets
                            #Debug.widgets {Grid changing $type '$widget' at ($row,$col)}
                            set changed [lassign [uplevel 1 [list $widget changes $r]] r]
                            if {[dict exists $r -repaint]} {
                                Debug.widgets {Grid '[namespace tail [self]]' subgrid repaint $type '$widget'}
                                return [list $r {}]	;# repaint
                            } else {
                                #Debug.widgets {Grid '[namespace tail [self]]' subgrid [string totitle $type] '$widget' at ($row,$col) ($val) -> ($changed)}
                            }
                        }

                        default {
                            if {[uplevel 1 [list $widget changed?]]} {
                                Debug.widgets {[namespace tail [self]] changing: ($row,$col) $widget [uplevel 1 [list $widget type]] reports it's changed}
                                uplevel 1 [list $widget reset]
                                set changed $widget
                                lappend changed [uplevel 1 [list $widget wid]]
                                lappend changed [uplevel 1 [list $widget update]]
                                lappend changed [uplevel 1 [list $widget type]]

                                Debug.widgets {Grid '[namespace tail [self]]' accumulate changes to [string totitle $type] '$widget' at ($row,$col) ($val) -> ($changed)}
                            }
                        }
                    }

                    lappend changes {*}$changed
                    set r [uplevel 1 [list $widget js $r]]
                }
            }
        }

        return [list $r {*}$changes]	;# return the dict of changes by id
    }

    method changed? {} {return 1}

    # render - construct a grid's <table>
    method grid_render {args} {
        variable maxrows; variable maxcols; variable grid
        Debug.widgets {grid render [dict keys grid] over $maxrows/$maxcols}

        set rows {}	;# accumulate rows
        for {set row 0} {$row < $maxrows} {incr row} {
            set cols {}	;# accumulate cols

            for {set col 0} {$col < $maxcols} {} {
                set columnspan 1
                if {[dict exists $grid $row $col]} {
                    set el [dict get $grid $row $col]
                    dict with el {
                        Debug.widgets {'[namespace tail [self]]' grid rendering $widget/$object}
                        for {set rt $row} {$rt < $rowspan} {incr rt} {
                            set rspan($widget,[expr {$row + $rt}].$col) 1
                            for {set ct $col} {$ct < $columnspan} {incr ct} {
                                set rspan($widget,$rt.[expr {$col + $ct}]) 1
                            }
                        }

                        if {$rowspan != 1} {
                            set rowspan [list rowspan $rowspan]
                        } else {
                            set rowspan {}
                        }

                        lappend cols [my <td> id [my id]_${row}_$col colspan $columnspan {*}$rowspan [$object render style $style sticky $sticky]]
                    }
                    incr col $columnspan
                } else {
                    # empty cell
                    if {[info exists widget]
                        && ![info exists rspan($widget,$row.$col)]
                    } {
                        lappend cols [my <td> id [my id]_${row}_$col "&nbsp;"]
                    }
                    incr col $columnspan
                }
            }

            # now we have a complete row - accumulate it
            # align and valign not allowed here
            lappend rows [my <tr> id [my id]_$row style width:100% [join $cols \n]]
        }

        variable oldgrid $grid	;# record the old grid
        set content [my <tbody> [join $rows \n]]
        dict set args width 100%
        variable border
        if {[my cget border]} {
            set b [list border 1px]
        } else {
            set b {}
        }

        set content [my <table> class grid {*}$b {*}[my style $args] $content]
        Debug.widgets {Grid '[namespace tail [self]]' rendered ($content)}
        return $content
    }

    method grid_js {} {
        set result ""
        variable grid
        dict for {rc row} $grid {
            dict for {cc col} $row {
                append result [[dict get $col object] js]
            }
        }
        return $result
    }

    method grid_anchor {master {anchor ""}} {
    }

    method grid_bbox {master args} {
    }
    method grid_columnconfigure {master index args} {
    }
    method grid_forget {widget} {
    }
    method grid_info {widget} {
    }
    method grid_location {master x y} {
    }
    method grid_propagate {master boolean} {
    }
    method grid_rowconfigure {master index args} {
    }

    method grid_remove {widget} {
        variable connection
        [$connection widget $widget] hide
    }

    method grid_size {master} {
        variable maxcols; variable maxrows
        return [list $maxcols $maxrows]
    }

    method grid_slaves {master {option all} {value 0}} {
        variable grid
        switch -- $option {
            -row {
                dict for {col v} [dict get $grid $value] {
                    lappend result [dict get $v widget]
                }
            }
            -column {
                dict for {row v} $grid {
                    if {[dict exists $v $value]} {
                        lappend result [dict get $v $value widget]
                    }
                }
            }
            default {
                dict for {row c} $grid {
                    dict for {col v} $c {
                        lappend result [dict get $v widget]
                    }
                }
            }
        }
        return $result
    }

    method grid_configure {widget args} {
        # set defaults
        set column 0
        set row 0
        set columnspan 1
        set rowspan 1
        set sticky ""
        set in ""
        set style ""

        foreach {var val} $args {
            set [string trim $var -] $val
        }

        variable maxcols
        set width [expr {$column + $columnspan}]
        if {$width > $maxcols} {
            set maxcols $width
        }

        variable maxrows
        set height [expr {$row + $rowspan}]
        if {$height > $maxrows} {
            set maxrows $height
        }

        variable connection
        set object [$connection widget $widget]

        variable grid
        dict set grid $row $column [list widget $widget columnspan $columnspan rowspan $rowspan sticky $sticky in $in style $style object $object]
        $object gridded columnspan $columnspan rowspan $rowspan sticky $sticky in $in style $style

        Debug.widgets {[namespace tail [self]] configure gridding $widget/$object}
        return $widget
    }

    method grid {cmd args} {
        tailcall my grid_$cmd {*}$args
    }

    constructor {args} {
        Debug.widgets {[self] GRID constructed ($args)}
        variable maxcols 1
        variable maxrows 1
        variable border 0
        variable grid {}
        variable connection [dict get $args -connection]

        next {*}$args
    }
}

# frame widget
class create ::NaTk::frame {
    superclass ::NaTk::Widget
    mixin ::NaTk::grid
    variable id props

    # render widget
    method render {args} {
        Debug.widgets {Frame [namespace tail [self]] render}
        if {[my cexists -div] || [my cget? -text] eq ""} {
            append content \n [my grid_render {*}$args]
            return [my <div> id $id class frame {*}[my style $args] $content]
        } else {
            set label [my cget? -text]
            if {$label ne ""} {
                set content [my <legend> [tclarmour $label]]
            }
            append content \n [my grid_render {*}$args]
            return [my <fieldset> $id class frame {*}[my style $args] -raw 1 $content]
        }
    }

    destructor {
        # TODO destroy all child widgets
        next
    }

    constructor {args} {
        set args [dict merge {} $args]
        if {[dict exists $args -width]} {
            variable width [dict get $args -width]
            set w [list width $width]
            dict unset args -width
        } else {
            set w {}
        }
        next {*}$args
        Debug.widgets {created Frame [self]}
    }
}

# toplevel widget
class create ::NaTk::toplevel {
    superclass ::NaTk::Widget
    mixin ::NaTk::grid
    variable id props

    # render widget
    method render {args} {
        variable tgrid
        Debug.widgets {[namespace tail [self]] toplevel render}

        ::nacl js toplevel.innerHTML=[::nacl jsquote [my <form> form_$id onsubmit "return false;" [my grid_render {*}$args]]]
        ::nacl js [::nacl jsquote [my grid_js]]
    }

    constructor {args} {
        next -titlebar 1 -menubar 1 -border 0 -toolbar 1 -location 1 -scrollbars 1 -status 1 -resizable 1 {*}$args
        set id toplevel
    }
}

Debug on form
Debug on widgets
Debug on widget
Debug on interp

::NaTk new

Added nacl/demo/NaTk/tcl.nmf.













>
>
>
>
>
>
1
2
3
4
5
6
{
  "nexes": {
    "x86-32": "../tcl32.nexe",
    "x86-64": "../tcl64.nexe",
  }
}