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"
35 #include "wx/osx/private.h"
37 // ============================================================================
38 // wxEventLoop implementation
39 // ============================================================================
42 static int CalculateNSEventMaskFromEventCategory(wxEventCategory cat)
46 NSRightMouseDownMask |
47 NSRightMouseUpMask = 1 << NSRightMouseUp,
48 NSMouseMovedMask = 1 << NSMouseMoved,
49 NSLeftMouseDraggedMask = 1 << NSLeftMouseDragged,
50 NSRightMouseDraggedMask = 1 << NSRightMouseDragged,
51 NSMouseEnteredMask = 1 << NSMouseEntered,
52 NSMouseExitedMask = 1 << NSMouseExited,
53 NSScrollWheelMask = 1 << NSScrollWheel,
54 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
55 NSTabletPointMask = 1 << NSTabletPoint,
56 NSTabletProximityMask = 1 << NSTabletProximity,
58 NSOtherMouseDownMask = 1 << NSOtherMouseDown,
59 NSOtherMouseUpMask = 1 << NSOtherMouseUp,
60 NSOtherMouseDraggedMask = 1 << NSOtherMouseDragged,
64 NSKeyDownMask = 1 << NSKeyDown,
65 NSKeyUpMask = 1 << NSKeyUp,
66 NSFlagsChangedMask = 1 << NSFlagsChanged,
68 NSAppKitDefinedMask = 1 << NSAppKitDefined,
69 NSSystemDefinedMask = 1 << NSSystemDefined,
70 NSApplicationDefinedMask = 1 << NSApplicationDefined,
71 NSPeriodicMask = 1 << NSPeriodic,
72 NSCursorUpdateMask = 1 << NSCursorUpdate,
74 NSAnyEventMask = 0xffffffffU
78 wxGUIEventLoop::wxGUIEventLoop()
83 void wxGUIEventLoop::WakeUp()
85 extern void wxMacWakeUp();
90 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
92 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
93 return [nsloop getCFRunLoop];
96 //-----------------------------------------------------------------------------
97 // events dispatch and loop handling
98 //-----------------------------------------------------------------------------
100 bool wxGUIEventLoop::Pending() const
103 // this code doesn't reliably detect pending events
104 // so better return true and have the dispatch deal with it
105 // as otherwise we end up in a tight loop when idle events are responded
106 // to by RequestMore(true)
107 wxMacAutoreleasePool autoreleasepool;
109 return [[NSApplication sharedApplication]
110 nextEventMatchingMask: NSAnyEventMask
112 inMode: NSDefaultRunLoopMode
119 bool wxGUIEventLoop::Dispatch()
124 wxMacAutoreleasePool autoreleasepool;
126 if(NSEvent *event = [NSApp
127 nextEventMatchingMask:NSAnyEventMask
128 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
129 inMode:NSDefaultRunLoopMode
133 wxTheApp->MacSetCurrentEvent(event, NULL);
135 [NSApp sendEvent: event];
140 wxTheApp->ProcessPendingEvents();
142 if ( wxTheApp->ProcessIdle() )
158 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
161 // Yielding from a non-gui thread needs to bail out, otherwise we end up
162 // possibly sending events in the thread too.
163 if ( !wxThread::IsMain() )
167 #endif // wxUSE_THREADS
169 m_isInsideYield = true;
170 m_eventsToProcessInsideYield = eventsToProcess;
173 // disable log flushing from here because a call to wxYield() shouldn't
174 // normally result in message boxes popping up &c
178 // process all pending events:
182 // it's necessary to call ProcessIdle() to update the frames sizes which
183 // might have been changed (it also will update other things set from
184 // OnUpdateUI() which is a nice (and desired) side effect)
185 while ( ProcessIdle() ) {}
187 // if there are pending events, we must process them.
189 wxTheApp->ProcessPendingEvents();
194 m_isInsideYield = false;
199 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
201 wxMacAutoreleasePool autoreleasepool;
203 NSEvent *event = [NSApp
204 nextEventMatchingMask:NSAnyEventMask
205 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
206 inMode:NSDefaultRunLoopMode
211 [NSApp sendEvent: event];