1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
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:
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
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.
23 ;; $Id: gobject.lisp,v 1.36 2005-04-23 16:48:51 espen Exp $
28 ;;;; Metaclass used for subclasses of gobject
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31 (defclass gobject-class (ginstance-class)
34 (defmethod validate-superclass ((class gobject-class) (super standard-class))
35 ; (subtypep (class-name super) 'gobject)
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)))
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)
48 (construct :initarg :construct)))
50 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
53 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
57 (defbinding %object-ref () pointer
60 (defbinding %object-unref () nil
63 (defmethod reference-foreign ((class gobject-class) location)
64 (declare (ignore class))
65 (%object-ref location))
67 (defmethod unreference-foreign ((class gobject-class) location)
68 (declare (ignore class))
69 (%object-unref location))
72 ; (defbinding object-class-install-param () nil
75 ; (parameter parameter))
77 ; (defbinding object-class-find-param-spec () parameter
81 (defun signal-name-to-string (name)
82 (substitute #\_ #\- (string-downcase (string name))))
85 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
86 (case (getf initargs :allocation)
87 (:property (find-class 'direct-property-slot-definition))
88 (:user-data (find-class 'direct-user-data-slot-definition))
89 (t (call-next-method))))
91 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
92 (case (getf initargs :allocation)
93 (:property (find-class 'effective-property-slot-definition))
94 (:user-data (find-class 'effective-user-data-slot-definition))
95 (t (call-next-method))))
97 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
98 (if (typep (first direct-slotds) 'direct-property-slot-definition)
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))
109 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
110 (let ((type (slot-definition-type slotd))
111 (pname (slot-definition-pname slotd)))
112 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
114 (slot-value slotd 'getter)
118 (setq reader (reader-function type)))
119 (let ((gvalue (gvalue-new type)))
120 (%object-get-property object pname gvalue)
122 (funcall reader gvalue +gvalue-value-offset+)
123 (gvalue-free gvalue t)))))))
125 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
127 (slot-value slotd 'setter)
129 #'(lambda (value object)
131 (setq writer (writer-function type)))
132 (let ((gvalue (gvalue-new type)))
133 (funcall writer value gvalue +gvalue-value-offset+)
134 (%object-set-property object pname gvalue)
135 (gvalue-free gvalue t)
140 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
141 (let ((slot-name (slot-definition-name slotd)))
142 (unless (slot-boundp slotd 'getter)
144 (slot-value slotd 'getter)
146 (prog1 (user-data object slot-name)))))
147 (unless (slot-boundp slotd 'setter)
149 (slot-value slotd 'setter)
150 #'(lambda (value object)
151 (setf (user-data object slot-name) value))))
152 (unless (slot-boundp slotd 'boundp)
154 (slot-value slotd 'boundp)
156 (user-data-p object slot-name)))))
160 ;;;; Super class for all classes in the GObject type hierarchy
162 (eval-when (:compile-toplevel :load-toplevel :execute)
163 (defclass gobject (ginstance)
165 (:metaclass gobject-class)
169 (defun initial-add (object function initargs key pkey)
171 as (initarg value . rest) = initargs then rest
173 ((eq initarg key) (funcall function object value))
174 ((eq initarg pkey) (mapc #'(lambda (value)
175 (funcall function object value))
179 (defun initial-apply-add (object function initargs key pkey)
180 (initial-add object #'(lambda (object value)
181 (apply function object (mklist value)))
185 (defmethod initialize-instance ((object gobject) &rest initargs)
186 (unless (slot-boundp object 'location)
187 ;; Extract initargs which we should pass directly to the GObeject
189 (let* ((slotds (class-slots (class-of object)))
192 as (key value . rest) = initargs then rest
195 (member key (slot-definition-initargs slotd)))
197 when (and (typep slotd 'effective-property-slot-definition)
198 (slot-value slotd 'construct))
202 (slot-definition-pname slotd)
203 (slot-definition-type slotd)
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+)))))
213 for (pname type value) in args
214 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
215 do (funcall string-writer pname tmp)
216 (gvalue-init (sap+ tmp string-size) type value))
219 (slot-value object 'location)
220 (%gobject-newv (type-number-of object) (length args) params))
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)))
228 (slot-value object 'location)
229 (%gobject-new (type-number-of object))))))
231 (apply #'call-next-method object initargs))
234 (defmethod instance-finalizer ((instance gobject))
235 (let ((location (proxy-location instance)))
237 (remove-cached-instance location)
238 (%object-unref location))))
241 (defbinding (%gobject-new "g_object_new") () pointer
245 (defbinding (%gobject-newv "g_object_newv") () pointer
247 (n-parameters unsigned-int)
254 (defbinding %object-set-property () nil
259 (defbinding %object-get-property () nil
264 (defbinding %object-notify () nil
268 (defbinding object-freeze-notify () nil
271 (defbinding object-thaw-notify () nil
277 (defbinding %object-set-qdata-full () nil
281 (destroy-marshal pointer))
283 (defcallback user-data-destroy-func (nil (id unsigned-int))
284 (destroy-user-data id))
286 (export 'user-data-destroy-func)
288 (defun (setf user-data) (data object key)
289 (%object-set-qdata-full object (quark-intern key)
290 (register-user-data data) (callback user-data-destroy-func))
294 (defun (setf object-data) (data object key &key (test #'eq))
295 (assert (eq test #'eq))
296 (setf (user-data object key) data))
298 (defbinding %object-get-qdata () unsigned-long
302 (defun user-data (object key)
303 (find-user-data (%object-get-qdata object (quark-intern key))))
306 (defun object-data (object key &key (test #'eq))
307 (assert (eq test #'eq))
308 (user-data object key))
310 (defun user-data-p (object key)
311 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
313 (defbinding %object-steal-qdata () unsigned-long
317 (defun unset-user-data (object key)
318 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
323 (defbinding %object-class-list-properties () pointer
325 (n-properties unsigned-int :out))
328 (defun %map-params (params length type inherited-p)
330 (map-c-vector 'list #'identity params 'param length)
331 (let ((properties ()))
334 (when (eql (param-owner-type param) type)
335 (push param properties)))
336 params 'param length)
337 (nreverse properties))))
339 (defun query-object-class-properties (type &optional inherited-p)
340 (let* ((type-number (find-type-number type t))
341 (class (type-class-ref type-number)))
343 (multiple-value-bind (array length)
344 (%object-class-list-properties class)
345 (unless (null-pointer-p array)
347 (%map-params array length type-number inherited-p)
348 (deallocate-memory array))))
349 ; (type-class-unref type-number)
353 (defun default-slot-name (name)
354 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
356 (defun default-slot-accessor (class-name slot-name type)
359 nil "~A-~A~A" class-name slot-name
360 (if (eq type 'boolean) "-P" ""))))
363 (defun slot-definition-from-property (class property &optional slot-name args)
364 (with-slots (name flags value-type documentation) property
365 (let* ((slot-name (or slot-name (default-slot-name name)))
366 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
367 (accessor (default-slot-accessor class slot-name slot-type)))
370 :allocation :property :pname ,name
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)))
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)))
387 ;; readable/writable/construct
388 ,@(when (or (not (member :writable flags))
389 (member :construct-only flags))
391 ,@(when (not (member :readable flags))
393 ,@(when (or (member :construct flags)
394 (member :construct-only flags))
398 ,@(if (find :initarg args)
399 (let ((initarg (getf args :initarg)))
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"))))
409 :documentation ,documentation))))
412 (defun slot-definitions (class properties slots)
414 for property in properties
416 (find (param-name property) slots
417 :key #'(lambda (slot) (getf (rest slot) :pname))
419 (find (param-name property) slots
420 :key #'first :test #'string-equal))
423 (push (slot-definition-from-property class property) slots))
424 ((getf (rest slot) :merge)
427 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
428 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
431 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
432 (let ((supers (cons (supertype type) (implements type)))
433 (class (type-from-number type))
434 (slots (getf options :slots)))
435 `(defclass ,class ,supers
437 (slot-definitions class (query-object-class-properties type) slots))
438 (:metaclass ,metaclass)
439 (:gtype ,(register-type-as type)))))
441 (defun gobject-dependencies (type)
446 (type-interfaces type)
447 (mapcar #'param-value-type (query-object-class-properties type))))))
450 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
453 ;;; Pseudo type for gobject instances which have their reference count
454 ;;; increased by the returning function
456 (defmethod alien-type ((type (eql 'referenced)) &rest args)
457 (declare (ignore type args))
458 (alien-type 'gobject))
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)))
467 (%object-unref (proxy-location ,instance)))
469 (error "~A is not a subclass of GOBJECT" type))))