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
122 wxTheApp->MacSetCurrentEvent(event, NULL);
124 [NSApp sendEvent: event];
129 wxTheApp->ProcessPendingEvents();
131 if ( wxTheApp->ProcessIdle() )
149 int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout)
151 wxMacAutoreleasePool autoreleasepool;
153 NSEvent *event = [NSApp
154 nextEventMatchingMask:NSAnyEventMask
155 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
156 inMode:NSDefaultRunLoopMode
162 [NSApp sendEvent: event];
167 void wxGUIEventLoop::DoRun()
169 wxMacAutoreleasePool autoreleasepool;
173 void wxGUIEventLoop::DoStop()
178 // TODO move into a evtloop_osx.cpp
180 wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow)
182 m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
183 wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" );
184 m_modalNativeWindow = m_modalWindow->GetWXWindow();
187 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow)
189 m_modalWindow = NULL;
190 wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" );
191 m_modalNativeWindow = modalNativeWindow;
194 // END move into a evtloop_osx.cpp
196 void wxModalEventLoop::DoRun()
198 wxMacAutoreleasePool pool;
200 // If the app hasn't started, flush the event queue
201 // If we don't do this, the Dock doesn't get the message that
202 // the app has started so will refuse to activate it.
203 [NSApplication sharedApplication];
204 if (![NSApp isRunning])
206 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
208 [NSApp sendEvent:event];
212 [NSApp runModalForWindow:m_modalNativeWindow];
215 void wxModalEventLoop::DoStop()