1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2005 Espen S. Johnsen <espen@users.sf.net>
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.
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.
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
18 ;; $Id: gobject.lisp,v 1.35 2005-03-11 10:56:58 espen Exp $
23 ;;;; Metaclass used for subclasses of gobject
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26 (defclass gobject-class (ginstance-class)
29 (defmethod validate-superclass ((class gobject-class) (super standard-class))
30 ; (subtypep (class-name super) 'gobject)
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)))
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)
43 (construct :initarg :construct)))
45 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
48 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
52 (defbinding %object-ref () pointer
55 (defbinding %object-unref () nil
58 (defmethod reference-foreign ((class gobject-class) location)
59 (declare (ignore class))
60 (%object-ref location))
62 (defmethod unreference-foreign ((class gobject-class) location)
63 (declare (ignore class))
64 (%object-unref location))
67 ; (defbinding object-class-install-param () nil
70 ; (parameter parameter))
72 ; (defbinding object-class-find-param-spec () parameter
76 (defun signal-name-to-string (name)
77 (substitute #\_ #\- (string-downcase (string name))))
80 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
81 (case (getf initargs :allocation)
82 (:property (find-class 'direct-property-slot-definition))
83 (:user-data (find-class 'direct-user-data-slot-definition))
84 (t (call-next-method))))
86 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
87 (case (getf initargs :allocation)
88 (:property (find-class 'effective-property-slot-definition))
89 (:user-data (find-class 'effective-user-data-slot-definition))
90 (t (call-next-method))))
92 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
93 (if (typep (first direct-slotds) 'direct-property-slot-definition)
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))
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 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
109 (slot-value slotd 'getter)
113 (setq reader (reader-function type)))
114 (let ((gvalue (gvalue-new type)))
115 (%object-get-property object pname gvalue)
117 (funcall reader gvalue +gvalue-value-offset+)
118 (gvalue-free gvalue t)))))))
120 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
122 (slot-value slotd 'setter)
124 #'(lambda (value object)
126 (setq writer (writer-function type)))
127 (let ((gvalue (gvalue-new type)))
128 (funcall writer value gvalue +gvalue-value-offset+)
129 (%object-set-property object pname gvalue)
130 (gvalue-free gvalue t)
135 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
136 (let ((slot-name (slot-definition-name slotd)))
137 (unless (slot-boundp slotd 'getter)
139 (slot-value slotd 'getter)
141 (prog1 (user-data object slot-name)))))
142 (unless (slot-boundp slotd 'setter)
144 (slot-value slotd 'setter)
145 #'(lambda (value object)
146 (setf (user-data object slot-name) value))))
147 (unless (slot-boundp slotd 'boundp)
149 (slot-value slotd 'boundp)
151 (user-data-p object slot-name)))))
155 ;;;; Super class for all classes in the GObject type hierarchy
157 (eval-when (:compile-toplevel :load-toplevel :execute)
158 (defclass gobject (ginstance)
160 (:metaclass gobject-class)
164 (defun initial-add (object function initargs key pkey)
166 as (initarg value . rest) = initargs then rest
168 ((eq initarg key) (funcall function object value))
169 ((eq initarg pkey) (mapc #'(lambda (value)
170 (funcall function object value))
174 (defun initial-apply-add (object function initargs key pkey)
175 (initial-add object #'(lambda (object value)
176 (apply function object (mklist value)))
180 (defmethod initialize-instance ((object gobject) &rest initargs)
181 (unless (slot-boundp object 'location)
182 ;; Extract initargs which we should pass directly to the GObeject
184 (let* ((slotds (class-slots (class-of object)))
187 as (key value . rest) = initargs then rest
190 (member key (slot-definition-initargs slotd)))
192 when (and (typep slotd 'effective-property-slot-definition)
193 (slot-value slotd 'construct))
197 (slot-definition-pname slotd)
198 (slot-definition-type slotd)
202 (let* ((string-size (size-of 'string))
203 (string-writer (writer-function 'string))
204 (string-destroy (destroy-function 'string))
205 (params (allocate-memory
206 (* (length args) (+ string-size +gvalue-size+)))))
208 for (pname type value) in args
209 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
210 do (funcall string-writer pname tmp)
211 (gvalue-init (sap+ tmp string-size) type value))
214 (slot-value object 'location)
215 (%gobject-newv (type-number-of object) (length args) params))
218 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
219 do (funcall string-destroy tmp)
220 (gvalue-unset (sap+ tmp string-size)))
221 (deallocate-memory params)))
223 (slot-value object 'location)
224 (%gobject-new (type-number-of object))))))
226 (apply #'call-next-method object initargs))
229 (defmethod instance-finalizer ((instance gobject))
230 (let ((location (proxy-location instance)))
232 (remove-cached-instance location)
233 (%object-unref location))))
236 (defbinding (%gobject-new "g_object_new") () pointer
240 (defbinding (%gobject-newv "g_object_newv") () pointer
242 (n-parameters unsigned-int)
249 (defbinding %object-set-property () nil
254 (defbinding %object-get-property () nil
259 (defbinding %object-notify () nil
263 (defbinding object-freeze-notify () nil
266 (defbinding object-thaw-notify () nil
272 (defbinding %object-set-qdata-full () nil
276 (destroy-marshal pointer))
278 (defcallback user-data-destroy-func (nil (id unsigned-int))
279 (destroy-user-data id))
281 (export 'user-data-destroy-func)
283 (defun (setf user-data) (data object key)
284 (%object-set-qdata-full object (quark-intern key)
285 (register-user-data data) (callback user-data-destroy-func))
289 (defun (setf object-data) (data object key &key (test #'eq))
290 (assert (eq test #'eq))
291 (setf (user-data object key) data))
293 (defbinding %object-get-qdata () unsigned-long
297 (defun user-data (object key)
298 (find-user-data (%object-get-qdata object (quark-intern key))))
301 (defun object-data (object key &key (test #'eq))
302 (assert (eq test #'eq))
303 (user-data object key))
305 (defun user-data-p (object key)
306 (user-data-exists-p (%object-get-qdata object (quark-intern key))))
308 (defbinding %object-steal-qdata () unsigned-long
312 (defun unset-user-data (object key)
313 (destroy-user-data (%object-steal-qdata object (quark-intern key))))
318 (defbinding %object-class-list-properties () pointer
320 (n-properties unsigned-int :out))
323 (defun %map-params (params length type inherited-p)
325 (map-c-vector 'list #'identity params 'param length)
326 (let ((properties ()))
329 (when (eql (param-owner-type param) type)
330 (push param properties)))
331 params 'param length)
332 (nreverse properties))))
334 (defun query-object-class-properties (type &optional inherited-p)
335 (let* ((type-number (find-type-number type t))
336 (class (type-class-ref type-number)))
338 (multiple-value-bind (array length)
339 (%object-class-list-properties class)
340 (unless (null-pointer-p array)
342 (%map-params array length type-number inherited-p)
343 (deallocate-memory array))))
344 ; (type-class-unref type-number)
348 (defun default-slot-name (name)
349 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
351 (defun default-slot-accessor (class-name slot-name type)
354 nil "~A-~A~A" class-name slot-name
355 (if (eq type 'boolean) "-P" ""))))
358 (defun slot-definition-from-property (class property &optional slot-name args)
359 (with-slots (name flags value-type documentation) property
360 (let* ((slot-name (or slot-name (default-slot-name name)))
361 (slot-type (or (getf args :type) (type-from-number value-type) 'pointer))
362 (accessor (default-slot-accessor class slot-name slot-type)))
365 :allocation :property :pname ,name
367 ,@(when (find :unbound args) (list :unbound (getf args :unbound)))
368 ,@(when (find :getter args) (list :getter (getf args :getter)))
369 ,@(when (find :setter args) (list :setter (getf args :setter)))
374 (member :writable flags) (member :readable flags)
375 (not (member :construct-only flags)))
376 (list :accessor accessor))
377 ((and (member :writable flags) (not (member :construct-only flags)))
378 (list :writer `(setf ,accessor)))
379 ((member :readable flags)
380 (list :reader accessor)))
382 ;; readable/writable/construct
383 ,@(when (or (not (member :writable flags))
384 (member :construct-only flags))
386 ,@(when (not (member :readable flags))
388 ,@(when (or (member :construct flags)
389 (member :construct-only flags))
393 ,@(if (find :initarg args)
394 (let ((initarg (getf args :initarg)))
397 (symbol `(:initarg ,initarg))))
398 (when (or (member :construct flags)
399 (member :construct-only flags)
400 (member :writable flags))
401 (list :initarg (intern (string slot-name) "KEYWORD"))))
404 :documentation ,documentation))))
407 (defun slot-definitions (class properties slots)
409 for property in properties
411 (find (param-name property) slots
412 :key #'(lambda (slot) (getf (rest slot) :pname))
414 (find (param-name property) slots
415 :key #'first :test #'string-equal))
418 (push (slot-definition-from-property class property) slots))
419 ((getf (rest slot) :merge)
422 (rest (slot-definition-from-property class property (first slot) (rest slot)))))))
423 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
426 (defun expand-gobject-type (type forward-p options &optional (metaclass 'gobject-class))
427 (let ((supers (cons (supertype type) (implements type)))
428 (class (type-from-number type))
429 (slots (getf options :slots)))
430 `(defclass ,class ,supers
432 (slot-definitions class (query-object-class-properties type) slots))
433 (:metaclass ,metaclass)
434 (:gtype ,(register-type-as type)))))
436 (defun gobject-dependencies (type)
441 (type-interfaces type)
442 (mapcar #'param-value-type (query-object-class-properties type))))))
445 (register-derivable-type 'gobject "GObject" 'expand-gobject-type 'gobject-dependencies)
448 ;;; Pseudo type for gobject instances which have their reference count
449 ;;; increased by the returning function
451 (defmethod alien-type ((type (eql 'referenced)) &rest args)
452 (declare (ignore type args))
453 (alien-type 'gobject))
455 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
456 (declare (ignore type))
457 (destructuring-bind (type) args
458 (if (subtypep type 'gobject)
459 (let ((instance (make-symbol "INSTANCE")))
460 `(let ((,instance ,(from-alien-form form type)))
462 (%object-unref (proxy-location ,instance)))
464 (error "~A is not a subclass of GOBJECT" type))))