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()
184 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
186 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
187 return [nsloop getCFRunLoop];
191 // TODO move into a evtloop_osx.cpp
193 wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow)
195 m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
196 wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" );
197 m_modalNativeWindow = m_modalWindow->GetWXWindow();
200 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow)
202 m_modalWindow = NULL;
203 wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" );
204 m_modalNativeWindow = modalNativeWindow;
207 // END move into a evtloop_osx.cpp
209 void wxModalEventLoop::DoRun()
211 wxMacAutoreleasePool pool;
213 // If the app hasn't started, flush the event queue
214 // If we don't do this, the Dock doesn't get the message that
215 // the app has started so will refuse to activate it.
216 [NSApplication sharedApplication];
217 if (![NSApp isRunning])
219 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
221 [NSApp sendEvent:event];
225 [NSApp runModalForWindow:m_modalNativeWindow];
228 void wxModalEventLoop::DoStop()