55212af1 |
1 | ;; Common Lisp bindings for GTK+ v2.x |
2 | ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net> |
0d07716f |
3 | ;; |
55212af1 |
4 | ;; Permission is hereby granted, free of charge, to any person obtaining |
5 | ;; a copy of this software and associated documentation files (the |
6 | ;; "Software"), to deal in the Software without restriction, including |
7 | ;; without limitation the rights to use, copy, modify, merge, publish, |
8 | ;; distribute, sublicense, and/or sell copies of the Software, and to |
9 | ;; permit persons to whom the Software is furnished to do so, subject to |
10 | ;; the following conditions: |
0d07716f |
11 | ;; |
55212af1 |
12 | ;; The above copyright notice and this permission notice shall be |
13 | ;; included in all copies or substantial portions of the Software. |
0d07716f |
14 | ;; |
55212af1 |
15 | ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
19 | ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
20 | ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
21 | ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0d07716f |
22 | |
039c2fdb |
23 | ;; $Id: gobject.lisp,v 1.51 2006/03/03 10:01:01 espen Exp $ |
0d07716f |
24 | |
25 | (in-package "GLIB") |
26 | |
27 | |
0c87a95a |
28 | ;;;; Metaclass used for subclasses of gobject |
29 | |
30 | (eval-when (:compile-toplevel :load-toplevel :execute) |
4c502835 |
31 | ;; (push :debug-ref-counting *features*) |
0c87a95a |
32 | (defclass gobject-class (ginstance-class) |
67985e36 |
33 | ((instance-slots-p :initform nil |
34 | :documentation "Non NIL if the class has slots with instance allocation"))) |
0c87a95a |
35 | |
3d36c5d6 |
36 | (defmethod validate-superclass ((class gobject-class) (super standard-class)) |
0c87a95a |
37 | ; (subtypep (class-name super) 'gobject) |
38 | t)) |
39 | |
40 | (defclass direct-property-slot-definition (direct-virtual-slot-definition) |
41 | ((pname :reader slot-definition-pname :initarg :pname) |
d5dae790 |
42 | (readable :reader slot-readable-p :initarg :readable) |
43 | (writable :reader slot-writable-p :initarg :writable) |
44 | (construct-only :initarg :construct-only :reader construct-only-property-p))) |
0c87a95a |
45 | |
46 | (defclass effective-property-slot-definition (effective-virtual-slot-definition) |
47 | ((pname :reader slot-definition-pname :initarg :pname) |
48 | (readable :reader slot-readable-p :initarg :readable) |
49 | (writable :reader slot-writable-p :initarg :writable) |
d5dae790 |
50 | (construct-only :initarg :construct-only :reader construct-only-property-p))) |
d4ddcf2c |
51 | |
52 | (defclass direct-user-data-slot-definition (direct-virtual-slot-definition) |
53 | ()) |
54 | |
55 | (defclass effective-user-data-slot-definition (effective-virtual-slot-definition) |
56 | ()) |
57 | |
0c87a95a |
58 | |
59 | (defbinding %object-ref () pointer |
60 | (location pointer)) |
61 | |
62 | (defbinding %object-unref () nil |
63 | (location pointer)) |
64 | |
d4665dd8 |
65 | #+glib2.8 |
124eb32c |
66 | (progn |
a92553bd |
67 | (define-callback toggle-ref-callback nil |
68 | ((data pointer) (location pointer) (last-ref-p boolean)) |
4d1fea77 |
69 | (declare (ignore data)) |
7ce0497d |
70 | #+debug-ref-counting |
71 | (if last-ref-p |
72 | (format t "Object at 0x~8,'0X has no foreign references~%" (sap-int location)) |
73 | (format t "Foreign reference added to object at 0x~8,'0X~%" (sap-int location))) |
124eb32c |
74 | (if last-ref-p |
75 | (cache-instance (find-cached-instance location) t) |
76 | (cache-instance (find-cached-instance location) nil))) |
77 | |
4660a86a |
78 | (defbinding %object-add-toggle-ref (location) pointer |
124eb32c |
79 | (location pointer) |
a92553bd |
80 | (toggle-ref-callback callback) |
124eb32c |
81 | (nil null)) |
82 | |
4660a86a |
83 | (defbinding %object-remove-toggle-ref (location) pointer |
124eb32c |
84 | (location pointer) |
a92553bd |
85 | (toggle-ref-callback callback) |
124eb32c |
86 | (nil null))) |
67985e36 |
87 | |
0c87a95a |
88 | (defmethod reference-foreign ((class gobject-class) location) |
89 | (declare (ignore class)) |
985e4aa3 |
90 | (%object-ref location)) |
0c87a95a |
91 | |
92 | (defmethod unreference-foreign ((class gobject-class) location) |
93 | (declare (ignore class)) |
124eb32c |
94 | (%object-unref location)) |
67985e36 |
95 | |
124eb32c |
96 | #+debug-ref-counting |
97 | (progn |
a92553bd |
98 | (define-callback weak-ref-callback nil ((data pointer) (location pointer)) |
124eb32c |
99 | (format t "Object at 0x~8,'0X being finalized~%" (sap-int location))) |
100 | |
4660a86a |
101 | (defbinding %object-weak-ref (location) pointer |
124eb32c |
102 | (location pointer) |
a92553bd |
103 | (weak-ref-callback callback) |
124eb32c |
104 | (nil null))) |
0c87a95a |
105 | |
106 | |
107 | ; (defbinding object-class-install-param () nil |
108 | ; (class pointer) |
109 | ; (id unsigned-int) |
110 | ; (parameter parameter)) |
111 | |
112 | ; (defbinding object-class-find-param-spec () parameter |
113 | ; (class pointer) |
114 | ; (name string)) |
115 | |
116 | (defun signal-name-to-string (name) |
117 | (substitute #\_ #\- (string-downcase (string name)))) |
118 | |
119 | |
120 | (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs) |
121 | (case (getf initargs :allocation) |
122 | (:property (find-class 'direct-property-slot-definition)) |
d4ddcf2c |
123 | (:user-data (find-class 'direct-user-data-slot-definition)) |
0c87a95a |
124 | (t (call-next-method)))) |
125 | |
126 | (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs) |
127 | (case (getf initargs :allocation) |
128 | (:property (find-class 'effective-property-slot-definition)) |
d4ddcf2c |
129 | (:user-data (find-class 'effective-user-data-slot-definition)) |
0c87a95a |
130 | (t (call-next-method)))) |
131 | |
132 | (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds) |
c23cc486 |
133 | (if (eq (slot-definition-allocation (first direct-slotds)) :property) |
0c87a95a |
134 | (nconc |
135 | (list :pname (signal-name-to-string |
d5dae790 |
136 | (most-specific-slot-value direct-slotds 'pname |
137 | (slot-definition-name (first direct-slotds)))) |
138 | :readable (most-specific-slot-value direct-slotds 'readable t) |
139 | :writable (most-specific-slot-value direct-slotds 'writable t) |
140 | :construct-only (most-specific-slot-value direct-slotds |
141 | 'construct-only nil)) |
0c87a95a |
142 | (call-next-method)) |
143 | (call-next-method))) |
144 | |
145 | |
d5dae790 |
146 | (defvar *ignore-setting-construct-only-property* nil) |
147 | (declaim (special *ignore-setting-construct-only-property*)) |
148 | |
0c87a95a |
149 | (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition)) |
dcb31db6 |
150 | (let ((type (slot-definition-type slotd)) |
151 | (pname (slot-definition-pname slotd))) |
64bce834 |
152 | (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd)) |
0c87a95a |
153 | (setf |
64bce834 |
154 | (slot-value slotd 'getter) |
dcb31db6 |
155 | (let ((reader nil)) |
64bce834 |
156 | #'(lambda (object) |
6556dccd |
157 | (unless reader |
158 | (setq reader (reader-function type))) |
dcb31db6 |
159 | (let ((gvalue (gvalue-new type))) |
64bce834 |
160 | (%object-get-property object pname gvalue) |
161 | (unwind-protect |
162 | (funcall reader gvalue +gvalue-value-offset+) |
163 | (gvalue-free gvalue t))))))) |
0c87a95a |
164 | |
d5dae790 |
165 | (when (not (slot-boundp slotd 'setter)) |
166 | (cond |
167 | ((slot-writable-p slotd) |
168 | (setf |
169 | (slot-value slotd 'setter) |
170 | (let ((writer nil)) |
171 | #'(lambda (value object) |
172 | (unless writer |
173 | (setq writer (writer-function type))) |
174 | (let ((gvalue (gvalue-new type))) |
175 | (funcall writer value gvalue +gvalue-value-offset+) |
176 | (%object-set-property object pname gvalue) |
177 | (gvalue-free gvalue t) |
178 | value))))) |
179 | |
180 | ((construct-only-property-p slotd) |
181 | (setf |
182 | (slot-value slotd 'setter) |
64bce834 |
183 | #'(lambda (value object) |
d5dae790 |
184 | (declare (ignore value object)) |
185 | (unless *ignore-setting-construct-only-property* |
186 | (error "Slot is not writable: ~A" (slot-definition-name slotd))))))))) |
64bce834 |
187 | |
0c87a95a |
188 | (call-next-method)) |
189 | |
d4ddcf2c |
190 | (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition)) |
191 | (let ((slot-name (slot-definition-name slotd))) |
74bad010 |
192 | (unless (slot-boundp slotd 'getter) |
193 | (setf |
194 | (slot-value slotd 'getter) |
195 | #'(lambda (object) |
196 | (prog1 (user-data object slot-name))))) |
197 | (unless (slot-boundp slotd 'setter) |
198 | (setf |
199 | (slot-value slotd 'setter) |
200 | #'(lambda (value object) |
201 | (setf (user-data object slot-name) value)))) |
202 | (unless (slot-boundp slotd 'boundp) |
203 | (setf |
204 | (slot-value slotd 'boundp) |
205 | #'(lambda (object) |
206 | (user-data-p object slot-name))))) |
d4ddcf2c |
207 | (call-next-method)) |
208 | |
67985e36 |
209 | (defmethod shared-initialize :after ((class gobject-class) names &rest initargs) |
210 | (declare (ignore initargs)) |
211 | (when (some #'(lambda (slotd) |
212 | (and |
213 | (eq (slot-definition-allocation slotd) :instance) |
82defe4d |
214 | (not (typep slotd 'effective-special-slot-definition)))) |
67985e36 |
215 | (class-slots class)) |
216 | (setf (slot-value class 'instance-slots-p) t))) |
217 | |
218 | |
0c87a95a |
219 | ;;;; Super class for all classes in the GObject type hierarchy |
220 | |
0d07716f |
221 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c9819f3e |
222 | (defclass gobject (ginstance) |
4c502835 |
223 | (#+debug-ref-counting |
224 | (ref-count :allocation :alien :type int :reader ref-count)) |
0c87a95a |
225 | (:metaclass gobject-class) |
dcb31db6 |
226 | (:gtype "GObject"))) |
6baf860c |
227 | |
4d1fea77 |
228 | (define-type-method callback-from-alien-form ((type gobject) form) |
229 | (from-alien-form type form)) |
230 | |
4c502835 |
231 | #+debug-ref-counting |
232 | (defmethod print-object ((instance gobject) stream) |
233 | (print-unreadable-object (instance stream :type t :identity nil) |
cf45719a |
234 | (if (proxy-valid-p instance) |
4c502835 |
235 | (format stream "at 0x~X (~D)" (sap-int (foreign-location instance)) (ref-count instance)) |
236 | (write-string "at \"unbound\"" stream)))) |
237 | |
64bce834 |
238 | |
239 | (defun initial-add (object function initargs key pkey) |
240 | (loop |
241 | as (initarg value . rest) = initargs then rest |
242 | do (cond |
243 | ((eq initarg key) (funcall function object value)) |
244 | ((eq initarg pkey) (mapc #'(lambda (value) |
245 | (funcall function object value)) |
246 | value))) |
247 | while rest)) |
248 | |
249 | (defun initial-apply-add (object function initargs key pkey) |
250 | (initial-add object #'(lambda (object value) |
251 | (apply function object (mklist value))) |
252 | initargs key pkey)) |
253 | |
254 | |
1d06a422 |
255 | (defmethod make-proxy-instance ((class gobject-class) location &rest initargs) |
256 | (declare (ignore location initargs)) |
257 | (if (slot-value class 'instance-slots-p) |
258 | (error "An object of class ~A has instance slots and should only be created with MAKE-INSTANCE" class) |
259 | (call-next-method))) |
260 | |
d5dae790 |
261 | |
262 | (defmethod allocate-foreign ((object gobject) &rest initargs) |
263 | (let ((init-slots ())) |
264 | (flet ((value-from-initargs (slotd) |
265 | (loop |
266 | with slot-initargs = (slot-definition-initargs slotd) |
267 | for (initarg value) on initargs by #'cddr |
268 | when (find initarg slot-initargs) |
269 | do (return (values value t))))) |
270 | |
271 | (loop |
272 | for slotd in (class-slots (class-of object)) |
273 | when (and |
274 | (eq (slot-definition-allocation slotd) :property) |
275 | (construct-only-property-p slotd)) |
276 | do (multiple-value-bind (value initarg-p) (value-from-initargs slotd) |
277 | (cond |
278 | (initarg-p (push (cons slotd value) init-slots)) |
279 | ((slot-definition-initfunction slotd) |
280 | (push |
281 | (cons slotd (funcall (slot-definition-initfunction slotd))) |
282 | init-slots)))))) |
283 | |
284 | (cond |
285 | (init-slots |
286 | (let ((element-size (+ +gvalue-size+ +size-of-pointer+)) |
287 | (num-slots (length init-slots))) |
288 | (with-allocated-memory (params (* num-slots element-size)) |
289 | (loop |
290 | with string-writer = (writer-function 'string) |
291 | for (slotd . value) in init-slots |
292 | as offset = params then (sap+ offset element-size) |
293 | as type = (slot-definition-type slotd) |
294 | as pname = (slot-definition-pname slotd) |
295 | do (funcall string-writer pname offset) |
296 | (gvalue-init (sap+ offset +size-of-pointer+) type value)) |
297 | |
298 | (unwind-protect |
299 | (%gobject-newv (type-number-of object) num-slots params) |
300 | |
301 | (loop |
302 | with string-destroy = (destroy-function 'string) |
303 | repeat num-slots |
304 | as offset = params then (sap+ offset element-size) |
305 | do (funcall string-destroy offset) |
306 | (gvalue-unset (sap+ offset +size-of-pointer+))))))) |
307 | |
308 | (t (%gobject-new (type-number-of object)))))) |
309 | |
310 | |
311 | (defmethod shared-initialize ((object gobject) names &rest initargs) |
312 | (declare (ignore names initargs)) |
313 | (let ((*ignore-setting-construct-only-property* t)) |
314 | (call-next-method))) |
315 | |
124eb32c |
316 | (defmethod initialize-instance :around ((object gobject) &rest initargs) |
317 | (declare (ignore initargs)) |
d5dae790 |
318 | (prog1 |
319 | (call-next-method) |
320 | #+debug-ref-counting(%object-weak-ref (foreign-location object)) |
321 | #+glib2.8 |
322 | (when (slot-value (class-of object) 'instance-slots-p) |
323 | (with-slots (location) object |
324 | (%object-add-toggle-ref location) |
325 | (%object-unref location))))) |
935a783c |
326 | |
327 | |
29933e83 |
328 | (defmethod instance-finalizer ((instance gobject)) |
7ce0497d |
329 | (let ((location (foreign-location instance))) |
d4665dd8 |
330 | #+glib2.8 |
67985e36 |
331 | (if (slot-value (class-of instance) 'instance-slots-p) |
332 | #'(lambda () |
124eb32c |
333 | #+debug-ref-counting |
334 | (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location)) |
67985e36 |
335 | (remove-cached-instance location) |
336 | (%object-remove-toggle-ref location)) |
337 | #'(lambda () |
124eb32c |
338 | #+debug-ref-counting |
339 | (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location)) |
67985e36 |
340 | (remove-cached-instance location) |
985e4aa3 |
341 | (%object-unref location))) |
d4665dd8 |
342 | #-glib2.8 |
985e4aa3 |
343 | #'(lambda () |
344 | (remove-cached-instance location) |
345 | (%object-unref location)))) |
29933e83 |
346 | |
6baf860c |
347 | |
3a935dfa |
348 | (defbinding (%gobject-new "g_object_new") () pointer |
b75a7b77 |
349 | (type type-number) |
350 | (nil null)) |
0d07716f |
351 | |
6baf860c |
352 | (defbinding (%gobject-newv "g_object_newv") () pointer |
935a783c |
353 | (type type-number) |
354 | (n-parameters unsigned-int) |
6baf860c |
355 | (params pointer)) |
9db52165 |
356 | |
357 | |
3a935dfa |
358 | |
383653f5 |
359 | ;;;; Property stuff |
0d07716f |
360 | |
c2cd6b3a |
361 | (defbinding %object-set-property () nil |
c9819f3e |
362 | (object gobject) |
363 | (name string) |
364 | (value gvalue)) |
a905f5d9 |
365 | |
c2cd6b3a |
366 | (defbinding %object-get-property () nil |
c9819f3e |
367 | (object gobject) |
368 | (name string) |
b75a7b77 |
369 | (value gvalue)) |
a905f5d9 |
370 | |
c2cd6b3a |
371 | (defbinding %object-notify () nil |
c9819f3e |
372 | (object gobject) |
373 | (name string)) |
a905f5d9 |
374 | |
c2cd6b3a |
375 | (defbinding object-freeze-notify () nil |
b75a7b77 |
376 | (object gobject)) |
a905f5d9 |
377 | |
c2cd6b3a |
378 | (defbinding object-thaw-notify () nil |
b75a7b77 |
379 | (object gobject)) |
a905f5d9 |
380 | |
850bf007 |
381 | |
382 | ;;;; User data |
383 | |
c2cd6b3a |
384 | (defbinding %object-set-qdata-full () nil |
a905f5d9 |
385 | (object gobject) |
386 | (id quark) |
387 | (data unsigned-long) |
a92553bd |
388 | (destroy-marshal callback)) |
a905f5d9 |
389 | |
a92553bd |
390 | (define-callback user-data-destroy-callback nil ((id unsigned-int)) |
3d36c5d6 |
391 | (destroy-user-data id)) |
392 | |
d4ddcf2c |
393 | (defun (setf user-data) (data object key) |
850bf007 |
394 | (%object-set-qdata-full object (quark-intern key) |
a92553bd |
395 | (register-user-data data) user-data-destroy-callback) |
a905f5d9 |
396 | data) |
397 | |
850bf007 |
398 | ;; deprecated |
d4ddcf2c |
399 | (defun (setf object-data) (data object key &key (test #'eq)) |
400 | (assert (eq test #'eq)) |
401 | (setf (user-data object key) data)) |
402 | |
c2cd6b3a |
403 | (defbinding %object-get-qdata () unsigned-long |
a905f5d9 |
404 | (object gobject) |
405 | (id quark)) |
406 | |
d4ddcf2c |
407 | (defun user-data (object key) |
850bf007 |
408 | (find-user-data (%object-get-qdata object (quark-intern key)))) |
d4ddcf2c |
409 | |
850bf007 |
410 | ;; deprecated |
a905f5d9 |
411 | (defun object-data (object key &key (test #'eq)) |
d4ddcf2c |
412 | (assert (eq test #'eq)) |
413 | (user-data object key)) |
414 | |
415 | (defun user-data-p (object key) |
850bf007 |
416 | (user-data-exists-p (%object-get-qdata object (quark-intern key)))) |
417 | |
418 | (defbinding %object-steal-qdata () unsigned-long |
419 | (object gobject) |
420 | (id quark)) |
421 | |
422 | (defun unset-user-data (object key) |
423 | (destroy-user-data (%object-steal-qdata object (quark-intern key)))) |
a905f5d9 |
424 | |
425 | |
3a935dfa |
426 | ;;;; |
427 | |
383653f5 |
428 | (defbinding %object-class-list-properties () pointer |
3a935dfa |
429 | (class pointer) |
430 | (n-properties unsigned-int :out)) |
431 | |
b7833c4c |
432 | |
433 | (defun %map-params (params length type inherited-p) |
434 | (if inherited-p |
6baf860c |
435 | (map-c-vector 'list #'identity params 'param length) |
b7833c4c |
436 | (let ((properties ())) |
6baf860c |
437 | (map-c-vector 'list |
b7833c4c |
438 | #'(lambda (param) |
439 | (when (eql (param-owner-type param) type) |
440 | (push param properties))) |
441 | params 'param length) |
442 | (nreverse properties)))) |
443 | |
444 | (defun query-object-class-properties (type &optional inherited-p) |
80031aba |
445 | (let* ((type-number (find-type-number type t)) |
b7833c4c |
446 | (class (type-class-ref type-number))) |
447 | (unwind-protect |
448 | (multiple-value-bind (array length) |
449 | (%object-class-list-properties class) |
6556dccd |
450 | (unless (null-pointer-p array) |
451 | (unwind-protect |
452 | (%map-params array length type-number inherited-p) |
453 | (deallocate-memory array)))) |
b7833c4c |
454 | ; (type-class-unref type-number) |
455 | ))) |
3a935dfa |
456 | |
457 | |
458 | (defun default-slot-name (name) |
459 | (intern (substitute #\- #\_ (string-upcase (string-upcase name))))) |
460 | |
461 | (defun default-slot-accessor (class-name slot-name type) |
462 | (intern |
463 | (format |
464 | nil "~A-~A~A" class-name slot-name |
935a783c |
465 | (if (eq type 'boolean) "-P" "")))) |
3a935dfa |
466 | |
383653f5 |
467 | |
3330adc9 |
468 | (defun slot-definition-from-property (class property &optional slot-name args) |
b7833c4c |
469 | (with-slots (name flags value-type documentation) property |
3330adc9 |
470 | (let* ((slot-name (or slot-name (default-slot-name name))) |
e9934f39 |
471 | (slot-type (or (getf args :type) (type-from-number value-type) 'pointer)) |
b7833c4c |
472 | (accessor (default-slot-accessor class slot-name slot-type))) |
473 | |
474 | `(,slot-name |
475 | :allocation :property :pname ,name |
64bce834 |
476 | |
80031aba |
477 | ,@(when (find :unbound args) (list :unbound (getf args :unbound))) |
478 | ,@(when (find :getter args) (list :getter (getf args :getter))) |
479 | ,@(when (find :setter args) (list :setter (getf args :setter))) |
b7833c4c |
480 | |
481 | ;; accessors |
482 | ,@(cond |
483 | ((and |
484 | (member :writable flags) (member :readable flags) |
485 | (not (member :construct-only flags))) |
486 | (list :accessor accessor)) |
487 | ((and (member :writable flags) (not (member :construct-only flags))) |
488 | (list :writer `(setf ,accessor))) |
489 | ((member :readable flags) |
490 | (list :reader accessor))) |
491 | |
492 | ;; readable/writable/construct |
493 | ,@(when (or (not (member :writable flags)) |
494 | (member :construct-only flags)) |
495 | '(:writable nil)) |
496 | ,@(when (not (member :readable flags)) |
497 | '(:readable nil)) |
d5dae790 |
498 | ,@(when (member :construct-only flags) |
499 | '(:construct-only t)) |
b7833c4c |
500 | |
501 | ;; initargs |
c7a44ea9 |
502 | ,@(if (find :initarg args) |
503 | (let ((initarg (getf args :initarg))) |
504 | (etypecase initarg |
505 | (null ()) |
506 | (symbol `(:initarg ,initarg)))) |
507 | (when (or (member :construct flags) |
508 | (member :construct-only flags) |
509 | (member :writable flags)) |
510 | (list :initarg (intern (string slot-name) "KEYWORD")))) |
b7833c4c |
511 | |
512 | :type ,slot-type |
513 | :documentation ,documentation)))) |
514 | |
515 | |
516 | (defun slot-definitions (class properties slots) |
517 | (loop |
b7833c4c |
518 | for property in properties |
64bce834 |
519 | as slot = (or |
520 | (find (param-name property) slots |
521 | :key #'(lambda (slot) (getf (rest slot) :pname)) |
522 | :test #'string=) |
523 | (find (param-name property) slots |
524 | :key #'first :test #'string-equal)) |
525 | do (cond |
526 | ((not slot) |
527 | (push (slot-definition-from-property class property) slots)) |
528 | ((getf (rest slot) :merge) |
529 | (setf |
530 | (rest slot) |
3330adc9 |
531 | (rest (slot-definition-from-property class property (first slot) (rest slot))))))) |
b7833c4c |
532 | (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots)) |
533 | |
534 | |
e9934f39 |
535 | (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class)) |
b7833c4c |
536 | (let ((supers (cons (supertype type) (implements type))) |
537 | (class (type-from-number type)) |
538 | (slots (getf options :slots))) |
539 | `(defclass ,class ,supers |
80031aba |
540 | ,(unless forward-p |
541 | (slot-definitions class (query-object-class-properties type) slots)) |
542 | (:metaclass ,metaclass) |
543 | (:gtype ,(register-type-as type))))) |
383653f5 |
544 | |
e9934f39 |
545 | (defun gobject-dependencies (type) |
6556dccd |
546 | (delete-duplicates |
547 | (cons |
548 | (supertype type) |
549 | (append |
550 | (type-interfaces type) |
551 | (mapcar #'param-value-type (query-object-class-properties type)))))) |
3a935dfa |
552 | |
e9934f39 |
553 | |
554 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies) |
7875d055 |
555 | |
556 | |
557 | ;;; Pseudo type for gobject instances which have their reference count |
558 | ;;; increased by the returning function |
559 | |
039c2fdb |
560 | (deftype referenced (type) type) |
7875d055 |
561 | |
4d1fea77 |
562 | (define-type-method alien-type ((type referenced)) |
7875d055 |
563 | (declare (ignore type)) |
4d1fea77 |
564 | (alien-type 'gobject)) |
565 | |
566 | (define-type-method from-alien-form ((type referenced) form) |
039c2fdb |
567 | (let ((class (second (type-expand-to 'referenced type)))) |
7875d055 |
568 | (if (subtypep type 'gobject) |
569 | (let ((instance (make-symbol "INSTANCE"))) |
039c2fdb |
570 | `(let ((,instance ,(from-alien-form class form))) |
7875d055 |
571 | (when ,instance |
7ce0497d |
572 | (%object-unref (foreign-location ,instance))) |
7875d055 |
573 | ,instance)) |
574 | (error "~A is not a subclass of GOBJECT" type)))) |
575 | |
576 | (export 'referenced) |