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