112ac1d3 |
1 | ;; Common Lisp bindings for GTK+ v2.x |
2 | ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net> |
560af5c5 |
3 | ;; |
112ac1d3 |
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: |
560af5c5 |
11 | ;; |
112ac1d3 |
12 | ;; The above copyright notice and this permission notice shall be |
13 | ;; included in all copies or substantial portions of the Software. |
560af5c5 |
14 | ;; |
112ac1d3 |
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. |
560af5c5 |
22 | |
b036d261 |
23 | ;; $Id: gobject.lisp,v 1.37 2006-02-02 19:51:33 espen Exp $ |
560af5c5 |
24 | |
25 | (in-package "GLIB") |
26 | |
27 | |
2176a9c7 |
28 | ;;;; Metaclass used for subclasses of gobject |
29 | |
30 | (eval-when (:compile-toplevel :load-toplevel :execute) |
31 | (defclass gobject-class (ginstance-class) |
b036d261 |
32 | ((instance-slots-p :initform nil |
33 | :documentation "Non NIL if the class has slots with instance allocation"))) |
2176a9c7 |
34 | |
73572c12 |
35 | (defmethod validate-superclass ((class gobject-class) (super standard-class)) |
2176a9c7 |
36 | ; (subtypep (class-name super) 'gobject) |
37 | t)) |
38 | |
39 | (defclass direct-property-slot-definition (direct-virtual-slot-definition) |
40 | ((pname :reader slot-definition-pname :initarg :pname) |
41 | (readable :initform t :reader slot-readable-p :initarg :readable) |
42 | (writable :initform t :reader slot-writable-p :initarg :writable) |
43 | (construct :initform nil :initarg :construct))) |
44 | |
45 | (defclass effective-property-slot-definition (effective-virtual-slot-definition) |
46 | ((pname :reader slot-definition-pname :initarg :pname) |
47 | (readable :reader slot-readable-p :initarg :readable) |
48 | (writable :reader slot-writable-p :initarg :writable) |
174e8a5c |
49 | (construct :initarg :construct))) |
50 | |
51 | (defclass direct-user-data-slot-definition (direct-virtual-slot-definition) |
52 | ()) |
53 | |
54 | (defclass effective-user-data-slot-definition (effective-virtual-slot-definition) |
55 | ()) |
56 | |
2176a9c7 |
57 | |
58 | (defbinding %object-ref () pointer |
59 | (location pointer)) |
60 | |
61 | (defbinding %object-unref () nil |
62 | (location pointer)) |
63 | |
b036d261 |
64 | (defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean)) |
65 | (if last-ref-p |
66 | (cache-instance (find-cached-instance location) t) |
67 | (cache-instance (find-cached-instance location) nil))) |
68 | |
69 | (defbinding %object-add-toggle-ref () pointer |
70 | (location pointer) |
71 | ((callback toggle-ref-callback) pointer) |
72 | (nil null)) |
73 | |
74 | (defbinding %object-remove-toggle-ref () pointer |
75 | (location pointer) |
76 | ((callback toggle-ref-callback) pointer) |
77 | (nil null)) |
78 | |
2176a9c7 |
79 | (defmethod reference-foreign ((class gobject-class) location) |
80 | (declare (ignore class)) |
b036d261 |
81 | (if (slot-value class 'instance-slots-p) |
82 | (%object-add-toggle-ref location) |
83 | (%object-ref location))) |
2176a9c7 |
84 | |
85 | (defmethod unreference-foreign ((class gobject-class) location) |
86 | (declare (ignore class)) |
b036d261 |
87 | (error "Should never be called on a GOBJECT-CLASS (if this is ever needed some redesigning would have to be done)") |
88 | ; (%object-unref location) |
89 | ) |
90 | |
2176a9c7 |
91 | |
92 | |
93 | ; (defbinding object-class-install-param () nil |
94 | ; (class pointer) |
95 | ; (id unsigned-int) |
96 | ; (parameter parameter)) |
97 | |
98 | ; (defbinding object-class-find-param-spec () parameter |
99 | ; (class pointer) |
100 | ; (name string)) |
101 | |
102 | (defun signal-name-to-string (name) |
103 | (substitute #\_ #\- (string-downcase (string name)))) |
104 | |
105 | |
106 | (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs) |
107 | (case (getf initargs :allocation) |
108 | (:property (find-class 'direct-property-slot-definition)) |
174e8a5c |
109 | (:user-data (find-class 'direct-user-data-slot-definition)) |
2176a9c7 |
110 | (t (call-next-method)))) |
111 | |
112 | (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs) |
113 | (case (getf initargs :allocation) |
114 | (:property (find-class 'effective-property-slot-definition)) |
174e8a5c |
115 | (:user-data (find-class 'effective-user-data-slot-definition)) |
2176a9c7 |
116 | (t (call-next-method)))) |
117 | |
118 | (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds) |
eeda1c2d |
119 | (if (typep (first direct-slotds) 'direct-property-slot-definition) |
2176a9c7 |
120 | (nconc |
121 | (list :pname (signal-name-to-string |
122 | (most-specific-slot-value direct-slotds 'pname)) |
123 | :readable (most-specific-slot-value direct-slotds 'readable) |
124 | :writable (most-specific-slot-value direct-slotds 'writable) |
125 | :construct (most-specific-slot-value direct-slotds 'construct)) |
126 | (call-next-method)) |
127 | (call-next-method))) |
128 | |
129 | |
130 | (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition)) |
dfa4f314 |
131 | (let ((type (slot-definition-type slotd)) |
132 | (pname (slot-definition-pname slotd))) |
eeda1c2d |
133 | (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd)) |
2176a9c7 |
134 | (setf |
eeda1c2d |
135 | (slot-value slotd 'getter) |
dfa4f314 |
136 | (let ((reader nil)) |
eeda1c2d |
137 | #'(lambda (object) |
21299acf |
138 | (unless reader |
139 | (setq reader (reader-function type))) |
dfa4f314 |
140 | (let ((gvalue (gvalue-new type))) |
eeda1c2d |
141 | (%object-get-property object pname gvalue) |
142 | (unwind-protect |
143 | (funcall reader gvalue +gvalue-value-offset+) |
144 | (gvalue-free gvalue t))))))) |
2176a9c7 |
145 | |
eeda1c2d |
146 | (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd)) |
2176a9c7 |
147 | (setf |
eeda1c2d |
148 | (slot-value slotd 'setter) |
dfa4f314 |
149 | (let ((writer nil)) |
eeda1c2d |
150 | #'(lambda (value object) |
21299acf |
151 | (unless writer |
152 | (setq writer (writer-function type))) |
dfa4f314 |
153 | (let ((gvalue (gvalue-new type))) |
eeda1c2d |
154 | (funcall writer value gvalue +gvalue-value-offset+) |
155 | (%object-set-property object pname gvalue) |
156 | (gvalue-free gvalue t) |
157 | value)))))) |
158 | |
2176a9c7 |
159 | (call-next-method)) |
160 | |
174e8a5c |
161 | (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition)) |
162 | (let ((slot-name (slot-definition-name slotd))) |
56870989 |
163 | (unless (slot-boundp slotd 'getter) |
164 | (setf |
165 | (slot-value slotd 'getter) |
166 | #'(lambda (object) |
167 | (prog1 (user-data object slot-name))))) |
168 | (unless (slot-boundp slotd 'setter) |
169 | (setf |
170 | (slot-value slotd 'setter) |
171 | #'(lambda (value object) |
172 | (setf (user-data object slot-name) value)))) |
173 | (unless (slot-boundp slotd 'boundp) |
174 | (setf |
175 | (slot-value slotd 'boundp) |
176 | #'(lambda (object) |
177 | (user-data-p object slot-name))))) |
174e8a5c |
178 | (call-next-method)) |
179 | |
b036d261 |
180 | (defmethod shared-initialize :after ((class gobject-class) names &rest initargs) |
181 | (declare (ignore initargs)) |
182 | (when (some #'(lambda (slotd) |
183 | (and |
184 | (eq (slot-definition-allocation slotd) :instance) |
185 | (not (eq (slot-definition-name slotd) 'location)))) |
186 | (class-slots class)) |
187 | (setf (slot-value class 'instance-slots-p) t))) |
188 | |
189 | |
2176a9c7 |
190 | |
191 | ;;;; Super class for all classes in the GObject type hierarchy |
192 | |
560af5c5 |
193 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c8c48a4c |
194 | (defclass gobject (ginstance) |
560af5c5 |
195 | () |
2176a9c7 |
196 | (:metaclass gobject-class) |
dfa4f314 |
197 | (:gtype "GObject"))) |
9adccb27 |
198 | |
eeda1c2d |
199 | |
200 | (defun initial-add (object function initargs key pkey) |
201 | (loop |
202 | as (initarg value . rest) = initargs then rest |
203 | do (cond |
204 | ((eq initarg key) (funcall function object value)) |
205 | ((eq initarg pkey) (mapc #'(lambda (value) |
206 | (funcall function object value)) |
207 | value))) |
208 | while rest)) |
209 | |
210 | (defun initial-apply-add (object function initargs key pkey) |
211 | (initial-add object #'(lambda (object value) |
212 | (apply function object (mklist value))) |
213 | initargs key pkey)) |
214 | |
215 | |
82747bbd |
216 | (defmethod initialize-instance ((object gobject) &rest initargs) |
040579dd |
217 | (unless (slot-boundp object 'location) |
b036d261 |
218 | ;; Extract initargs which we should pass directly to the GObject |
040579dd |
219 | ;; constructor |
220 | (let* ((slotds (class-slots (class-of object))) |
221 | (args (when initargs |
222 | (loop |
223 | as (key value . rest) = initargs then rest |
224 | as slotd = (find-if |
225 | #'(lambda (slotd) |
226 | (member key (slot-definition-initargs slotd))) |
227 | slotds) |
228 | when (and (typep slotd 'effective-property-slot-definition) |
229 | (slot-value slotd 'construct)) |
230 | collect (progn |
231 | (remf initargs key) |
232 | (list |
233 | (slot-definition-pname slotd) |
234 | (slot-definition-type slotd) |
235 | value)) |
236 | while rest)))) |
237 | (if args |
238 | (let* ((string-size (size-of 'string)) |
239 | (string-writer (writer-function 'string)) |
240 | (string-destroy (destroy-function 'string)) |
241 | (params (allocate-memory |
242 | (* (length args) (+ string-size +gvalue-size+))))) |
9adccb27 |
243 | (loop |
040579dd |
244 | for (pname type value) in args |
9adccb27 |
245 | as tmp = params then (sap+ tmp (+ string-size +gvalue-size+)) |
040579dd |
246 | do (funcall string-writer pname tmp) |
247 | (gvalue-init (sap+ tmp string-size) type value)) |
248 | (unwind-protect |
249 | (setf |
250 | (slot-value object 'location) |
251 | (%gobject-newv (type-number-of object) (length args) params)) |
252 | (loop |
253 | repeat (length args) |
254 | as tmp = params then (sap+ tmp (+ string-size +gvalue-size+)) |
255 | do (funcall string-destroy tmp) |
256 | (gvalue-unset (sap+ tmp string-size))) |
257 | (deallocate-memory params))) |
8755b1a5 |
258 | (setf |
259 | (slot-value object 'location) |
040579dd |
260 | (%gobject-new (type-number-of object)))))) |
8755b1a5 |
261 | |
4d83a8a6 |
262 | (apply #'call-next-method object initargs)) |
263 | |
264 | |
1dbf4216 |
265 | (defmethod instance-finalizer ((instance gobject)) |
266 | (let ((location (proxy-location instance))) |
b036d261 |
267 | (if (slot-value (class-of instance) 'instance-slots-p) |
268 | #'(lambda () |
269 | (remove-cached-instance location) |
270 | (%object-remove-toggle-ref location)) |
271 | #'(lambda () |
272 | (remove-cached-instance location) |
273 | (%object-unref location))))) |
1dbf4216 |
274 | |
9adccb27 |
275 | |
d4b21b08 |
276 | (defbinding (%gobject-new "g_object_new") () pointer |
a9044181 |
277 | (type type-number) |
278 | (nil null)) |
560af5c5 |
279 | |
9adccb27 |
280 | (defbinding (%gobject-newv "g_object_newv") () pointer |
4d83a8a6 |
281 | (type type-number) |
282 | (n-parameters unsigned-int) |
9adccb27 |
283 | (params pointer)) |
de8516ca |
284 | |
285 | |
d4b21b08 |
286 | |
323d4265 |
287 | ;;;; Property stuff |
560af5c5 |
288 | |
26b133ed |
289 | (defbinding %object-set-property () nil |
c8c48a4c |
290 | (object gobject) |
291 | (name string) |
292 | (value gvalue)) |
86d9d6ab |
293 | |
26b133ed |
294 | (defbinding %object-get-property () nil |
c8c48a4c |
295 | (object gobject) |
296 | (name string) |
a9044181 |
297 | (value gvalue)) |
86d9d6ab |
298 | |
26b133ed |
299 | (defbinding %object-notify () nil |
c8c48a4c |
300 | (object gobject) |
301 | (name string)) |
86d9d6ab |
302 | |
26b133ed |
303 | (defbinding object-freeze-notify () nil |
a9044181 |
304 | (object gobject)) |
86d9d6ab |
305 | |
26b133ed |
306 | (defbinding object-thaw-notify () nil |
a9044181 |
307 | (object gobject)) |
86d9d6ab |
308 | |
a00ba56a |
309 | |
310 | ;;;; User data |
311 | |
26b133ed |
312 | (defbinding %object-set-qdata-full () nil |
86d9d6ab |
313 | (object gobject) |
314 | (id quark) |
315 | (data unsigned-long) |
316 | (destroy-marshal pointer)) |
317 | |
73572c12 |
318 | (defcallback user-data-destroy-func (nil (id unsigned-int)) |
319 | (destroy-user-data id)) |
320 | |
321 | (export 'user-data-destroy-func) |
322 | |
174e8a5c |
323 | (defun (setf user-data) (data object key) |
a00ba56a |
324 | (%object-set-qdata-full object (quark-intern key) |
73572c12 |
325 | (register-user-data data) (callback user-data-destroy-func)) |
86d9d6ab |
326 | data) |
327 | |
a00ba56a |
328 | ;; deprecated |
174e8a5c |
329 | (defun (setf object-data) (data object key &key (test #'eq)) |
330 | (assert (eq test #'eq)) |
331 | (setf (user-data object key) data)) |
332 | |
26b133ed |
333 | (defbinding %object-get-qdata () unsigned-long |
86d9d6ab |
334 | (object gobject) |
335 | (id quark)) |
336 | |
174e8a5c |
337 | (defun user-data (object key) |
a00ba56a |
338 | (find-user-data (%object-get-qdata object (quark-intern key)))) |
174e8a5c |
339 | |
a00ba56a |
340 | ;; deprecated |
86d9d6ab |
341 | (defun object-data (object key &key (test #'eq)) |
174e8a5c |
342 | (assert (eq test #'eq)) |
343 | (user-data object key)) |
344 | |
345 | (defun user-data-p (object key) |
a00ba56a |
346 | (user-data-exists-p (%object-get-qdata object (quark-intern key)))) |
347 | |
348 | (defbinding %object-steal-qdata () unsigned-long |
349 | (object gobject) |
350 | (id quark)) |
351 | |
352 | (defun unset-user-data (object key) |
353 | (destroy-user-data (%object-steal-qdata object (quark-intern key)))) |
86d9d6ab |
354 | |
355 | |
d4b21b08 |
356 | ;;;; |
357 | |
323d4265 |
358 | (defbinding %object-class-list-properties () pointer |
d4b21b08 |
359 | (class pointer) |
360 | (n-properties unsigned-int :out)) |
361 | |
9c03a07c |
362 | |
363 | (defun %map-params (params length type inherited-p) |
364 | (if inherited-p |
9adccb27 |
365 | (map-c-vector 'list #'identity params 'param length) |
9c03a07c |
366 | (let ((properties ())) |
9adccb27 |
367 | (map-c-vector 'list |
9c03a07c |
368 | #'(lambda (param) |
369 | (when (eql (param-owner-type param) type) |
370 | (push param properties))) |
371 | params 'param length) |
372 | (nreverse properties)))) |
373 | |
374 | (defun query-object-class-properties (type &optional inherited-p) |
735a29da |
375 | (let* ((type-number (find-type-number type t)) |
9c03a07c |
376 | (class (type-class-ref type-number))) |
377 | (unwind-protect |
378 | (multiple-value-bind (array length) |
379 | (%object-class-list-properties class) |
21299acf |
380 | (unless (null-pointer-p array) |
381 | (unwind-protect |
382 | (%map-params array length type-number inherited-p) |
383 | (deallocate-memory array)))) |
9c03a07c |
384 | ; (type-class-unref type-number) |
385 | ))) |
d4b21b08 |
386 | |
387 | |
388 | (defun default-slot-name (name) |
389 | (intern (substitute #\- #\_ (string-upcase (string-upcase name))))) |
390 | |
391 | (defun default-slot-accessor (class-name slot-name type) |
392 | (intern |
393 | (format |
394 | nil "~A-~A~A" class-name slot-name |
4d83a8a6 |
395 | (if (eq type 'boolean) "-P" "")))) |
d4b21b08 |
396 | |
323d4265 |
397 | |
646466b0 |
398 | (defun slot-definition-from-property (class property &optional slot-name args) |
9c03a07c |
399 | (with-slots (name flags value-type documentation) property |
646466b0 |
400 | (let* ((slot-name (or slot-name (default-slot-name name))) |
62f12808 |
401 | (slot-type (or (getf args :type) (type-from-number value-type) 'pointer)) |
9c03a07c |
402 | (accessor (default-slot-accessor class slot-name slot-type))) |
403 | |
404 | `(,slot-name |
405 | :allocation :property :pname ,name |
eeda1c2d |
406 | |
735a29da |
407 | ,@(when (find :unbound args) (list :unbound (getf args :unbound))) |
408 | ,@(when (find :getter args) (list :getter (getf args :getter))) |
409 | ,@(when (find :setter args) (list :setter (getf args :setter))) |
9c03a07c |
410 | |
411 | ;; accessors |
412 | ,@(cond |
413 | ((and |
414 | (member :writable flags) (member :readable flags) |
415 | (not (member :construct-only flags))) |
416 | (list :accessor accessor)) |
417 | ((and (member :writable flags) (not (member :construct-only flags))) |
418 | (list :writer `(setf ,accessor))) |
419 | ((member :readable flags) |
420 | (list :reader accessor))) |
421 | |
422 | ;; readable/writable/construct |
423 | ,@(when (or (not (member :writable flags)) |
424 | (member :construct-only flags)) |
425 | '(:writable nil)) |
426 | ,@(when (not (member :readable flags)) |
427 | '(:readable nil)) |
428 | ,@(when (or (member :construct flags) |
429 | (member :construct-only flags)) |
430 | '(:construct t)) |
431 | |
432 | ;; initargs |
e5c7586b |
433 | ,@(if (find :initarg args) |
434 | (let ((initarg (getf args :initarg))) |
435 | (etypecase initarg |
436 | (null ()) |
437 | (symbol `(:initarg ,initarg)))) |
438 | (when (or (member :construct flags) |
439 | (member :construct-only flags) |
440 | (member :writable flags)) |
441 | (list :initarg (intern (string slot-name) "KEYWORD")))) |
9c03a07c |
442 | |
443 | :type ,slot-type |
444 | :documentation ,documentation)))) |
445 | |
446 | |
447 | (defun slot-definitions (class properties slots) |
448 | (loop |
9c03a07c |
449 | for property in properties |
eeda1c2d |
450 | as slot = (or |
451 | (find (param-name property) slots |
452 | :key #'(lambda (slot) (getf (rest slot) :pname)) |
453 | :test #'string=) |
454 | (find (param-name property) slots |
455 | :key #'first :test #'string-equal)) |
456 | do (cond |
457 | ((not slot) |
458 | (push (slot-definition-from-property class property) slots)) |
459 | ((getf (rest slot) :merge) |
460 | (setf |
461 | (rest slot) |
646466b0 |
462 | (rest (slot-definition-from-property class property (first slot) (rest slot))))))) |
9c03a07c |
463 | (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots)) |
464 | |
465 | |
62f12808 |
466 | (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class)) |
9c03a07c |
467 | (let ((supers (cons (supertype type) (implements type))) |
468 | (class (type-from-number type)) |
469 | (slots (getf options :slots))) |
470 | `(defclass ,class ,supers |
735a29da |
471 | ,(unless forward-p |
472 | (slot-definitions class (query-object-class-properties type) slots)) |
473 | (:metaclass ,metaclass) |
474 | (:gtype ,(register-type-as type))))) |
323d4265 |
475 | |
62f12808 |
476 | (defun gobject-dependencies (type) |
21299acf |
477 | (delete-duplicates |
478 | (cons |
479 | (supertype type) |
480 | (append |
481 | (type-interfaces type) |
482 | (mapcar #'param-value-type (query-object-class-properties type)))))) |
d4b21b08 |
483 | |
62f12808 |
484 | |
485 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies) |
ae37d096 |
486 | |
487 | |
488 | ;;; Pseudo type for gobject instances which have their reference count |
489 | ;;; increased by the returning function |
490 | |
491 | (defmethod alien-type ((type (eql 'referenced)) &rest args) |
492 | (declare (ignore type args)) |
493 | (alien-type 'gobject)) |
494 | |
495 | (defmethod from-alien-form (form (type (eql 'referenced)) &rest args) |
496 | (declare (ignore type)) |
497 | (destructuring-bind (type) args |
498 | (if (subtypep type 'gobject) |
499 | (let ((instance (make-symbol "INSTANCE"))) |
500 | `(let ((,instance ,(from-alien-form form type))) |
501 | (when ,instance |
502 | (%object-unref (proxy-location ,instance))) |
503 | ,instance)) |
504 | (error "~A is not a subclass of GOBJECT" type)))) |
505 | |
506 | (export 'referenced) |