chiark / gitweb /
Added allocation :USER-DATA to the metaclass GOBJECT-CLASS
[clg] / glib / gobject.lisp
CommitLineData
0d07716f 1;; Common Lisp bindings for GTK+ v2.0
ca0a8e30 2;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
0d07716f 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
d4ddcf2c 18;; $Id: gobject.lisp,v 1.26 2004/12/29 21:07:46 espen Exp $
0d07716f 19
20(in-package "GLIB")
21
22
0c87a95a 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)
d4ddcf2c 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
0c87a95a 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))
d4ddcf2c 84 (:user-data (find-class 'direct-user-data-slot-definition))
0c87a95a 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))
d4ddcf2c 90 (:user-data (find-class 'effective-user-data-slot-definition))
0c87a95a 91 (t (call-next-method))))
92
93(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
64bce834 94 (if (typep (first direct-slotds) 'direct-property-slot-definition)
0c87a95a 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)))
64bce834 109 (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
0c87a95a 110 (setf
64bce834 111 (slot-value slotd 'getter)
112 (let ((reader nil))
113 #'(lambda (object)
114 (unless reader
7875d055 115 (setq reader (reader-function type))) ;(type-from-number type-number))))
64bce834 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)))))))
0c87a95a 121
64bce834 122 (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
0c87a95a 123 (setf
64bce834 124 (slot-value slotd 'setter)
125 (let ((writer nil))
126 #'(lambda (value object)
127 (unless writer
7875d055 128 (setq writer (writer-function type))) ;(type-from-number type-number))))
64bce834 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
0c87a95a 135 (call-next-method))
136
d4ddcf2c 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
0c87a95a 153
154;;;; Super class for all classes in the GObject type hierarchy
155
0d07716f 156(eval-when (:compile-toplevel :load-toplevel :execute)
c9819f3e 157 (defclass gobject (ginstance)
0d07716f 158 ()
0c87a95a 159 (:metaclass gobject-class)
6baf860c 160 (:alien-name "GObject")))
161
64bce834 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
ca0a8e30 179(defmethod initialize-instance ((object gobject) &rest initargs)
22a2e918 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+)))))
6baf860c 206 (loop
22a2e918 207 for (pname type value) in args
6baf860c 208 as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
22a2e918 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)))
7bde5a67 221 (setf
222 (slot-value object 'location)
22a2e918 223 (%gobject-new (type-number-of object))))))
7bde5a67 224
935a783c 225 (apply #'call-next-method object initargs))
226
227
29933e83 228(defmethod instance-finalizer ((instance gobject))
229 (let ((location (proxy-location instance)))
230 #'(lambda ()
231 (remove-cached-instance location)
29933e83 232 (%object-unref location))))
233
6baf860c 234
3a935dfa 235(defbinding (%gobject-new "g_object_new") () pointer
b75a7b77 236 (type type-number)
237 (nil null))
0d07716f 238
6baf860c 239(defbinding (%gobject-newv "g_object_newv") () pointer
935a783c 240 (type type-number)
241 (n-parameters unsigned-int)
6baf860c 242 (params pointer))
9db52165 243
244
3a935dfa 245
383653f5 246;;;; Property stuff
0d07716f 247
c2cd6b3a 248(defbinding %object-set-property () nil
c9819f3e 249 (object gobject)
250 (name string)
251 (value gvalue))
a905f5d9 252
c2cd6b3a 253(defbinding %object-get-property () nil
c9819f3e 254 (object gobject)
255 (name string)
b75a7b77 256 (value gvalue))
a905f5d9 257
c2cd6b3a 258(defbinding %object-notify () nil
c9819f3e 259 (object gobject)
260 (name string))
a905f5d9 261
c2cd6b3a 262(defbinding object-freeze-notify () nil
b75a7b77 263 (object gobject))
a905f5d9 264
c2cd6b3a 265(defbinding object-thaw-notify () nil
b75a7b77 266 (object gobject))
a905f5d9 267
c2cd6b3a 268(defbinding %object-set-qdata-full () nil
a905f5d9 269 (object gobject)
270 (id quark)
271 (data unsigned-long)
272 (destroy-marshal pointer))
273
b75a7b77 274
275;;;; User data
276
d4ddcf2c 277(defun (setf user-data) (data object key)
a905f5d9 278 (%object-set-qdata-full
d4ddcf2c 279 object (quark-from-object key)
a5c3a597 280 (register-user-data data) (callback %destroy-user-data))
a905f5d9 281 data)
282
d4ddcf2c 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
c2cd6b3a 288(defbinding %object-get-qdata () unsigned-long
a905f5d9 289 (object gobject)
290 (id quark))
291
d4ddcf2c 292(defun user-data (object key)
293 (find-user-data (%object-get-qdata object (quark-from-object key))))
294
295;; depecated
a905f5d9 296(defun object-data (object key &key (test #'eq))
d4ddcf2c 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)))))
a905f5d9 302
303
3a935dfa 304;;;;
305
383653f5 306(defbinding %object-class-list-properties () pointer
3a935dfa 307 (class pointer)
308 (n-properties unsigned-int :out))
309
b7833c4c 310
311(defun %map-params (params length type inherited-p)
312 (if inherited-p
6baf860c 313 (map-c-vector 'list #'identity params 'param length)
b7833c4c 314 (let ((properties ()))
6baf860c 315 (map-c-vector 'list
b7833c4c 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 )))
3a935dfa 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
935a783c 342 (if (eq type 'boolean) "-P" ""))))
3a935dfa 343
383653f5 344
64bce834 345(defun slot-definition-from-property (class property &optional args)
b7833c4c 346 (with-slots (name flags value-type documentation) property
347 (let* ((slot-name (default-slot-name name))
64bce834 348 (slot-type (or (getf args :type) (type-from-number value-type) value-type))
b7833c4c 349 (accessor (default-slot-accessor class slot-name slot-type)))
350
351 `(,slot-name
352 :allocation :property :pname ,name
64bce834 353
64bce834 354 ,@(cond
7875d055 355 ((find :unbound args) (list :unbound (getf args :unbound))))
b7833c4c 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
b7833c4c 390 for property in properties
64bce834 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)))))))
b7833c4c 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)))))
383653f5 415
3a935dfa 416
b7833c4c 417(register-derivable-type 'gobject "GObject" 'expand-gobject-type)
7875d055 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)