1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/evtloop.mm
3 // Purpose: implementation of wxEventLoop for OS X
4 // Author: Vadim Zeitlin, Stefan Csomor
7 // RCS-ID: $Id: evtloop.cpp 54845 2008-07-30 14:52:41Z SC $
8 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/evtloop.h"
31 #include "wx/nonownedwnd.h"
36 #include "wx/osx/private.h"
38 // ============================================================================
39 // wxEventLoop implementation
40 // ============================================================================
43 static int CalculateNSEventMaskFromEventCategory(wxEventCategory cat)
47 NSRightMouseDownMask |
48 NSRightMouseUpMask = 1 << NSRightMouseUp,
49 NSMouseMovedMask = 1 << NSMouseMoved,
50 NSLeftMouseDraggedMask = 1 << NSLeftMouseDragged,
51 NSRightMouseDraggedMask = 1 << NSRightMouseDragged,
52 NSMouseEnteredMask = 1 << NSMouseEntered,
53 NSMouseExitedMask = 1 << NSMouseExited,
54 NSScrollWheelMask = 1 << NSScrollWheel,
55 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
56 NSTabletPointMask = 1 << NSTabletPoint,
57 NSTabletProximityMask = 1 << NSTabletProximity,
59 NSOtherMouseDownMask = 1 << NSOtherMouseDown,
60 NSOtherMouseUpMask = 1 << NSOtherMouseUp,
61 NSOtherMouseDraggedMask = 1 << NSOtherMouseDragged,
65 NSKeyDownMask = 1 << NSKeyDown,
66 NSKeyUpMask = 1 << NSKeyUp,
67 NSFlagsChangedMask = 1 << NSFlagsChanged,
69 NSAppKitDefinedMask = 1 << NSAppKitDefined,
70 NSSystemDefinedMask = 1 << NSSystemDefined,
71 NSApplicationDefinedMask = 1 << NSApplicationDefined,
72 NSPeriodicMask = 1 << NSPeriodic,
73 NSCursorUpdateMask = 1 << NSCursorUpdate,
75 NSAnyEventMask = 0xffffffffU
79 wxGUIEventLoop::wxGUIEventLoop()
83 //-----------------------------------------------------------------------------
84 // events dispatch and loop handling
85 //-----------------------------------------------------------------------------
89 bool wxGUIEventLoop::Pending() const
92 // this code doesn't reliably detect pending events
93 // so better return true and have the dispatch deal with it
94 // as otherwise we end up in a tight loop when idle events are responded
95 // to by RequestMore(true)
96 wxMacAutoreleasePool autoreleasepool;
98 return [[NSApplication sharedApplication]
99 nextEventMatchingMask: NSAnyEventMask
101 inMode: NSDefaultRunLoopMode
108 bool wxGUIEventLoop::Dispatch()
113 wxMacAutoreleasePool autoreleasepool;
115 if(NSEvent *event = [NSApp
116 nextEventMatchingMask:NSAnyEventMask
117 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
118 inMode:NSDefaultRunLoopMode
121 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
122 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
125 wxTheApp->MacSetCurrentEvent(event, NULL);
127 [NSApp sendEvent: event];
130 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
135 wxTheApp->ProcessPendingEvents();
137 if ( wxTheApp->ProcessIdle() )
155 int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout)
157 wxMacAutoreleasePool autoreleasepool;
159 NSEvent *event = [NSApp
160 nextEventMatchingMask:NSAnyEventMask
161 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
162 inMode:NSDefaultRunLoopMode
168 [NSApp sendEvent: event];
173 void wxGUIEventLoop::DoRun()
175 wxMacAutoreleasePool autoreleasepool;
179 void wxGUIEventLoop::DoStop()
182 // only calling stop: is not enough when called from a runloop-observer,
183 // therefore add a dummy event, to make sure the runloop gets another round
184 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
185 location:NSMakePoint(0.0, 0.0)
190 subtype:0 data1:0 data2:0];
191 [NSApp postEvent:event atStart:FALSE];
194 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
196 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
197 return [nsloop getCFRunLoop];
201 // TODO move into a evtloop_osx.cpp
203 wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow)
205 m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
206 wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" );
207 m_modalNativeWindow = m_modalWindow->GetWXWindow();
210 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow)
212 m_modalWindow = NULL;
213 wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" );
214 m_modalNativeWindow = modalNativeWindow;
217 // END move into a evtloop_osx.cpp
219 void wxModalEventLoop::DoRun()
221 wxMacAutoreleasePool pool;
223 // If the app hasn't started, flush the event queue
224 // If we don't do this, the Dock doesn't get the message that
225 // the app has started so will refuse to activate it.
226 [NSApplication sharedApplication];
227 if (![NSApp isRunning])
229 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
231 [NSApp sendEvent:event];
235 [NSApp runModalForWindow:m_modalNativeWindow];
238 void wxModalEventLoop::DoStop()