1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/evtloop.mm
3 // Purpose: implements wxEventLoop for Cocoa
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott <dfe@cox.net>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/evtloop.h"
19 #import <AppKit/NSApplication.h>
20 #import <AppKit/NSEvent.h>
21 #import <Foundation/NSRunLoop.h>
23 // ========================================================================
25 // ========================================================================
27 class WXDLLEXPORT wxEventLoopImpl
31 wxEventLoopImpl() { SetExitCode(0); }
33 // set/get the exit code
34 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
35 int GetExitCode() const { return m_exitcode; }
38 // the exit code of the event loop
42 // ========================================================================
44 // ========================================================================
46 // ----------------------------------------------------------------------------
47 // wxEventLoop running and exiting
48 // ----------------------------------------------------------------------------
50 wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
52 wxEventLoop::~wxEventLoop()
54 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
57 bool wxEventLoop::IsRunning() const
62 int wxEventLoop::Run()
64 // event loops are not recursive, you need to create another loop!
65 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
67 wxEventLoop *oldLoop = ms_activeLoop;
70 m_impl = new wxEventLoopImpl;
72 [[NSApplication sharedApplication] run];
74 int exitcode = m_impl->GetExitCode();
78 ms_activeLoop = oldLoop;
83 void wxEventLoop::Exit(int rc)
85 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
87 m_impl->SetExitCode(rc);
89 NSApplication *cocoaApp = [NSApplication sharedApplication];
90 wxLogDebug("wxEventLoop::Exit isRunning=%d", (int)[cocoaApp isRunning]);
91 [cocoaApp stop: cocoaApp];
94 // ----------------------------------------------------------------------------
95 // wxEventLoop message processing dispatching
96 // ----------------------------------------------------------------------------
98 bool wxEventLoop::Pending() const
100 // a pointer to the event is returned if there is one, or nil if not
101 return [[NSApplication sharedApplication]
102 nextEventMatchingMask: NSAnyEventMask
103 untilDate: nil /* Equivalent to [NSDate distantPast] */
104 inMode: NSDefaultRunLoopMode
108 bool wxEventLoop::Dispatch()
110 // This check is required by wxGTK but probably not really for wxCocoa
111 // Keep it here to encourage developers to write cross-platform code
112 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
113 NSApplication *cocoaApp = [NSApplication sharedApplication];
114 // Block to retrieve an event then send it
115 if(NSEvent *event = [cocoaApp
116 nextEventMatchingMask:NSAnyEventMask
117 untilDate:[NSDate distantFuture]
118 inMode:NSDefaultRunLoopMode
121 [cocoaApp sendEvent: event];