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