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