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: wxWidgets licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/evtloop.h"
20 #import <AppKit/NSApplication.h>
21 #import <AppKit/NSEvent.h>
22 #import <Foundation/NSRunLoop.h>
24 // ========================================================================
26 // ========================================================================
28 class WXDLLEXPORT wxEventLoopImpl
32 wxEventLoopImpl() { SetExitCode(0); }
34 // set/get the exit code
35 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
36 int GetExitCode() const { return m_exitcode; }
39 // the exit code of the event loop
43 // ========================================================================
45 // ========================================================================
47 // ----------------------------------------------------------------------------
48 // wxGUIEventLoop running and exiting
49 // ----------------------------------------------------------------------------
51 wxGUIEventLoop::~wxGUIEventLoop()
53 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
56 int wxGUIEventLoop::Run()
58 // event loops are not recursive, you need to create another loop!
59 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
61 wxEventLoopActivator activate(this);
63 m_impl = new wxEventLoopImpl;
65 [[NSApplication sharedApplication] run];
69 int exitcode = m_impl->GetExitCode();
76 void wxGUIEventLoop::Exit(int rc)
78 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
80 m_impl->SetExitCode(rc);
82 NSApplication *cocoaApp = [NSApplication sharedApplication];
83 wxLogTrace(wxTRACE_COCOA,wxT("wxEventLoop::Exit isRunning=%d"), (int)[cocoaApp isRunning]);
84 wxTheApp->WakeUpIdle();
86 If we're being called from idle time (which occurs while checking the
87 queue for new events) there may or may not be any events in the queue.
88 In order to successfully stop the event loop, at least one event must
89 be processed. To ensure this always happens, WakeUpIdle is called.
91 If the application was active when closed then this is unnecessary
92 because it would receive a deactivate event anyway. However, if the
93 application was not active when closed, then no events would be
94 added to the queue by Cocoa and thus the application would wait
95 indefinitely for the next event.
97 [cocoaApp stop: cocoaApp];
100 // ----------------------------------------------------------------------------
101 // wxEventLoop message processing dispatching
102 // ----------------------------------------------------------------------------
104 bool wxGUIEventLoop::Pending() const
106 // a pointer to the event is returned if there is one, or nil if not
107 return [[NSApplication sharedApplication]
108 nextEventMatchingMask: NSAnyEventMask
109 untilDate: nil /* Equivalent to [NSDate distantPast] */
110 inMode: NSDefaultRunLoopMode
114 bool wxGUIEventLoop::Dispatch()
116 // This check is required by wxGTK but probably not really for wxCocoa
117 // Keep it here to encourage developers to write cross-platform code
118 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
119 NSApplication *cocoaApp = [NSApplication sharedApplication];
120 // Block to retrieve an event then send it
121 if(NSEvent *event = [cocoaApp
122 nextEventMatchingMask:NSAnyEventMask
123 untilDate:[NSDate distantFuture]
124 inMode:NSDefaultRunLoopMode
127 [cocoaApp sendEvent: event];