X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/clg/blobdiff_plain/86d9d6ab4254f6f17fe7fbd15bee6b869ff8a145..de8516caf7f9031a1cbffa1382c4a3f97a892350:/glib/gobject.lisp diff --git a/glib/gobject.lisp b/glib/gobject.lisp index 09b7cf7..6e4e4e0 100644 --- a/glib/gobject.lisp +++ b/glib/gobject.lisp @@ -1,5 +1,5 @@ ;; Common Lisp bindings for GTK+ v2.0 -;; Copyright (C) 2000 Espen S. Johnsen +;; Copyright (C) 2000-2001 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 @@ -15,109 +15,82 @@ ;; License along with this library; if not, write to the Free Software ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -;; $Id: gobject.lisp,v 1.2 2000-08-23 21:40:38 espen Exp $ +;; $Id: gobject.lisp,v 1.11 2002-03-24 12:50:30 espen Exp $ (in-package "GLIB") (eval-when (:compile-toplevel :load-toplevel :execute) - (defclass gobject (gtype) + (defclass gobject (ginstance) () - (:metaclass gtype-class) - (:alien-name "GObject")) - - (defclass gobject-class (gtype-class))) - - -;;;; Reference counting for gobject - -;; Specializing reference-instance and unreference-instance on gobject -;; is not really necessary but done for efficiency - -(defmethod reference-instance ((object gobject)) - (%object-ref object) - object) - -(defmethod unreference-instance ((object gobject)) - (%object-unref object)) - -(deftype-method alien-ref gobject (type-spec) - (declare (ignore type-spec)) - '%object-ref) - -(deftype-method alien-unref gobject (type-spec) - (declare (ignore type-spec)) - '%object-unref) + (:metaclass ginstance-class) + (:alien-name "GObject") + (:copy %object-ref) + (:free %object-unref))) + +(defmethod initialize-instance ((object gobject) &rest initargs) + (declare (ignore initargs)) + (setf (slot-value object 'location) (%gobject-new (type-number-of object))) + (call-next-method)) -(define-foreign %object-ref () pointer - (object (or gobject pointer))) +(defbinding (%gobject-new "g_object_new") () pointer + (type type-number) + (nil null)) -(define-foreign %object-unref () nil - (object (or gobject pointer))) +(defbinding %object-ref (type location) pointer + (location pointer)) -;; Parameter stuff not yet implemented +(defbinding %object-unref (type location) nil + (location pointer)) -; (define-foreign object-set-param () nil -; (object gobject) -; (name string) -; (value gvalue)) -; (define-foreign object-get-param () nil -; (object gobject) -; (name string) -; (value gvalue :out)) +(defun object-ref (object) + (%object-ref nil (proxy-location object))) -; (define-foreign object-queue-param-changed () nil -; (object gobject) -; (name string)) +(defun object-unref (object) + (%object-unref nil (proxy-location object))) -;;;; User data mechanism -(declaim (fixnum *user-data-count*)) +;;;; Property stuff -(defvar *user-data* (make-hash-table)) -(defvar *user-data-count* 0) +(defbinding %object-set-property () nil + (object gobject) + (name string) + (value gvalue)) -;; Until the callback mechanism is moved to glib, the value of -;; *destroy-marshal* is set in gtkobject.lisp -(defvar *destroy-marshal* nil) +(defbinding %object-get-property () nil + (object gobject) + (name string) + (value gvalue)) -(defun register-user-data (object &optional destroy-function) - (check-type destroy-function (or null symbol function)) -; (incf *user-data-count*) - (setq *user-data-count* (the fixnum (1+ *user-data-count*))) - (setf - (gethash *user-data-count* *user-data*) - (cons object destroy-function)) - *user-data-count*) +(defbinding %object-notify () nil + (object gobject) + (name string)) -(defun find-user-data (id) - (check-type id fixnum) - (multiple-value-bind (user-data p) (gethash id *user-data*) - (values (car user-data) p))) +(defbinding object-freeze-notify () nil + (object gobject)) -(defun destroy-user-data (id) - (check-type id fixnum) - (let ((user-data (gethash id *user-data*))) - (when (cdr user-data) - (funcall (cdr user-data) (car user-data)))) - (remhash id *user-data*)) +(defbinding object-thaw-notify () nil + (object gobject)) -(define-foreign %object-set-qdata-full () nil +(defbinding %object-set-qdata-full () nil (object gobject) (id quark) (data unsigned-long) (destroy-marshal pointer)) + +;;;; User data + (defun (setf object-data) (data object key &key (test #'eq)) (%object-set-qdata-full object (quark-from-object key :test test) - (register-user-data data) *destroy-marshal*) + (register-user-data data) *destroy-notify*) data) -(define-foreign %object-get-qdata () unsigned-long +(defbinding %object-get-qdata () unsigned-long (object gobject) (id quark)) @@ -127,31 +100,173 @@ (defun object-data (object key &key (test #'eq)) -;;;; Methods for gobject-class +;;;; Metaclass used for subclasses of gobject -(defmethod shared-initialize ((class gobject-class) names &rest initargs - &key type-init name) - (declare (ignore initargs names)) - (let ((alien - (alien::%heap-alien - (alien::make-heap-alien-info - :type (alien::parse-alien-type '(function (unsigned 32))) - :sap-form (system:foreign-symbol-address - (or - (first type-init) - (default-alien-func-name - (format - nil "~A_get_type" (or name (class-name class)))))))))) - (alien:alien-funcall alien)) - (call-next-method)) +(eval-when (:compile-toplevel :load-toplevel :execute) + (defclass gobject-class (ginstance-class)) + + (defclass direct-gobject-slot-definition (direct-virtual-slot-definition) + ((pname :reader slot-definition-pname))) + (defclass effective-gobject-slot-definition + (effective-virtual-slot-definition))) -; (define-foreign object-class-install-param () nil + + +; (defbinding object-class-install-param () nil ; (class pointer) ; (id unsigned-int) ; (parameter parameter)) -; (define-foreign object-class-find-param-spec () parameter +; (defbinding object-class-find-param-spec () parameter ; (class pointer) ; (name string)) +(defun signal-name-to-string (name) + (substitute #\_ #\- (string-downcase (string name)))) + +(defmethod initialize-instance :after ((slotd direct-gobject-slot-definition) + &rest initargs &key pname) + (declare (ignore initargs)) + (when pname + (setf + (slot-value slotd 'pname) + (signal-name-to-string (slot-definition-name slotd))))) + +(defmethod direct-slot-definition-class ((class gobject-class) initargs) + (case (getf initargs :allocation) + (:property (find-class 'direct-gobject-slot-definition)) + (t (call-next-method)))) + +(defmethod effective-slot-definition-class ((class gobject-class) initargs) + (case (getf initargs :allocation) + (:property (find-class 'effective-gobject-slot-definition)) + (t (call-next-method)))) + +(defmethod compute-virtual-slot-accessors + ((class gobject-class) (slotd effective-gobject-slot-definition) + direct-slotds) + (with-slots (type) slotd + (let ((pname (slot-definition-pname (first direct-slotds))) + (type-number (find-type-number type))) + (list + #'(lambda (object) + (with-gc-disabled + (let ((gvalue (gvalue-new type-number))) + (%object-get-property object pname gvalue) + (unwind-protect + (funcall + (intern-reader-function type) gvalue +gvalue-value-offset+) + (gvalue-free gvalue t))))) + #'(lambda (value object) + (with-gc-disabled + (let ((gvalue (gvalue-new type-number))) + (funcall + (intern-writer-function type) + value gvalue +gvalue-value-offset+) + (%object-set-property object pname gvalue) + (funcall + (intern-destroy-function type) + gvalue +gvalue-value-offset+) + (gvalue-free gvalue nil) + value))))))) + +(defmethod validate-superclass ((class gobject-class) + (super pcl::standard-class)) +; (subtypep (class-name super) 'gobject) + t) + + + +;;;; + +(defbinding %object-class-list-properties () pointer + (class pointer) + (n-properties unsigned-int :out)) + +(defun query-object-class-properties (type-number &optional + inherited-properties) + (let ((class (type-class-ref type-number))) + (multiple-value-bind (array length) + (%object-class-list-properties class) + (unwind-protect + (let ((all-properties + (map-c-array 'list #'identity array 'param length))) + (if (not inherited-properties) + (delete-if + #'(lambda (param) + (not (eql type-number (param-owner-type param)))) + all-properties) + all-properties)) + (deallocate-memory array))))) + + +(defun default-slot-name (name) + (intern (substitute #\- #\_ (string-upcase (string-upcase name))))) + +(defun default-slot-accessor (class-name slot-name type) + (intern + (format + nil "~A-~A~A" class-name slot-name + (if (eq 'boolean type) "-P" "")))) + +(defun expand-gobject-type (type-number &optional options + (metaclass 'gobject-class)) + (let* ((supers (cons (supertype type-number) (implements type-number))) + (class (type-from-number type-number)) + (override-slots (getf options :slots)) + (expanded-slots + (mapcar + #'(lambda (param) + (with-slots (name flags value-type documentation) param + (let* ((slot-name (default-slot-name name)) + (slot-type (type-from-number value-type #|t|#)) + (accessor + (default-slot-accessor class slot-name slot-type))) + `(,slot-name + :allocation :property + :pname ,name + ,@(cond + ((and + (member :writable flags) + (member :readable flags)) + (list :accessor accessor)) + ((member :writable flags) + (list :writer `(setf ,accessor))) + ((member :readable flags) + (list :reader accessor))) + ,@(when (or + (member :construct flags) + (member :writable flags)) + (list :initarg (intern (string slot-name) "KEYWORD"))) + :type ,slot-type + ,@(when documentation + (list :documentation documentation)))))) + (query-object-class-properties type-number)))) + + (dolist (slot-def override-slots) + (let ((name (car slot-def)) + (pname (getf (cdr slot-def) :pname))) + (setq + expanded-slots + (delete-if + #'(lambda (expanded-slot-def) + (or + (eq name (car expanded-slot-def)) + (and + pname + (string= pname (getf (cdr expanded-slot-def) :pname))))) + expanded-slots)) + + (unless (getf (cdr slot-def) :ignore) + (push slot-def expanded-slots)))) + + `(progn + (defclass ,class ,supers + ,expanded-slots + (:metaclass ,metaclass) + (:alien-name ,(find-type-name type-number)))))) + + +(register-derivable-type 'gobject "GObject" 'expand-gobject-type) +