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 // This works around a bug in Cocoa.
92 [NSEvent startPeriodicEventsAfterDelay:0.0 withPeriod:5.0];
94 This function is most often called during idle time. See
95 wxApp::CocoaInstallIdleHandler() for an overview of the implications
96 of idle event time. In short, Cocoa must have at least one real event
97 in the queue (of which an idle "event" is not) in order for it to
98 realize that the application has been stopped. The above method
99 generates the first periodic event immediately, and would generate
100 further events every 5 seconds if not for the fact that the next
101 method stops the event loop.
103 If the application was active when closed then this is unnecessary
104 because it would receive a deactivate event anyway. However, if the
105 application was not active when closed, then no events would be
106 added to the queue by Cocoa and thus the application would wait
107 indefinitely for the next event.
109 [cocoaApp stop: cocoaApp];
112 // ----------------------------------------------------------------------------
113 // wxEventLoop message processing dispatching
114 // ----------------------------------------------------------------------------
116 bool wxEventLoop::Pending() const
118 // a pointer to the event is returned if there is one, or nil if not
119 return [[NSApplication sharedApplication]
120 nextEventMatchingMask: NSAnyEventMask
121 untilDate: nil /* Equivalent to [NSDate distantPast] */
122 inMode: NSDefaultRunLoopMode
126 bool wxEventLoop::Dispatch()
128 // This check is required by wxGTK but probably not really for wxCocoa
129 // Keep it here to encourage developers to write cross-platform code
130 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
131 NSApplication *cocoaApp = [NSApplication sharedApplication];
132 // Block to retrieve an event then send it
133 if(NSEvent *event = [cocoaApp
134 nextEventMatchingMask:NSAnyEventMask
135 untilDate:[NSDate distantFuture]
136 inMode:NSDefaultRunLoopMode
139 [cocoaApp sendEvent: event];