+;;;; Metaclass used for subclasses of gobject
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+ (defclass gobject-class (ginstance-class)
+ ((instance-slots-p :initform nil
+ :documentation "Non NIL if the class has slots with instance allocation")))
+
+ (defmethod validate-superclass ((class gobject-class) (super standard-class))
+; (subtypep (class-name super) 'gobject)
+ t))
+
+(defclass direct-property-slot-definition (direct-virtual-slot-definition)
+ ((pname :reader slot-definition-pname :initarg :pname)
+ (readable :initform t :reader slot-readable-p :initarg :readable)
+ (writable :initform t :reader slot-writable-p :initarg :writable)
+ (construct :initform nil :initarg :construct)))
+
+(defclass effective-property-slot-definition (effective-virtual-slot-definition)
+ ((pname :reader slot-definition-pname :initarg :pname)
+ (readable :reader slot-readable-p :initarg :readable)
+ (writable :reader slot-writable-p :initarg :writable)
+ (construct :initarg :construct)))
+
+(defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
+ ())
+
+(defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
+ ())
+
+
+(defbinding %object-ref () pointer
+ (location pointer))
+
+(defbinding %object-unref () nil
+ (location pointer))
+
+(defcallback toggle-ref-callback (nil (data pointer) (location pointer) (last-ref-p boolean))
+ (if last-ref-p
+ (cache-instance (find-cached-instance location) t)
+ (cache-instance (find-cached-instance location) nil)))
+
+(defbinding %object-add-toggle-ref () pointer
+ (location pointer)
+ ((callback toggle-ref-callback) pointer)
+ (nil null))
+
+(defbinding %object-remove-toggle-ref () pointer
+ (location pointer)
+ ((callback toggle-ref-callback) pointer)
+ (nil null))
+
+(defmethod reference-foreign ((class gobject-class) location)
+ (declare (ignore class))
+ (if (slot-value class 'instance-slots-p)
+ (%object-add-toggle-ref location)
+ (%object-ref location)))
+
+(defmethod unreference-foreign ((class gobject-class) location)
+ (declare (ignore class))
+ (error "Should never be called on a GOBJECT-CLASS (if this is ever needed some redesigning would have to be done)")
+; (%object-unref location)
+)
+
+
+
+; (defbinding object-class-install-param () nil
+; (class pointer)
+; (id unsigned-int)
+; (parameter parameter))
+
+; (defbinding object-class-find-param-spec () parameter
+; (class pointer)
+; (name string))
+
+(defun signal-name-to-string (name)
+ (substitute #\_ #\- (string-downcase (string name))))
+
+
+(defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
+ (case (getf initargs :allocation)
+ (:property (find-class 'direct-property-slot-definition))
+ (:user-data (find-class 'direct-user-data-slot-definition))
+ (t (call-next-method))))
+
+(defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
+ (case (getf initargs :allocation)
+ (:property (find-class 'effective-property-slot-definition))
+ (:user-data (find-class 'effective-user-data-slot-definition))
+ (t (call-next-method))))
+
+(defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
+ (if (typep (first direct-slotds) 'direct-property-slot-definition)
+ (nconc
+ (list :pname (signal-name-to-string
+ (most-specific-slot-value direct-slotds 'pname))
+ :readable (most-specific-slot-value direct-slotds 'readable)
+ :writable (most-specific-slot-value direct-slotds 'writable)
+ :construct (most-specific-slot-value direct-slotds 'construct))
+ (call-next-method))
+ (call-next-method)))
+
+
+(defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
+ (let ((type (slot-definition-type slotd))
+ (pname (slot-definition-pname slotd)))
+ (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
+ (setf
+ (slot-value slotd 'getter)
+ (let ((reader nil))
+ #'(lambda (object)
+ (unless reader
+ (setq reader (reader-function type)))
+ (let ((gvalue (gvalue-new type)))
+ (%object-get-property object pname gvalue)
+ (unwind-protect
+ (funcall reader gvalue +gvalue-value-offset+)
+ (gvalue-free gvalue t)))))))
+
+ (when (and (not (slot-boundp slotd 'setter)) (slot-writable-p slotd))
+ (setf
+ (slot-value slotd 'setter)
+ (let ((writer nil))
+ #'(lambda (value object)
+ (unless writer
+ (setq writer (writer-function type)))
+ (let ((gvalue (gvalue-new type)))
+ (funcall writer value gvalue +gvalue-value-offset+)
+ (%object-set-property object pname gvalue)
+ (gvalue-free gvalue t)
+ value))))))
+
+ (call-next-method))
+
+(defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ (unless (slot-boundp slotd 'getter)
+ (setf
+ (slot-value slotd 'getter)
+ #'(lambda (object)
+ (prog1 (user-data object slot-name)))))
+ (unless (slot-boundp slotd 'setter)
+ (setf
+ (slot-value slotd 'setter)
+ #'(lambda (value object)
+ (setf (user-data object slot-name) value))))
+ (unless (slot-boundp slotd 'boundp)
+ (setf
+ (slot-value slotd 'boundp)
+ #'(lambda (object)
+ (user-data-p object slot-name)))))
+ (call-next-method))
+
+(defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
+ (declare (ignore initargs))
+ (when (some #'(lambda (slotd)
+ (and
+ (eq (slot-definition-allocation slotd) :instance)
+ (not (typep slotd 'effective-special-slot-definition))))
+ (class-slots class))
+ (setf (slot-value class 'instance-slots-p) t)))
+
+
+
+;;;; Super class for all classes in the GObject type hierarchy
+