X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/clg/blobdiff_plain/dfa4f31424800fdb6fb63514121b9cc893269276..b8a9e4d372bce9188a094d07cc738554239d8470:/glib/gparam.lisp diff --git a/glib/gparam.lisp b/glib/gparam.lisp index 2307924..ea3fa44 100644 --- a/glib/gparam.lisp +++ b/glib/gparam.lisp @@ -1,21 +1,26 @@ -;; Common Lisp bindings for GTK+ v2.0 -;; Copyright (C) 2000-2005 Espen S. Johnsen +;; Common Lisp bindings for GTK+ v2.x +;; Copyright 2000-2006 Espen S. Johnsen ;; -;; This library is free software; you can redistribute it and/or -;; modify it under the terms of the GNU Lesser General Public -;; License as published by the Free Software Foundation; either -;; version 2 of the License, or (at your option) any later version. +;; Permission is hereby granted, free of charge, to any person obtaining +;; a copy of this software and associated documentation files (the +;; "Software"), to deal in the Software without restriction, including +;; without limitation the rights to use, copy, modify, merge, publish, +;; distribute, sublicense, and/or sell copies of the Software, and to +;; permit persons to whom the Software is furnished to do so, subject to +;; the following conditions: ;; -;; This library is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -;; Lesser General Public License for more details. +;; The above copyright notice and this permission notice shall be +;; included in all copies or substantial portions of the Software. ;; -;; You should have received a copy of the GNU Lesser General Public -;; License along with this library; if not, write to the Free Software -;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -;; $Id: gparam.lisp,v 1.16 2005-03-06 17:26:23 espen Exp $ +;; $Id: gparam.lisp,v 1.26 2007-08-20 10:50:25 espen Exp $ (in-package "GLIB") @@ -26,10 +31,9 @@ (register-type 'gvalue '|g_value_get_type|) (eval-when (:compile-toplevel :load-toplevel :execute) (defbinding (size-of-gvalue "size_of_gvalue") () unsigned-int)) -;(defconstant +gvalue-size+ (+ (size-of 'type-number) (* 2 (size-of 'double-float)))) -(defconstant +gvalue-size+ #.(size-of-gvalue)) - -(defconstant +gvalue-value-offset+ (size-of 'type-number)) +(defconstant +gvalue-size+ (size-of-gvalue)) +(defconstant +gvalue-value-offset+ + (max (size-of 'type-number) (type-alignment '(unsigned-byte 64)))) (defbinding (%gvalue-init "g_value_init") () nil (value gvalue) @@ -38,10 +42,10 @@ (defbinding (%gvalue-init "g_value_init") () nil (defbinding (gvalue-unset "g_value_unset") () nil (value gvalue)) -(defun gvalue-init (gvalue type &optional (value nil value-p)) +(defun gvalue-init (gvalue type &optional (value nil value-p) temp-p) (%gvalue-init gvalue (find-type-number type)) (when value-p - (funcall (writer-function type) value gvalue +gvalue-value-offset+))) + (funcall (writer-function type :temp temp-p) value gvalue +gvalue-value-offset+))) (defun gvalue-new (&optional type (value nil value-p)) (let ((gvalue (allocate-memory +gvalue-size+))) @@ -57,12 +61,33 @@ (defun gvalue-free (gvalue &optional (unset-p t)) (deallocate-memory gvalue))) (defun gvalue-type (gvalue) - (type-from-number (sap-ref-32 gvalue 0))) + ;; We need to search for the for the most specific known type + ;; because internal types, unknown to Lisp, may be passed in GValues + (labels ((find-most-specific-known-type (type) + (or + (type-from-number type) + (let ((parent (type-parent type))) + (unless (zerop parent) + (find-most-specific-known-type parent)))))) + (let ((type-number (ref-type-number gvalue))) + (unless (zerop type-number) + (or + (find-most-specific-known-type type-number) + ;; This will signal an error if the type hierarchy is unknown + (type-from-number type-number t)))))) (defun gvalue-get (gvalue) (funcall (reader-function (gvalue-type gvalue)) gvalue +gvalue-value-offset+)) +(defun gvalue-peek (gvalue) + (funcall (reader-function (gvalue-type gvalue) :ref :peek) + gvalue +gvalue-value-offset+)) + +(defun gvalue-take (gvalue) + (funcall (reader-function (gvalue-type gvalue) :ref :get) + gvalue +gvalue-value-offset+)) + (defun gvalue-set (gvalue value) (funcall (writer-function (gvalue-type gvalue)) value gvalue +gvalue-value-offset+) @@ -72,15 +97,12 @@ (defbinding (gvalue-p "g_type_check_value") () boolean (location pointer)) (defmacro with-gvalue ((gvalue &optional type (value nil value-p)) &body body) - `(let ((,gvalue ,(cond - ((and type value-p) `(gvalue-new ,type ,value)) - (type `(gvalue-new ,type)) - (`(gvalue-new))))) - (unwind-protect - (progn - ,@body - ,(unless value-p `(gvalue-get ,gvalue))) - (gvalue-free ,gvalue)))) + `(with-memory (,gvalue +gvalue-size+) + ,(cond + ((and type value-p) `(gvalue-init ,gvalue ,type ,value t)) + (type `(gvalue-init ,gvalue ,type))) + ,@body + ,(unless value-p `(gvalue-take ,gvalue)))) (deftype param-flag-type () @@ -96,6 +118,14 @@ (eval-when (:compile-toplevel :load-toplevel :execute) (defclass param-spec-class (ginstance-class) ()) + (defmethod shared-initialize ((class param-spec-class) names &rest initargs) + (declare (ignore names initargs)) + (call-next-method) + (unless (slot-boundp class 'ref) + (setf (slot-value class 'ref) '%param-spec-ref)) + (unless (slot-boundp class 'unref) + (setf (slot-value class 'unref) '%param-spec-unref))) + (defmethod validate-superclass ((class param-spec-class) (super standard-class)) t ;(subtypep (class-name super) 'param) )) @@ -107,15 +137,6 @@ (defbinding %param-spec-ref () pointer (defbinding %param-spec-unref () nil (location pointer)) -(defmethod reference-foreign ((class param-spec-class) location) - (declare (ignore class)) - (%param-spec-ref location)) - -(defmethod unreference-foreign ((class param-spec-class) location) - (declare (ignore class)) - (%param-spec-unref location)) - - ;; TODO: rename to param-spec (defclass param (ginstance) @@ -152,15 +173,15 @@ (defclass param (ginstance) (defclass param-char (param) ((minimum :allocation :alien - :reader param-char-minimum + :reader param-minimum :type char) (maximum :allocation :alien - :reader param-char-maximum + :reader param-maximum :type char) (default-value :allocation :alien - :reader param-char-default-value + :reader param-default-value :type char)) (:metaclass param-spec-class) (:gtype "GParamChar")) @@ -186,7 +207,7 @@ (defclass param-unsigned-char (param) (defclass param-boolean (param) ((default-value :allocation :alien - :reader param-boolean-default-value + :reader param-default-value :type boolean)) (:metaclass param-spec-class) (:gtype "GParamBoolean")) @@ -194,15 +215,15 @@ (defclass param-boolean (param) (defclass param-int (param) ((minimum :allocation :alien - :reader param-int-minimum + :reader param-minimum :type int) (maximum :allocation :alien - :reader param-int-maximum + :reader param-maximum :type int) (default-value :allocation :alien - :reader param-int-default-value + :reader param-default-value :type int)) (:metaclass param-spec-class) (:gtype "GParamInt")) @@ -210,15 +231,15 @@ (default-value (defclass param-unsigned-int (param) ((minimum :allocation :alien - :reader param-unsigned-int-minimum + :reader param-minimum :type unsigned-int) (maximum :allocation :alien - :reader param-unsigned-int-maximum + :reader param-maximum :type unsigned-int) (default-value :allocation :alien - :reader param-unsigned-int-default-value + :reader param-default-value :type unsigned-int)) (:metaclass param-spec-class) (:gtype "GParamUInt")) @@ -226,15 +247,15 @@ (default-value (defclass param-long (param) ((minimum :allocation :alien - :reader param-long-minimum + :reader param-minimum :type long) (maximum :allocation :alien - :reader param-long-maximum + :reader param-maximum :type long) (default-value :allocation :alien - :reader param-long-default-value + :reader param-default-value :type long)) (:metaclass param-spec-class) (:gtype "GParam")) @@ -242,15 +263,15 @@ (default-value (defclass param-unsigned-long (param) ((minimum :allocation :alien - :reader param-unsigned-long-minimum + :reader param-minimum :type unsigned-long) (maximum :allocation :alien - :reader param-unsigned-long-maximum + :reader param-maximum :type unsigned-long) (default-value :allocation :alien - :reader param-unsigned-long-default-value + :reader param-default-value :type unsigned-long)) (:metaclass param-spec-class) (:gtype "GParamULong")) @@ -267,7 +288,7 @@ (defclass param-enum (param) :type pointer) (default-value :allocation :alien - :reader param-enum-default-value + :reader param-default-value :type long)) (:metaclass param-spec-class) (:gtype "GParamEnum")) @@ -279,7 +300,7 @@ (defclass param-flags (param) :type pointer) (default-value :allocation :alien - :reader param-flags-default-value + :reader param-default-value :type long)) (:metaclass param-spec-class) (:gtype "GParamFlags")) @@ -287,19 +308,19 @@ (default-value (defclass param-single-float (param) ((minimum :allocation :alien - :reader param-single-float-minimum + :reader param-minimum :type single-float) (maximum :allocation :alien - :reader param-single-float-maximum + :reader param-maximum :type single-float) (default-value :allocation :alien - :reader param-single-float-default-value + :reader param-default-value :type single-float) (epsilon :allocation :alien - :reader param-single-float-epsilon + :reader param-float-epsilon :type single-float)) (:metaclass param-spec-class) (:gtype "GParamFloat")) @@ -307,19 +328,19 @@ (default-value (defclass param-double-float (param) ((minimum :allocation :alien - :reader param-double-float-minimum + :reader param-minimum :type double-float) (maximum :allocation :alien - :reader param-double-float-maximum + :reader param-maximum :type double-float) (default-value :allocation :alien - :reader param-double-float-default-value + :reader param-default-value :type double-float) (epsilon :allocation :alien - :reader param-double-float-epsilon + :reader param-float-epsilon :type double-float)) (:metaclass param-spec-class) (:gtype "GParamDouble")) @@ -327,7 +348,7 @@ (default-value (defclass param-string (param) ((default-value :allocation :alien - :reader param-string-default-value + :reader param-default-value :type string)) (:metaclass param-spec-class) (:gtype "GParamString"))