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.14 2004/10/28 09:34:35 espen Exp $
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass gobject (ginstance)
26 (:metaclass ginstance-class)
27 (:alien-name "GObject")
29 (:free %object-unref)))
32 (defmethod initialize-instance ((object gobject) &rest initargs)
33 (let ((slotds (class-slots (class-of object)))
34 (names (make-array 0 :adjustable t :fill-pointer t))
35 (values (make-array 0 :adjustable t :fill-pointer t)))
38 as tmp = initargs then (cddr tmp) while tmp
40 as value = (second tmp)
43 (member key (slot-definition-initargs slotd)))
45 when (and (typep slotd 'effective-gobject-slot-definition)
46 (slot-value slotd 'construct))
47 do (let ((type (find-type-number (slot-definition-type slotd))))
48 (vector-push-extend (slot-definition-pname slotd) names)
49 (vector-push-extend (gvalue-new type value) values)
53 (slot-value object 'location)
54 (if (zerop (length names))
55 (%gobject-new (type-number-of object))
56 (%gobject-newvv (type-number-of object) (length names) names values)))
58 (mapc #'gvalue-free values))
60 (apply #'call-next-method object initargs))
64 (defbinding (%gobject-new "g_object_new") () pointer
68 (defbinding (%gobject-newvv "g_object_newvv") () pointer
70 (n-parameters unsigned-int)
71 (names (vector string))
72 (values (vector gvalue)))
75 (defbinding %object-ref (type location) pointer
78 (defbinding %object-unref (type location) nil
81 (defun object-ref (object)
82 (%object-ref nil (proxy-location object)))
84 (defun object-unref (object)
85 (%object-unref nil (proxy-location object)))
91 (defbinding %object-set-property () nil
96 (defbinding %object-get-property () nil
101 (defbinding %object-notify () nil
105 (defbinding object-freeze-notify () nil
108 (defbinding object-thaw-notify () nil
111 (defbinding %object-set-qdata-full () nil
115 (destroy-marshal pointer))
120 (defun (setf object-data) (data object key &key (test #'eq))
121 (%object-set-qdata-full
122 object (quark-from-object key :test test)
123 (register-user-data data) *destroy-notify*)
126 (defbinding %object-get-qdata () unsigned-long
130 (defun object-data (object key &key (test #'eq))
132 (%object-get-qdata object (quark-from-object key :test test))))
136 ;;;; Metaclass used for subclasses of gobject
138 (eval-when (:compile-toplevel :load-toplevel :execute)
139 (defclass gobject-class (ginstance-class)
142 (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
143 ((pname :reader slot-definition-pname :initarg :pname)
144 (readable :initform t :reader slot-readable-p :initarg :readable)
145 (writable :initform t :reader slot-writable-p :initarg :writable)
146 (construct :initform nil :initarg :construct)))
148 (defclass effective-gobject-slot-definition (effective-virtual-slot-definition)
149 ((pname :reader slot-definition-pname :initarg :pname)
150 (readable :reader slot-readable-p :initarg :readable)
151 (writable :reader slot-writable-p :initarg :writable)
152 (construct :initarg :construct))))
156 ; (defbinding object-class-install-param () nil
159 ; (parameter parameter))
161 ; (defbinding object-class-find-param-spec () parameter
165 (defun signal-name-to-string (name)
166 (substitute #\_ #\- (string-downcase (string name))))
169 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
170 (case (getf initargs :allocation)
171 (:property (find-class 'direct-gobject-slot-definition))
172 (t (call-next-method))))
174 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
175 (case (getf initargs :allocation)
176 (:property (find-class 'effective-gobject-slot-definition))
177 (t (call-next-method))))
179 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
180 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
182 (list :pname (signal-name-to-string
183 (most-specific-slot-value direct-slotds 'pname))
184 :readable (most-specific-slot-value direct-slotds 'readable)
185 :writable (most-specific-slot-value direct-slotds 'writable)
186 :construct (most-specific-slot-value direct-slotds 'construct))
191 (defmethod initialize-internal-slot-functions ((slotd effective-gobject-slot-definition))
192 (let* ((type (slot-definition-type slotd))
193 (pname (slot-definition-pname slotd))
194 (type-number (find-type-number type)))
195 (unless (slot-boundp slotd 'reader-function)
197 (slot-value slotd 'reader-function)
198 (if (slot-readable-p slotd)
201 (let ((gvalue (gvalue-new type-number)))
202 (%object-get-property object pname gvalue)
205 (intern-reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+) ; temporary workaround for wrong topological sorting of types
206 (gvalue-free gvalue t)))))
207 #'(lambda (value object)
208 (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
210 (unless (slot-boundp slotd 'writer-function)
212 (slot-value slotd 'writer-function)
213 (if (slot-writable-p slotd)
214 #'(lambda (value object)
216 (let ((gvalue (gvalue-new type-number)))
218 (intern-writer-function (type-from-number type-number)) ; temporary
219 value gvalue +gvalue-value-offset+)
220 (%object-set-property object pname gvalue)
222 (intern-destroy-function (type-from-number type-number)) ; temporary
223 gvalue +gvalue-value-offset+)
224 (gvalue-free gvalue nil)
226 #'(lambda (value object)
227 (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
229 (unless (slot-boundp slotd 'boundp-function)
231 (slot-value slotd 'boundp-function)
233 (declare (ignore object))
238 (defmethod validate-superclass ((class gobject-class)
239 (super pcl::standard-class))
240 ; (subtypep (class-name super) 'gobject)
247 (defbinding %object-class-list-properties () pointer
249 (n-properties unsigned-int :out))
251 (defun query-object-class-properties (type-number &optional
252 inherited-properties)
253 (let ((class (type-class-ref type-number)))
254 (multiple-value-bind (array length)
255 (%object-class-list-properties class)
257 (let ((all-properties
258 (map-c-array 'list #'identity array 'param length)))
259 (if (not inherited-properties)
262 (not (eql type-number (param-owner-type param))))
265 (deallocate-memory array)))))
268 (defun default-slot-name (name)
269 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
271 (defun default-slot-accessor (class-name slot-name type)
274 nil "~A-~A~A" class-name slot-name
275 (if (eq type 'boolean) "-P" ""))))
277 (defun expand-gobject-type (type-number &optional options
278 (metaclass 'gobject-class))
279 (let* ((supers (cons (supertype type-number) (implements type-number)))
280 (class (type-from-number type-number))
281 (manual-slots (getf options :slots))
285 (with-slots (name flags value-type documentation) param
286 (let* ((slot-name (default-slot-name name))
287 ; (slot-type value-type) ;(type-from-number value-type t))
288 (slot-type (or (type-from-number value-type) value-type))
290 (default-slot-accessor class slot-name slot-type)));(type-from-number slot-type)))) ; temporary workaround for wrong topological sorting of types
293 :allocation :property
297 (member :writable flags)
298 (member :readable flags)
299 (not (member :construct-only flags)))
300 (list :accessor accessor))
301 ((and (member :writable flags)
302 (not (member :construct-only flags)))
303 (list :writer `(setf ,accessor)))
304 ((member :readable flags)
305 (list :reader accessor)))
307 (not (member :writable flags))
308 (member :construct-only flags))
309 (list :writable nil))
310 ,@(when (not (member :readable flags))
311 (list :readable nil))
313 (member :construct flags)
314 (member :construct-only flags))
317 (member :construct flags)
318 (member :construct-only flags)
319 (member :writable flags))
320 (list :initarg (intern (string slot-name) "KEYWORD")))
322 ,@(when documentation
323 (list :documentation documentation))))))
324 (query-object-class-properties type-number))))
326 (dolist (slot-def (reverse manual-slots))
327 (let ((name (car slot-def))
328 (pname (getf (cdr slot-def) :pname)))
332 #'(lambda (expanded-slot-def)
334 (eq name (car expanded-slot-def))
337 (string= pname (getf (cdr expanded-slot-def) :pname)))))
340 (unless (getf (cdr slot-def) :ignore)
341 (push slot-def expanded-slots))))
344 (defclass ,class ,supers
346 (:metaclass ,metaclass)
347 (:alien-name ,(find-type-name type-number))))))
350 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)