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.26 2004-12-29 21:07:46 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 (defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
49 (defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
53 (defbinding %object-ref () pointer
56 (defbinding %object-unref () nil
59 (defmethod reference-foreign ((class gobject-class) location)
60 (declare (ignore class))
61 (%object-ref location))
63 (defmethod unreference-foreign ((class gobject-class) location)
64 (declare (ignore class))
65 (%object-unref location))
68 ; (defbinding object-class-install-param () nil
71 ; (parameter parameter))
73 ; (defbinding object-class-find-param-spec () parameter
77 (defun signal-name-to-string (name)
78 (substitute #\_ #\- (string-downcase (string name))))
81 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
82 (case (getf initargs :allocation)
83 (:property (find-class 'direct-property-slot-definition))
84 (:user-data (find-class 'direct-user-data-slot-definition))
85 (t (call-next-method))))
87 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
88 (case (getf initargs :allocation)
89 (:property (find-class 'effective-property-slot-definition))
90 (:user-data (find-class 'effective-user-data-slot-definition))
91 (t (call-next-method))))
93 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
94 (if (typep (first direct-slotds) 'direct-property-slot-definition)
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))
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)))
109 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
111 (slot-value slotd 'getter)
115 (setq reader (reader-function type))) ;(type-from-number type-number))))
116 (let ((gvalue (gvalue-new type-number)))
117 (%object-get-property object pname gvalue)
119 (funcall reader gvalue +gvalue-value-offset+)
120 (gvalue-free gvalue t)))))))
122 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
124 (slot-value slotd 'setter)
126 #'(lambda (value object)
128 (setq writer (writer-function type))) ;(type-from-number type-number))))
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)
137 (defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
138 (let ((slot-name (slot-definition-name slotd)))
140 (slot-value slotd 'getter)
142 (prog1 (user-data object slot-name))))
144 (slot-value slotd 'setter)
145 #'(lambda (value object)
146 (setf (user-data object slot-name) value)))
148 (slot-value slotd 'boundp)
150 (user-data-p object slot-name))))
154 ;;;; Super class for all classes in the GObject type hierarchy
156 (eval-when (:compile-toplevel :load-toplevel :execute)
157 (defclass gobject (ginstance)
159 (:metaclass gobject-class)
160 (:alien-name "GObject")))
163 (defun initial-add (object function initargs key pkey)
165 as (initarg value . rest) = initargs then rest
167 ((eq initarg key) (funcall function object value))
168 ((eq initarg pkey) (mapc #'(lambda (value)
169 (funcall function object value))
173 (defun initial-apply-add (object function initargs key pkey)
174 (initial-add object #'(lambda (object value)
175 (apply function object (mklist value)))
179 (defmethod initialize-instance ((object gobject) &rest initargs)
180 (unless (slot-boundp object 'location)
181 ;; Extract initargs which we should pass directly to the GObeject
183 (let* ((slotds (class-slots (class-of object)))
186 as (key value . rest) = initargs then rest
189 (member key (slot-definition-initargs slotd)))
191 when (and (typep slotd 'effective-property-slot-definition)
192 (slot-value slotd 'construct))
196 (slot-definition-pname slotd)
197 (slot-definition-type slotd)
201 (let* ((string-size (size-of 'string))
202 (string-writer (writer-function 'string))
203 (string-destroy (destroy-function 'string))
204 (params (allocate-memory
205 (* (length args) (+ string-size +gvalue-size+)))))
207 for (pname type value) in args
208 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
209 do (funcall string-writer pname tmp)
210 (gvalue-init (sap+ tmp string-size) type value))
213 (slot-value object 'location)
214 (%gobject-newv (type-number-of object) (length args) params))
217 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
218 do (funcall string-destroy tmp)
219 (gvalue-unset (sap+ tmp string-size)))
220 (deallocate-memory params)))
222 (slot-value object 'location)
223 (%gobject-new (type-number-of object))))))
225 (apply #'call-next-method object initargs))
228 (defmethod instance-finalizer ((instance gobject))
229 (let ((location (proxy-location instance)))
231 (remove-cached-instance location)
232 (%object-unref location))))
235 (defbinding (%gobject-new "g_object_new") () pointer
239 (defbinding (%gobject-newv "g_object_newv") () pointer
241 (n-parameters unsigned-int)
248 (defbinding %object-set-property () nil
253 (defbinding %object-get-property () nil
258 (defbinding %object-notify () nil
262 (defbinding object-freeze-notify () nil
265 (defbinding object-thaw-notify () nil
268 (defbinding %object-set-qdata-full () nil
272 (destroy-marshal pointer))
277 (defun (setf user-data) (data object key)
278 (%object-set-qdata-full
279 object (quark-from-object key)
280 (register-user-data data) (callback %destroy-user-data))
284 (defun (setf object-data) (data object key &key (test #'eq))
285 (assert (eq test #'eq))
286 (setf (user-data object key) data))
288 (defbinding %object-get-qdata () unsigned-long
292 (defun user-data (object key)
293 (find-user-data (%object-get-qdata object (quark-from-object key))))
296 (defun object-data (object key &key (test #'eq))
297 (assert (eq test #'eq))
298 (user-data object key))
300 (defun user-data-p (object key)
301 (nth-value 1 (find-user-data (%object-get-qdata object (quark-from-object key)))))
306 (defbinding %object-class-list-properties () pointer
308 (n-properties unsigned-int :out))
311 (defun %map-params (params length type inherited-p)
313 (map-c-vector 'list #'identity params 'param length)
314 (let ((properties ()))
317 (when (eql (param-owner-type param) type)
318 (push param properties)))
319 params 'param length)
320 (nreverse properties))))
322 (defun query-object-class-properties (type &optional inherited-p)
323 (let* ((type-number (find-type-number type))
324 (class (type-class-ref type-number)))
326 (multiple-value-bind (array length)
327 (%object-class-list-properties class)
329 (%map-params array length type-number inherited-p)
330 (deallocate-memory array)))
331 ; (type-class-unref type-number)
335 (defun default-slot-name (name)
336 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
338 (defun default-slot-accessor (class-name slot-name type)
341 nil "~A-~A~A" class-name slot-name
342 (if (eq type 'boolean) "-P" ""))))
345 (defun slot-definition-from-property (class property &optional args)
346 (with-slots (name flags value-type documentation) property
347 (let* ((slot-name (default-slot-name name))
348 (slot-type (or (getf args :type) (type-from-number value-type) value-type))
349 (accessor (default-slot-accessor class slot-name slot-type)))
352 :allocation :property :pname ,name
355 ((find :unbound args) (list :unbound (getf args :unbound))))
360 (member :writable flags) (member :readable flags)
361 (not (member :construct-only flags)))
362 (list :accessor accessor))
363 ((and (member :writable flags) (not (member :construct-only flags)))
364 (list :writer `(setf ,accessor)))
365 ((member :readable flags)
366 (list :reader accessor)))
368 ;; readable/writable/construct
369 ,@(when (or (not (member :writable flags))
370 (member :construct-only flags))
372 ,@(when (not (member :readable flags))
374 ,@(when (or (member :construct flags)
375 (member :construct-only flags))
379 ,@(when (or (member :construct flags)
380 (member :construct-only flags)
381 (member :writable flags))
382 (list :initarg (intern (string slot-name) "KEYWORD")))
385 :documentation ,documentation))))
388 (defun slot-definitions (class properties slots)
390 for property in properties
392 (find (param-name property) slots
393 :key #'(lambda (slot) (getf (rest slot) :pname))
395 (find (param-name property) slots
396 :key #'first :test #'string-equal))
399 (push (slot-definition-from-property class property) slots))
400 ((getf (rest slot) :merge)
403 (rest (slot-definition-from-property class property (rest slot)))))))
404 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
407 (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
408 (let ((supers (cons (supertype type) (implements type)))
409 (class (type-from-number type))
410 (slots (getf options :slots)))
411 `(defclass ,class ,supers
412 ,(slot-definitions class (query-object-class-properties type) slots)
413 (:metaclass ,metaclass)
414 (:alien-name ,(find-type-name type)))))
417 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
420 ;;; Pseudo type for gobject instances which have their reference count
421 ;;; increased by the returning function
423 (defmethod alien-type ((type (eql 'referenced)) &rest args)
424 (declare (ignore type args))
425 (alien-type 'gobject))
427 (defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
428 (declare (ignore type))
429 (destructuring-bind (type) args
430 (if (subtypep type 'gobject)
431 (let ((instance (make-symbol "INSTANCE")))
432 `(let ((,instance ,(from-alien-form form type)))
434 (%object-unref (proxy-location ,instance)))
436 (error "~A is not a subclass of GOBJECT" type))))