Added bindings to GObject ref/unref functions
[clg] / glib / gobject.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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
18 ;; $Id: gobject.lisp,v 1.11 2002-03-24 12:50:30 espen Exp $
19
20 (in-package "GLIB")
21
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defclass gobject (ginstance)
25 ()
26 (:metaclass ginstance-class)
27 (:alien-name "GObject")
28 (:copy %object-ref)
29 (:free %object-unref)))
30
31 (defmethod initialize-instance ((object gobject) &rest initargs)
32 (declare (ignore initargs))
33 (setf (slot-value object 'location) (%gobject-new (type-number-of object)))
34 (call-next-method))
35
36 (defbinding (%gobject-new "g_object_new") () pointer
37 (type type-number)
38 (nil null))
39
40
41 (defbinding %object-ref (type location) pointer
42 (location pointer))
43
44 (defbinding %object-unref (type location) nil
45 (location pointer))
46
47
48 (defun object-ref (object)
49 (%object-ref nil (proxy-location object)))
50
51 (defun object-unref (object)
52 (%object-unref nil (proxy-location object)))
53
54
55
56 ;;;; Property stuff
57
58 (defbinding %object-set-property () nil
59 (object gobject)
60 (name string)
61 (value gvalue))
62
63 (defbinding %object-get-property () nil
64 (object gobject)
65 (name string)
66 (value gvalue))
67
68 (defbinding %object-notify () nil
69 (object gobject)
70 (name string))
71
72 (defbinding object-freeze-notify () nil
73 (object gobject))
74
75 (defbinding object-thaw-notify () nil
76 (object gobject))
77
78 (defbinding %object-set-qdata-full () nil
79 (object gobject)
80 (id quark)
81 (data unsigned-long)
82 (destroy-marshal pointer))
83
84
85 ;;;; User data
86
87 (defun (setf object-data) (data object key &key (test #'eq))
88 (%object-set-qdata-full
89 object (quark-from-object key :test test)
90 (register-user-data data) *destroy-notify*)
91 data)
92
93 (defbinding %object-get-qdata () unsigned-long
94 (object gobject)
95 (id quark))
96
97 (defun object-data (object key &key (test #'eq))
98 (find-user-data
99 (%object-get-qdata object (quark-from-object key :test test))))
100
101
102
103 ;;;; Metaclass used for subclasses of gobject
104
105 (eval-when (:compile-toplevel :load-toplevel :execute)
106 (defclass gobject-class (ginstance-class))
107
108 (defclass direct-gobject-slot-definition (direct-virtual-slot-definition)
109 ((pname :reader slot-definition-pname)))
110
111 (defclass effective-gobject-slot-definition
112 (effective-virtual-slot-definition)))
113
114
115
116 ; (defbinding object-class-install-param () nil
117 ; (class pointer)
118 ; (id unsigned-int)
119 ; (parameter parameter))
120
121 ; (defbinding object-class-find-param-spec () parameter
122 ; (class pointer)
123 ; (name string))
124
125 (defun signal-name-to-string (name)
126 (substitute #\_ #\- (string-downcase (string name))))
127
128 (defmethod initialize-instance :after ((slotd direct-gobject-slot-definition)
129 &rest initargs &key pname)
130 (declare (ignore initargs))
131 (when pname
132 (setf
133 (slot-value slotd 'pname)
134 (signal-name-to-string (slot-definition-name slotd)))))
135
136 (defmethod direct-slot-definition-class ((class gobject-class) initargs)
137 (case (getf initargs :allocation)
138 (:property (find-class 'direct-gobject-slot-definition))
139 (t (call-next-method))))
140
141 (defmethod effective-slot-definition-class ((class gobject-class) initargs)
142 (case (getf initargs :allocation)
143 (:property (find-class 'effective-gobject-slot-definition))
144 (t (call-next-method))))
145
146 (defmethod compute-virtual-slot-accessors
147 ((class gobject-class) (slotd effective-gobject-slot-definition)
148 direct-slotds)
149 (with-slots (type) slotd
150 (let ((pname (slot-definition-pname (first direct-slotds)))
151 (type-number (find-type-number type)))
152 (list
153 #'(lambda (object)
154 (with-gc-disabled
155 (let ((gvalue (gvalue-new type-number)))
156 (%object-get-property object pname gvalue)
157 (unwind-protect
158 (funcall
159 (intern-reader-function type) gvalue +gvalue-value-offset+)
160 (gvalue-free gvalue t)))))
161 #'(lambda (value object)
162 (with-gc-disabled
163 (let ((gvalue (gvalue-new type-number)))
164 (funcall
165 (intern-writer-function type)
166 value gvalue +gvalue-value-offset+)
167 (%object-set-property object pname gvalue)
168 (funcall
169 (intern-destroy-function type)
170 gvalue +gvalue-value-offset+)
171 (gvalue-free gvalue nil)
172 value)))))))
173
174 (defmethod validate-superclass ((class gobject-class)
175 (super pcl::standard-class))
176 ; (subtypep (class-name super) 'gobject)
177 t)
178
179
180
181 ;;;;
182
183 (defbinding %object-class-list-properties () pointer
184 (class pointer)
185 (n-properties unsigned-int :out))
186
187 (defun query-object-class-properties (type-number &optional
188 inherited-properties)
189 (let ((class (type-class-ref type-number)))
190 (multiple-value-bind (array length)
191 (%object-class-list-properties class)
192 (unwind-protect
193 (let ((all-properties
194 (map-c-array 'list #'identity array 'param length)))
195 (if (not inherited-properties)
196 (delete-if
197 #'(lambda (param)
198 (not (eql type-number (param-owner-type param))))
199 all-properties)
200 all-properties))
201 (deallocate-memory array)))))
202
203
204 (defun default-slot-name (name)
205 (intern (substitute #\- #\_ (string-upcase (string-upcase name)))))
206
207 (defun default-slot-accessor (class-name slot-name type)
208 (intern
209 (format
210 nil "~A-~A~A" class-name slot-name
211 (if (eq 'boolean type) "-P" ""))))
212
213 (defun expand-gobject-type (type-number &optional options
214 (metaclass 'gobject-class))
215 (let* ((supers (cons (supertype type-number) (implements type-number)))
216 (class (type-from-number type-number))
217 (override-slots (getf options :slots))
218 (expanded-slots
219 (mapcar
220 #'(lambda (param)
221 (with-slots (name flags value-type documentation) param
222 (let* ((slot-name (default-slot-name name))
223 (slot-type (type-from-number value-type #|t|#))
224 (accessor
225 (default-slot-accessor class slot-name slot-type)))
226 `(,slot-name
227 :allocation :property
228 :pname ,name
229 ,@(cond
230 ((and
231 (member :writable flags)
232 (member :readable flags))
233 (list :accessor accessor))
234 ((member :writable flags)
235 (list :writer `(setf ,accessor)))
236 ((member :readable flags)
237 (list :reader accessor)))
238 ,@(when (or
239 (member :construct flags)
240 (member :writable flags))
241 (list :initarg (intern (string slot-name) "KEYWORD")))
242 :type ,slot-type
243 ,@(when documentation
244 (list :documentation documentation))))))
245 (query-object-class-properties type-number))))
246
247 (dolist (slot-def override-slots)
248 (let ((name (car slot-def))
249 (pname (getf (cdr slot-def) :pname)))
250 (setq
251 expanded-slots
252 (delete-if
253 #'(lambda (expanded-slot-def)
254 (or
255 (eq name (car expanded-slot-def))
256 (and
257 pname
258 (string= pname (getf (cdr expanded-slot-def) :pname)))))
259 expanded-slots))
260
261 (unless (getf (cdr slot-def) :ignore)
262 (push slot-def expanded-slots))))
263
264 `(progn
265 (defclass ,class ,supers
266 ,expanded-slots
267 (:metaclass ,metaclass)
268 (:alien-name ,(find-type-name type-number))))))
269
270
271 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
272