ea3fa447b56c5779eada5a1e52eb5702fbcfa55d
[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.26 2007-08-20 10:50:25 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 '(unsigned-byte 64))))
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) temp-p)
46 (%gvalue-init gvalue (find-type-number type))
47 (when value-p
48 (funcall (writer-function type :temp temp-p) 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 ;; We need to search for the for the most specific known type
65 ;; because internal types, unknown to Lisp, may be passed in GValues
66 (labels ((find-most-specific-known-type (type)
67 (or
68 (type-from-number type)
69 (let ((parent (type-parent type)))
70 (unless (zerop parent)
71 (find-most-specific-known-type parent))))))
72 (let ((type-number (ref-type-number gvalue)))
73 (unless (zerop type-number)
74 (or
75 (find-most-specific-known-type type-number)
76 ;; This will signal an error if the type hierarchy is unknown
77 (type-from-number type-number t))))))
78
79 (defun gvalue-get (gvalue)
80 (funcall (reader-function (gvalue-type gvalue))
81 gvalue +gvalue-value-offset+))
82
83 (defun gvalue-peek (gvalue)
84 (funcall (reader-function (gvalue-type gvalue) :ref :peek)
85 gvalue +gvalue-value-offset+))
86
87 (defun gvalue-take (gvalue)
88 (funcall (reader-function (gvalue-type gvalue) :ref :get)
89 gvalue +gvalue-value-offset+))
90
91 (defun gvalue-set (gvalue value)
92 (funcall (writer-function (gvalue-type gvalue))
93 value gvalue +gvalue-value-offset+)
94 value)
95
96 (defbinding (gvalue-p "g_type_check_value") () boolean
97 (location pointer))
98
99 (defmacro with-gvalue ((gvalue &optional type (value nil value-p)) &body body)
100 `(with-memory (,gvalue +gvalue-size+)
101 ,(cond
102 ((and type value-p) `(gvalue-init ,gvalue ,type ,value t))
103 (type `(gvalue-init ,gvalue ,type)))
104 ,@body
105 ,(unless value-p `(gvalue-take ,gvalue))))
106
107
108 (deftype param-flag-type ()
109 '(flags
110 (:readable 1)
111 (:writable 2)
112 (:construct 4)
113 (:construct-only 8)
114 (:lax-validation 16)
115 (:private 32)))
116
117 (eval-when (:compile-toplevel :load-toplevel :execute)
118 (defclass param-spec-class (ginstance-class)
119 ())
120
121 (defmethod shared-initialize ((class param-spec-class) names &rest initargs)
122 (declare (ignore names initargs))
123 (call-next-method)
124 (unless (slot-boundp class 'ref)
125 (setf (slot-value class 'ref) '%param-spec-ref))
126 (unless (slot-boundp class 'unref)
127 (setf (slot-value class 'unref) '%param-spec-unref)))
128
129 (defmethod validate-superclass ((class param-spec-class) (super standard-class))
130 t ;(subtypep (class-name super) 'param)
131 ))
132
133
134 (defbinding %param-spec-ref () pointer
135 (location pointer))
136
137 (defbinding %param-spec-unref () nil
138 (location pointer))
139
140
141 ;; TODO: rename to param-spec
142 (defclass param (ginstance)
143 ((name
144 :allocation :alien
145 :reader param-name
146 :type string)
147 (flags
148 :allocation :alien
149 :reader param-flags
150 :type param-flag-type)
151 (value-type
152 :allocation :alien
153 :reader param-value-type
154 :type type-number)
155 (owner-type
156 :allocation :alien
157 :reader param-owner-type
158 :type type-number)
159 (nickname
160 :allocation :virtual
161 :getter "g_param_spec_get_nick"
162 :reader param-nickname
163 :type (copy-of string))
164 (documentation
165 :allocation :virtual
166 :getter "g_param_spec_get_blurb"
167 :reader param-documentation
168 :type (copy-of string)))
169 (:metaclass param-spec-class)
170 (:gtype "GParam"))
171
172
173 (defclass param-char (param)
174 ((minimum
175 :allocation :alien
176 :reader param-minimum
177 :type char)
178 (maximum
179 :allocation :alien
180 :reader param-maximum
181 :type char)
182 (default-value
183 :allocation :alien
184 :reader param-default-value
185 :type char))
186 (:metaclass param-spec-class)
187 (:gtype "GParamChar"))
188
189 (defclass param-unsigned-char (param)
190 (
191 ; (minimum
192 ; :allocation :alien
193 ; :reader param-unsigned-char-minimum
194 ; :type unsigned-char)
195 ; (maximum
196 ; :allocation :alien
197 ; :reader param-unsigned-char-maximum
198 ; :type unsigned-char)
199 ; (default-value
200 ; :allocation :alien
201 ; :reader param-unsigned-char-default-value
202 ; :type unsigned-char)
203 )
204 (:metaclass param-spec-class)
205 (:gtype "GParamUChar"))
206
207 (defclass param-boolean (param)
208 ((default-value
209 :allocation :alien
210 :reader param-default-value
211 :type boolean))
212 (:metaclass param-spec-class)
213 (:gtype "GParamBoolean"))
214
215 (defclass param-int (param)
216 ((minimum
217 :allocation :alien
218 :reader param-minimum
219 :type int)
220 (maximum
221 :allocation :alien
222 :reader param-maximum
223 :type int)
224 (default-value
225 :allocation :alien
226 :reader param-default-value
227 :type int))
228 (:metaclass param-spec-class)
229 (:gtype "GParamInt"))
230
231 (defclass param-unsigned-int (param)
232 ((minimum
233 :allocation :alien
234 :reader param-minimum
235 :type unsigned-int)
236 (maximum
237 :allocation :alien
238 :reader param-maximum
239 :type unsigned-int)
240 (default-value
241 :allocation :alien
242 :reader param-default-value
243 :type unsigned-int))
244 (:metaclass param-spec-class)
245 (:gtype "GParamUInt"))
246
247 (defclass param-long (param)
248 ((minimum
249 :allocation :alien
250 :reader param-minimum
251 :type long)
252 (maximum
253 :allocation :alien
254 :reader param-maximum
255 :type long)
256 (default-value
257 :allocation :alien
258 :reader param-default-value
259 :type long))
260 (:metaclass param-spec-class)
261 (:gtype "GParam"))
262
263 (defclass param-unsigned-long (param)
264 ((minimum
265 :allocation :alien
266 :reader param-minimum
267 :type unsigned-long)
268 (maximum
269 :allocation :alien
270 :reader param-maximum
271 :type unsigned-long)
272 (default-value
273 :allocation :alien
274 :reader param-default-value
275 :type unsigned-long))
276 (:metaclass param-spec-class)
277 (:gtype "GParamULong"))
278
279 (defclass param-unichar (param)
280 ()
281 (:metaclass param-spec-class)
282 (:gtype "GParamUnichar"))
283
284 (defclass param-enum (param)
285 ((class
286 :allocation :alien
287 :reader param-enum-class
288 :type pointer)
289 (default-value
290 :allocation :alien
291 :reader param-default-value
292 :type long))
293 (:metaclass param-spec-class)
294 (:gtype "GParamEnum"))
295
296 (defclass param-flags (param)
297 ((class
298 :allocation :alien
299 :reader param-flags-class
300 :type pointer)
301 (default-value
302 :allocation :alien
303 :reader param-default-value
304 :type long))
305 (:metaclass param-spec-class)
306 (:gtype "GParamFlags"))
307
308 (defclass param-single-float (param)
309 ((minimum
310 :allocation :alien
311 :reader param-minimum
312 :type single-float)
313 (maximum
314 :allocation :alien
315 :reader param-maximum
316 :type single-float)
317 (default-value
318 :allocation :alien
319 :reader param-default-value
320 :type single-float)
321 (epsilon
322 :allocation :alien
323 :reader param-float-epsilon
324 :type single-float))
325 (:metaclass param-spec-class)
326 (:gtype "GParamFloat"))
327
328 (defclass param-double-float (param)
329 ((minimum
330 :allocation :alien
331 :reader param-minimum
332 :type double-float)
333 (maximum
334 :allocation :alien
335 :reader param-maximum
336 :type double-float)
337 (default-value
338 :allocation :alien
339 :reader param-default-value
340 :type double-float)
341 (epsilon
342 :allocation :alien
343 :reader param-float-epsilon
344 :type double-float))
345 (:metaclass param-spec-class)
346 (:gtype "GParamDouble"))
347
348 (defclass param-string (param)
349 ((default-value
350 :allocation :alien
351 :reader param-default-value
352 :type string))
353 (:metaclass param-spec-class)
354 (:gtype "GParamString"))
355
356 (defclass param-param (param)
357 ()
358 (:metaclass param-spec-class)
359 (:gtype "GParamParam"))
360
361 (defclass param-boxed (param)
362 ()
363 (:metaclass param-spec-class)
364 (:gtype "GParamBoxed"))
365
366 (defclass param-pointer (param)
367 ()
368 (:metaclass param-spec-class)
369 (:gtype "GParamPointer"))
370
371 (defclass param-value-array (param)
372 ((element-spec
373 :allocation :alien
374 :reader param-value-array-element-spec
375 :type param)
376 (length
377 :allocation :alien
378 :reader param-value-array-length
379 :type unsigned-int))
380 (:metaclass param-spec-class)
381 (:gtype "GParamValueArray"))
382
383 (defclass param-object (param)
384 ()
385 (:metaclass param-spec-class)
386 (:gtype "GParamObject"))
387
388 (defclass param-overrride (param)
389 ()
390 (:metaclass param-spec-class)
391 (:gtype "GParamOverride"))