-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqt.scm
798 lines (728 loc) · 23.5 KB
/
qt.scm
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
(module qt *
(import scheme chicken foreign data-structures extras srfi-1 srfi-69)
(declare (emit-external-prototypes-first))
(require-extension srfi-69)
(import-for-syntax matchable)
;; There exists no way to call foreign-type-declaration, so hack it
(define-for-syntax (foreign-type-decl-hack typesym)
(string-substitute "-ptr" "*" (->string typesym)))
(define-record-type qt-proxy
(make-qt-proxy member-table)
qt-proxy?
(member-table qt-proxy:get-member-table))
(define (qt-class:get-proxy-member class member)
(hash-table-ref
(qt-proxy:get-member-table
(qt-class:get-proxy
class))
member))
(define (proxy-call-impl method class . params)
(apply
(qt-class:get-proxy-member class method)
(cons class params)))
(define-syntax proxy-call
(syntax-rules ()
((proxy-call method class params ...)
(proxy-call-impl 'method class params ...))))
(define-syntax define-qt-class
(lambda (e r c)
(match e
(('define-qt-class
class-name
constructor
parent-constructor
methods ...)
`(define (,constructor . params)
(let ((qt-proxy-instance (make-qt-proxy (make-hash-table))))
,@(map
(match-lambda
((method-name params body ...)
`(hash-table-set!
(qt-proxy:get-member-table qt-proxy-instance)
',method-name
(lambda ,params ,@body))))
methods)
(apply ,parent-constructor (cons qt-proxy-instance params))))))))
;; Macros need hygiene!
(define-record-type qt-class-type
(make-qt-class-type name parent message-table)
qt-class-type?
(name qt-class:get-name)
(parent qt-class:get-parent)
(message-table qt-class:get-message-table))
(define-record-type qt-class
(make-qt-class type qt-ptr)
qt-class?
(type qt-class:get-type)
(qt-ptr qt-class:get-ptr qt-class:set-ptr!)
(proxy qt-class:get-proxy qt-class:set-proxy!))
(define (get-class-method type method)
(hash-table-ref
(qt-class:get-message-table type)
method
(lambda ()
(if (qt-class:get-parent type)
(get-class-method
(qt-class:get-parent type)
method)
'()))))
(define (maybe-get-qt-ptr var)
(if (qt-class? var)
(qt-class:get-ptr var)
var))
(define (qt-class:set-class-method! class name func)
(hash-table-set!
(qt-class:get-message-table class)
name
func))
(define (call-impl method class . params)
(apply
(get-class-method (qt-class:get-type class) method)
(cons class params)))
;; Need better diagnostics when call params fail!
(define-syntax call
(syntax-rules ()
((call method class params ...)
(call-impl 'method class params ...))))
(define qt-class-list (make-hash-table))
(define-for-syntax qt-class:gen-make
(lambda (class-name self set preconstruct-func)
`(letrec ((,self
(make-qt-class
(hash-table-ref qt-class-list ',class-name)
'())))
,(if preconstruct-func
`(,preconstruct-func ,self)
'#f)
(qt-class:set-ptr!
,self
,set)
,self)))
(define-for-syntax make-name
(lambda (i)
(string->symbol
(string-append
"s"
(->string i)))))
(define-for-syntax ptr-type-name
(lambda (sym)
(symbol-append sym '-ptr)))
(define-for-syntax ref-type-name
(lambda (sym)
(symbol-append sym '-ref)))
(define-for-syntax param-list
(lambda (func params i)
(if (null? params)
'()
(let ((param (car params)))
(cons
(func param i)
(param-list func (cdr params) (+ i 1)))))))
(define-for-syntax map-names
(lambda (func params)
(param-list
(lambda (param i)
(func param (make-name i)))
params
0)))
(define-for-syntax symbol-append
(lambda symbol-list
(string->symbol
(apply
string-append
(map
(lambda (x)
(->string x))
symbol-list)))))
(define-for-syntax qt-proxy-callback-name
(lambda (name)
(string->symbol
(string-append
"C_callback_"
(->string name)))))
(define-for-syntax qt-proxy-callback
(lambda (callback-name
params
return
callback)
`(define-external (,(qt-proxy-callback-name callback-name)
(scheme-object self)
,@(map-names
(lambda (param name)
(list
param
name))
params))
,return
(,callback
self
,@(map-names
(lambda (param name)
name)
params)))))
(define-syntax qt-foreign-define
(lambda (e r c)
(let
((class (cadr e))
(method (caaddr e))
(self (cadr (caddr e)))
(params (cddr (caddr e)))
(return (cadddr e))
(body (car (cddddr e))))
`(qt-class:set-class-method!
(hash-table-ref
qt-class-list
',class)
',method
(lambda (,(cadr self)
,@(map
cadr
params))
((foreign-safe-lambda*
,return
(,self
,@params)
,body)
(qt-class:get-ptr ,(cadr self))
,@(map
(lambda (param)
(cadr param))
params)))))))
(define-syntax qt-define-method
(lambda (e r c)
(let ((class (cadr e))
(method (caaddr e))
(c-method (caaddr e))
(params (cdaddr e))
(return (cadddr e)))
(if (list? method)
(begin
(set! method (car method))
(set! c-method (cadr c-method))))
`(qt-foreign-define
,class
(,method
(c-pointer self)
,@(param-list
(lambda (param i)
(list
param
(make-name i)))
params 0))
,return
,(apply
string-append
`(
,(if (eq? return 'void)
""
"C_return(")
"(("
,(->string class)
"*)self)->"
,(->string c-method)
"("
,@(param-list
(lambda (param i)
(string-append
(if (> i 0)
", "
"")
(->string (make-name i))))
params 0)
,(if (eq? return 'void)
""
")")
");"))))))
(define-syntax qt-static-method
(lambda (e r c)
(match
e
(('qt-static-method
class-name
method-name
return-type
arg-types)
`(define
,(symbol-append
class-name ': method-name)
(foreign-lambda*
,return-type
,(param-list
(lambda (param i)
(list
param
(make-name i)))
arg-types 0)
,(apply
string-append
`(
,(if (eq? return-type 'void)
""
"C_return(")
,(->string class-name)
"::"
,(->string method-name)
"("
,@(param-list
(lambda (param i)
(string-append
(if (> i 0)
", "
"")
(->string (make-name i))))
arg-types 0)
")"
,(if (eq? return-type 'void)
""
")")
";"))))))))
;; TODO: Create a constructor call that causes the object to be destroyed with the Scheme object.
(define-syntax qt-class
(lambda (e r c)
(match
e
(('qt-class class-name constructor-list method-list)
(set! parent-name '())
(if (list? class-name)
(begin
(set! parent-name (cadr class-name))
(set! class-name (car class-name))))
`(begin
(hash-table-set!
qt-class-list
',class-name
(make-qt-class-type
',class-name
,(if (not (null? parent-name))
`(hash-table-ref/default qt-class-list ',parent-name #f)
#f)
(make-hash-table)))
(define-foreign-type
,(ptr-type-name class-name)
(c-pointer (struct ,class-name))
maybe-get-qt-ptr
(lambda (qt-ptr)
,(qt-class:gen-make
class-name
(r 'self)
'qt-ptr
#f)))
,(match
constructor-list
((constructor . constructor-params)
`(define ,constructor
(lambda (,@(delete
#f
(param-list
(lambda (param i)
(if (not (eq? param 'self))
(make-name i)
#f))
constructor-params 0)))
;; Can't use the foreign type cohersion functions, because
;; if we need to pass the scheme ptr to C we need a
;; reference before the call.
,(qt-class:gen-make
class-name
(r 'self)
`((foreign-safe-lambda*
c-pointer
,(delete
#f
(param-list
(lambda (param i)
(if (eq? param 'proxy)
#f
(list
(if (eq? param 'self)
'scheme-object
param)
(make-name i))))
constructor-params 0))
,(apply
string-append
`("C_return(new "
,(->string class-name)
"("
,@(param-list
(lambda (param i)
(if (eq? param 'proxy)
""
(string-append
(if (> i 0)
", "
"")
(->string (make-name i)))))
constructor-params 0)
"));")))
,@(delete
#f
(param-list
(lambda (param i)
(if (eq? param 'proxy)
#f
(if (eq? param 'self)
(r 'self)
(make-name i))))
constructor-params 0)))
(if (any (lambda (P) (eq? P 'proxy)) constructor-params)
`(lambda (self)
(qt-class:set-proxy!
self
,@(delete
#f
(param-list
(lambda (param i)
(if (eq? param 'proxy)
(make-name i)
#f))
constructor-params 0))))
#f)))))
(()
''()))
,@(map
(lambda (method)
(match
method
((method-name params method-return)
`(qt-define-method
,class-name
(,method-name ,@params)
,method-return))))
method-list)
(qt-foreign-define
,class-name
(delete (c-pointer self))
void
,(string-append
"delete (("
(->string class-name)
"*)self);")))))))
;; by-ref classes aren't allowed to have parent classes.
;; TODO: fix this, QPixmap has a parent.
(define-syntax qt-ref-class
(lambda (e r c)
`(begin
(qt-class ,@(cdr e))
;; This relies on a class having a copy constructor, should ensure this is true.
,(let ((class-name (cadr e)))
`(define-foreign-type
,(ref-type-name class-name)
(ref (struct ,class-name))
maybe-get-qt-ptr
(lambda (qt-ptr)
,(qt-class:gen-make
class-name
(r 'self)
`((foreign-lambda* c-pointer (((ref (struct ,class-name)) ref))
,(string-append
"C_return(new "
(->string class-name)
"(ref)) ;"))
qt-ptr)
#f)))))))
(define-syntax qt-proxy-class
(lambda (e r c)
(match
e
(('qt-proxy-class
class-name
parent-class
(constructor
constructor-params)
parent-params)
`(qt-proxy-class
,class-name
,parent-class
(,constructor
,constructor-params)
,parent-params
()))
(('qt-proxy-class
class-name
parent-class
(constructor
constructor-params)
parent-params
proxies)
`(begin
;; Pre-Constructor callback
;; qt-ptr doesn't get set before the constructor is called, so we set it here
,(qt-proxy-callback
(symbol-append
class-name
'preconstruct)
`(,(ptr-type-name
class-name))
'void
'(lambda (self c-self)
(qt-class:set-ptr! self c-self)))
;; Constructor callback
,(qt-proxy-callback
(symbol-append
class-name
'constructor)
(map
(lambda (X)
(car X))
constructor-params)
'void
'(qt-class:get-proxy-member self 'constructor))
;; Destructor callback
,(qt-proxy-callback
(symbol-append
class-name
'destructor)
'()
'void
'(qt-class:get-proxy-member self 'destructor))
;; Proxies callbacks
,@(map
(lambda (proxy)
(apply
qt-proxy-callback
(append
proxy
`((qt-class:get-proxy-member self ',(car proxy))))))
proxies)
(foreign-declare
,(apply
string-append
(append
(list "class " (->string class-name) " : public " (->string parent-class)
"{"
" void* proxy_root;"
"public:"
(->string class-name) "("
"C_word proxy")
(map
(lambda (param)
(string-append
", "
(foreign-type-decl-hack (car param))
" "
(->string (cadr param))))
constructor-params)
(list
") :"
(->string parent-class) "(")
(map
(lambda (params)
(->string params))
parent-params)
(list
")"
"{"
" proxy_root = CHICKEN_new_gc_root();"
" CHICKEN_gc_root_set(proxy_root, proxy);"
(->string (qt-proxy-callback-name
(symbol-append
class-name
'preconstruct)))
"(proxy, this);"
(->string (qt-proxy-callback-name
(symbol-append
class-name
'constructor)))
"(proxy")
(map
(lambda (param)
(string-append
", "
(->string (cadr param))))
constructor-params)
(list
");"
"}"
"~" (->string class-name) "()"
"{"
(->string (qt-proxy-callback-name
(symbol-append
class-name
'destructor)))
"(CHICKEN_gc_root_ref(proxy_root));"
"CHICKEN_delete_gc_root(proxy_root);"
"}")
;; Proxy method wrappers
(map
(lambda (proxy)
(match
proxy
((proxy-name params return-type)
(apply
string-append
(append
(list
(->string return-type)
" "
(->string proxy-name)
"(")
(param-list
(lambda (param i)
(string-append
(if (> i 0)
", "
"")
(foreign-type-decl-hack param)
" "
(->string
(make-name i))))
params
0)
(list
")"
"{"
(if (eq? return-type 'void)
""
"")
(->string
(qt-proxy-callback-name proxy-name))
"(CHICKEN_gc_root_ref(proxy_root)")
(map-names
(lambda (param name)
(string-append
", "
(->string name)))
params)
(list
");"
"}"))))))
proxies)
(list "};"))))
(qt-class
(,class-name ,parent-class)
(,constructor
self
proxy
,@(map
(lambda (param)
(car param))
constructor-params))
()))))))
(define-syntax qt-app:create-exec-window!
(lambda (e r c)
(match
e
(('qt-app:create-exec-window!
create-func
window-var)
`(begin
;; Define-external doesn't get closure environment,
;; needs to be global.
(define global-window-func #f)
(define (,create-func window-func)
(set! global-window-func window-func)
(define-external (init_window) scheme-object
(global-window-func))
(define-external (finalize_window (scheme-object window))
void
(call delete window))
((foreign-safe-lambda*
void
()
,(string-append
"int argc = 0;"
"char** argv = NULL;"
"QApplication a(argc, argv);"
"C_word W = init_window();"
"a.exec();"
"finalize_window(W);")))))))))
(define (qt:connect sender recv signal slot)
(call connect recv sender (string-append "2" signal) (string-append "1" slot)))
(foreign-declare "#include <QtGui/QApplication>")
(foreign-declare "#include <QtGui/QMainWindow>")
(foreign-declare "#include <QtGui/QVBoxLayout>")
(foreign-declare "#include <QtGui/QPushButton>")
(foreign-declare "#include <QtUiTools/QUiLoader>")
(foreign-declare "#include <QtCore/QFile>")
(foreign-declare "#include <QtGui/QFileDialog>")
(foreign-declare "#include <QtGui/QGraphicsScene>")
(foreign-declare "#include <QtGui/QGraphicsView>")
(foreign-declare "#include <QtGui/QGraphicsPixmapItem>")
(qt-ref-class
QByteArray
()
((data () (const c-string))))
(qt-ref-class
QString
(make-QString (const c-string))
((toAscii () QByteArray-ref)))
(qt-class
QObject
(make-QObject QObject-ptr)
((objectName () QString-ref)
(connect (QObject-ptr c-string c-string) bool)
(setObjectName (QString-ref) void)
(findChild<QWidget*> (QString-ref) QObject-ptr)))
(qt-class
QLayout
() ())
(qt-class
(QWidget QObject)
(make-QWidget QWidget-ptr)
((show () void)
(setLayout (QLayout-ptr) void)))
(qt-class
(QIODevice QObject)
()
())
(qt-static-method
QFileDialog
getOpenFileName
QString-ref
(QWidget-ptr
QString-ref
QString-ref
QString-ref))
(qt-class
(QFile QIODevice)
(make-QFile QString-ref)
((open ((enum QIODevice::OpenModeFlag)) bool)
(close () void)))
(qt-ref-class
QPixmap
(make-QPixmap QString-ref)
())
(qt-class
QGraphicsPixmapItem
()
())
(qt-class
(QGraphicsScene QObject)
(make-QGraphicsScene)
((addPixmap (QPixmap-ref) QGraphicsPixmapItem-ptr)))
(qt-class
(QGraphicsView QWidget)
()
((setScene (QGraphicsScene-ptr) void)))
(qt-class
(QMainWindow QWidget)
(make-QMainWindow QWidget-ptr)
((isAnimated () bool)
(setCentralWidget (QWidget-ptr) void)
(centralWidget () QWidget-ptr)))
(qt-class
(QUiLoader QObject)
(make-QUiLoader QObject-ptr)
((load (QIODevice-ptr QWidget-ptr) QWidget-ptr)))
(qt-class
QPushButton
(make-QPushButton (ref (struct QString)) (c-pointer (struct QWidget)))
())
(qt-class
QBoxLayout
()
((addWidget (QWidget-ptr) void)))
(qt-class
(QVBoxLayout QBoxLayout)
(make-QVBoxLayout QWidget-ptr)
())
(qt-proxy-class
QMainWindowProxy
QMainWindow
(Make-MainWindow-Proxy
((QWidget-ptr parent #f)))
(parent))
(qt-app:create-exec-window!
initialize-qt
MyImageWindow))