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.13 2004-10-27 14:58:59 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))))
57 (apply #'call-next-method object initargs))
61 (defbinding (%gobject-new "g_object_new") () pointer
65 (defbinding (%gobject-newvv "g_object_newvv") () pointer
67 (n-parameters unsigned-int)
68 (names (vector string))
69 (values (vector gvalue)))
72 (defbinding %object-ref (type location) pointer
75 (defbinding %object-unref (type location) nil
78 (defun object-ref (object)
79 (%object-ref nil (proxy-location object)))
81 (defun object-unref (object)
82 (%object-unref nil (proxy-location object)))
88 (defbinding %object-set-property () nil
93 (defbinding %object-get-property () nil
98 (defbinding %object-notify () nil
102 (defbinding object-freeze-notify () nil
105 (defbinding object-thaw-notify () nil
108 (defbinding %object-set-qdata-full () nil
112 (destroy-marshal pointer))
117 (defun (setf object-data) (data object key &key (test #'eq))
118 (%object-set-qdata-full
119 object (quark-from-object key :test test)
120 (register-user-data data) *destroy-notify*)
123 (defbinding %object-get-qdata () unsigned-long
127 (defun object-data (object key &key (test #'eq))
129 (%object-get-qdata object (quark-from-object key :test test))))
133 ;;;; Metaclass used for subclasses of gobject
135 (eval-when (:compile-toplevel :load-toplevel :execute)
136 (defclass gobject-class (ginstance-class)
139 (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
140 ((pname :reader slot-definition-pname :initarg :pname)
141 (readable :initform t :reader slot-readable-p :initarg :readable)
142 (writable :initform t :reader slot-writable-p :initarg :writable)
143 (construct :initform nil :initarg :construct)))
145 (defclass effective-gobject-slot-definition (effective-virtual-slot-definition)
146 ((pname :reader slot-definition-pname :initarg :pname)
147 (readable :reader slot-readable-p :initarg :readable)
148 (writable :reader slot-writable-p :initarg :writable)
149 (construct :initarg :construct))))
153 ; (defbinding object-class-install-param () nil
156 ; (parameter parameter))
158 ; (defbinding object-class-find-param-spec () parameter
162 (defun signal-name-to-string (name)
163 (substitute #\_ #\- (string-downcase (string name))))
166 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
167 (case (getf initargs :allocation)
168 (:property (find-class 'direct-gobject-slot-definition))
169 (t (call-next-method))))
171 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
172 (case (getf initargs :allocation)
173 (:property (find-class 'effective-gobject-slot-definition))
174 (t (call-next-method))))
176 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
177 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
179 (list :pname (signal-name-to-string
180 (most-specific-slot-value direct-slotds 'pname))
181 :readable (most-specific-slot-value direct-slotds 'readable)
182 :writable (most-specific-slot-value direct-slotds 'writable)
183 :construct (most-specific-slot-value direct-slotds 'construct))
188 (defmethod initialize-internal-slot-functions ((slotd effective-gobject-slot-definition))
189 (let* ((type (slot-definition-type slotd))
190 (pname (slot-definition-pname slotd))
191 (type-number (find-type-number type)))
192 (unless (slot-boundp slotd 'reader-function)
194 (slot-value slotd 'reader-function)
195 (if (slot-readable-p slotd)
198 (let ((gvalue (gvalue-new type-number)))
199 (%object-get-property object pname gvalue)
202 (intern-reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+) ; temporary workaround for wrong topological sorting of types
203 (gvalue-free gvalue t)))))
204 #'(lambda (value object)
205 (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
207 (unless (slot-boundp slotd 'writer-function)
209 (slot-value slotd 'writer-function)
210 (if (slot-writable-p slotd)
211 #'(lambda (value object)
213 (let ((gvalue (gvalue-new type-number)))
215 (intern-writer-function (type-from-number type-number)) ; temporary
216 value gvalue +gvalue-value-offset+)
217 (%object-set-property object pname gvalue)
219 (intern-destroy-function (type-from-number type-number)) ; temporary
220 gvalue +gvalue-value-offset+)
221 (gvalue-free gvalue nil)
223 #'(lambda (value object)
224 (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
226 (unless (slot-boundp slotd 'boundp-function)
228 (slot-value slotd 'boundp-function)
230 (declare (ignore object))
235 (defmethod validate-superclass ((class gobject-class)
236 (super pcl::standard-class))
237 ; (subtypep (class-name super) 'gobject)
244 (defbinding %object-class-list-properties () pointer
246 (n-properties unsigned-int :out))
248 (defun query-object-class-properties (type-number &optional
249 inherited-properties)
250 (let ((class (type-class-ref type-number)))
251 (multiple-value-bind (array length)
252 (%object-class-list-properties class)
254 (let ((all-properties
255 (map-c-array 'list #'identity array 'param length)))
256 (if (not inherited-properties)
259 (not (eql type-number (param-owner-type param))))
262 (deallocate-memory array)))))
265 (defun default-slot-name (name)
266 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
268 (defun default-slot-accessor (class-name slot-name type)
271 nil "~A-~A~A" class-name slot-name
272 (if (eq type 'boolean) "-P" ""))))
274 (defun expand-gobject-type (type-number &optional options
275 (metaclass 'gobject-class))
276 (let* ((supers (cons (supertype type-number) (implements type-number)))
277 (class (type-from-number type-number))
278 (manual-slots (getf options :slots))
282 (with-slots (name flags value-type documentation) param
283 (let* ((slot-name (default-slot-name name))
284 ; (slot-type value-type) ;(type-from-number value-type t))
285 (slot-type (or (type-from-number value-type) value-type))
287 (default-slot-accessor class slot-name slot-type)));(type-from-number slot-type)))) ; temporary workaround for wrong topological sorting of types
290 :allocation :property
294 (member :writable flags)
295 (member :readable flags)
296 (not (member :construct-only flags)))
297 (list :accessor accessor))
298 ((and (member :writable flags)
299 (not (member :construct-only flags)))
300 (list :writer `(setf ,accessor)))
301 ((member :readable flags)
302 (list :reader accessor)))
304 (not (member :writable flags))
305 (member :construct-only flags))
306 (list :writable nil))
307 ,@(when (not (member :readable flags))
308 (list :readable nil))
310 (member :construct flags)
311 (member :construct-only flags))
314 (member :construct flags)
315 (member :construct-only flags)
316 (member :writable flags))
317 (list :initarg (intern (string slot-name) "KEYWORD")))
319 ,@(when documentation
320 (list :documentation documentation))))))
321 (query-object-class-properties type-number))))
323 (dolist (slot-def (reverse manual-slots))
324 (let ((name (car slot-def))
325 (pname (getf (cdr slot-def) :pname)))
329 #'(lambda (expanded-slot-def)
331 (eq name (car expanded-slot-def))
334 (string= pname (getf (cdr expanded-slot-def) :pname)))))
337 (unless (getf (cdr slot-def) :ignore)
338 (push slot-def expanded-slots))))
341 (defclass ,class ,supers
343 (:metaclass ,metaclass)
344 (:alien-name ,(find-type-name type-number))))))
347 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)