| 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: genums.lisp,v 1.20 2006-04-25 21:59:37 espen Exp $ |
| 24 | |
| 25 | (in-package "GLIB") |
| 26 | |
| 27 | ;;;; Definition of enums and flags by introspection |
| 28 | |
| 29 | (eval-when (:compile-toplevel :load-toplevel :execute) |
| 30 | (defclass enum-value (struct) |
| 31 | ((value :allocation :alien :type int) |
| 32 | (name :allocation :alien :type string) |
| 33 | (nickname :allocation :alien :type string)) |
| 34 | (:metaclass struct-class))) |
| 35 | |
| 36 | (defun map-enum-values (values) |
| 37 | (map 'list |
| 38 | #'(lambda (enum-value) |
| 39 | (with-slots (nickname value) enum-value |
| 40 | (list |
| 41 | (intern (substitute #\- #\_ (string-upcase nickname)) "KEYWORD") |
| 42 | value))) |
| 43 | values)) |
| 44 | |
| 45 | (defbinding enum-class-values () (static (vector (inlined enum-value) n-values)) |
| 46 | (class pointer) |
| 47 | (n-values unsigned-int :out)) |
| 48 | |
| 49 | (defbinding flags-class-values () (static (vector (inlined enum-value) n-values)) |
| 50 | (class pointer) |
| 51 | (n-values unsigned-int :out)) |
| 52 | |
| 53 | (defun query-enum-values (type) |
| 54 | (let ((class (type-class-ref type))) |
| 55 | (map-enum-values (if (eq (supertype type) 'enum) |
| 56 | (enum-class-values class) |
| 57 | (flags-class-values class))))) |
| 58 | |
| 59 | (defun expand-enum-type (type-number forward-p options) |
| 60 | (declare (ignore forward-p)) |
| 61 | (let* ((type (type-from-number type-number)) |
| 62 | (mappings (getf options :mappings)) |
| 63 | (expanded-mappings |
| 64 | (append |
| 65 | (delete-if |
| 66 | #'(lambda (mapping) |
| 67 | (or |
| 68 | (assoc (first mapping) mappings) |
| 69 | (rassoc (cdr mapping) mappings :test #'equal))) |
| 70 | (query-enum-values type-number)) |
| 71 | (remove-if |
| 72 | #'(lambda (mapping) (eq (second mapping) nil)) mappings)))) |
| 73 | `(progn |
| 74 | (register-type ',type ',(find-type-init-function type-number)) |
| 75 | ,(ecase (supertype type-number) |
| 76 | (enum `(define-enum-type ,type ,@expanded-mappings)) |
| 77 | (flags `(define-flags-type ,type ,@expanded-mappings)))))) |
| 78 | |
| 79 | |
| 80 | (register-derivable-type 'enum "GEnum" 'expand-enum-type) |
| 81 | (register-derivable-type 'flags "GFlags" 'expand-enum-type) |
| 82 | |