1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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.25 2004/12/28 20:29:05 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)
30 (super pcl::standard-class))
31 ; (subtypep (class-name super) 'gobject)
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)))
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)
44 (construct :initarg :construct)));)
46 (defbinding %object-ref () pointer
49 (defbinding %object-unref () nil
52 (defmethod reference-foreign ((class gobject-class) location)
53 (declare (ignore class))
54 (%object-ref location))
56 (defmethod unreference-foreign ((class gobject-class) location)
57 (declare (ignore class))
58 (%object-unref location))
61 ; (defbinding object-class-install-param () nil
64 ; (parameter parameter))
66 ; (defbinding object-class-find-param-spec () parameter
70 (defun signal-name-to-string (name)
71 (substitute #\_ #\- (string-downcase (string name))))
74 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
75 (case (getf initargs :allocation)
76 (:property (find-class 'direct-property-slot-definition))
77 (t (call-next-method))))
79 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
80 (case (getf initargs :allocation)
81 (:property (find-class 'effective-property-slot-definition))
82 (t (call-next-method))))
84 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
85 (if (typep (first direct-slotds) 'direct-property-slot-definition)
87 (list :pname (signal-name-to-string
88 (most-specific-slot-value direct-slotds 'pname))
89 :readable (most-specific-slot-value direct-slotds 'readable)
90 :writable (most-specific-slot-value direct-slotds 'writable)
91 :construct (most-specific-slot-value direct-slotds 'construct))
96 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
97 (let* ((type (slot-definition-type slotd))
98 (pname (slot-definition-pname slotd))
99 (type-number (find-type-number type)))
100 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
102 (slot-value slotd 'getter)
106 (setq reader (reader-function type))) ;(type-from-number type-number))))
107 (let ((gvalue (gvalue-new type-number)))
108 (%object-get-property object pname gvalue)
110 (funcall reader gvalue +gvalue-value-offset+)
111 (gvalue-free gvalue t)))))))
113 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
115 (slot-value slotd 'setter)
117 #'(lambda (value object)
119 (setq writer (writer-function type))) ;(type-from-number type-number))))
120 (let ((gvalue (gvalue-new type-number)))
121 (funcall writer value gvalue +gvalue-value-offset+)
122 (%object-set-property object pname gvalue)
123 (gvalue-free gvalue t)
129 ;;;; Super class for all classes in the GObject type hierarchy
131 (eval-when (:compile-toplevel :load-toplevel :execute)
132 (defclass gobject (ginstance)
134 (:metaclass gobject-class)
135 (:alien-name "GObject")))
138 (defun initial-add (object function initargs key pkey)
140 as (initarg value . rest) = initargs then rest
142 ((eq initarg key) (funcall function object value))
143 ((eq initarg pkey) (mapc #'(lambda (value)
144 (funcall function object value))
148 (defun initial-apply-add (object function initargs key pkey)
149 (initial-add object #'(lambda (object value)
150 (apply function object (mklist value)))
154 (defmethod initialize-instance ((object gobject) &rest initargs)
155 (unless (slot-boundp object 'location)
156 ;; Extract initargs which we should pass directly to the GObeject
158 (let* ((slotds (class-slots (class-of object)))
161 as (key value . rest) = initargs then rest
164 (member key (slot-definition-initargs slotd)))
166 when (and (typep slotd 'effective-property-slot-definition)
167 (slot-value slotd 'construct))
171 (slot-definition-pname slotd)
172 (slot-definition-type slotd)
176 (let* ((string-size (size-of 'string))
177 (string-writer (writer-function 'string))
178 (string-destroy (destroy-function 'string))
179 (params (allocate-memory
180 (* (length args) (+ string-size +gvalue-size+)))))
182 for (pname type value) in args
183 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
184 do (funcall string-writer pname tmp)
185 (gvalue-init (sap+ tmp string-size) type value))
188 (slot-value object 'location)
189 (%gobject-newv (type-number-of object) (length args) params))
192 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
193 do (funcall string-destroy tmp)
194 (gvalue-unset (sap+ tmp string-size)))
195 (deallocate-memory params)))
197 (slot-value object 'location)
198 (%gobject-new (type-number-of object))))))
200 (apply #'call-next-method object initargs))
203 (defmethod instance-finalizer ((instance gobject))
204 (let ((location (proxy-location instance)))
206 (remove-cached-instance location)
207 (%object-unref location))))
210 (defbinding (%gobject-new "g_object_new") () pointer
214 (defbinding (%gobject-newv "g_object_newv") () pointer
216 (n-parameters unsigned-int)
223 (defbinding %object-set-property () nil
228 (defbinding %object-get-property () nil
233 (defbinding %object-notify () nil
237 (defbinding object-freeze-notify () nil
240 (defbinding object-thaw-notify () nil
243 (defbinding %object-set-qdata-full () nil
247 (destroy-marshal pointer))
252 (defun (setf object-data) (data object key &key (test #'eq))
253 (%object-set-qdata-full
254 object (quark-from-object key :test test)
255 (register-user-data data) (callback %destroy-user-data))
258 (defbinding %object-get-qdata () unsigned-long
262 (defun object-data (object key &key (test #'eq))
264 (%object-get-qdata object (quark-from-object key :test test))))
269 (defbinding %object-class-list-properties () pointer
271 (n-properties unsigned-int :out))
274 (defun %map-params (params length type inherited-p)
276 (map-c-vector 'list #'identity params 'param length)
277 (let ((properties ()))
280 (when (eql (param-owner-type param) type)
281 (push param properties)))
282 params 'param length)
283 (nreverse properties))))
285 (defun query-object-class-properties (type &optional inherited-p)
286 (let* ((type-number (find-type-number type))
287 (class (type-class-ref type-number)))
289 (multiple-value-bind (array length)
290 (%object-class-list-properties class)
292 (%map-params array length type-number inherited-p)
293 (deallocate-memory array)))
294 ; (type-class-unref type-number)
298 (defun default-slot-name (name)
299 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
301 (defun default-slot-accessor (class-name slot-name type)
304 nil "~A-~A~A" class-name slot-name
305 (if (eq type 'boolean) "-P" ""))))
308 (defun slot-definition-from-property (class property &optional args)
309 (with-slots (name flags value-type documentation) property
310 (let* ((slot-name (default-slot-name name))
311 (slot-type (or (getf args :type) (type-from-number value-type) value-type))
312 (accessor (default-slot-accessor class slot-name slot-type)))
315 :allocation :property :pname ,name
318 ((find :unbound args) (list :unbound (getf args :unbound))))
323 (member :writable flags) (member :readable flags)
324 (not (member :construct-only flags)))
325 (list :accessor accessor))
326 ((and (member :writable flags) (not (member :construct-only flags)))
327 (list :writer `(setf ,accessor)))
328 ((member :readable flags)
329 (list :reader accessor)))
331 ;; readable/writable/construct
332 ,@(when (or (not (member :writable flags))
333 (member :construct-only flags))
335 ,@(when (not (member :readable flags))
337 ,@(when (or (member :construct flags)
338 (member :construct-only flags))
342 ,@(when (or (member :construct flags)
343 (member :construct-only flags)
344 (member :writable flags))
345 (list :initarg (intern (string slot-name) "KEYWORD")))
348 :documentation ,documentation))))
351 (defun slot-definitions (class properties slots)
353 for property in properties
355 (find (param-name property) slots
356 :key #'(lambda (slot) (getf (rest slot) :pname))
358 (find (param-name property) slots
359 :key #'first :test #'string-equal))
362 (push (slot-definition-from-property class property) slots))
363 ((getf (rest slot) :merge)
366 (rest (slot-definition-from-property class property (rest slot)))))))
367 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
370 (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
371 (let ((supers (cons (supertype type) (implements type)))
372 (class (type-from-number type))
373 (slots (getf options :slots)))
374 `(defclass ,class ,supers
375 ,(slot-definitions class (query-object-class-properties type) slots)
376 (:metaclass ,metaclass)
377 (:alien-name ,(find-type-name type)))))
380 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
383 ;;; Pseudo type for gobject instances which have their reference count
384 ;;; increased by the returning function
386 (defmethod alien-type ((type (eql 'referenced)) &rest args)
387 (declare (ignore type args))
388 (alien-type 'gobject))
390 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
391 (declare (ignore type))
392 (destructuring-bind (type) args
393 (if (subtypep type 'gobject)
394 (let ((instance (make-symbol "INSTANCE")))
395 `(let ((,instance ,(from-alien-form form type)))
397 (%object-unref (proxy-location ,instance)))
399 (error "~A is not a subclass of GOBJECT" type))))