chiark / gitweb /
Callbacks from C done properly
[clg] / glib / gobject.lisp
CommitLineData
560af5c5 1;; Common Lisp bindings for GTK+ v2.0
82747bbd 2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
560af5c5 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
9c03a07c 18;; $Id: gobject.lisp,v 1.15 2004-10-31 00:56:29 espen Exp $
560af5c5 19
20(in-package "GLIB")
21
22
23(eval-when (:compile-toplevel :load-toplevel :execute)
c8c48a4c 24 (defclass gobject (ginstance)
560af5c5 25 ()
c8c48a4c 26 (:metaclass ginstance-class)
82747bbd 27 (:alien-name "GObject")
de8516ca 28 (:copy %object-ref)
29 (:free %object-unref)))
560af5c5 30
4d83a8a6 31
82747bbd 32(defmethod initialize-instance ((object gobject) &rest initargs)
4d83a8a6 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)
9c03a07c 45 when (and (typep slotd 'effective-property-slot-definition)
4d83a8a6 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))
a4b3ec58 56 (%gobject-newvv (type-number-of object) (length names) names values)))
57
9c03a07c 58; (map 'nil #'gvalue-free values)
59 )
a4b3ec58 60
4d83a8a6 61 (apply #'call-next-method object initargs))
62
63
a9044181 64
d4b21b08 65(defbinding (%gobject-new "g_object_new") () pointer
a9044181 66 (type type-number)
67 (nil null))
560af5c5 68
4d83a8a6 69(defbinding (%gobject-newvv "g_object_newvv") () pointer
70 (type type-number)
71 (n-parameters unsigned-int)
72 (names (vector string))
73 (values (vector gvalue)))
560af5c5 74
de8516ca 75
4d83a8a6 76(defbinding %object-ref (type location) pointer
de8516ca 77 (location pointer))
78
4d83a8a6 79 (defbinding %object-unref (type location) nil
80 (location pointer))
de8516ca 81
82(defun object-ref (object)
83 (%object-ref nil (proxy-location object)))
84
85(defun object-unref (object)
86 (%object-unref nil (proxy-location object)))
87
88
d4b21b08 89
323d4265 90;;;; Property stuff
560af5c5 91
26b133ed 92(defbinding %object-set-property () nil
c8c48a4c 93 (object gobject)
94 (name string)
95 (value gvalue))
86d9d6ab 96
26b133ed 97(defbinding %object-get-property () nil
c8c48a4c 98 (object gobject)
99 (name string)
a9044181 100 (value gvalue))
86d9d6ab 101
26b133ed 102(defbinding %object-notify () nil
c8c48a4c 103 (object gobject)
104 (name string))
86d9d6ab 105
26b133ed 106(defbinding object-freeze-notify () nil
a9044181 107 (object gobject))
86d9d6ab 108
26b133ed 109(defbinding object-thaw-notify () nil
a9044181 110 (object gobject))
86d9d6ab 111
26b133ed 112(defbinding %object-set-qdata-full () nil
86d9d6ab 113 (object gobject)
114 (id quark)
115 (data unsigned-long)
116 (destroy-marshal pointer))
117
a9044181 118
119;;;; User data
120
86d9d6ab 121(defun (setf object-data) (data object key &key (test #'eq))
122 (%object-set-qdata-full
123 object (quark-from-object key :test test)
c8c48a4c 124 (register-user-data data) *destroy-notify*)
86d9d6ab 125 data)
126
26b133ed 127(defbinding %object-get-qdata () unsigned-long
86d9d6ab 128 (object gobject)
129 (id quark))
130
131(defun object-data (object key &key (test #'eq))
132 (find-user-data
133 (%object-get-qdata object (quark-from-object key :test test))))
134
135
560af5c5 136
a9044181 137;;;; Metaclass used for subclasses of gobject
138
139(eval-when (:compile-toplevel :load-toplevel :execute)
4d83a8a6 140 (defclass gobject-class (ginstance-class)
141 ())
c8c48a4c 142
9c03a07c 143 (defclass direct-property-slot-definition (direct-virtual-slot-definition)
4d83a8a6 144 ((pname :reader slot-definition-pname :initarg :pname)
145 (readable :initform t :reader slot-readable-p :initarg :readable)
146 (writable :initform t :reader slot-writable-p :initarg :writable)
147 (construct :initform nil :initarg :construct)))
148
9c03a07c 149 (defclass effective-property-slot-definition (effective-virtual-slot-definition)
4d83a8a6 150 ((pname :reader slot-definition-pname :initarg :pname)
151 (readable :reader slot-readable-p :initarg :readable)
152 (writable :reader slot-writable-p :initarg :writable)
153 (construct :initarg :construct))))
c8c48a4c 154
155
560af5c5 156
26b133ed 157; (defbinding object-class-install-param () nil
560af5c5 158; (class pointer)
159; (id unsigned-int)
160; (parameter parameter))
161
26b133ed 162; (defbinding object-class-find-param-spec () parameter
560af5c5 163; (class pointer)
164; (name string))
165
de8516ca 166(defun signal-name-to-string (name)
167 (substitute #\_ #\- (string-downcase (string name))))
a9044181 168
a9044181 169
4d83a8a6 170(defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
a9044181 171 (case (getf initargs :allocation)
9c03a07c 172 (:property (find-class 'direct-property-slot-definition))
a9044181 173 (t (call-next-method))))
174
4d83a8a6 175(defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
a9044181 176 (case (getf initargs :allocation)
9c03a07c 177 (:property (find-class 'effective-property-slot-definition))
a9044181 178 (t (call-next-method))))
179
4d83a8a6 180(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
181 (if (eq (most-specific-slot-value direct-slotds 'allocation) :property)
182 (nconc
183 (list :pname (signal-name-to-string
184 (most-specific-slot-value direct-slotds 'pname))
185 :readable (most-specific-slot-value direct-slotds 'readable)
186 :writable (most-specific-slot-value direct-slotds 'writable)
187 :construct (most-specific-slot-value direct-slotds 'construct))
188 (call-next-method))
189 (call-next-method)))
190
191
9c03a07c 192(defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
4d83a8a6 193 (let* ((type (slot-definition-type slotd))
194 (pname (slot-definition-pname slotd))
195 (type-number (find-type-number type)))
196 (unless (slot-boundp slotd 'reader-function)
197 (setf
198 (slot-value slotd 'reader-function)
199 (if (slot-readable-p slotd)
200 #'(lambda (object)
201 (with-gc-disabled
202 (let ((gvalue (gvalue-new type-number)))
203 (%object-get-property object pname gvalue)
204 (unwind-protect
205 (funcall
206 (intern-reader-function (type-from-number type-number)) gvalue +gvalue-value-offset+) ; temporary workaround for wrong topological sorting of types
207 (gvalue-free gvalue t)))))
208 #'(lambda (value object)
209 (error "Slot is not readable: ~A" (slot-definition-name slotd))))))
210
211 (unless (slot-boundp slotd 'writer-function)
212 (setf
213 (slot-value slotd 'writer-function)
214 (if (slot-writable-p slotd)
215 #'(lambda (value object)
216 (with-gc-disabled
217 (let ((gvalue (gvalue-new type-number)))
218 (funcall
219 (intern-writer-function (type-from-number type-number)) ; temporary
220 value gvalue +gvalue-value-offset+)
221 (%object-set-property object pname gvalue)
222 (funcall
223 (intern-destroy-function (type-from-number type-number)) ; temporary
224 gvalue +gvalue-value-offset+)
225 (gvalue-free gvalue nil)
226 value)))
227 #'(lambda (value object)
228 (error "Slot is not writable: ~A" (slot-definition-name slotd))))))
229
230 (unless (slot-boundp slotd 'boundp-function)
231 (setf
232 (slot-value slotd 'boundp-function)
a9044181 233 #'(lambda (object)
4d83a8a6 234 (declare (ignore object))
235 t))))
236 (call-next-method))
237
a9044181 238
a9044181 239(defmethod validate-superclass ((class gobject-class)
240 (super pcl::standard-class))
4de90d10 241; (subtypep (class-name super) 'gobject)
242 t)
d4b21b08 243
244
245
246;;;;
247
323d4265 248(defbinding %object-class-list-properties () pointer
d4b21b08 249 (class pointer)
250 (n-properties unsigned-int :out))
251
9c03a07c 252
253(defun %map-params (params length type inherited-p)
254 (if inherited-p
255 (map-c-array 'list #'identity params 'param length)
256 (let ((properties ()))
257 (map-c-array 'list
258 #'(lambda (param)
259 (when (eql (param-owner-type param) type)
260 (push param properties)))
261 params 'param length)
262 (nreverse properties))))
263
264(defun query-object-class-properties (type &optional inherited-p)
265 (let* ((type-number (find-type-number type))
266 (class (type-class-ref type-number)))
267 (unwind-protect
268 (multiple-value-bind (array length)
269 (%object-class-list-properties class)
270 (unwind-protect
271 (%map-params array length type-number inherited-p)
272 (deallocate-memory array)))
273; (type-class-unref type-number)
274 )))
d4b21b08 275
276
277(defun default-slot-name (name)
278 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
279
280(defun default-slot-accessor (class-name slot-name type)
281 (intern
282 (format
283 nil "~A-~A~A" class-name slot-name
4d83a8a6 284 (if (eq type 'boolean) "-P" ""))))
d4b21b08 285
323d4265 286
9c03a07c 287(defun slot-definition-from-property (class property)
288 (with-slots (name flags value-type documentation) property
289 (let* ((slot-name (default-slot-name name))
290 (slot-type (or (type-from-number value-type) value-type))
291 (accessor (default-slot-accessor class slot-name slot-type)))
292
293 `(,slot-name
294 :allocation :property :pname ,name
295
296 ;; accessors
297 ,@(cond
298 ((and
299 (member :writable flags) (member :readable flags)
300 (not (member :construct-only flags)))
301 (list :accessor accessor))
302 ((and (member :writable flags) (not (member :construct-only flags)))
303 (list :writer `(setf ,accessor)))
304 ((member :readable flags)
305 (list :reader accessor)))
306
307 ;; readable/writable/construct
308 ,@(when (or (not (member :writable flags))
309 (member :construct-only flags))
310 '(:writable nil))
311 ,@(when (not (member :readable flags))
312 '(:readable nil))
313 ,@(when (or (member :construct flags)
314 (member :construct-only flags))
315 '(:construct t))
316
317 ;; initargs
318 ,@(when (or (member :construct flags)
319 (member :construct-only flags)
320 (member :writable flags))
321 (list :initarg (intern (string slot-name) "KEYWORD")))
322
323 :type ,slot-type
324 :documentation ,documentation))))
325
326
327(defun slot-definitions (class properties slots)
328 (loop
329 with manual-slots = slots
330 for property in properties
331 unless (find-if
332 #'(lambda (slot)
333 (destructuring-bind (name &rest args) slot
334 (or
335 (equal (param-name property) (getf args :pname))
336 (eq (default-slot-name (param-name property)) name))))
337 manual-slots)
338 do (push (slot-definition-from-property class property) slots))
339 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
340
341
342(defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
343 (let ((supers (cons (supertype type) (implements type)))
344 (class (type-from-number type))
345 (slots (getf options :slots)))
346 `(defclass ,class ,supers
347 ,(slot-definitions class (query-object-class-properties type) slots)
348 (:metaclass ,metaclass)
349 (:alien-name ,(find-type-name type)))))
323d4265 350
d4b21b08 351
9c03a07c 352(register-derivable-type 'gobject "GObject" 'expand-gobject-type)