Computing +gvalue-value-offset+ propperly
[clg] / glib / gparam.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
3 ;;
4 ;; Permission is hereby granted, free of charge, to any person obtaining
5 ;; a copy of this software and associated documentation files (the
6 ;; "Software"), to deal in the Software without restriction, including
7 ;; without limitation the rights to use, copy, modify, merge, publish,
8 ;; distribute, sublicense, and/or sell copies of the Software, and to
9 ;; permit persons to whom the Software is furnished to do so, subject to
10 ;; the following conditions:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
15 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 ;; $Id: gparam.lisp,v 1.22 2007-02-23 12:50:54 espen Exp $
24
25 (in-package "GLIB")
26
27 (deftype gvalue () 'pointer)
28
29 (register-type 'gvalue '|g_value_get_type|)
30
31 (eval-when (:compile-toplevel :load-toplevel :execute)
32 (defbinding (size-of-gvalue "size_of_gvalue") () unsigned-int))
33
34 (defconstant +gvalue-size+ (size-of-gvalue))
35 (defconstant +gvalue-value-offset+
36 (max (size-of 'type-number) (type-alignment 'type-number)))
37
38 (defbinding (%gvalue-init "g_value_init") () nil
39 (value gvalue)
40 (type type-number))
41
42 (defbinding (gvalue-unset "g_value_unset") () nil
43 (value gvalue))
44
45 (defun gvalue-init (gvalue type &optional (value nil value-p))
46 (%gvalue-init gvalue (find-type-number type))
47 (when value-p
48 (funcall (writer-function type) value gvalue +gvalue-value-offset+)))
49
50 (defun gvalue-new (&optional type (value nil value-p))
51 (let ((gvalue (allocate-memory +gvalue-size+)))
52 (cond
53 (value-p (gvalue-init gvalue type value))
54 (type (gvalue-init gvalue type)))
55 gvalue))
56
57 (defun gvalue-free (gvalue &optional (unset-p t))
58 (unless (null-pointer-p gvalue)
59 (when unset-p
60 (gvalue-unset gvalue))
61 (deallocate-memory gvalue)))
62
63 (defun gvalue-type (gvalue)
64 (type-from-number (ref-type-number gvalue)))
65
66 (defun gvalue-get (gvalue)
67 (funcall (reader-function (gvalue-type gvalue))
68 gvalue +gvalue-value-offset+))
69
70 (defun gvalue-peek (gvalue)
71 (funcall (reader-function (gvalue-type gvalue) :ref :peek)
72 gvalue +gvalue-value-offset+))
73
74 (defun gvalue-take (gvalue)
75 (funcall (reader-function (gvalue-type gvalue) :ref :get)
76 gvalue +gvalue-value-offset+))
77
78 (defun gvalue-set (gvalue value)
79 (funcall (writer-function (gvalue-type gvalue))
80 value gvalue +gvalue-value-offset+)
81 value)
82
83 (defbinding (gvalue-p "g_type_check_value") () boolean
84 (location pointer))
85
86 (defmacro with-gvalue ((gvalue &optional type (value nil value-p)) &body body)
87 `(with-memory (,gvalue +gvalue-size+)
88 ,(cond
89 ((and type value-p) `(gvalue-init ,gvalue ,type ,value))
90 (type `(gvalue-init ,gvalue ,type)))
91 ,@body
92 ,(unless value-p `(gvalue-take ,gvalue))))
93
94
95 (deftype param-flag-type ()
96 '(flags
97 (:readable 1)
98 (:writable 2)
99 (:construct 4)
100 (:construct-only 8)
101 (:lax-validation 16)
102 (:private 32)))
103
104 (eval-when (:compile-toplevel :load-toplevel :execute)
105 (defclass param-spec-class (ginstance-class)
106 ())
107
108 (defmethod shared-initialize ((class param-spec-class) names &rest initargs)
109 (declare (ignore names initargs))
110 (call-next-method)
111 (unless (slot-boundp class 'ref)
112 (setf (slot-value class 'ref) '%param-spec-ref))
113 (unless (slot-boundp class 'unref)
114 (setf (slot-value class 'unref) '%param-spec-unref)))
115
116 (defmethod validate-superclass ((class param-spec-class) (super standard-class))
117 t ;(subtypep (class-name super) 'param)
118 ))
119
120
121 (defbinding %param-spec-ref () pointer
122 (location pointer))
123
124 (defbinding %param-spec-unref () nil
125 (location pointer))
126
127
128 ;; TODO: rename to param-spec
129 (defclass param (ginstance)
130 ((name
131 :allocation :alien
132 :reader param-name
133 :type string)
134 (flags
135 :allocation :alien
136 :reader param-flags
137 :type param-flag-type)
138 (value-type
139 :allocation :alien
140 :reader param-value-type
141 :type type-number)
142 (owner-type
143 :allocation :alien
144 :reader param-owner-type
145 :type type-number)
146 (nickname
147 :allocation :virtual
148 :getter "g_param_spec_get_nick"
149 :reader param-nickname
150 :type (copy-of string))
151 (documentation
152 :allocation :virtual
153 :getter "g_param_spec_get_blurb"
154 :reader param-documentation
155 :type (copy-of string)))
156 (:metaclass param-spec-class)
157 (:gtype "GParam"))
158
159
160 (defclass param-char (param)
161 ((minimum
162 :allocation :alien
163 :reader param-minimum
164 :type char)
165 (maximum
166 :allocation :alien
167 :reader param-maximum
168 :type char)
169 (default-value
170 :allocation :alien
171 :reader param-default-value
172 :type char))
173 (:metaclass param-spec-class)
174 (:gtype "GParamChar"))
175
176 (defclass param-unsigned-char (param)
177 (
178 ; (minimum
179 ; :allocation :alien
180 ; :reader param-unsigned-char-minimum
181 ; :type unsigned-char)
182 ; (maximum
183 ; :allocation :alien
184 ; :reader param-unsigned-char-maximum
185 ; :type unsigned-char)
186 ; (default-value
187 ; :allocation :alien
188 ; :reader param-unsigned-char-default-value
189 ; :type unsigned-char)
190 )
191 (:metaclass param-spec-class)
192 (:gtype "GParamUChar"))
193
194 (defclass param-boolean (param)
195 ((default-value
196 :allocation :alien
197 :reader param-default-value
198 :type boolean))
199 (:metaclass param-spec-class)
200 (:gtype "GParamBoolean"))
201
202 (defclass param-int (param)
203 ((minimum
204 :allocation :alien
205 :reader param-minimum
206 :type int)
207 (maximum
208 :allocation :alien
209 :reader param-maximum
210 :type int)
211 (default-value
212 :allocation :alien
213 :reader param-default-value
214 :type int))
215 (:metaclass param-spec-class)
216 (:gtype "GParamInt"))
217
218 (defclass param-unsigned-int (param)
219 ((minimum
220 :allocation :alien
221 :reader param-minimum
222 :type unsigned-int)
223 (maximum
224 :allocation :alien
225 :reader param-maximum
226 :type unsigned-int)
227 (default-value
228 :allocation :alien
229 :reader param-default-value
230 :type unsigned-int))
231 (:metaclass param-spec-class)
232 (:gtype "GParamUInt"))
233
234 (defclass param-long (param)
235 ((minimum
236 :allocation :alien
237 :reader param-minimum
238 :type long)
239 (maximum
240 :allocation :alien
241 :reader param-maximum
242 :type long)
243 (default-value
244 :allocation :alien
245 :reader param-default-value
246 :type long))
247 (:metaclass param-spec-class)
248 (:gtype "GParam"))
249
250 (defclass param-unsigned-long (param)
251 ((minimum
252 :allocation :alien
253 :reader param-minimum
254 :type unsigned-long)
255 (maximum
256 :allocation :alien
257 :reader param-maximum
258 :type unsigned-long)
259 (default-value
260 :allocation :alien
261 :reader param-default-value
262 :type unsigned-long))
263 (:metaclass param-spec-class)
264 (:gtype "GParamULong"))
265
266 (defclass param-unichar (param)
267 ()
268 (:metaclass param-spec-class)
269 (:gtype "GParamUnichar"))
270
271 (defclass param-enum (param)
272 ((class
273 :allocation :alien
274 :reader param-enum-class
275 :type pointer)
276 (default-value
277 :allocation :alien
278 :reader param-default-value
279 :type long))
280 (:metaclass param-spec-class)
281 (:gtype "GParamEnum"))
282
283 (defclass param-flags (param)
284 ((class
285 :allocation :alien
286 :reader param-flags-class
287 :type pointer)
288 (default-value
289 :allocation :alien
290 :reader param-default-value
291 :type long))
292 (:metaclass param-spec-class)
293 (:gtype "GParamFlags"))
294
295 (defclass param-single-float (param)
296 ((minimum
297 :allocation :alien
298 :reader param-minimum
299 :type single-float)
300 (maximum
301 :allocation :alien
302 :reader param-maximum
303 :type single-float)
304 (default-value
305 :allocation :alien
306 :reader param-default-value
307 :type single-float)
308 (epsilon
309 :allocation :alien
310 :reader param-float-epsilon
311 :type single-float))
312 (:metaclass param-spec-class)
313 (:gtype "GParamFloat"))
314
315 (defclass param-double-float (param)
316 ((minimum
317 :allocation :alien
318 :reader param-minimum
319 :type double-float)
320 (maximum
321 :allocation :alien
322 :reader param-maximum
323 :type double-float)
324 (default-value
325 :allocation :alien
326 :reader param-default-value
327 :type double-float)
328 (epsilon
329 :allocation :alien
330 :reader param-float-epsilon
331 :type double-float))
332 (:metaclass param-spec-class)
333 (:gtype "GParamDouble"))
334
335 (defclass param-string (param)
336 ((default-value
337 :allocation :alien
338 :reader param-default-value
339 :type string))
340 (:metaclass param-spec-class)
341 (:gtype "GParamString"))
342
343 (defclass param-param (param)
344 ()
345 (:metaclass param-spec-class)
346 (:gtype "GParamParam"))
347
348 (defclass param-boxed (param)
349 ()
350 (:metaclass param-spec-class)
351 (:gtype "GParamBoxed"))
352
353 (defclass param-pointer (param)
354 ()
355 (:metaclass param-spec-class)
356 (:gtype "GParamPointer"))
357
358 (defclass param-value-array (param)
359 ((element-spec
360 :allocation :alien
361 :reader param-value-array-element-spec
362 :type param)
363 (length
364 :allocation :alien
365 :reader param-value-array-length
366 :type unsigned-int))
367 (:metaclass param-spec-class)
368 (:gtype "GParamValueArray"))
369
370 (defclass param-object (param)
371 ()
372 (:metaclass param-spec-class)
373 (:gtype "GParamObject"))
374
375 (defclass param-overrride (param)
376 ()
377 (:metaclass param-spec-class)
378 (:gtype "GParamOverride"))