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