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