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()
82 //-----------------------------------------------------------------------------
83 // events dispatch and loop handling
84 //-----------------------------------------------------------------------------
88 bool wxGUIEventLoop::Pending() const
91 // this code doesn't reliably detect pending events
92 // so better return true and have the dispatch deal with it
93 // as otherwise we end up in a tight loop when idle events are responded
94 // to by RequestMore(true)
95 wxMacAutoreleasePool autoreleasepool;
97 return [[NSApplication sharedApplication]
98 nextEventMatchingMask: NSAnyEventMask
100 inMode: NSDefaultRunLoopMode
107 bool wxGUIEventLoop::Dispatch()
112 wxMacAutoreleasePool autoreleasepool;
114 if(NSEvent *event = [NSApp
115 nextEventMatchingMask:NSAnyEventMask
116 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
117 inMode:NSDefaultRunLoopMode
121 wxTheApp->MacSetCurrentEvent(event, NULL);
123 [NSApp sendEvent: event];
128 wxTheApp->ProcessPendingEvents();
130 if ( wxTheApp->ProcessIdle() )
148 int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout)
150 wxMacAutoreleasePool autoreleasepool;
152 NSEvent *event = [NSApp
153 nextEventMatchingMask:NSAnyEventMask
154 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
155 inMode:NSDefaultRunLoopMode
161 [NSApp sendEvent: event];
166 void wxGUIEventLoop::DoRun()
168 wxMacAutoreleasePool autoreleasepool;
172 void wxGUIEventLoop::DoStop()
177 void wxModalEventLoop::DoRun()
179 wxMacAutoreleasePool pool;
181 // If the app hasn't started, flush the event queue
182 // If we don't do this, the Dock doesn't get the message that
183 // the app has started so will refuse to activate it.
184 [NSApplication sharedApplication];
185 if (![NSApp isRunning])
187 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
189 [NSApp sendEvent:event];
193 NSWindow* theWindow = m_modalWindow->GetWXWindow();
194 [NSApp runModalForWindow:theWindow];
197 void wxModalEventLoop::DoStop()