Moved to alien directory
[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
935a783c 18;; $Id: gobject.lisp,v 1.13 2004/10/27 14:58:59 espen Exp $
0d07716f 19
20(in-package "GLIB")
21
22
23(eval-when (:compile-toplevel :load-toplevel :execute)
c9819f3e 24 (defclass gobject (ginstance)
0d07716f 25 ()
c9819f3e 26 (:metaclass ginstance-class)
ca0a8e30 27 (:alien-name "GObject")
9db52165 28 (:copy %object-ref)
29 (:free %object-unref)))
0d07716f 30
935a783c 31
ca0a8e30 32(defmethod initialize-instance ((object gobject) &rest initargs)
935a783c 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
b75a7b77 60
3a935dfa 61(defbinding (%gobject-new "g_object_new") () pointer
b75a7b77 62 (type type-number)
63 (nil null))
0d07716f 64
935a783c 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)))
0d07716f 70
9db52165 71
935a783c 72(defbinding %object-ref (type location) pointer
9db52165 73 (location pointer))
74
935a783c 75 (defbinding %object-unref (type location) nil
76 (location pointer))
9db52165 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
3a935dfa 85
383653f5 86;;;; Property stuff
0d07716f 87
c2cd6b3a 88(defbinding %object-set-property () nil
c9819f3e 89 (object gobject)
90 (name string)
91 (value gvalue))
a905f5d9 92
c2cd6b3a 93(defbinding %object-get-property () nil
c9819f3e 94 (object gobject)
95 (name string)
b75a7b77 96 (value gvalue))
a905f5d9 97
c2cd6b3a 98(defbinding %object-notify () nil
c9819f3e 99 (object gobject)
100 (name string))
a905f5d9 101
c2cd6b3a 102(defbinding object-freeze-notify () nil
b75a7b77 103 (object gobject))
a905f5d9 104
c2cd6b3a 105(defbinding object-thaw-notify () nil
b75a7b77 106 (object gobject))
a905f5d9 107
c2cd6b3a 108(defbinding %object-set-qdata-full () nil
a905f5d9 109 (object gobject)
110 (id quark)
111 (data unsigned-long)
112 (destroy-marshal pointer))
113
b75a7b77 114
115;;;; User data
116
a905f5d9 117(defun (setf object-data) (data object key &key (test #'eq))
118 (%object-set-qdata-full
119 object (quark-from-object key :test test)
c9819f3e 120 (register-user-data data) *destroy-notify*)
a905f5d9 121 data)
122
c2cd6b3a 123(defbinding %object-get-qdata () unsigned-long
a905f5d9 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
0d07716f 132
b75a7b77 133;;;; Metaclass used for subclasses of gobject
134
135(eval-when (:compile-toplevel :load-toplevel :execute)
935a783c 136 (defclass gobject-class (ginstance-class)
137 ())
c9819f3e 138
3a935dfa 139 (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
935a783c 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))))
c9819f3e 150
151
0d07716f 152
c2cd6b3a 153; (defbinding object-class-install-param () nil
0d07716f 154; (class pointer)
155; (id unsigned-int)
156; (parameter parameter))
157
c2cd6b3a 158; (defbinding object-class-find-param-spec () parameter
0d07716f 159; (class pointer)
160; (name string))
161
9db52165 162(defun signal-name-to-string (name)
163 (substitute #\_ #\- (string-downcase (string name))))
b75a7b77 164
b75a7b77 165
935a783c 166(defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
b75a7b77 167 (case (getf initargs :allocation)
383653f5 168 (:property (find-class 'direct-gobject-slot-definition))
b75a7b77 169 (t (call-next-method))))
170
935a783c 171(defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
b75a7b77 172 (case (getf initargs :allocation)
383653f5 173 (:property (find-class 'effective-gobject-slot-definition))
b75a7b77 174 (t (call-next-method))))
175
935a783c 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)
b75a7b77 229 #'(lambda (object)
935a783c 230 (declare (ignore object))
231 t))))
232 (call-next-method))
233
b75a7b77 234
b75a7b77 235(defmethod validate-superclass ((class gobject-class)
236 (super pcl::standard-class))
b011356b 237; (subtypep (class-name super) 'gobject)
238 t)
3a935dfa 239
240
241
242;;;;
243
383653f5 244(defbinding %object-class-list-properties () pointer
3a935dfa 245 (class pointer)
246 (n-properties unsigned-int :out))
247
383653f5 248(defun query-object-class-properties (type-number &optional
249 inherited-properties)
b011356b 250 (let ((class (type-class-ref type-number)))
3a935dfa 251 (multiple-value-bind (array length)
383653f5 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)))))
3a935dfa 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
935a783c 272 (if (eq type 'boolean) "-P" ""))))
3a935dfa 273
383653f5 274(defun expand-gobject-type (type-number &optional options
b011356b 275 (metaclass 'gobject-class))
7858d45e 276 (let* ((supers (cons (supertype type-number) (implements type-number)))
3a935dfa 277 (class (type-from-number type-number))
935a783c 278 (manual-slots (getf options :slots))
3a935dfa 279 (expanded-slots
280 (mapcar
281 #'(lambda (param)
383653f5 282 (with-slots (name flags value-type documentation) param
3a935dfa 283 (let* ((slot-name (default-slot-name name))
935a783c 284; (slot-type value-type) ;(type-from-number value-type t))
285 (slot-type (or (type-from-number value-type) value-type))
3a935dfa 286 (accessor
935a783c 287 (default-slot-accessor class slot-name slot-type)));(type-from-number slot-type)))) ; temporary workaround for wrong topological sorting of types
288
3a935dfa 289 `(,slot-name
383653f5 290 :allocation :property
291 :pname ,name
b011356b 292 ,@(cond
293 ((and
294 (member :writable flags)
935a783c 295 (member :readable flags)
296 (not (member :construct-only flags)))
b011356b 297 (list :accessor accessor))
935a783c 298 ((and (member :writable flags)
299 (not (member :construct-only flags)))
3a935dfa 300 (list :writer `(setf ,accessor)))
b011356b 301 ((member :readable flags)
302 (list :reader accessor)))
303 ,@(when (or
935a783c 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
b011356b 314 (member :construct flags)
935a783c 315 (member :construct-only flags)
b011356b 316 (member :writable flags))
3a935dfa 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
935a783c 323 (dolist (slot-def (reverse manual-slots))
383653f5 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))))
3a935dfa 339
383653f5 340 `(progn
7858d45e 341 (defclass ,class ,supers
383653f5 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)
3a935dfa 348