chiark / gitweb /
New function UPDATE-USER-DATA and some bug fixes
[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
174e8a5c 18;; $Id: gobject.lisp,v 1.26 2004-12-29 21:07:46 espen Exp $
560af5c5 19
20(in-package "GLIB")
21
22
2176a9c7 23;;;; Metaclass used for subclasses of gobject
24
25(eval-when (:compile-toplevel :load-toplevel :execute)
26 (defclass gobject-class (ginstance-class)
27 ())
28
29 (defmethod validate-superclass ((class gobject-class)
30 (super pcl::standard-class))
31; (subtypep (class-name super) 'gobject)
32 t))
33
34(defclass direct-property-slot-definition (direct-virtual-slot-definition)
35 ((pname :reader slot-definition-pname :initarg :pname)
36 (readable :initform t :reader slot-readable-p :initarg :readable)
37 (writable :initform t :reader slot-writable-p :initarg :writable)
38 (construct :initform nil :initarg :construct)))
39
40(defclass effective-property-slot-definition (effective-virtual-slot-definition)
41 ((pname :reader slot-definition-pname :initarg :pname)
42 (readable :reader slot-readable-p :initarg :readable)
43 (writable :reader slot-writable-p :initarg :writable)
174e8a5c 44 (construct :initarg :construct)))
45
46(defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
47 ())
48
49(defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
50 ())
51
2176a9c7 52
53(defbinding %object-ref () pointer
54 (location pointer))
55
56(defbinding %object-unref () nil
57 (location pointer))
58
59(defmethod reference-foreign ((class gobject-class) location)
60 (declare (ignore class))
61 (%object-ref location))
62
63(defmethod unreference-foreign ((class gobject-class) location)
64 (declare (ignore class))
65 (%object-unref location))
66
67
68; (defbinding object-class-install-param () nil
69; (class pointer)
70; (id unsigned-int)
71; (parameter parameter))
72
73; (defbinding object-class-find-param-spec () parameter
74; (class pointer)
75; (name string))
76
77(defun signal-name-to-string (name)
78 (substitute #\_ #\- (string-downcase (string name))))
79
80
81(defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
82 (case (getf initargs :allocation)
83 (:property (find-class 'direct-property-slot-definition))
174e8a5c 84 (:user-data (find-class 'direct-user-data-slot-definition))
2176a9c7 85 (t (call-next-method))))
86
87(defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
88 (case (getf initargs :allocation)
89 (:property (find-class 'effective-property-slot-definition))
174e8a5c 90 (:user-data (find-class 'effective-user-data-slot-definition))
2176a9c7 91 (t (call-next-method))))
92
93(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
eeda1c2d 94 (if (typep (first direct-slotds) 'direct-property-slot-definition)
2176a9c7 95 (nconc
96 (list :pname (signal-name-to-string
97 (most-specific-slot-value direct-slotds 'pname))
98 :readable (most-specific-slot-value direct-slotds 'readable)
99 :writable (most-specific-slot-value direct-slotds 'writable)
100 :construct (most-specific-slot-value direct-slotds 'construct))
101 (call-next-method))
102 (call-next-method)))
103
104
105(defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
106 (let* ((type (slot-definition-type slotd))
107 (pname (slot-definition-pname slotd))
108 (type-number (find-type-number type)))
eeda1c2d 109 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
2176a9c7 110 (setf
eeda1c2d 111 (slot-value slotd 'getter)
112 (let ((reader nil))
113 #'(lambda (object)
114 (unless reader
ae37d096 115 (setq reader (reader-function type))) ;(type-from-number type-number))))
eeda1c2d 116 (let ((gvalue (gvalue-new type-number)))
117 (%object-get-property object pname gvalue)
118 (unwind-protect
119 (funcall reader gvalue +gvalue-value-offset+)
120 (gvalue-free gvalue t)))))))
2176a9c7 121
eeda1c2d 122 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
2176a9c7 123 (setf
eeda1c2d 124 (slot-value slotd 'setter)
125 (let ((writer nil))
126 #'(lambda (value object)
127 (unless writer
ae37d096 128 (setq writer (writer-function type))) ;(type-from-number type-number))))
eeda1c2d 129 (let ((gvalue (gvalue-new type-number)))
130 (funcall writer value gvalue +gvalue-value-offset+)
131 (%object-set-property object pname gvalue)
132 (gvalue-free gvalue t)
133 value))))))
134
2176a9c7 135 (call-next-method))
136
174e8a5c 137(defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
138 (let ((slot-name (slot-definition-name slotd)))
139 (setf
140 (slot-value slotd 'getter)
141 #'(lambda (object)
142 (prog1 (user-data object slot-name))))
143 (setf
144 (slot-value slotd 'setter)
145 #'(lambda (value object)
146 (setf (user-data object slot-name) value)))
147 (setf
148 (slot-value slotd 'boundp)
149 #'(lambda (object)
150 (user-data-p object slot-name))))
151 (call-next-method))
152
2176a9c7 153
154;;;; Super class for all classes in the GObject type hierarchy
155
560af5c5 156(eval-when (:compile-toplevel :load-toplevel :execute)
c8c48a4c 157 (defclass gobject (ginstance)
560af5c5 158 ()
2176a9c7 159 (:metaclass gobject-class)
9adccb27 160 (:alien-name "GObject")))
161
eeda1c2d 162
163(defun initial-add (object function initargs key pkey)
164 (loop
165 as (initarg value . rest) = initargs then rest
166 do (cond
167 ((eq initarg key) (funcall function object value))
168 ((eq initarg pkey) (mapc #'(lambda (value)
169 (funcall function object value))
170 value)))
171 while rest))
172
173(defun initial-apply-add (object function initargs key pkey)
174 (initial-add object #'(lambda (object value)
175 (apply function object (mklist value)))
176 initargs key pkey))
177
178
82747bbd 179(defmethod initialize-instance ((object gobject) &rest initargs)
040579dd 180 (unless (slot-boundp object 'location)
181 ;; Extract initargs which we should pass directly to the GObeject
182 ;; constructor
183 (let* ((slotds (class-slots (class-of object)))
184 (args (when initargs
185 (loop
186 as (key value . rest) = initargs then rest
187 as slotd = (find-if
188 #'(lambda (slotd)
189 (member key (slot-definition-initargs slotd)))
190 slotds)
191 when (and (typep slotd 'effective-property-slot-definition)
192 (slot-value slotd 'construct))
193 collect (progn
194 (remf initargs key)
195 (list
196 (slot-definition-pname slotd)
197 (slot-definition-type slotd)
198 value))
199 while rest))))
200 (if args
201 (let* ((string-size (size-of 'string))
202 (string-writer (writer-function 'string))
203 (string-destroy (destroy-function 'string))
204 (params (allocate-memory
205 (* (length args) (+ string-size +gvalue-size+)))))
9adccb27 206 (loop
040579dd 207 for (pname type value) in args
9adccb27 208 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
040579dd 209 do (funcall string-writer pname tmp)
210 (gvalue-init (sap+ tmp string-size) type value))
211 (unwind-protect
212 (setf
213 (slot-value object 'location)
214 (%gobject-newv (type-number-of object) (length args) params))
215 (loop
216 repeat (length args)
217 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
218 do (funcall string-destroy tmp)
219 (gvalue-unset (sap+ tmp string-size)))
220 (deallocate-memory params)))
8755b1a5 221 (setf
222 (slot-value object 'location)
040579dd 223 (%gobject-new (type-number-of object))))))
8755b1a5 224
4d83a8a6 225 (apply #'call-next-method object initargs))
226
227
1dbf4216 228(defmethod instance-finalizer ((instance gobject))
229 (let ((location (proxy-location instance)))
230 #'(lambda ()
231 (remove-cached-instance location)
1dbf4216 232 (%object-unref location))))
233
9adccb27 234
d4b21b08 235(defbinding (%gobject-new "g_object_new") () pointer
a9044181 236 (type type-number)
237 (nil null))
560af5c5 238
9adccb27 239(defbinding (%gobject-newv "g_object_newv") () pointer
4d83a8a6 240 (type type-number)
241 (n-parameters unsigned-int)
9adccb27 242 (params pointer))
de8516ca 243
244
d4b21b08 245
323d4265 246;;;; Property stuff
560af5c5 247
26b133ed 248(defbinding %object-set-property () nil
c8c48a4c 249 (object gobject)
250 (name string)
251 (value gvalue))
86d9d6ab 252
26b133ed 253(defbinding %object-get-property () nil
c8c48a4c 254 (object gobject)
255 (name string)
a9044181 256 (value gvalue))
86d9d6ab 257
26b133ed 258(defbinding %object-notify () nil
c8c48a4c 259 (object gobject)
260 (name string))
86d9d6ab 261
26b133ed 262(defbinding object-freeze-notify () nil
a9044181 263 (object gobject))
86d9d6ab 264
26b133ed 265(defbinding object-thaw-notify () nil
a9044181 266 (object gobject))
86d9d6ab 267
26b133ed 268(defbinding %object-set-qdata-full () nil
86d9d6ab 269 (object gobject)
270 (id quark)
271 (data unsigned-long)
272 (destroy-marshal pointer))
273
a9044181 274
275;;;; User data
276
174e8a5c 277(defun (setf user-data) (data object key)
86d9d6ab 278 (%object-set-qdata-full
174e8a5c 279 object (quark-from-object key)
0f134a29 280 (register-user-data data) (callback %destroy-user-data))
86d9d6ab 281 data)
282
174e8a5c 283;; depecated
284(defun (setf object-data) (data object key &key (test #'eq))
285 (assert (eq test #'eq))
286 (setf (user-data object key) data))
287
26b133ed 288(defbinding %object-get-qdata () unsigned-long
86d9d6ab 289 (object gobject)
290 (id quark))
291
174e8a5c 292(defun user-data (object key)
293 (find-user-data (%object-get-qdata object (quark-from-object key))))
294
295;; depecated
86d9d6ab 296(defun object-data (object key &key (test #'eq))
174e8a5c 297 (assert (eq test #'eq))
298 (user-data object key))
299
300(defun user-data-p (object key)
301 (nth-value 1 (find-user-data (%object-get-qdata object (quark-from-object key)))))
86d9d6ab 302
303
d4b21b08 304;;;;
305
323d4265 306(defbinding %object-class-list-properties () pointer
d4b21b08 307 (class pointer)
308 (n-properties unsigned-int :out))
309
9c03a07c 310
311(defun %map-params (params length type inherited-p)
312 (if inherited-p
9adccb27 313 (map-c-vector 'list #'identity params 'param length)
9c03a07c 314 (let ((properties ()))
9adccb27 315 (map-c-vector 'list
9c03a07c 316 #'(lambda (param)
317 (when (eql (param-owner-type param) type)
318 (push param properties)))
319 params 'param length)
320 (nreverse properties))))
321
322(defun query-object-class-properties (type &optional inherited-p)
323 (let* ((type-number (find-type-number type))
324 (class (type-class-ref type-number)))
325 (unwind-protect
326 (multiple-value-bind (array length)
327 (%object-class-list-properties class)
328 (unwind-protect
329 (%map-params array length type-number inherited-p)
330 (deallocate-memory array)))
331; (type-class-unref type-number)
332 )))
d4b21b08 333
334
335(defun default-slot-name (name)
336 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
337
338(defun default-slot-accessor (class-name slot-name type)
339 (intern
340 (format
341 nil "~A-~A~A" class-name slot-name
4d83a8a6 342 (if (eq type 'boolean) "-P" ""))))
d4b21b08 343
323d4265 344
eeda1c2d 345(defun slot-definition-from-property (class property &optional args)
9c03a07c 346 (with-slots (name flags value-type documentation) property
347 (let* ((slot-name (default-slot-name name))
eeda1c2d 348 (slot-type (or (getf args :type) (type-from-number value-type) value-type))
9c03a07c 349 (accessor (default-slot-accessor class slot-name slot-type)))
350
351 `(,slot-name
352 :allocation :property :pname ,name
eeda1c2d 353
eeda1c2d 354 ,@(cond
ae37d096 355 ((find :unbound args) (list :unbound (getf args :unbound))))
9c03a07c 356
357 ;; accessors
358 ,@(cond
359 ((and
360 (member :writable flags) (member :readable flags)
361 (not (member :construct-only flags)))
362 (list :accessor accessor))
363 ((and (member :writable flags) (not (member :construct-only flags)))
364 (list :writer `(setf ,accessor)))
365 ((member :readable flags)
366 (list :reader accessor)))
367
368 ;; readable/writable/construct
369 ,@(when (or (not (member :writable flags))
370 (member :construct-only flags))
371 '(:writable nil))
372 ,@(when (not (member :readable flags))
373 '(:readable nil))
374 ,@(when (or (member :construct flags)
375 (member :construct-only flags))
376 '(:construct t))
377
378 ;; initargs
379 ,@(when (or (member :construct flags)
380 (member :construct-only flags)
381 (member :writable flags))
382 (list :initarg (intern (string slot-name) "KEYWORD")))
383
384 :type ,slot-type
385 :documentation ,documentation))))
386
387
388(defun slot-definitions (class properties slots)
389 (loop
9c03a07c 390 for property in properties
eeda1c2d 391 as slot = (or
392 (find (param-name property) slots
393 :key #'(lambda (slot) (getf (rest slot) :pname))
394 :test #'string=)
395 (find (param-name property) slots
396 :key #'first :test #'string-equal))
397 do (cond
398 ((not slot)
399 (push (slot-definition-from-property class property) slots))
400 ((getf (rest slot) :merge)
401 (setf
402 (rest slot)
403 (rest (slot-definition-from-property class property (rest slot)))))))
9c03a07c 404 (delete-if #'(lambda (slot) (getf (rest slot) :ignore)) slots))
405
406
407(defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
408 (let ((supers (cons (supertype type) (implements type)))
409 (class (type-from-number type))
410 (slots (getf options :slots)))
411 `(defclass ,class ,supers
412 ,(slot-definitions class (query-object-class-properties type) slots)
413 (:metaclass ,metaclass)
414 (:alien-name ,(find-type-name type)))))
323d4265 415
d4b21b08 416
9c03a07c 417(register-derivable-type 'gobject "GObject" 'expand-gobject-type)
ae37d096 418
419
420;;; Pseudo type for gobject instances which have their reference count
421;;; increased by the returning function
422
423(defmethod alien-type ((type (eql 'referenced)) &rest args)
424 (declare (ignore type args))
425 (alien-type 'gobject))
426
427(defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
428 (declare (ignore type))
429 (destructuring-bind (type) args
430 (if (subtypep type 'gobject)
431 (let ((instance (make-symbol "INSTANCE")))
432 `(let ((,instance ,(from-alien-form form type)))
433 (when ,instance
434 (%object-unref (proxy-location ,instance)))
435 ,instance))
436 (error "~A is not a subclass of GOBJECT" type))))
437
438(export 'referenced)