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