Updated for CMUCL 19a and glib-2.4
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
3 ;;
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.
8 ;;
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.
13 ;;
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
17
18 ;; $Id: gobject.lisp,v 1.13 2004-10-27 14:58:59 espen Exp $
19
20 (in-package "GLIB")
21
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass gobject (ginstance)
25 ()
26 (:metaclass ginstance-class)
27 (:alien-name "GObject")
28 (:copy %object-ref)
29 (:free %object-unref)))
30
31
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)))
36
37 (loop
38 as tmp = initargs then (cddr tmp) while tmp
39 as key = (first tmp)
40 as value = (second tmp)
41 as slotd = (find-if
42 #'(lambda (slotd)
43 (member key (slot-definition-initargs slotd)))
44 slotds)
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)
50 (remf initargs key)))
51
52 (setf
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))
58
59
60
61 (defbinding (%gobject-new "g_object_new") () pointer
62 (type type-number)
63 (nil null))
64
65 (defbinding (%gobject-newvv "g_object_newvv") () pointer
66 (type type-number)
67 (n-parameters unsigned-int)
68 (names (vector string))
69 (values (vector gvalue)))
70
71
72 (defbinding %object-ref (type location) pointer
73 (location pointer))
74
75 (defbinding %object-unref (type location) nil
76 (location pointer))
77
78 (defun object-ref (object)
79 (%object-ref nil (proxy-location object)))
80
81 (defun object-unref (object)
82 (%object-unref nil (proxy-location object)))
83
84
85
86 ;;;; Property stuff
87
88 (defbinding %object-set-property () nil
89 (object gobject)
90 (name string)
91 (value gvalue))
92
93 (defbinding %object-get-property () nil
94 (object gobject)
95 (name string)
96 (value gvalue))
97
98 (defbinding %object-notify () nil
99 (object gobject)
100 (name string))
101
102 (defbinding object-freeze-notify () nil
103 (object gobject))
104
105 (defbinding object-thaw-notify () nil
106 (object gobject))
107
108 (defbinding %object-set-qdata-full () nil
109 (object gobject)
110 (id quark)
111 (data unsigned-long)
112 (destroy-marshal pointer))
113
114
115 ;;;; User data
116
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*)
121 data)
122
123 (defbinding %object-get-qdata () unsigned-long
124 (object gobject)
125 (id quark))
126
127 (defun object-data (object key &key (test #'eq))
128 (find-user-data
129 (%object-get-qdata object (quark-from-object key :test test))))
130
131
132
133 ;;;; Metaclass used for subclasses of gobject
134
135 (eval-when (:compile-toplevel :load-toplevel :execute)
136 (defclass gobject-class (ginstance-class)
137 ())
138
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)))
144
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))))
150
151
152
153 ; (defbinding object-class-install-param () nil
154 ; (class pointer)
155 ; (id unsigned-int)
156 ; (parameter parameter))
157
158 ; (defbinding object-class-find-param-spec () parameter
159 ; (class pointer)
160 ; (name string))
161
162 (defun signal-name-to-string (name)
163 (substitute #\_ #\- (string-downcase (string name))))
164
165
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))))
170
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))))
175
176 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
177 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
178 (nconc
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))
184 (call-next-method))
185 (call-next-method)))
186
187
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)
193 (setf
194 (slot-value slotd 'reader-function)
195 (if (slot-readable-p slotd)
196 #'(lambda (object)
197 (with-gc-disabled
198 (let ((gvalue (gvalue-new type-number)))
199 (%object-get-property object pname gvalue)
200 (unwind-protect
201 (funcall
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))))))
206
207 (unless (slot-boundp slotd 'writer-function)
208 (setf
209 (slot-value slotd 'writer-function)
210 (if (slot-writable-p slotd)
211 #'(lambda (value object)
212 (with-gc-disabled
213 (let ((gvalue (gvalue-new type-number)))
214 (funcall
215 (intern-writer-function (type-from-number type-number)) ; temporary
216 value gvalue +gvalue-value-offset+)
217 (%object-set-property object pname gvalue)
218 (funcall
219 (intern-destroy-function (type-from-number type-number)) ; temporary
220 gvalue +gvalue-value-offset+)
221 (gvalue-free gvalue nil)
222 value)))
223 #'(lambda (value object)
224 (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
225
226 (unless (slot-boundp slotd 'boundp-function)
227 (setf
228 (slot-value slotd 'boundp-function)
229 #'(lambda (object)
230 (declare (ignore object))
231 t))))
232 (call-next-method))
233
234
235 (defmethod validate-superclass ((class gobject-class)
236 (super pcl::standard-class))
237 ; (subtypep (class-name super) 'gobject)
238 t)
239
240
241
242 ;;;;
243
244 (defbinding %object-class-list-properties () pointer
245 (class pointer)
246 (n-properties unsigned-int :out))
247
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)
253 (unwind-protect
254 (let ((all-properties
255 (map-c-array 'list #'identity array 'param length)))
256 (if (not inherited-properties)
257 (delete-if
258 #'(lambda (param)
259 (not (eql type-number (param-owner-type param))))
260 all-properties)
261 all-properties))
262 (deallocate-memory array)))))
263
264
265 (defun default-slot-name (name)
266 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
267
268 (defun default-slot-accessor (class-name slot-name type)
269 (intern
270 (format
271 nil "~A-~A~A" class-name slot-name
272 (if (eq type 'boolean) "-P" ""))))
273
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))
279 (expanded-slots
280 (mapcar
281 #'(lambda (param)
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))
286 (accessor
287 (default-slot-accessor class slot-name slot-type)));(type-from-number slot-type)))) ; temporary workaround for wrong topological sorting of types
288
289 `(,slot-name
290 :allocation :property
291 :pname ,name
292 ,@(cond
293 ((and
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)))
303 ,@(when (or
304 (not (member :writable flags))
305 (member :construct-only flags))
306 (list :writable nil))
307 ,@(when (not (member :readable flags))
308 (list :readable nil))
309 ,@(when (or
310 (member :construct flags)
311 (member :construct-only flags))
312 (list :construct t))
313 ,@(when (or
314 (member :construct flags)
315 (member :construct-only flags)
316 (member :writable flags))
317 (list :initarg (intern (string slot-name) "KEYWORD")))
318 :type ,slot-type
319 ,@(when documentation
320 (list :documentation documentation))))))
321 (query-object-class-properties type-number))))
322
323 (dolist (slot-def (reverse manual-slots))
324 (let ((name (car slot-def))
325 (pname (getf (cdr slot-def) :pname)))
326 (setq
327 expanded-slots
328 (delete-if
329 #'(lambda (expanded-slot-def)
330 (or
331 (eq name (car expanded-slot-def))
332 (and
333 pname
334 (string= pname (getf (cdr expanded-slot-def) :pname)))))
335 expanded-slots))
336
337 (unless (getf (cdr slot-def) :ignore)
338 (push slot-def expanded-slots))))
339
340 `(progn
341 (defclass ,class ,supers
342 ,expanded-slots
343 (:metaclass ,metaclass)
344 (:alien-name ,(find-type-name type-number))))))
345
346
347 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
348