chiark / gitweb /
Changed to MIT license
[clg] / glib / gparam.lisp
index 530e0a2dd47093729e3569976cb01669b1a43e54..d7065807238d251d31b25cb5a9ed9b980d1fbcef 100644 (file)
-;; Common Lisp bindings for GTK+ v2.0
-;; Copyright (C) 2000 Espen S. Johnsen <esj@stud.cs.uit.no>
+;; Common Lisp bindings for GTK+ v2.x
+;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
 ;;
-;; 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.2 2001-04-29 20:17:52 espen Exp $
+;; $Id: gparam.lisp,v 1.17 2005-04-23 16:48:51 espen Exp $
 
 (in-package "GLIB")
 
 (deftype gvalue () 'pointer)
 
-(defconstant +gvalue-size+ (+ (size-of 'type-number) (* 4 (size-of 'double-float))))
+(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))
 
-(define-foreign ("g_value_init" gvalue-init) () nil
+(defbinding (%gvalue-init "g_value_init") () nil
+  (value gvalue)
   (type type-number))
 
-(defun gvalue-new (type)
+(defbinding (gvalue-unset "g_value_unset") () nil
+  (value gvalue))
+
+(defun gvalue-init (gvalue type &optional (value nil value-p))
+  (%gvalue-init gvalue (find-type-number type))
+  (when value-p
+    (funcall (writer-function type) value gvalue +gvalue-value-offset+)))
+
+(defun gvalue-new (&optional type (value nil value-p))
   (let ((gvalue (allocate-memory +gvalue-size+)))
-    (setf (system:sap-ref-32 gvalue 0) type)
-;    (gvalue-init (type-number-of type))
+    (cond
+     (value-p (gvalue-init gvalue type value))
+     (type (gvalue-init gvalue type)))
     gvalue))
 
-(defun gvalue-free (gvalue free-content)
+(defun gvalue-free (gvalue &optional (unset-p t))
   (unless (null-pointer-p gvalue)
-    (when free-content
-      (funcall
-       (intern-destroy-function (gvalue-type gvalue))
-       gvalue +gvalue-value-offset+))
+    (when unset-p
+      (gvalue-unset gvalue))
     (deallocate-memory gvalue)))
 
 (defun gvalue-type (gvalue)
-  (type-from-number (system:sap-ref-32 gvalue 0)))
+  (type-from-number (sap-ref-32 gvalue 0)))
 
 (defun gvalue-get (gvalue)
-  (funcall
-   (intern-reader-function (gvalue-type gvalue))
+  (funcall (reader-function (gvalue-type gvalue))
    gvalue +gvalue-value-offset+))
 
 (defun gvalue-set (gvalue value)
-  (funcall
-   (intern-writer-function (gvalue-type gvalue))
+  (funcall (writer-function (gvalue-type gvalue))
    value gvalue +gvalue-value-offset+)
   value)
 
+(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))))
+
 
 (deftype param-flag-type ()
   '(flags
-    :readable
-    :writable
-    :construct
-    :construct-only
-    :lax-validation
-    :private))
+    (:readable 1)
+    (:writable 2)
+    (:construct 4)
+    (:construct-only 8)
+    (:lax-validation 16)
+    (:private 32)))
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
-  (defclass param (ginstance)
-    ((name
-      :allocation :alien
-      :reader param-name
-      :type string)
-     (nickname
-      :allocation :alien
-      :reader param-nickname
-      :type string)
-     (documentation
-      :allocation :alien
-      :reader param-documentation
-      :type string)
-     (flags
-      :allocation :alien
-      :reader param-flags
-      :type param-flag-type)
-     (type
-      :allocation :alien
-      :reader param-type
-      :type type-number))
-    (:metaclass ginstance-class)
-    (:ref "g_param_spec_ref")
-    (:unref "g_param_spec_unref")))
+  (defclass param-spec-class (ginstance-class)
+    ())
+
+  (defmethod validate-superclass  ((class param-spec-class) (super standard-class))
+    t ;(subtypep (class-name super) 'param)
+))
+
+
+(defbinding %param-spec-ref () pointer
+  (location 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)
+  ((name
+    :allocation :alien
+    :reader param-name
+    :type string)
+   (flags
+    :allocation :alien
+    :reader param-flags
+    :type param-flag-type)
+   (value-type
+    :allocation :alien
+    :reader param-value-type
+    :type type-number)
+   (owner-type
+    :allocation :alien
+    :reader param-owner-type
+    :type type-number)
+   (nickname
+    :allocation :virtual
+    :getter "g_param_spec_get_nick"
+    :reader param-nickname
+    :type (copy-of string))
+   (documentation
+    :allocation :virtual
+    :getter "g_param_spec_get_blurb"
+    :reader param-documentation
+    :type (copy-of string)))
+  (:metaclass param-spec-class)
+  (:gtype "GParam"))
 
 
 (defclass param-char (param)
@@ -105,7 +167,8 @@    (default-value
     :allocation :alien
     :reader param-char-default-value
     :type char))
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamChar"))
 
 (defclass param-unsigned-char (param)
   (
@@ -122,15 +185,16 @@ (defclass param-unsigned-char (param)
 ;     :reader param-unsigned-char-default-value
 ;     :type unsigned-char)
    )
-  (:metaclass ginstance-class)
-  (:alien-name "GParamUChar"))
+  (:metaclass param-spec-class)
+  (:gtype "GParamUChar"))
 
 (defclass param-boolean (param)
   ((default-value
      :allocation :alien
      :reader param-boolean-default-value
      :type boolean))
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamBoolean"))
 
 (defclass param-int (param)
   ((minimum
@@ -145,7 +209,8 @@    (default-value
     :allocation :alien
     :reader param-int-default-value
     :type int))
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamInt"))
 
 (defclass param-unsigned-int (param)
   ((minimum
@@ -160,8 +225,8 @@    (default-value
     :allocation :alien
     :reader param-unsigned-int-default-value
     :type unsigned-int))
-  (:metaclass ginstance-class)
-  (:alien-name "GParamUInt"))
+  (:metaclass param-spec-class)
+  (:gtype "GParamUInt"))
 
 (defclass param-long (param)
   ((minimum
@@ -176,7 +241,8 @@    (default-value
     :allocation :alien
     :reader param-long-default-value
     :type long))
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParam"))
 
 (defclass param-unsigned-long (param)
   ((minimum
@@ -191,12 +257,13 @@    (default-value
     :allocation :alien
     :reader param-unsigned-long-default-value
     :type unsigned-long))
-  (:metaclass ginstance-class)
-  (:alien-name "GParamULong"))
+  (:metaclass param-spec-class)
+  (:gtype "GParamULong"))
 
 (defclass param-unichar (param)
   ()
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamUnichar"))
 
 (defclass param-enum (param)
   ((class
@@ -207,7 +274,8 @@    (default-value
     :allocation :alien
     :reader param-enum-default-value
     :type long))
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamEnum"))
 
 (defclass param-flags (param)
   ((class
@@ -218,7 +286,8 @@    (default-value
     :allocation :alien
     :reader param-flags-default-value
     :type long))
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamFlags"))
 
 (defclass param-single-float (param)
   ((minimum
@@ -237,8 +306,8 @@    (default-value
     :allocation :alien
     :reader param-single-float-epsilon
     :type single-float))
-  (:metaclass ginstance-class)
-  (:alien-name "GParamFloat"))
+  (:metaclass param-spec-class)
+  (:gtype "GParamFloat"))
 
 (defclass param-double-float (param)
   ((minimum
@@ -257,27 +326,31 @@    (default-value
     :allocation :alien
     :reader param-double-float-epsilon
     :type double-float))
-  (:metaclass ginstance-class)
-  (:alien-name "GParamDouble"))
+  (:metaclass param-spec-class)
+  (:gtype "GParamDouble"))
 
 (defclass param-string (param)
   ((default-value
     :allocation :alien
     :reader param-string-default-value
     :type string))
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamString"))
 
 (defclass param-param (param)
   ()
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamParam"))
 
 (defclass param-boxed (param)
   ()
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamBoxed"))
 
 (defclass param-pointer (param)
   ()
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamPointer"))
 
 (defclass param-value-array (param)
   ((element-spec
@@ -288,15 +361,15 @@ (defclass param-value-array (param)
     :allocation :alien
     :reader param-value-array-length
     :type unsigned-int))
-  (:metaclass ginstance-class))
-
-(defclass param-closure (param)
-  ()
-  (:metaclass ginstance-class))
+  (:metaclass param-spec-class)
+  (:gtype "GParamValueArray"))
 
 (defclass param-object (param)
   ()
-  (:metaclass ginstance-class))
-
-
+  (:metaclass param-spec-class)
+  (:gtype "GParamObject"))
 
+(defclass param-overrride (param)
+  ()
+  (:metaclass param-spec-class)
+  (:gtype "GParamOverride"))