-
Notifications
You must be signed in to change notification settings - Fork 0
/
idmt.rkt
454 lines (428 loc) · 16 KB
/
idmt.rkt
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
#lang racket/base
(provide (all-defined-out))
(require file/convertible
pict
(prefix-in pict: pict)
racket/set
racket/list
racket/math
racket/draw
racket/class
racket/gui/base
racket/serialize
racket/stxparam
racket/splicing
racket/match
(for-syntax racket/base
racket/syntax
syntax/parse))
(define serial-key (generate-member-key))
(define deserial-key (generate-member-key))
(define copy-key (generate-member-key))
(begin-for-syntax
(define-syntax-class defstate
#:literals (define-state)
(pattern (define-state name body ...)))
(define-syntax-class defpubstate
#:literals (define-public-state)
(pattern (define-public-state name body ...))))
(define-syntax-parameter defstate-parameter
(syntax-parser
[(_ stx who)
(raise-syntax-error #'who "Use outside of define-idmt is an error" this-syntax)]))
(define-syntax (define-state stx)
(syntax-parse stx
[x:defstate
#`(defstate-parameter #,stx define-state)]))
(define-syntax (define-public-state stx)
(syntax-parse stx
[x:defpubstate
#`(defstate-parameter #,stx define-public-state)]))
(define-syntax (~define-idmt stx)
(syntax-parse stx
[(_ orig-stx name:id supclss (interfaces ...)
(~or (~optional (~seq #:base? b?) #:defaults ([b? #'#f]))
(~optional (~seq #:direct-deserialize? dd?) #:defaults ([dd? #'#t])))
...
(~and
(~seq (~or state:defstate public-state:defpubstate internal-body) ...)
(~seq body ...)))
#:with name-deserialize (format-id stx "~a:fdeserialize" #'name)
(define dd?* (syntax-e #'dd?))
(unless (or (not dd?*) (eq? 'module (syntax-local-context)))
(raise-syntax-error #f "Must be defined at the module level" #'orig-stx))
(define serialize-method (gensym 'serialize))
(define deserialize-method (gensym 'deserialize))
(define copy-method (gensym 'copy))
(define base? (syntax-e (attribute b?)))
#`(begin
(define-member-name #,serialize-method serial-key)
(define-member-name #,deserialize-method deserial-key)
(define-member-name #,copy-method copy-key)
#,@(if dd?*
(list
#`(provide name-deserialize)
#`(define name-deserialize
(make-deserialize-info
(λ (sup table public-table)
(define this (new name))
(send this #,deserialize-method (vector sup table public-table))
this)
(λ ()
(define pattern (new name))
(values pattern
(λ (other)
(send pattern #,copy-method other)))))))
'())
(splicing-syntax-parameterize ([defstate-parameter
(syntax-parser
[(_ st:defstate who)
#'(field [st.name st.body (... ...)])]
[(_ st:defpubstate who)
#'(field [st.name st.body (... ...)])])])
(define name
(let ()
#,@(for/list ([i (in-list (attribute state.name))])
#`(define-local-member-name #,i))
(class/derived
orig-stx
(name
supclss
((interface* () ([prop:serializable
(make-serialize-info
(λ (this)
(send this #,serialize-method))
#'name-deserialize
#t
(or (current-load-relative-directory) (current-directory)))]))
interfaces ...)
#f)
(define (#,serialize-method)
(vector #,(if base?
#'#f
#`(super #,serialize-method))
(make-immutable-hash
`#,(for/list ([i (in-list (attribute state.name))])
#`(#,(syntax->datum i) . ,#,i)))
(make-immutable-hash
`#,(for/list ([i (in-list (attribute public-state.name))])
#`(#,(syntax->datum i) . ,#,i)))))
(#,(if base? #'public #'override) #,serialize-method)
(define (#,deserialize-method data)
(define sup (vector-ref data 0))
(define table (vector-ref data 1))
(define public-table (vector-ref data 2))
#,(if base?
#`(void)
#`(super #,deserialize-method sup))
#,@(for/list ([i (in-list (attribute state.name))])
#`(set! #,i (hash-ref table '#,(syntax->datum i))))
#,@(for/list ([i (in-list (attribute public-state.name))])
#`(set! #,i (hash-ref public-table '#,(syntax->datum i)))))
(#,(if base? #'public #'override) #,deserialize-method)
(define (#,copy-method other)
#,(if base?
#`(void)
#`(super #,copy-method other))
#,@(for/list ([i (in-list (attribute state.name))])
#`(set! #,i (get-field #,i other)))
#,@(for/list ([i (in-list (attribute public-state.name))])
#`(set! #,i (get-field #,i other)))
(void))
(#,(if base? #'public #'override) #,copy-method)
body ...)))))]))
(define-syntax (define-base-idmt* stx)
(syntax-parse stx
[(_ name:id super (interfaces ...) body ...)
#`(~define-idmt #,stx name super (interfaces ...) #:base? #t body ...)]))
(define-syntax (define-idmt stx)
(syntax-parse stx
[(_ name:id super
(~or (~optional (~seq #:interfaces (interfaces ...)) #:defaults ([(interfaces 1) '()])))
...
body ...)
#`(~define-idmt #,stx name super (interfaces ...) body ...)]))
(define-syntax (define-idmt-mixin stx)
(syntax-parse stx
[(_ name:id
(~or (~optional (~seq #:interfaces (interfaces ...)) #:defaults ([(interfaces 1) '()]))
(~optional (~seq #:mixins (mixins ...)) #:defaults ([(mixins 1) '()])))
...
body ...)
#`(begin
(define (name $)
(~define-idmt #,stx
name
((compose #,@(reverse (attribute mixins))) $)
(interfaces ...)
#:direct-deserialize? #f
body ...)
name))]))
(define idmt<$>
(interface*
()
([prop:convertible
(λ (this format default)
(case format
[(png-bytes)
(define-values (w* h*) (send this get-min-extent))
(define w (exact-ceiling (max w* 1)))
(define h (exact-ceiling (max h* 1)))
(define bit (make-object bitmap% w h))
(send this draw (new bitmap-dc% [bitmap bit]) 0 0 w h)
(define s (open-output-bytes))
(send bit save-file s 'png)
(get-output-bytes s)]
[else default]))])
get-min-extent
get-max-extent
set-current-extent
get-current-extent
draw
on-mouse-event
on-keyboard-event))
(define-base-idmt* base$ object% (idmt<$>)
(super-new)
(define-state x #f)
(define-state y #f)
(define-state width #f)
(define-state height #f)
(define/public (add-data key val)
(void))
(define/public (draw dc x y w h)
(void))
(define/public (get-min-extent)
(values 0 0))
(define/public (on-mouse-event event)
(void))
(define/public (on-keyboard-event event)
(void))
(define/public (get-max-extent)
(values +inf.0 +inf.0))
(define/public (set-current-extent nx ny nw nh)
(set! x nx)
(set! y ny)
(set! width nw)
(set! height nh))
(define/public (get-current-extent)
(values x y width height)))
(define-idmt-mixin receiver$
(super-new)
(define/public (on-receive event)
(void))
(define/public (signal event)
(on-receive event)))
(define-idmt-mixin signaler$
(super-new)
(define-public-state receivers (mutable-set))
(define/public (signal event)
(for ([r (in-set receivers)])
(send r signal event)))
(define/public (register-receiver x)
(set-add! receivers x))
(define/public (unregister-receiver x)
(set-remove! receivers x)))
(define idmt-canvas%
(class canvas%
(init-field idmt)
(define-values (init-min-width init-min-height)
(send idmt get-min-extent))
(super-new [min-width (exact-ceiling init-min-width)]
[min-height (exact-ceiling init-min-height)]
[paint-callback (λ (c dc)
(send idmt set-current-extent 0 0 (send c get-width) (send c get-height))
(send idmt draw dc 0 0 (send c get-width) (send c get-height))
(define-values (w h) (send idmt get-min-extent))
(send this min-width (exact-ceiling w))
(send this min-height (exact-ceiling h)))])
(define/override (on-event event)
(send idmt on-mouse-event event)
(send this refresh))
(define/override (on-char event)
(send idmt on-keyboard-event event)
(send this refresh))))
(define-idmt widget$ base$
(super-new)
(init-field [font normal-control-font])
(define-public-state vert-margin 2)
(define-public-state horiz-margin 2)
(define-public-state style '())
(define-public-state parent #f)
(define/override (get-min-extent)
(values (* 2 vert-margin) (* 2 horiz-margin)))
(define/public (register-parent other)
(set! parent other))
(define/public (get-child-extent child)
(error 'get-child-extent "IDMT does not have children")))
(define-idmt list-widget$ widget$
(super-new)
(define-public-state idmt-list '())
(define/public (add-idmt idmt)
(set! idmt-list (append idmt-list (list idmt)))
(send idmt register-parent this))
(define/public (remove-idmt idmt)
(when (empty? idmt-list)
(error 'remove-idmt "List widget already emtpy"))
(send idmt register-parent #f)
(set! idmt-list (take idmt-list (sub1 (length idmt-list))))))
(define-idmt vertical-block$ list-widget$
(super-new)
(inherit-field idmt-list)
(define/override (get-min-extent)
(define-values (base-w base-h)
(super get-min-extent))
(define-values (w h)
(for/fold ([width 0]
[height 0])
([i (in-list idmt-list)])
(define-values (w h)
(send i get-min-extent))
(values (max w width)
(+ h height))))
(values (+ base-w w) (+ base-h h)))
(define/override (draw dc x y w h)
(define item-height (if (empty? idmt-list) #f (/ h (length idmt-list))))
(for/fold ([y y])
([i (in-list idmt-list)])
(send i draw dc x y w item-height)
(values (+ y item-height)))
(void))
(define/override (set-current-extent x y w h)
(super set-current-extent x y w h)
(define item-height (if (empty? idmt-list) #f (/ h (length idmt-list))))
(for/fold ([y y])
([i (in-list idmt-list)])
(send i set-current-extent x y w item-height)
(values (+ y item-height)))
(void))
(define/override (on-mouse-event event)
(super on-mouse-event event)
(for/list ([i (in-list idmt-list)])
(send i on-mouse-event event))))
(define-idmt horizontal-block$ list-widget$
(super-new)
(inherit-field idmt-list)
(define/override (get-min-extent)
(define-values (b-w b-h)
(super get-min-extent))
(define-values (w h)
(for/fold ([width 0]
[height 0])
([i (in-list idmt-list)])
(define-values (w h)
(send i get-min-extent))
(values (+ w width)
(max h height))))
(values (+ b-w w) (+ b-h h)))
(define/override (draw dc x y w h)
(define item-width (if (empty? idmt-list) #f (/ w (length idmt-list))))
(for/fold ([x x])
([i (in-list idmt-list)])
(send i draw dc x y item-width h)
(values (+ x item-width)))
(void)))
(define-idmt label$ widget$
(super-new)
(inherit-field font
horiz-margin
vert-margin)
(init [(internal-text text) #f])
(define-state text* internal-text)
(define/override (get-min-extent)
(define-values (b-w b-h)
(super get-min-extent))
(define pic (pict:text (or text* "---") font))
(values (+ b-w (pict-width pic)) (+ b-h (pict-height pic))))
(define/override (draw dc x y w h)
(define old-font (send dc get-font))
(send dc set-font font)
(send dc draw-text (or text* "---") (+ horiz-margin x) (+ vert-margin y))
(send dc set-font old-font)))
(define-idmt button$ (signaler$ widget$)
(super-new)
(inherit-field horiz-margin
vert-margin)
(init [(internal-label label) (new label$)])
(define mouse-state 'up)
(define-state label* internal-label)
(define-state up-color "WhiteSmoke")
(define-state hover-color "Gainsboro")
(define-state down-color "LightGray")
(define/override (on-mouse-event event)
(define-values (x y w h)
(send this get-current-extent))
(define x-max (+ x w))
(define y-max (+ y h))
(define mouse-x (send event get-x))
(define mouse-y (send event get-y))
(define in-button?
(and (<= x mouse-x x-max)
(<= y mouse-y y-max)))
(match (send event get-event-type)
['left-down
(when (and in-button? (eq? mouse-state 'hover))
(set! mouse-state 'down))]
['left-up
(when (and in-button? (eq? mouse-state 'down))
(if in-button?
(set! mouse-state 'hover)
(set! mouse-state 'up))
(send this signal this))]
['motion
(match mouse-state
[(or 'up 'hover)
(if in-button?
(set! mouse-state 'hover)
(set! mouse-state 'up))]
['down
(unless in-button?
(set! mouse-state 'up))])]
[_ (void)]))
(define/override (get-min-extent)
(define-values (b-w b-h)
(super get-min-extent))
(define-values (w h)
(send label* get-min-extent))
(values (+ b-w w) (+ b-h h)))
(define/override (draw dc x y w h)
(define old-brush (send dc get-brush))
(send dc set-brush
(new brush% [color (make-object color%
(match mouse-state
['up up-color]
['hover hover-color]
['down down-color]))]))
(send dc draw-rectangle
(+ x horiz-margin) (+ y vert-margin)
(- w (* 2 horiz-margin)) (- h (* 2 vert-margin)))
(send label* draw dc
(+ x horiz-margin) (+ y vert-margin)
(- w (* 2 horiz-margin)) (- h (* 2 vert-margin)))
(send dc set-brush old-brush)))
(define-idmt toggle$ widget$
(super-new))
(define-idmt radio$ list-widget$
(super-new))
(define-idmt field$ widget$
(super-new))
; ==========
(define f (new frame% [label "IDMT"]))
(define idmt (new vertical-block$))
(define counter 0)
(define-idmt add-item$ (receiver$ base$)
(super-new)
(define/override (on-receive event)
(send idmt add-idmt (new label$ [text (format "Item: ~a" counter)]))
(set! counter (add1 counter))))
(send idmt add-idmt (new label$ [text "Hello"]))
(send idmt add-idmt (new label$ [text "World"]))
(send idmt add-idmt (new label$ [text "I am an IDMT!!!"]))
(define btn (new button$ [label (new label$ [text "CLICK ME!"])]))
(send btn register-receiver (new add-item$))
(send idmt add-idmt btn)
(new idmt-canvas%
[parent f]
[idmt idmt])
(send f show #t)
(serialize idmt)
(deserialize (serialize idmt))