Changed to MIT license
[clg] / glib / gparam.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 2000-2005 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.17 2005-04-23 16:48:51 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 'type-number) (* 2 (size-of 'double-float))))
35 (defconstant +gvalue-size+ #.(size-of-gvalue))
36
37 (defconstant +gvalue-value-offset+ (size-of 'type-number))
38
39 (defbinding (%gvalue-init "g_value_init") () nil
40 (value gvalue)
41 (type type-number))
42
43 (defbinding (gvalue-unset "g_value_unset") () nil
44 (value gvalue))
45
46 (defun gvalue-init (gvalue type &optional (value nil value-p))
47 (%gvalue-init gvalue (find-type-number type))
48 (when value-p
49 (funcall (writer-function type) value gvalue +gvalue-value-offset+)))
50
51 (defun gvalue-new (&optional type (value nil value-p))
52 (let ((gvalue (allocate-memory +gvalue-size+)))
53 (cond
54 (value-p (gvalue-init gvalue type value))
55 (type (gvalue-init gvalue type)))
56 gvalue))
57
58 (defun gvalue-free (gvalue &optional (unset-p t))
59 (unless (null-pointer-p gvalue)
60 (when unset-p
61 (gvalue-unset gvalue))
62 (deallocate-memory gvalue)))
63
64 (defun gvalue-type (gvalue)
65 (type-from-number (sap-ref-32 gvalue 0)))
66
67 (defun gvalue-get (gvalue)
68 (funcall (reader-function (gvalue-type gvalue))
69 gvalue +gvalue-value-offset+))
70
71 (defun gvalue-set (gvalue value)
72 (funcall (writer-function (gvalue-type gvalue))
73 value gvalue +gvalue-value-offset+)
74 value)
75
76 (defbinding (gvalue-p "g_type_check_value") () boolean
77 (location pointer))
78
79 (defmacro with-gvalue ((gvalue &optional type (value nil value-p)) &body body)
80 `(let ((,gvalue ,(cond
81 ((and type value-p) `(gvalue-new ,type ,value))
82 (type `(gvalue-new ,type))
83 (`(gvalue-new)))))
84 (unwind-protect
85 (progn
86 ,@body
87 ,(unless value-p `(gvalue-get ,gvalue)))
88 (gvalue-free ,gvalue))))
89
90
91 (deftype param-flag-type ()
92 '(flags
93 (:readable 1)
94 (:writable 2)
95 (:construct 4)
96 (:construct-only 8)
97 (:lax-validation 16)
98 (:private 32)))
99
100 (eval-when (:compile-toplevel :load-toplevel :execute)
101 (defclass param-spec-class (ginstance-class)
102 ())
103
104 (defmethod validate-superclass ((class param-spec-class) (super standard-class))
105 t ;(subtypep (class-name super) 'param)
106 ))
107
108
109 (defbinding %param-spec-ref () pointer
110 (location pointer))
111
112 (defbinding %param-spec-unref () nil
113 (location pointer))
114
115 (defmethod reference-foreign ((class param-spec-class) location)
116 (declare (ignore class))
117 (%param-spec-ref location))
118
119 (defmethod unreference-foreign ((class param-spec-class) location)
120 (declare (ignore class))
121 (%param-spec-unref location))
122
123
124
125 ;; TODO: rename to param-spec
126 (defclass param (ginstance)
127 ((name
128 :allocation :alien
129 :reader param-name
130 :type string)
131 (flags
132 :allocation :alien
133 :reader param-flags
134 :type param-flag-type)
135 (value-type
136 :allocation :alien
137 :reader param-value-type
138 :type type-number)
139 (owner-type
140 :allocation :alien
141 :reader param-owner-type
142 :type type-number)
143 (nickname
144 :allocation :virtual
145 :getter "g_param_spec_get_nick"
146 :reader param-nickname
147 :type (copy-of string))
148 (documentation
149 :allocation :virtual
150 :getter "g_param_spec_get_blurb"
151 :reader param-documentation
152 :type (copy-of string)))
153 (:metaclass param-spec-class)
154 (:gtype "GParam"))
155
156
157 (defclass param-char (param)
158 ((minimum
159 :allocation :alien
160 :reader param-char-minimum
161 :type char)
162 (maximum
163 :allocation :alien
164 :reader param-char-maximum
165 :type char)
166 (default-value
167 :allocation :alien
168 :reader param-char-default-value
169 :type char))
170 (:metaclass param-spec-class)
171 (:gtype "GParamChar"))
172
173 (defclass param-unsigned-char (param)
174 (
175 ; (minimum
176 ; :allocation :alien
177 ; :reader param-unsigned-char-minimum
178 ; :type unsigned-char)
179 ; (maximum
180 ; :allocation :alien
181 ; :reader param-unsigned-char-maximum
182 ; :type unsigned-char)
183 ; (default-value
184 ; :allocation :alien
185 ; :reader param-unsigned-char-default-value
186 ; :type unsigned-char)
187 )
188 (:metaclass param-spec-class)
189 (:gtype "GParamUChar"))
190
191 (defclass param-boolean (param)
192 ((default-value
193 :allocation :alien
194 :reader param-boolean-default-value
195 :type boolean))
196 (:metaclass param-spec-class)
197 (:gtype "GParamBoolean"))
198
199 (defclass param-int (param)
200 ((minimum
201 :allocation :alien
202 :reader param-int-minimum
203 :type int)
204 (maximum
205 :allocation :alien
206 :reader param-int-maximum
207 :type int)
208 (default-value
209 :allocation :alien
210 :reader param-int-default-value
211 :type int))
212 (:metaclass param-spec-class)
213 (:gtype "GParamInt"))
214
215 (defclass param-unsigned-int (param)
216 ((minimum
217 :allocation :alien
218 :reader param-unsigned-int-minimum
219 :type unsigned-int)
220 (maximum
221 :allocation :alien
222 :reader param-unsigned-int-maximum
223 :type unsigned-int)
224 (default-value
225 :allocation :alien
226 :reader param-unsigned-int-default-value
227 :type unsigned-int))
228 (:metaclass param-spec-class)
229 (:gtype "GParamUInt"))
230
231 (defclass param-long (param)
232 ((minimum
233 :allocation :alien
234 :reader param-long-minimum
235 :type long)
236 (maximum
237 :allocation :alien
238 :reader param-long-maximum
239 :type long)
240 (default-value
241 :allocation :alien
242 :reader param-long-default-value
243 :type long))
244 (:metaclass param-spec-class)
245 (:gtype "GParam"))
246
247 (defclass param-unsigned-long (param)
248 ((minimum
249 :allocation :alien
250 :reader param-unsigned-long-minimum
251 :type unsigned-long)
252 (maximum
253 :allocation :alien
254 :reader param-unsigned-long-maximum
255 :type unsigned-long)
256 (default-value
257 :allocation :alien
258 :reader param-unsigned-long-default-value
259 :type unsigned-long))
260 (:metaclass param-spec-class)
261 (:gtype "GParamULong"))
262
263 (defclass param-unichar (param)
264 ()
265 (:metaclass param-spec-class)
266 (:gtype "GParamUnichar"))
267
268 (defclass param-enum (param)
269 ((class
270 :allocation :alien
271 :reader param-enum-class
272 :type pointer)
273 (default-value
274 :allocation :alien
275 :reader param-enum-default-value
276 :type long))
277 (:metaclass param-spec-class)
278 (:gtype "GParamEnum"))
279
280 (defclass param-flags (param)
281 ((class
282 :allocation :alien
283 :reader param-flags-class
284 :type pointer)
285 (default-value
286 :allocation :alien
287 :reader param-flags-default-value
288 :type long))
289 (:metaclass param-spec-class)
290 (:gtype "GParamFlags"))
291
292 (defclass param-single-float (param)
293 ((minimum
294 :allocation :alien
295 :reader param-single-float-minimum
296 :type single-float)
297 (maximum
298 :allocation :alien
299 :reader param-single-float-maximum
300 :type single-float)
301 (default-value
302 :allocation :alien
303 :reader param-single-float-default-value
304 :type single-float)
305 (epsilon
306 :allocation :alien
307 :reader param-single-float-epsilon
308 :type single-float))
309 (:metaclass param-spec-class)
310 (:gtype "GParamFloat"))
311
312 (defclass param-double-float (param)
313 ((minimum
314 :allocation :alien
315 :reader param-double-float-minimum
316 :type double-float)
317 (maximum
318 :allocation :alien
319 :reader param-double-float-maximum
320 :type double-float)
321 (default-value
322 :allocation :alien
323 :reader param-double-float-default-value
324 :type double-float)
325 (epsilon
326 :allocation :alien
327 :reader param-double-float-epsilon
328 :type double-float))
329 (:metaclass param-spec-class)
330 (:gtype "GParamDouble"))
331
332 (defclass param-string (param)
333 ((default-value
334 :allocation :alien
335 :reader param-string-default-value
336 :type string))
337 (:metaclass param-spec-class)
338 (:gtype "GParamString"))
339
340 (defclass param-param (param)
341 ()
342 (:metaclass param-spec-class)
343 (:gtype "GParamParam"))
344
345 (defclass param-boxed (param)
346 ()
347 (:metaclass param-spec-class)
348 (:gtype "GParamBoxed"))
349
350 (defclass param-pointer (param)
351 ()
352 (:metaclass param-spec-class)
353 (:gtype "GParamPointer"))
354
355 (defclass param-value-array (param)
356 ((element-spec
357 :allocation :alien
358 :reader param-value-array-element-spec
359 :type param)
360 (length
361 :allocation :alien
362 :reader param-value-array-length
363 :type unsigned-int))
364 (:metaclass param-spec-class)
365 (:gtype "GParamValueArray"))
366
367 (defclass param-object (param)
368 ()
369 (:metaclass param-spec-class)
370 (:gtype "GParamObject"))
371
372 (defclass param-overrride (param)
373 ()
374 (:metaclass param-spec-class)
375 (:gtype "GParamOverride"))