-
Notifications
You must be signed in to change notification settings - Fork 3
/
gui.lisp
1768 lines (1667 loc) · 71.1 KB
/
gui.lisp
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
(import * from graphics)
(import * from layout)
(import * from locale)
(defmacro set-style (element &rest properties)
"Allows settings multiple style properties for a DOM node, example:[[
(set-style mynode
position \"absolute\"
px/left 0
px/top 0
px/width 200
px/height 300)
]]
The [px/] prefix means a CSS unit that will be appended to the expression."
(let ((el '#.(gensym))
(elstyle '#.(gensym)))
`(let* ((,el ,element)
(,elstyle (. ,el style)))
,@(map (lambda (i)
(let* ((prop (aref properties i))
(value (aref properties (1+ i)))
(pname (symbol-name prop))
(um (index "/" pname)))
(if (= um -1)
`(setf (. ,elstyle ,prop) ,value)
`(setf (. ,elstyle ,(intern (slice pname (1+ um))))
(+ ,value ,(slice pname 0 um))))))
(range 0 (length properties) 2))
,el)))
(defun screen-width ()
"Returns current width of browser window"
(js-code "window").innerWidth)
(defun screen-height ()
"Returns current height of browser window"
(js-code "window").innerHeight)
(defun element-pos (x)
"Returns [(left top)] position of specified DOM element."
(let ((left 0) (top 0))
(do ()
((or (null? x)
(undefined? x.offsetParent))
(list left top))
(incf left x.offsetLeft)
(incf top x.offsetTop)
(setf x x.offsetParent))))
(defun event-pos (event)
"Returns [(x y)] absolute position of the specified mouse event."
(let ((x 0) (y 0))
(cond
((or event.pageX event.pageY)
(setf x event.pageX)
(setf y event.pageY))
((or event.clientX event.clientY)
(setf x (+ event.clientX
document.body.scrollLeft
document.documentElement.scrollLeft))
(setf y (+ event.clientY
document.body.scrollTop
document.documentElement.scrollTop))))
(list x y)))
(defun relative-pos (event element)
"Returns [(x y)] relative position of specified mouse [event] in respect to [element]"
(let (((ex ey) (event-pos event))
((cx cy) (element-pos element)))
(list (- ex cx) (- ey cy))))
(defun show (x)
"Displays the specified DOM element by adding it to document body"
(append-child document.body x))
(defun hide (x)
"Removes the specified DOM element from its parent (hiding it)"
(remove-child x.parentNode x))
(defmacro set-handler (element event &rest body)
"Sets an event handler. The return value can be used in {{unset-handler}} \
to deactivate the installed handler. Example:[[
(set-handler mywidget onmousedown
(display ~\"Mouse pressed at {(event-pos event)}\"))
]]"
(unless (= (slice (symbol-name event) 0 2) "on")
(error "Event name must start with 'on'"))
(let ((elm '#.(gensym))
(handler '#.(gensym)))
`(labels ((,handler (,#"event")
(declare (ignorable ,#"event"))
,@body))
(let ((,elm ,element))
((. ,elm addEventListener) ,(slice (symbol-name event) 2)
#',handler)
(list ,elm ,(slice (symbol-name event) 2) #',handler)))))
(defun unset-handler (x)
"Removes an event handler installed with {{set-handler}}."
(let (((elm event handler) x))
(elm.removeEventListener event handler)))
(defun tracking (f &optional end cursor zero)
"Starts tracking mouse movements with calls to [f] until mouseup and \
then call [end] if specified. The parameter [cursor] is the shape \
that the cursor should use (e.g. \"move\") and the parameter [zero] \
if specified is an [(x y)] point to be use as zero for the coordinates."
(let ((cover (set-style (create-element "div")
position "absolute"
zIndex 999999999
cursor cursor
px/left 0
px/top 0
px/right 0
px/bottom 0
opacity 0.001
backgroundColor "#000000"))
(zx (if zero (first zero) 0))
(zy (if zero (second zero) 0)))
(labels ((call (f event)
(event.preventDefault)
(event.stopPropagation)
(when f
(let (((xx yy) (event-pos event)))
(funcall f (- xx zx) (- yy zy)))))
(ignore (event)
(event.preventDefault)
(event.stopPropagation))
(move (event)
(event.preventDefault)
(event.stopPropagation)
(call f event))
(up (event)
(event.preventDefault)
(event.stopPropagation)
(document.removeEventListener "contextmenu" #'ignore true)
(document.removeEventListener "mousemove" #'move true)
(document.removeEventListener "mouseup" #'up true)
(hide cover)
(call end event)))
(document.addEventListener "contextmenu" #'ignore true)
(document.addEventListener "mousemove" #'move true)
(document.addEventListener "mouseup" #'up true)
(show cover))))
(defun dragging (div x0 y0)
"Starts dragging an absolute DOM element starting from position [(x0, y0)]"
(tracking (lambda (x y)
(let ((dx (- x x0))
(dy (- y y0)))
(set-style div
px/left (+ div.offsetLeft dx)
px/top (+ div.offsetTop dy))
(setf x0 x)
(setf y0 y)))))
;; Modal gray screen
(defun modal-screen ()
"Creates, displays and returns a modal translucent screen that blocks mouse clicks"
(let ((modal (set-style (create-element "div")
position "absolute"
px/left 0
px/top 0
px/right 0
px/bottom 0
backgroundColor "rgba(0,0,0,0.25)")))
(set-handler modal onmousedown
(event.preventDefault)
(event.stopPropagation))
(show modal)
modal))
;;
(defmacro def-accessor (class name field)
`(progn
(defmethod ,name (widget) (= widget.% ,class)
,field)
(defmethod ,#"set-{name}" (widget ,name) (= widget.% ,class)
(setf ,field ,name))))
(defun node (widget)
"Returns the HTML5 DOM node associated to [widget] if it's not the widget itself"
(or (and widget widget.node) widget))
;; A gui window object
(defobject window
(frame ;; DOM node
titlebar ;; DOM node
resizer ;; DOM node
closer ;; DOM node
close-cback ;; invoked after closing
resize-cback ;; invoked after resizing
client ;; DOM node
data)) ;; Opaque payload
(defun window (x0 y0 w h &key title client (close true) (resize true))
"Creates an initially invisible window object"
(when (< x0 1) (setf x0 (* x0 (screen-width))))
(when (< y0 1) (setf y0 (* y0 (screen-height))))
(when (< w 1) (setf w (* w (screen-width))))
(when (< h 1) (setf h (* h (screen-height))))
(let ((frame (create-element "div"))
(titlebar (create-element "div"))
(resizer (create-element "canvas"))
(closer (create-element "canvas"))
(window null))
(unless client
(setf client (create-element "div")))
(set-style frame
position "absolute"
px/left x0
px/top y0
px/width w
px/height h
borderRadius "4px"
backgroundColor "#FFFFFF"
boxShadow "5px 5px 10px rgba(0,0,0,0.5)"
border "solid 1px #000000")
(set-style titlebar
display (if (undefined? title) "none" "block")
position "absolute"
px/left 0
px/top 0
px/right 0
px/height 20
borderTopLeftRadius "4px"
borderTopRightRadius "4px"
backgroundColor "#6389b7"
borderBottom "1px solid #000000"
color "#FFFFFF"
fontFamily "Arial"
px/fontSize 16
fontWeight "bold"
textAlign "center"
cursor "move")
(setf titlebar.innerHTML title)
(append-child frame titlebar)
(set-handler titlebar onmousedown
(event.preventDefault)
(event.stopPropagation)
(append-child document.body frame)
(dragging frame
(first (event-pos event))
(second (event-pos event))))
(set-style client
position "absolute"
px/left 0
px/top (if (undefined? title) 0 20)
px/width w
px/height (- h
(if (undefined? title) 0 20)
(if resize 12 0))
overflow "auto")
(append-child frame client)
(set-style resizer
display (if resize "block" "none")
position "absolute"
px/right 0
px/bottom 0
px/width 12
px/height 12
cursor "se-resize")
(setf resizer.width 12)
(setf resizer.height 12)
(with-canvas resizer
(line-width 1)
(dolist (i (list 0 5))
(stroke-style "#000000")
(line 10 i i 10)
(stroke-style "#FFFFFF")
(line 10 (1+ i) (1+ i) 10)))
(append-child frame resizer)
(set-handler resizer onmousedown
(event.preventDefault)
(event.stopPropagation)
(append-child document.body frame)
(let ((x0 (first (event-pos event)))
(y0 (second (event-pos event)))
(disp (append-child document.body (set-style (create-element "div")
position "absolute"
backgroundColor "rgba(0,0,0,0.75)"
px/padding 4
px/borderRadius 4
color "#FFF"
fontFamily "sans-serif"
px/fontSize 16
fontWeight "bold"))))
(tracking (lambda (x y)
(let ((dx (- x x0))
(dy (- y y0)))
(set-style frame
px/width (+ frame.clientWidth dx)
px/height (+ frame.clientHeight dy))
(setf disp.textContent ~"{frame.clientWidth} x {frame.clientHeight}")
(set-style disp
px/left (+ 20 x)
px/top (+ 20 y))
(setf x0 x)
(setf y0 y))
(set-style window.client
px/width frame.clientWidth
px/height (- frame.clientHeight
(if (undefined? title) 0 20)
(if resize 12 0)))
(when window.resize-cback
(window.resize-cback client.offsetLeft
client.offsetTop
(+ client.offsetLeft
client.clientWidth)
(+ client.offsetTop
client.clientHeight))))
(lambda ()
(remove-child document.body disp)))))
(set-style closer
display (if close "block" "none")
position "absolute"
px/right 2
px/top 2
px/width 16
px/height 16
cursor "default")
(setf closer.width 16)
(setf closer.height 16)
(with-canvas closer
(stroke-style "#000000")
(line-width 1)
(rect 0 0 16 16)
(stroke)
(stroke-style "#FFFFFF")
(line-width 2)
(line 4 4 12 12)
(line 12 4 4 12))
(append-child frame closer)
(set-handler closer onmousedown
(event.preventDefault)
(event.stopPropagation)
(when window.close-cback
(window.close-cback))
(hide frame))
(setf window (make-window frame: frame
titlebar: titlebar
resizer: resizer
closer: closer
resize-cback: null
close-cback: null
client: client))
window))
(defun hide-window (w)
"Closes the specified window"
(when w.close-cback
(w.close-cback))
(hide w.frame))
(defun screen-width ()
"Returns the width of the usable area in the browser"
(js-code "window").innerWidth)
(defun screen-height ()
"Returns the height of the usable area in the browser"
(js-code "window").innerHeight)
(defun show-window (w &key center modal)
"Displays the specified window"
(let ((modal (when modal (modal-screen)))
(ca document.activeElement))
(show w.frame)
(when center
(set-style w.frame
px/left (/ (- (screen-width) w.frame.offsetWidth) 2)
px/top (/ (- (screen-height) w.frame.offsetHeight) 2)))
(when w.resize-cback
(let ((client w.client))
(w.resize-cback client.offsetLeft
client.offsetTop
(+ client.offsetLeft
client.offsetWidth)
(+ client.offsetTop
client.offsetHeight))))
(when modal
(let ((ocb w.close-cback))
(setf w.close-cback
(lambda ()
(setf w.close-cback ocb)
(hide modal)
(when ocb (funcall ocb))
(when ca (set-timeout (lambda () (ca.focus))
100))))))))
;;
(defun button (text action &key (default (= text "OK")) (cancel (= text "Cancel")))
"Creates a button DOM object with provided [text] and callback [action]"
(let ((button (create-element "input")))
(setf button.type "button")
(setf button.value text)
(set-style button
position "absolute")
(set-handler button onclick
(event.preventDefault)
(event.stopPropagation)
(funcall action))
(setf button.% #'button)
(setf button.default default)
(setf button.cancel cancel)
button))
(def-accessor #'button caption widget.value)
(defun check-default-actions (container event)
(cond
((= event.which 13)
(dolist (x container.children)
(when (and (= x.% #'button) x.default)
((node x).click))))
((= event.which 27)
(dolist (x container.children)
(when (and (= x.% #'button) x.cancel)
((node x).click)))))
true)
(defmacro lbutton (text &rest body)
"Syntactic sugar for simple buttons with inlined actions"
`(button ,text (lambda () ,@body)))
(defun static-text (content)
"Creates a static text object"
(let ((text (create-element "div")))
(set-style text
position "absolute")
(setf text.textContent content)
(setf text.% #'static-text)
text))
(def-accessor #'static-text text widget.textContent)
(defun checkbox (caption &optional action)
"Creates a checkbox DOM object with provided [caption] ad an optional callback [action]"
(let ((checkbox (create-element "input"))
(text (set-style (create-element "span")
fontFamily "Arial"
px/fontSize 16))
(container (create-element "label")))
(setf checkbox.type "checkbox")
(setf text.textContent caption)
(set-style container
position "absolute")
(append-child container checkbox)
(append-child container text)
(when action
(set-handler checkbox onchange
(funcall action)))
(setf container.% #'checkbox)
(setf container.node checkbox)
container))
(def-accessor #'checkbox caption widget.lastChild.textContent)
(def-accessor #'checkbox action widget.firstChild.onchange)
(def-accessor #'checkbox checked widget.firstChild.checked)
(defun radio (group caption &optional action)
"Creates a radio button DOM object with specified logical [group], the provided [caption] ad an optional callback [action]"
(let ((radio (create-element "input"))
(text (set-style (create-element "span")
fontFamily "Arial"
px/fontSize 16))
(container (create-element "label")))
(setf radio.type "radio")
(setf radio.name group)
(setf text.textContent caption)
(set-style container
position "absolute")
(append-child container radio)
(append-child container text)
(when action
(set-handler radio onchange
(funcall action)))
(setf container.% #'radio)
(setf container.node radio)
container))
(def-accessor #'radio caption widget.lastChild.textContent)
(def-accessor #'radio action widget.firstChild.onchange)
(def-accessor #'radio checked widget.firstChild.checked)
(defun label (txt)
(let ((label (create-element "div")))
(setf label.textContent txt)
(set-style label
fontFamily "sans-serif"
%/fontSize 80
color "#666666"
whiteSpace "pre"
fontWeight "bold")
(setf label.% #'label)
label))
(def-accessor #'label caption widget.textContent)
(defun focus (widget)
"Sets the focus to DOM node of [widget] after 10ms.
Delaying is necessary because setting the focus in the handler \
of an event (e.g. a button click) is not going to work."
(set-timeout (lambda () ((node widget).focus)) 10))
(defun input (caption &key (autofocus false) (autoselect true))
"Creates an input field with specified [caption]"
(let ((input (create-element "input"))
(label (label caption))
(container (create-element "div")))
(set-style container
position "absolute")
(set-style input
%/width 100
%/fontSize 110
fontWeight "bold"
border "none"
px/padding 1
px/margin 0
backgroundColor "#EEEEEE")
(setf input.type "text")
(append-child container label)
(append-child container input)
(set-handler input onkeydown
(check-default-actions container.parentNode event))
(if autoselect
(set-handler input onfocus
(input.setSelectionRange 0 (length input.value))))
(setf container.% #'input)
(setf container.node input)
(when autofocus (focus input))
container))
(def-accessor #'input caption widget.firstChild.textContent)
(def-accessor #'input text widget.lastChild.value)
(defun input-with-help (caption helper)
"Creates an input field with specified [caption] and an helper button"
(let ((input (create-element "input"))
(label (label caption))
(help (create-element "input"))
(container (create-element "div")))
(set-style container
position "absolute")
(set-style input
position "absolute"
%/fontSize 110
fontWeight "bold"
border "none"
px/padding 1
px/margin 0
backgroundColor "#EEEEEE")
(set-style help
position "absolute"
px/width 30)
(setf help.type "button")
(setf help.value "?")
(setf input.type "text")
(append-child container label)
(append-child container help)
(append-child container input)
(setf container."data-resize"
(lambda (x0 y0 x1 y1)
(let ((ly (+ label.offsetTop label.offsetHeight)))
(set-style input
px/left 0
px/top ly
px/width (- x1 x0 25)
px/height (- y1 y0 ly))
(set-style help
px/left (- x1 x0 25 -2)
px/top ly
px/width 25
px/height (- y1 y0 ly -2)))))
(set-handler help onclick
(funcall helper container))
(setf help.tabIndex -1)
(set-handler input onkeydown
(when (= event.which 112)
(event.preventDefault)
(event.stopPropagation)
(funcall helper container)))
(setf container.% #'input-with-help)
(setf container.node input)
container))
(def-accessor #'input-with-help text widget.lastChild.value)
(def-accessor #'input-with-help caption widget.firstChild.textContent)
(def-accessor #'input-with-help action widget.firstChild.nextSibling.onclick)
(defun text-area (caption)
"Creates a multiline input field with specified [caption]"
;; Textarea HTML specs is badly broken and there is
;; no way to specify top/left/right/bottom in absolute
;; coordinates (works in WebKit, but only because of a bug).
;; Solution here is to wrap the textarea in another
;; div (that has no problem with abs positioning) and setting
;; width 100% and height 100% to use up all the space.
(let** ((container (set-style (create-element "div")
position "absolute"))
(label (append-child container (label caption)))
(txcont (set-style (append-child container (create-element "div"))
position "absolute"
px/left 0
px/top 16
px/right 8
px/bottom 8))
(input (set-style (append-child txcont (create-element "textarea"))
%/fontSize 110
fontWeight "bold"
position "absolute"
px/left 0
px/top 0
%/width 100
%/height 100
border "none"
px/padding 4
px/margin 0
resize "none"
backgroundColor "#EEEEEE")))
(setf container.% #'text-area)
(setf container.node input)
container))
(def-accessor #'text-area text widget.node.value)
(def-accessor #'text-area caption widget.firstChild.textContent)
(defun select (caption values)
"Creates an select field with specified [caption] and list of [values]"
(let ((select (create-element "select"))
(label (label caption))
(container (create-element "div")))
(set-style container
position "absolute")
(set-style select
%/width 100
%/fontSize 110
fontWeight "bold"
border "none"
px/padding 0
px/margin 0
backgroundColor "#EEEEEE")
(dolist (x values)
(let ((item (create-element "option")))
(setf item.textContent x)
(append-child select item)))
(append-child container label)
(append-child container select)
(setf container.% #'select)
(setf container.node select)
(set-handler select onkeydown
(check-default-actions container.parentNode event))
container))
(def-accessor #'select caption widget.firstChild.textContent)
(defmethod text (widget) (= widget.% #'select)
(aref (node widget).options
(node widget).selectedIndex).value)
(defmethod set-text (widget text) (= widget.% #'select)
(let ((ix (index text (map (get value) (node widget).options))))
(setf (node widget).selectedIndex ix)
(if (>= ix 0) text null)))
(defun group (&optional title)
"A group of related fields with an optional [title]"
(let ((group (create-element "div"))
(caption (create-element "span")))
(set-style group
position "absolute"
border "solid 1px #CCCCCC"
pointerEvents "none"
px/borderRadius 4)
(set-style caption
fontFamily "sans-serif"
whiteSpace "pre"
%/fontSize 80
color "#666666"
backgroundColor "#FFFFFF"
position "relative"
px/left 10
px/top -11)
(setf caption.textContent (if title ~" {title} " ""))
(append-child group caption)
(setf group.% #'group)
group))
(def-accessor #'group caption widget.caption)
;; Layout node for DOM elements
(defobject dom (element layout x0 y0 x1 y1))
(defmethod set-coords (node x0 y0 x1 y1) (dom? node)
(set-style node.element
px/left (round x0)
px/top (round y0)
px/width (round (- x1 x0))
px/height (round (- y1 y0)))
(setf node.x0 x0)
(setf node.y0 y0)
(setf node.x1 x1)
(setf node.y1 y1)
(when node.element."data-resize"
(node.element."data-resize" x0 y0 x1 y1))
(if node.layout
(set-coords node.layout x0 y0 x1 y1)
(list x0 y0 x1 y1)))
(defun dom (element &optional layout)
(setf element."data-layout-node"
(new-dom element layout)))
(defun dom-replace (element new-element)
"Replaces an [element] that has been wrapped in a [dom] \
layout node with a [new-element]. See {{dom}}."
(let ((layout element."data-layout-node"))
(append-child element.parentNode new-element)
(setf layout.element new-element)
(setf new-element."data-layout-node" layout)
(set-coords layout layout.x0 layout.y0 layout.x1 layout.y1)
(hide element)
new-element))
;; Splitters
(defun setsplit (w0 container layout min max)
(cond
((< w0 (/ min 2))
(setf w0 0))
((< w0 min)
(setf w0 min))
((> w0 (/ (+ max 100) 2))
(setf w0 100))
((>= w0 max)
(setf w0 max)))
(setf (first layout.elements).weight w0)
(set-style (first layout.elements).element.element
display (if (= w0 0) "none" null))
(setf (last layout.elements).weight (- 100 w0))
(set-style (last layout.elements).element.element
display (if (= w0 100) "none" null))
(set-coords layout 0 0
container.offsetWidth
container.offsetHeight))
(defun h-splitter (a b &key (split 50) (min 10) (max 90))
(let ((container (create-element "div"))
(splitter (create-element "div")))
(append-child container a)
(append-child container b)
(append-child container splitter)
(set-style splitter
position "absolute"
cursor "move"
backgroundColor "#DDDDDD")
(let ((layout (H spacing: 2
weight: split
(dom a)
size: 8
(dom splitter)
weight: (- 100 split)
(dom b))))
(set-handler splitter onmousedown
(event.preventDefault)
(event.stopPropagation)
(tracking (lambda (x y)
(declare (ignorable y))
(let ((w container.offsetWidth))
(decf x (first (element-pos container)))
(setsplit (/ x w 0.01) container layout min max)))
(lambda ())
"move"))
(setf container."data-resize"
(lambda (x0 y0 x1 y1)
(set-coords layout 0 0 (- x1 x0) (- y1 y0))))
(setf container.partition
(lambda (w) (setsplit w container layout min max))))
container))
(defun v-splitter (a b &key (split 50) (min 10) (max 90))
(let ((container (create-element "div"))
(splitter (create-element "div")))
(append-child container a)
(append-child container b)
(append-child container splitter)
(set-style container
position "absolute")
(set-style splitter
position "absolute"
cursor "move"
backgroundColor "#DDDDDD")
(let ((layout (V spacing: 2
weight: split
(dom a)
size: 8
(dom splitter)
weight: (- 100 split)
(dom b))))
(set-handler splitter onmousedown
(event.preventDefault)
(event.stopPropagation)
(tracking (lambda (x y)
(declare (ignorable x))
(let ((h container.offsetHeight))
(decf y (second (element-pos container)))
(setsplit (/ y h 0.01) container layout min max)))
(lambda ())
"move"))
(setf container."data-resize"
(lambda (x0 y0 x1 y1)
(set-coords layout 0 0 (- x1 x0) (- y1 y0))))
(setf container.partition
(lambda (w) (setsplit w container layout min max))))
container))
;; Table widget
(defun table (data &key cols rows row-click cell-click)
(let ((cells (list))
(table (create-element "div")))
(dolist (row data)
(let ((rowcells (list)))
(push (list) cells)
(dolist (col row)
(let ((cell col))
(when (or (number? cell)
(string? cell))
(setf cell (create-element "div"))
(set-style cell
overflow "hidden"
textAlign "center"
position "absolute"
cursor "default"
backgroundColor "#EEEEEE")
(setf cell.textContent col))
(append-child table cell)
(push cell rowcells)
(cond
(row-click
(set-handler cell onmousedown
(event.preventDefault)
(event.stopPropagation)
(funcall row-click row rowcells)))
(cell-click
(set-handler cell onmousedown
(event.preventDefault)
(event.stopPropagation)
(funcall cell-click col cell))))
(push (dom cell) (last cells))))))
(let ((layout (V border: 4
(tablayout columns: cols
rows: rows
cells))))
(set-style table
position "absolute"
backgroundColor "#CCCCCC"
overflow "hidden")
(setf table."data-resize"
(lambda ()
(setf table.style.overflow "hidden")
(let* ((w table.offsetWidth)
(h table.offsetHeight)
((x0 y0 x1 y1) (set-coords layout 0 0 w h)))
(when (or (< x0 0) (> x1 w) (< y0 0) (> y1 h))
(setf table.style.overflow "auto")
(setf w table.clientWidth)
(setf h table.clientHeight)
(set-coords layout 0 0 w h)))))
(setf table.data-cols cols)
(setf table.data-rows rows)
(setf table.% #'table)
table)))
;; Tree view
(defun tree-view (tree &key onclick (text-of (get text)) (children-of (get children)) (closed-nodes (list)))
(let** ((container (set-style (create-element "div")
backgroundColor "#EEEEEE"
overflow "auto"))
(select-nodes null)
(select-places null)
(#'rebuild ()
(do () ((not container.firstChild))
(remove-child container container.firstChild))
(let** ((#'add (n depth)
(let ((row (create-element "div")))
(set-style row
px/paddingLeft (+ 4 depth)
px/paddingTop 2
px/paddingBottom 2
px/fontSize 16
whiteSpace "nowrap"
cursor "default"
fontFamily "monospace"
fontWeight "bold")
(set-handler row onmouseover
(when select-nodes
(set-style row backgroundColor "#FF0000")))
(set-handler row onmouseout
(set-style row backgroundColor ""))
(set-handler row onmousedown
(event.preventDefault)
(event.stopPropagation)
(cond
(select-nodes
(let ((f select-nodes))
(setf select-nodes null)
(funcall f n)))
(select-places)
(onclick
(funcall onclick n)))
false)
(let ((expander (create-element "span")))
(set-style expander
cursor "default"
backgroundColor "#FFFFFF"
px/paddingLeft 3
px/paddingRight 3
px/marginRight 9
border "solid 1px #000000")
(setf expander.textContent
(if (find n closed-nodes) "+" "-"))
(set-handler expander onmousedown
(event.preventDefault)
(event.stopPropagation)
(if (find n closed-nodes)
(nremove n closed-nodes)
(push n closed-nodes))
(rebuild)
false)
(append-child row expander))
(append-child row (document.createTextNode (funcall text-of n)))
(append-child container row)
(let** ((#'place (parent index d)
(let ((place (set-style (create-element "div")
px/marginLeft d
px/height 4)))
(set-handler place onmouseover
(when select-places
(set-style place backgroundColor "#FF0000")))
(set-handler place onmouseout
(set-style place backgroundColor ""))
(set-handler place onmousedown
(event.preventDefault)
(event.stopPropagation)
(when select-places
(let ((f select-places))
(setf select-places null)
(funcall f parent index)))
false)
place)))
(unless (find n closed-nodes)
(append-child container (place n 0 (+ depth 28)))
(enumerate (i c n.children)
(add c (+ depth 28))
(when (find c closed-nodes)
(append-child container (place n (1+ i) (+ depth 28))))))))))
(when container.tree
(add container.tree 0)))))
(setf container.rebuild #'rebuild)
(setf container.tree tree)
(setf container.select-node (lambda (f)
(setf select-nodes f)))
(setf container.select-place (lambda (f)
(setf select-places f)))
(rebuild)
container))
;; Tab control
(defun tabbed ()
(let** ((tabbed (set-style (create-element "div")
position "absolute"
fontFamily "Arial"
fontWeight "bold"))
(ribbon (append-child tabbed (set-style (create-element "div")
position "absolute"
whiteSpace "pre")))
(area (append-child tabbed (set-style (create-element "div")
position "absolute"
border "solid 1px #000000"
backgroundColor "#FFFFFF")))
(layout (V spacing: 0
size: 22 (dom ribbon)
size: undefined (dom area)))
(tabs (list))
(pages (list))
(current -1)
(#'current ()
(aref pages current))
(#'add (label page &optional (close false))