[Openmcl-devel] $_InstallEventHandler Difficulty

David Steuber david at david-steuber.com
Tue Dec 21 22:08:17 PST 2004


I'm deep into chapter six of Learning Carbon and I have a problem with 
my event handler.  The symptom I see is that the main window appears 
and then the application quits.  I think my event handler is not 
installed correctly or declared correctly.  This is my code:

(defun main ()
   ;; Allow SLIME to connect
   (when *swank-port*
     (swank:create-server :port *swank-port* :dont-close t))

   (setf *main-window* (ccl::make-record :<w>indow<r>ef))

   (rlet ((nibref :<ibn>ib<r>ef)
          (mainspec :<e>vent<t>ype<s>pec :event<c>lass 
#$kEventClassCommand :event<k>ind #$kEventCommandProcess))
     (let ((err (#_CreateNibReference (const-cfstring "main") nibref)))
       (assert (eql err #$noErr) ()
               "unable to get the main menu nib reference")
       (setf err (#_SetMenuBarFromNib (ccl::%get-ptr nibref) 
(const-cfstring "MainMenu")))
       (assert (eql err #$noErr) ()
               "Can't set the menubar!")
       (setf err (#_CreateWindowFromNib (ccl::%get-ptr nibref) 
(const-cfstring "MainWindow") *main-window*))
       (assert (eql err #$noErr) ()
               "Can't create the main window!")
       (#_ShowWindow (ccl::%get-ptr *main-window*))
       (#_DisposeNibReference (ccl::%get-ptr nibref))

       ;; Install event handler
       #+(or)
       (#_InstallEventHandler (#_GetWindowEventTarget *main-window*)
                              (#_NewEventHandlerUPP 
#'main-window-event-handler)
                              1 mainspec
                              (ccl::%null-ptr)
                              (ccl::%null-ptr))

       ;; Start the main event loop
       (#_RunApplicationEventLoop)))

   (#_free *main-window*) ; superfluous since we are about to exit anyway

   (quit))

(ccl::defcallback main-window-event-handler
     (:<e>vent<h>andler<c>all<r>ef handler :<e>vent<r>ef event (:* t) 
user-data :<oss>tatus)
   (declare (ignore handler))
   (rlet ((command :<hic>ommand))
     (#_GetEventParameter event #$kEventParamDirectObject #$typeHICommand
                          (ccl::%null-ptr) 
(ccl::%foreign-type-or-record-size :<HIC>ommand :bytes)
                          (ccl::%null-ptr) command)
     (cond ((equal (ccl::pref command :<hic>ommand.command<id>)
                   +compute-command+)
            (compute-command-handler user-data)
            #$noErr)
           (t #$eventNotHandledErr))))

(defun compute-command-handler (windowref)
   (declare (ignore windowref))
   nil)

The required C function signature for a callback is this:

pascal OSStatus HandlerName (EventHandlerCallRef nextHandler, EventRef 
event, void* userData);

That's what I intended with main-window-event-handler.  My code 
compiles without warnings.  Enabling the call to $_InstallEventHandler 
causes the app to exit the instant the main window becomes visible.  
I'm hoping an experienced eye can spot my error.

If not, is there a way I can see what's going on?  I'm currently doing 
a batch build and then run cycle as if I was a C programmer.




More information about the Openmcl-devel mailing list