chiark / gitweb /
Made signal-connect accept NIL as callback function
[clg] / glib / gcallback.lisp
index 1f7a8bb11c77488e94aed31d1a6a390be7335d1d..2a8eea6bb4a4912ce0fd5f6a2e43e40897789541 100644 (file)
@@ -15,7 +15,7 @@
 ;; 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: gcallback.lisp,v 1.13 2004/11/07 01:23:38 espen Exp $
+;; $Id: gcallback.lisp,v 1.15 2004/12/04 00:29:57 espen Exp $
 
 (in-package "GLIB")
 
@@ -160,15 +160,16 @@ (defmethod signal-connect ((gobject gobject) signal function &key after object)
  function, or if :OBJECT is any other non NIL value, it is passed as the first 
  argument instead. If :AFTER is non NIL, the handler will be called after the 
  default handler of the signal."
-  (let ((callback-id
-        (make-callback-closure
-         (cond
-          ((or (eq object t) (eq object gobject)) function)
-          ((not object)
-           #'(lambda (&rest args) (apply function (cdr args))))
-          (t
-           #'(lambda (&rest args) (apply function object (rest args))))))))
-    (signal-connect-closure gobject signal callback-id :after after)))
+  (when function
+    (let ((callback-id
+          (make-callback-closure
+           (cond
+             ((or (eq object t) (eq object gobject)) function)
+             ((not object)
+              #'(lambda (&rest args) (apply function (cdr args))))
+             (t
+              #'(lambda (&rest args) (apply function object (rest args))))))))
+      (signal-connect-closure gobject signal callback-id :after after))))
 
 
 ;;; Message logging
@@ -181,3 +182,25 @@ (def-callback log-handler (c-call:void (domain c-call:c-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))
+  (let ((names (loop 
+               for arg in args 
+               collect (if (atom arg) (gensym) (first arg))))
+       (types (loop 
+               for arg in args 
+               collect (if (atom arg) arg (second arg)))))
+    `(defcallback ,name (,return-type ,@(mapcar #'list names types)
+                        (callback-id unsigned-int))
+      (invoke-callback callback-id ',return-type ,@names))))
+
+(defmacro with-callback-function ((id function) &body body)
+  `(let ((,id (register-callback-function ,function)))
+    (unwind-protect
+        (progn ,@body)
+      (destroy-user-data ,id))))
+
+