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 | |
55212af1 |
23 | ;; $Id: gobject.lisp,v 1.36 2005/04/23 16:48:51 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) |
31 | (defclass gobject-class (ginstance-class) |
32 | ()) |
33 | |
3d36c5d6 |
34 | (defmethod validate-superclass ((class gobject-class) (super standard-class)) |
0c87a95a |
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) |
d4ddcf2c |
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 | |
0c87a95a |
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)) |
d4ddcf2c |
88 | (:user-data (find-class 'direct-user-data-slot-definition)) |
0c87a95a |
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)) |
d4ddcf2c |
94 | (:user-data (find-class 'effective-user-data-slot-definition)) |
0c87a95a |
95 | (t (call-next-method)))) |
96 | |
97 | (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds) |
64bce834 |
98 | (if (typep (first direct-slotds) 'direct-property-slot-definition) |
0c87a95a |
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)) |
dcb31db6 |
110 | (let ((type (slot-definition-type slotd)) |
111 | (pname (slot-definition-pname slotd))) |
64bce834 |
112 | (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd)) |
0c87a95a |
113 | (setf |
64bce834 |
114 | (slot-value slotd 'getter) |
dcb31db6 |
115 | (let ((reader nil)) |
64bce834 |
116 | #'(lambda (object) |
6556dccd |
117 | (unless reader |
118 | (setq reader (reader-function type))) |
dcb31db6 |
119 | (let ((gvalue (gvalue-new type))) |
64bce834 |
120 | (%object-get-property object pname gvalue) |
121 | (unwind-protect |
122 | (funcall reader gvalue +gvalue-value-offset+) |
123 | (gvalue-free gvalue t))))))) |
0c87a95a |
124 | |
64bce834 |
125 | (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd)) |
0c87a95a |
126 | (setf |
64bce834 |
127 | (slot-value slotd 'setter) |
dcb31db6 |
128 | (let ((writer nil)) |
64bce834 |
129 | #'(lambda (value object) |
6556dccd |
130 | (unless writer |
131 | (setq writer (writer-function type))) |
dcb31db6 |
132 | (let ((gvalue (gvalue-new type))) |
64bce834 |
133 | (funcall writer value gvalue +gvalue-value-offset+) |
134 | (%object-set-property object pname gvalue) |
135 | (gvalue-free gvalue t) |
136 | value)))))) |
137 | |
0c87a95a |
138 | (call-next-method)) |
139 | |
d4ddcf2c |
140 | (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition)) |
141 | (let ((slot-name (slot-definition-name slotd))) |
74bad010 |
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))))) |
d4ddcf2c |
157 | (call-next-method)) |
158 | |
0c87a95a |
159 | |
160 | ;;;; Super class for all classes in the GObject type hierarchy |
161 | |
0d07716f |
162 | (eval-when (:compile-toplevel :load-toplevel :execute) |
c9819f3e |
163 | (defclass gobject (ginstance) |
0d07716f |
164 | () |
0c87a95a |
165 | (:metaclass gobject-class) |
dcb31db6 |
166 | (:gtype "GObject"))) |
6baf860c |
167 | |
64bce834 |
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 | |
ca0a8e30 |
185 | (defmethod initialize-instance ((object gobject) &rest initargs) |
22a2e918 |
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+))))) |
6baf860c |
212 | (loop |
22a2e918 |
213 | for (pname type value) in args |
6baf860c |
214 | as tmp = params then (sap+ tmp (+ string-size +gvalue-size+)) |
22a2e918 |
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))) |
7bde5a67 |
227 | (setf |
228 | (slot-value object 'location) |
22a2e918 |
229 | (%gobject-new (type-number-of object)))))) |
7bde5a67 |
230 | |
935a783c |
231 | (apply #'call-next-method object initargs)) |
232 | |
233 | |
29933e83 |
234 | (defmethod instance-finalizer ((instance gobject)) |
235 | (let ((location (proxy-location instance))) |
236 | #'(lambda () |
237 | (remove-cached-instance location) |
29933e83 |
238 | (%object-unref location)))) |
239 | |
6baf860c |
240 | |
3a935dfa |
241 | (defbinding (%gobject-new "g_object_new") () pointer |
b75a7b77 |
242 | (type type-number) |
243 | (nil null)) |
0d07716f |
244 | |
6baf860c |
245 | (defbinding (%gobject-newv "g_object_newv") () pointer |
935a783c |
246 | (type type-number) |
247 | (n-parameters unsigned-int) |
6baf860c |
248 | (params pointer)) |
9db52165 |
249 | |
250 | |
3a935dfa |
251 | |
383653f5 |
252 | ;;;; Property stuff |
0d07716f |
253 | |
c2cd6b3a |
254 | (defbinding %object-set-property () nil |
c9819f3e |
255 | (object gobject) |
256 | (name string) |
257 | (value gvalue)) |
a905f5d9 |
258 | |
c2cd6b3a |
259 | (defbinding %object-get-property () nil |
c9819f3e |
260 | (object gobject) |
261 | (name string) |
b75a7b77 |
262 | (value gvalue)) |
a905f5d9 |
263 | |
c2cd6b3a |
264 | (defbinding %object-notify () nil |
c9819f3e |
265 | (object gobject) |
266 | (name string)) |
a905f5d9 |
267 | |
c2cd6b3a |
268 | (defbinding object-freeze-notify () nil |
b75a7b77 |
269 | (object gobject)) |
a905f5d9 |
270 | |
c2cd6b3a |
271 | (defbinding object-thaw-notify () nil |
b75a7b77 |
272 | (object gobject)) |
a905f5d9 |
273 | |
850bf007 |
274 | |
275 | ;;;; User data |
276 | |
c2cd6b3a |
277 | (defbinding %object-set-qdata-full () nil |
a905f5d9 |
278 | (object gobject) |
279 | (id quark) |
280 | (data unsigned-long) |
281 | (destroy-marshal pointer)) |
282 | |
3d36c5d6 |
283 | (defcallback user-data-destroy-func (nil (id unsigned-int)) |
284 | (destroy-user-data id)) |
285 | |
286 | (export 'user-data-destroy-func) |
287 | |
d4ddcf2c |
288 | (defun (setf user-data) (data object key) |
850bf007 |
289 | (%object-set-qdata-full object (quark-intern key) |
3d36c5d6 |
290 | (register-user-data data) (callback user-data-destroy-func)) |
a905f5d9 |
291 | data) |
292 | |
850bf007 |
293 | ;; deprecated |
d4ddcf2c |
294 | (defun (setf object-data) (data object key &key (test #'eq)) |
295 | (assert (eq test #'eq)) |
296 | (setf (user-data object key) data)) |
297 | |
c2cd6b3a |
298 | (defbinding %object-get-qdata () unsigned-long |
a905f5d9 |
299 | (object gobject) |
300 | (id quark)) |
301 | |
d4ddcf2c |
302 | (defun user-data (object key) |
850bf007 |
303 | (find-user-data (%object-get-qdata object (quark-intern key)))) |
d4ddcf2c |
304 | |
850bf007 |
305 | ;; deprecated |
a905f5d9 |
306 | (defun object-data (object key &key (test #'eq)) |
d4ddcf2c |
307 | (assert (eq test #'eq)) |
308 | (user-data object key)) |
309 | |
310 | (defun user-data-p (object key) |
850bf007 |
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)))) |
a905f5d9 |
319 | |
320 | |
3a935dfa |
321 | ;;;; |
322 | |
383653f5 |
323 | (defbinding %object-class-list-properties () pointer |
3a935dfa |
324 | (class pointer) |
325 | (n-properties unsigned-int :out)) |
326 | |
b7833c4c |
327 | |
328 | (defun %map-params (params length type inherited-p) |
329 | (if inherited-p |
6baf860c |
330 | (map-c-vector 'list #'identity params 'param length) |
b7833c4c |
331 | (let ((properties ())) |
6baf860c |
332 | (map-c-vector 'list |
b7833c4c |
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) |
80031aba |
340 | (let* ((type-number (find-type-number type t)) |
b7833c4c |
341 | (class (type-class-ref type-number))) |
342 | (unwind-protect |
343 | (multiple-value-bind (array length) |
344 | (%object-class-list-properties class) |
6556dccd |
345 | (unless (null-pointer-p array) |
346 | (unwind-protect |
347 | (%map-params array length type-number inherited-p) |
348 | (deallocate-memory array)))) |
b7833c4c |
349 | ; (type-class-unref type-number) |
350 | ))) |
3a935dfa |
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 |
935a783c |
360 | (if (eq type 'boolean) "-P" "")))) |
3a935dfa |
361 | |
383653f5 |
362 | |
3330adc9 |
363 | (defun slot-definition-from-property (class property &optional slot-name args) |
b7833c4c |
364 | (with-slots (name flags value-type documentation) property |
3330adc9 |
365 | (let* ((slot-name (or slot-name (default-slot-name name))) |
e9934f39 |
366 | (slot-type (or (getf args :type) (type-from-number value-type) 'pointer)) |
b7833c4c |
367 | (accessor (default-slot-accessor class slot-name slot-type))) |
368 | |
369 | `(,slot-name |
370 | :allocation :property :pname ,name |
64bce834 |
371 | |
80031aba |
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))) |
b7833c4c |
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 |
c7a44ea9 |
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")))) |
b7833c4c |
407 | |
408 | :type ,slot-type |
409 | :documentation ,documentation)))) |
410 | |
411 | |
412 | (defun slot-definitions (class properties slots) |
413 | (loop |
b7833c4c |
414 | for property in properties |
64bce834 |
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) |
3330adc9 |
427 | (rest (slot-definition-from-property class property (first slot) (rest slot))))))) |
b7833c4c |
428 | (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots)) |
429 | |
430 | |
e9934f39 |
431 | (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class)) |
b7833c4c |
432 | (let ((supers (cons (supertype type) (implements type))) |
433 | (class (type-from-number type)) |
434 | (slots (getf options :slots))) |
435 | `(defclass ,class ,supers |
80031aba |
436 | ,(unless forward-p |
437 | (slot-definitions class (query-object-class-properties type) slots)) |
438 | (:metaclass ,metaclass) |
439 | (:gtype ,(register-type-as type))))) |
383653f5 |
440 | |
e9934f39 |
441 | (defun gobject-dependencies (type) |
6556dccd |
442 | (delete-duplicates |
443 | (cons |
444 | (supertype type) |
445 | (append |
446 | (type-interfaces type) |
447 | (mapcar #'param-value-type (query-object-class-properties type)))))) |
3a935dfa |
448 | |
e9934f39 |
449 | |
450 | (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies) |
7875d055 |
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) |