1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
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.
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.
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
18 ;; $Id: genums.lisp,v 1.4 2004-11-06 21:39:58 espen Exp $
23 (defun %map-enum (args op)
24 (let ((current-value 0))
27 (destructuring-bind (symbol &optional (value current-value))
29 (setf current-value (1+ value))
31 (:enum-int (list symbol value))
32 (:flags-int (list symbol value))
33 (:int-enum (list value symbol))
34 (:int-flags (list value symbol))
38 (defun %query-enum-or-flags-values (query-function class type)
39 (multiple-value-bind (sap length)
40 (funcall query-function (type-class-ref type))
42 (size (proxy-instance-size (find-class class)))
43 (proxy (make-instance class :location sap)))
45 (with-slots (location nickname value) proxy
47 (setq sap (sap+ sap size))
50 (intern (substitute #\- #\_ (string-upcase nickname)) "KEYWORD")
56 ;;;; Generic enum type
58 (deftype enum (&rest args)
59 `(member ,@(%map-enum args :symbols)))
61 (defmethod alien-type ((type (eql 'enum)) &rest args)
62 (declare (ignore type args))
65 (defmethod size-of ((type (eql 'enum)) &rest args)
66 (declare (ignore type args))
69 (defmethod to-alien-form (form (type (eql 'enum)) &rest args)
70 (declare (ignore type))
72 ,@(%map-enum args :enum-int)))
74 (defmethod from-alien-form (form (type (eql 'enum)) &rest args)
75 (declare (ignore type))
77 ,@(%map-enum args :int-enum)))
79 (defmethod to-alien-function ((type (eql 'enum)) &rest args)
80 (let ((mappings (%map-enum args :enum-int)))
83 (second (assoc enum mappings))
84 (error "~S is not of type ~S" enum (cons type args))))))
86 (defmethod from-alien-function ((type (eql 'enum)) &rest args)
87 (declare (ignore type))
88 (let ((mappings (%map-enum args :int-enum)))
90 (second (assoc int mappings)))))
92 (defmethod writer-function ((type (eql 'enum)) &rest args)
93 (declare (ignore type))
94 (let ((writer (writer-function 'signed))
95 (function (apply #'to-alien-function 'enum args)))
96 #'(lambda (enum location &optional (offset 0))
97 (funcall writer (funcall function enum) location offset))))
99 (defmethod reader-function ((type (eql 'enum)) &rest args)
100 (declare (ignore type))
101 (let ((reader (reader-function 'signed))
102 (function (apply #'from-alien-function 'enum args)))
103 #'(lambda (location &optional (offset 0))
104 (funcall function (funcall reader location offset)))))
108 (defclass %enum-value (struct)
109 ((value :allocation :alien :type int)
110 (name :allocation :alien :type string)
111 (nickname :allocation :alien :type string))
112 (:metaclass static-struct-class))
114 (defbinding %enum-class-values () pointer
116 (n-values unsigned-int :out))
118 (defun query-enum-values (type)
119 (%query-enum-or-flags-values #'%enum-class-values '%enum-value type))
123 ;;;; Generic flags type
125 (deftype flags (&rest args)
126 `(or null (cons (member ,@(%map-enum args :symbols)) list)))
128 (defmethod alien-type ((type (eql 'flags)) &rest args)
129 (declare (ignore type args))
130 (alien-type 'unsigned))
132 (defmethod size-of ((type (eql 'flags)) &rest args)
133 (declare (ignore type args))
136 (defmethod to-alien-form (flags (type (eql 'flags)) &rest args)
140 for flag in (mklist flags)
143 (second (assoc flag ',(%map-enum args :flags-int)))
144 (error "~S is not of type ~S" flags '(,type ,@args)))))
145 (setq value (logior value flagval)))
146 finally (return value)))
148 (defmethod from-alien-form (int (type (eql 'flags)) &rest args)
149 (declare (ignore type))
151 for mapping in ',(%map-enum args :int-flags)
152 unless (zerop (logand int (first mapping)))
153 collect (second mapping)))
155 (defmethod to-alien-function ((type (eql 'flags)) &rest args)
156 (let ((mappings (%map-enum args :flags-int)))
160 for flag in (mklist flags)
161 do (let ((flagval (or
162 (second (assoc flag mappings))
163 (error "~S is not of type ~S" flags (cons type args)))))
164 (setq value (logior value flagval)))
165 finally (return value)))))
167 (defmethod from-alien-function ((type (eql 'flags)) &rest args)
168 (declare (ignore type))
169 (let ((mappings (%map-enum args :int-flags)))
172 for mapping in mappings
173 unless (zerop (logand int (first mapping)))
174 collect (second mapping)))))
176 (defmethod writer-function ((type (eql 'flags)) &rest args)
177 (declare (ignore type))
178 (let ((writer (writer-function 'unsigned))
179 (function (apply #'to-alien-function 'flags args)))
180 #'(lambda (flags location &optional (offset 0))
181 (funcall writer (funcall function flags) location offset))))
183 (defmethod reader-function ((type (eql 'flags)) &rest args)
184 (declare (ignore type))
185 (let ((reader (reader-function 'unsigned))
186 (function (apply #'from-alien-function 'flags args)))
187 #'(lambda (location &optional (offset 0))
188 (funcall function (funcall reader location offset)))))
192 (defclass %flags-value (struct)
193 ((value :allocation :alien :type unsigned-int)
194 (name :allocation :alien :type string)
195 (nickname :allocation :alien :type string))
196 (:metaclass static-struct-class))
198 (defbinding %flags-class-values () pointer
200 (n-values unsigned-int :out))
202 (defun query-flags-values (type)
203 (%query-enum-or-flags-values #'%flags-class-values '%flags-value type))
209 (defun expand-enum-type (type-number &optional options)
210 (let* ((super (supertype type-number))
211 (type (type-from-number type-number))
212 (mappings (getf options :mappings))
218 (assoc (first mapping) mappings)
219 (rassoc (cdr mapping) mappings :test #'equal)))
221 (query-enum-values type-number)
222 (query-flags-values type-number)))
224 #'(lambda (mapping) (eq (second mapping) nil)) mappings))))
226 (register-type ',type ,(find-type-name type-number))
227 (deftype ,type () '(,super ,@expanded-mappings)))))
230 (register-derivable-type 'enum "GEnum" 'expand-enum-type)
231 (register-derivable-type 'flags "GFlags" 'expand-enum-type)