chiark / gitweb /
Added code to re-register sub-classed gobject classes when initializing a saved core...
[clg] / glib / gcallback.lisp
index 15d3b94408b3c2e0c16397a5e26ebc11dcfbc0a5..976bc64528a731132fd4bf777cef1f0f05a6ffd1 100644 (file)
@@ -1,21 +1,26 @@
-;; 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 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: gcallback.lisp,v 1.20 2005-02-04 00:15:24 espen Exp $
+;; $Id: gcallback.lisp,v 1.29 2006-02-08 19:56:25 espen Exp $
 
 (in-package "GLIB")
 
@@ -36,6 +41,7 @@ (defcallback closure-marshal (nil
                              (param-values pointer)
                              (invocation-hint pointer) 
                              (callback-id unsigned-int))
+  (declare (ignore gclosure invocation-hint))
   (callback-trampoline callback-id n-params param-values return-value))
 
 ;; Callback function for emission hooks
@@ -52,10 +58,17 @@ (defun callback-trampoline (callback-id n-params param-values &optional
                        (gvalue-type return-value)))
         (args (loop
                for n from 0 below n-params
-               collect (gvalue-get (sap+ param-values (* n +gvalue-size+))))))
-    (let ((result (apply #'invoke-callback callback-id return-type args)))
-      (when return-type
-       (gvalue-set return-value result)))))
+               for offset from 0 by +gvalue-size+
+               collect (gvalue-get (sap+ param-values offset) t))))
+    (unwind-protect
+       (let ((result (apply #'invoke-callback callback-id return-type args)))
+         (when return-type
+           (gvalue-set return-value result)))
+      (loop 
+       for arg in args
+       when (typep arg 'proxy)
+       do (invalidate-instance arg)))))
+
 
 (defun invoke-callback (callback-id return-type &rest args)
   (restart-case
@@ -180,6 +193,78 @@ (defun describe-signal (signal-id &optional type)
 
 ;;;; Signal connecting and controlling
 
+(defvar *overridden-signals* (make-hash-table :test 'equalp))
+
+(defbinding %signal-override-class-closure () nil
+  (signal-id unsigned-int)
+  (type-number type-number)
+  (callback-closure pointer))
+
+
+(defun signal-override-class-closure (name type function)
+  (let* ((signal-id (ensure-signal-id-from-type name type))
+        (type-number (find-type-number type t))
+        (callback-id (gethash (cons type-number signal-id) *overridden-signals*)))
+    (if callback-id
+       (update-user-data callback-id function)
+      (multiple-value-bind (callback-closure callback-id)
+         (make-callback-closure function)
+       (%signal-override-class-closure signal-id type-number callback-closure)
+       (setf 
+        (gethash (cons type-number signal-id) *overridden-signals*)
+        callback-id)))))
+
+
+(defbinding %signal-chain-from-overridden () nil
+  (args pointer)
+  (return-value (or null gvalue)))
+
+
+(defun %call-next-handler (n-params types args return-type)
+  (let ((params (allocate-memory (* n-params +gvalue-size+))))
+    (loop 
+     for arg in args
+     for type in types
+     for offset from 0 by +gvalue-size+
+     do (gvalue-init (sap+ params offset) type arg))
+
+    (unwind-protect
+       (if return-type
+           (with-gvalue (return-value return-type)
+             (%signal-chain-from-overridden params return-value))
+         (%signal-chain-from-overridden params nil))
+      (progn
+       (loop
+        repeat n-params
+        for offset from 0 by +gvalue-size+
+        do (gvalue-unset (sap+ params offset)))
+       (deallocate-memory params)))))
+
+
+(defmacro define-signal-handler (name ((object class) &rest args) &body body)
+  (let* ((info (signal-query (ensure-signal-id-from-type name class)))
+        (types (cons class (signal-param-types info)))
+        (n-params (1+ (slot-value info 'n-params)))
+        (return-type (type-from-number (slot-value info 'return-type)))
+        (vars (loop
+               for arg in args
+               until (eq arg '&rest)
+               collect arg))
+        (rest (cadr (member '&rest args)))
+        (next (make-symbol "ARGS"))
+        (default (make-symbol "DEFAULT")))
+
+    `(progn
+       (signal-override-class-closure ',name ',class 
+       #'(lambda (,object ,@args)
+           (let ((,default (list* ,object ,@vars ,rest)))
+             (flet ((call-next-handler (&rest ,next)
+                      (%call-next-handler 
+                       ,n-params ',types (or ,next ,default) ',return-type))))
+             ,@body)))
+       ',name)))
+
+
 (defbinding %signal-stop-emission () nil
   (instance ginstance)
   (signal-id unsigned-int)
@@ -238,6 +323,7 @@ (defbinding signal-handler-is-connected-p () boolean
   (handler-id unsigned-int))
 
 (deftype gclosure () 'pointer)
+(register-type 'gclosure "GClosure")
 
 (defbinding (callback-closure-new "clg_callback_closure_new") () gclosure
   (callback-id unsigned-int) 
@@ -252,16 +338,34 @@ (defun make-callback-closure (function)
       (callback user-data-destroy-func))
      callback-id)))
 
-(defmethod create-callback-function ((gobject gobject) function arg1)
+(defgeneric compute-signal-function (gobject signal function object))
+
+(defmethod compute-signal-function ((gobject gobject) signal function object)
+  (declare (ignore signal))
   (cond
-   ((or (eq arg1 t) (eq arg1 gobject)) function)
-   ((not arg1)
+   ((or (eq object t) (eq object gobject)) function)
+   ((not object)
     #'(lambda (&rest args) (apply function (rest args))))
    (t
-    #'(lambda (&rest args) (apply function arg1 (rest args))))))
+    #'(lambda (&rest args) (apply function object (rest args))))))
+
+
+(defgeneric compute-signal-id (gobject signal))
+
+(defmethod compute-signal-id ((gobject gobject) signal)
+  (ensure-signal-id signal gobject))
+
+
+(defgeneric signal-connect (gobject signal function &key detail after object remove))
+
+(defmethod signal-connect :around ((gobject gobject) signal function &rest args)
+  (declare (ignore gobject signal args))
+  (when function
+    (call-next-method)))
+
 
 (defmethod signal-connect ((gobject gobject) signal function
-                          &key (detail 0) after object remove)
+                          &key detail after object remove)
 "Connects a callback function to a signal for a particular object. If
 :OBJECT is T, the object connected to is passed as the first argument
 to the callback function, or if :OBJECT is any other non NIL value, it
@@ -269,19 +373,19 @@ (defmethod signal-connect ((gobject gobject) signal function
 handler will be called after the default handler for the signal. If
 :REMOVE is non NIL, the handler will be removed after beeing invoked
 once."
-  (when function
-    (let* ((signal-id (ensure-signal-id signal gobject))
-          (signal-stop-emission
-           #'(lambda ()
-               (%signal-stop-emission gobject signal-id detail)))
-          (callback (create-callback-function gobject function object))
-          (wrapper #'(lambda (&rest args)
-                       (let ((*signal-stop-emission* signal-stop-emission))
-                         (apply callback args)))))
+(let* ((signal-id (compute-signal-id gobject signal))
+       (detail-quark (if detail (quark-intern detail) 0))
+       (signal-stop-emission
+       #'(lambda ()
+           (%signal-stop-emission gobject signal-id detail-quark)))
+       (callback (compute-signal-function gobject signal function object))
+       (wrapper #'(lambda (&rest args)
+                   (let ((*signal-stop-emission* signal-stop-emission))
+                     (apply callback args)))))
       (multiple-value-bind (closure-id callback-id)
          (make-callback-closure wrapper)
        (let ((handler-id (%signal-connect-closure-by-id 
-                          gobject signal-id detail closure-id after)))
+                          gobject signal-id detail-quark closure-id after)))
          (when remove
            (update-user-data callback-id
             #'(lambda (&rest args)
@@ -289,7 +393,7 @@ (defmethod signal-connect ((gobject gobject) signal function
                     (let ((*signal-stop-emission* signal-stop-emission))
                       (apply callback args))
                   (signal-handler-disconnect gobject handler-id)))))
-         handler-id)))))
+         handler-id))))
 
 
 ;;;; Signal emission
@@ -343,18 +447,6 @@ (defun signal-emit (object signal &rest args)
   (apply #'signal-emit-with-detail object signal 0 args))
 
 
-;;; Message logging
-
-;; TODO: define and signal conditions based on log-level
-
-;; (defcallback log-handler (nil (domain (copy-of string))
-;;                       (log-level int)
-;;                       (message (copy-of string)))
-;;   (error "~A: ~A" domain message))
-
-;; (setf (extern-alien "log_handler" system-area-pointer) (callback log-handler))
-
-
 ;;;; Convenient macros
 
 (defmacro def-callback-marshal (name (return-type &rest args))