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