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.17 2004/11/06 21:39:58 espen Exp $
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass gobject (ginstance)
26 (:metaclass ginstance-class)
27 (:alien-name "GObject")))
29 (defmethod print-object ((instance gobject) stream)
30 (print-unreadable-object (instance stream :type t :identity nil)
31 (if (slot-boundp instance 'location)
32 (format stream "at 0x~X" (sap-int (proxy-location instance)))
33 (write-string "(destroyed)" stream))))
36 (defmethod initialize-instance ((object gobject) &rest initargs)
37 ;; Extract initargs which we should pass directly to the GObeject
39 (let* ((slotds (class-slots (class-of object)))
41 as tmp = initargs then (cddr tmp) while tmp
43 as value = (second tmp)
46 (member key (slot-definition-initargs slotd)))
48 when (and (typep slotd 'effective-property-slot-definition)
49 (slot-value slotd 'construct))
53 (slot-definition-pname slotd)
54 (slot-definition-type slotd)
57 (let* ((string-size (size-of 'string))
58 (string-writer (writer-function 'string))
59 (string-destroy (destroy-function 'string))
60 (params (allocate-memory
61 (* (length args) (+ string-size +gvalue-size+)))))
63 for (pname type value) in args
64 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
65 do (funcall string-writer pname tmp)
66 (gvalue-init (sap+ tmp string-size) type value))
69 (slot-value object 'location)
70 (%gobject-newv (type-number-of object) (length args) params))
73 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
74 do (funcall string-destroy tmp)
75 (gvalue-unset (sap+ tmp string-size)))
76 (deallocate-memory params)))
78 (slot-value object 'location)
79 (%gobject-new (type-number-of object)))))
81 (%object-weak-ref object)
82 (apply #'call-next-method object initargs))
85 (defmethod initialize-instance :around ((object gobject) &rest initargs)
86 (declare (ignore initargs))
88 (%object-weak-ref object))
91 (def-callback weak-notify (c-call:void (data c-call:int) (location system-area-pointer))
92 (let ((object (find-cached-instance location)))
94 ;; (warn "~A being finalized by the GObject system while still in existence in lisp" object)
95 (slot-makunbound object 'location)
96 (remove-cached-instance location))))
98 (defbinding %object-weak-ref (object) nil
100 ((callback weak-notify) pointer)
103 (defbinding (%gobject-new "g_object_new") () pointer
107 (defbinding (%gobject-newv "g_object_newv") () pointer
109 (n-parameters unsigned-int)
116 (defbinding %object-set-property () nil
121 (defbinding %object-get-property () nil
126 (defbinding %object-notify () nil
130 (defbinding object-freeze-notify () nil
133 (defbinding object-thaw-notify () nil
136 (defbinding %object-set-qdata-full () nil
140 (destroy-marshal pointer))
145 (defun (setf object-data) (data object key &key (test #'eq))
146 (%object-set-qdata-full
147 object (quark-from-object key :test test)
148 (register-user-data data) (callback %destroy-user-data))
151 (defbinding %object-get-qdata () unsigned-long
155 (defun object-data (object key &key (test #'eq))
157 (%object-get-qdata object (quark-from-object key :test test))))
161 ;;;; Metaclass used for subclasses of gobject
163 ;(eval-when (:compile-toplevel :load-toplevel :execute)
164 (defclass gobject-class (ginstance-class)
167 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
168 ((pname :reader slot-definition-pname :initarg :pname)
169 (readable :initform t :reader slot-readable-p :initarg :readable)
170 (writable :initform t :reader slot-writable-p :initarg :writable)
171 (construct :initform nil :initarg :construct)))
173 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
174 ((pname :reader slot-definition-pname :initarg :pname)
175 (readable :reader slot-readable-p :initarg :readable)
176 (writable :reader slot-writable-p :initarg :writable)
177 (construct :initarg :construct)));)
179 (defbinding %object-ref () pointer
182 (defbinding %object-unref () nil
185 (defmethod reference-foreign ((class gobject-class) location)
186 (declare (ignore class))
187 (%object-ref location))
189 (defmethod unreference-foreign ((class gobject-class) location)
190 (declare (ignore class))
191 (%object-unref location))
194 ; (defbinding object-class-install-param () nil
197 ; (parameter parameter))
199 ; (defbinding object-class-find-param-spec () parameter
203 (defun signal-name-to-string (name)
204 (substitute #\_ #\- (string-downcase (string name))))
207 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
208 (case (getf initargs :allocation)
209 (:property (find-class 'direct-property-slot-definition))
210 (t (call-next-method))))
212 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
213 (case (getf initargs :allocation)
214 (:property (find-class 'effective-property-slot-definition))
215 (t (call-next-method))))
217 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
218 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
220 (list :pname (signal-name-to-string
221 (most-specific-slot-value direct-slotds 'pname))
222 :readable (most-specific-slot-value direct-slotds 'readable)
223 :writable (most-specific-slot-value direct-slotds 'writable)
224 :construct (most-specific-slot-value direct-slotds 'construct))
229 (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
230 (let* ((type (slot-definition-type slotd))
231 (pname (slot-definition-pname slotd))
232 (type-number (find-type-number type)))
233 (unless (slot-boundp slotd 'reader-function)
235 (slot-value slotd 'reader-function)
236 (if (slot-readable-p slotd)
237 (let () ;(reader (reader-function (type-from-number type-number))))
239 (let ((gvalue (gvalue-new type-number)))
240 (%object-get-property object pname gvalue)
242 (funcall #|reader|# (reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+)
243 (gvalue-free gvalue t)))))
244 #'(lambda (value object)
245 (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
247 (unless (slot-boundp slotd 'writer-function)
249 (slot-value slotd 'writer-function)
250 (if (slot-writable-p slotd)
251 (let ();; (writer (writer-function (type-from-number type-number)))
252 ;; (destroy (destroy-function (type-from-number type-number))))
253 #'(lambda (value object)
254 (let ((gvalue (gvalue-new type-number)))
255 (funcall #|writer|# (writer-function (type-from-number type-number)) value gvalue +gvalue-value-offset+)
256 (%object-set-property object pname gvalue)
257 ; (funcall #|destroy|#(destroy-function (type-from-number type-number)) gvalue +gvalue-value-offset+)
258 (gvalue-free gvalue t)
260 #'(lambda (value object)
261 (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
263 (unless (slot-boundp slotd 'boundp-function)
265 (slot-value slotd 'boundp-function)
267 (declare (ignore object))
272 (defmethod validate-superclass ((class gobject-class)
273 (super pcl::standard-class))
274 ; (subtypep (class-name super) 'gobject)
281 (defbinding %object-class-list-properties () pointer
283 (n-properties unsigned-int :out))
286 (defun %map-params (params length type inherited-p)
288 (map-c-vector 'list #'identity params 'param length)
289 (let ((properties ()))
292 (when (eql (param-owner-type param) type)
293 (push param properties)))
294 params 'param length)
295 (nreverse properties))))
297 (defun query-object-class-properties (type &optional inherited-p)
298 (let* ((type-number (find-type-number type))
299 (class (type-class-ref type-number)))
301 (multiple-value-bind (array length)
302 (%object-class-list-properties class)
304 (%map-params array length type-number inherited-p)
305 (deallocate-memory array)))
306 ; (type-class-unref type-number)
310 (defun default-slot-name (name)
311 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
313 (defun default-slot-accessor (class-name slot-name type)
316 nil "~A-~A~A" class-name slot-name
317 (if (eq type 'boolean) "-P" ""))))
320 (defun slot-definition-from-property (class property)
321 (with-slots (name flags value-type documentation) property
322 (let* ((slot-name (default-slot-name name))
323 (slot-type (or (type-from-number value-type) value-type))
324 (accessor (default-slot-accessor class slot-name slot-type)))
327 :allocation :property :pname ,name
332 (member :writable flags) (member :readable flags)
333 (not (member :construct-only flags)))
334 (list :accessor accessor))
335 ((and (member :writable flags) (not (member :construct-only flags)))
336 (list :writer `(setf ,accessor)))
337 ((member :readable flags)
338 (list :reader accessor)))
340 ;; readable/writable/construct
341 ,@(when (or (not (member :writable flags))
342 (member :construct-only flags))
344 ,@(when (not (member :readable flags))
346 ,@(when (or (member :construct flags)
347 (member :construct-only flags))
351 ,@(when (or (member :construct flags)
352 (member :construct-only flags)
353 (member :writable flags))
354 (list :initarg (intern (string slot-name) "KEYWORD")))
357 :documentation ,documentation))))
360 (defun slot-definitions (class properties slots)
362 with manual-slots = slots
363 for property in properties
366 (destructuring-bind (name &rest args) slot
368 (equal (param-name property) (getf args :pname))
369 (eq (default-slot-name (param-name property)) name))))
371 do (push (slot-definition-from-property class property) slots))
372 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
375 (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
376 (let ((supers (cons (supertype type) (implements type)))
377 (class (type-from-number type))
378 (slots (getf options :slots)))
379 `(defclass ,class ,supers
380 ,(slot-definitions class (query-object-class-properties type) slots)
381 (:metaclass ,metaclass)
382 (:alien-name ,(find-type-name type)))))
385 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)