1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/evtloop.mm
3 // Purpose: implementation of wxEventLoop for OS X
4 // Author: Vadim Zeitlin, Stefan Csomor
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 // ============================================================================
44 // in case we want to integrate this
46 static NSUInteger CalculateNSEventMaskFromEventCategory(wxEventCategory cat)
48 // the masking system doesn't really help, as only the lowlevel UI events
49 // are split in a useful way, all others are way to broad
51 if ( (cat | wxEVT_CATEGORY_USER_INPUT) && (cat | (~wxEVT_CATEGORY_USER_INPUT) ) )
52 return NSAnyEventMask;
56 if ( cat | wxEVT_CATEGORY_USER_INPUT )
61 NSRightMouseDownMask |
64 NSLeftMouseDraggedMask |
65 NSRightMouseDraggedMask |
69 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
71 NSTabletProximityMask |
73 NSOtherMouseDownMask |
75 NSOtherMouseDraggedMask |
80 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
85 NSEventMaskBeginGesture |
86 NSEventMaskEndGesture |
91 if ( cat | (~wxEVT_CATEGORY_USER_INPUT) )
96 NSApplicationDefinedMask |
106 wxGUIEventLoop::wxGUIEventLoop()
108 m_modalSession = nil;
110 m_modalNestedLevel = 0;
111 m_modalWindow = NULL;
114 wxGUIEventLoop::~wxGUIEventLoop()
116 wxASSERT( m_modalSession == nil );
117 wxASSERT( m_dummyWindow == nil );
118 wxASSERT( m_modalNestedLevel == 0 );
121 //-----------------------------------------------------------------------------
122 // events dispatch and loop handling
123 //-----------------------------------------------------------------------------
127 bool wxGUIEventLoop::Pending() const
130 // this code doesn't reliably detect pending events
131 // so better return true and have the dispatch deal with it
132 // as otherwise we end up in a tight loop when idle events are responded
133 // to by RequestMore(true)
134 wxMacAutoreleasePool autoreleasepool;
136 return [[NSApplication sharedApplication]
137 nextEventMatchingMask: NSAnyEventMask
139 inMode: NSDefaultRunLoopMode
146 bool wxGUIEventLoop::Dispatch()
151 wxMacAutoreleasePool autoreleasepool;
153 if(NSEvent *event = [NSApp
154 nextEventMatchingMask:NSAnyEventMask
155 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
156 inMode:NSDefaultRunLoopMode
159 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
160 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
163 wxTheApp->MacSetCurrentEvent(event, NULL);
165 [NSApp sendEvent: event];
168 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
173 wxTheApp->ProcessPendingEvents();
175 if ( wxTheApp->ProcessIdle() )
193 int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout)
195 wxMacAutoreleasePool autoreleasepool;
197 if ( m_modalSession )
199 NSInteger response = [NSApp runModalSession:(NSModalSession)m_modalSession];
203 case NSRunContinuesResponse:
205 if ( [[NSApplication sharedApplication]
206 nextEventMatchingMask: NSAnyEventMask
208 inMode: NSDefaultRunLoopMode
209 dequeue: NO] != nil )
215 case NSRunStoppedResponse:
216 case NSRunAbortedResponse:
219 wxFAIL_MSG("unknown response code");
226 NSEvent *event = [NSApp
227 nextEventMatchingMask:NSAnyEventMask
228 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
229 inMode:NSDefaultRunLoopMode
235 [NSApp sendEvent: event];
241 void wxGUIEventLoop::DoRun()
243 wxMacAutoreleasePool autoreleasepool;
247 void wxGUIEventLoop::DoStop()
249 // only calling stop: is not enough when called from a runloop-observer,
250 // therefore add a dummy event, to make sure the runloop gets another round
255 void wxGUIEventLoop::WakeUp()
257 wxMacAutoreleasePool autoreleasepool;
258 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
259 location:NSMakePoint(0.0, 0.0)
264 subtype:0 data1:0 data2:0];
265 [NSApp postEvent:event atStart:FALSE];
268 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
270 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
271 return [nsloop getCFRunLoop];
275 // TODO move into a evtloop_osx.cpp
277 wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow)
279 m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
280 wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" );
281 m_modalNativeWindow = m_modalWindow->GetWXWindow();
284 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow)
286 m_modalWindow = NULL;
287 wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" );
288 m_modalNativeWindow = modalNativeWindow;
291 // END move into a evtloop_osx.cpp
293 void wxModalEventLoop::DoRun()
295 wxMacAutoreleasePool pool;
297 // If the app hasn't started, flush the event queue
298 // If we don't do this, the Dock doesn't get the message that
299 // the app has started so will refuse to activate it.
300 [NSApplication sharedApplication];
301 if (![NSApp isRunning])
303 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
305 [NSApp sendEvent:event];
309 [NSApp runModalForWindow:m_modalNativeWindow];
312 void wxModalEventLoop::DoStop()
317 void wxGUIEventLoop::BeginModalSession( wxWindow* modalWindow )
319 WXWindow nsnow = nil;
321 if ( m_modalNestedLevel > 0 )
323 wxASSERT_MSG( m_modalWindow == modalWindow, "Nested Modal Sessions must be based on same window");
324 m_modalNestedLevel++;
328 m_modalWindow = modalWindow;
329 m_modalNestedLevel = 1;
333 // we must show now, otherwise beginModalSessionForWindow does it but it
334 // also would do a centering of the window before overriding all our position
335 if ( !modalWindow->IsShownOnScreen() )
338 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
339 wxASSERT_MSG( now != NULL, "must pass in a toplevel window for modal event loop" );
340 nsnow = now ? now->GetWXWindow() : nil;
344 NSRect r = NSMakeRect(10, 10, 0, 0);
345 nsnow = [NSPanel alloc];
346 [nsnow initWithContentRect:r
347 styleMask:NSBorderlessWindowMask
348 backing:NSBackingStoreBuffered
351 [nsnow orderOut:nil];
352 m_dummyWindow = nsnow;
354 m_modalSession = [NSApp beginModalSessionForWindow:nsnow];
355 wxASSERT_MSG(m_modalSession != NULL, "modal session couldn't be started");
358 void wxGUIEventLoop::EndModalSession()
360 wxASSERT_MSG(m_modalSession != NULL, "no modal session active");
362 wxASSERT_MSG(m_modalNestedLevel > 0, "incorrect modal nesting level");
364 if ( --m_modalNestedLevel == 0 )
366 [NSApp endModalSession:(NSModalSession)m_modalSession];
367 m_modalSession = nil;
370 [m_dummyWindow release];
380 wxWindowDisabler::wxWindowDisabler(bool disable)
382 m_modalEventLoop = NULL;
383 m_disabled = disable;
388 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
391 DoDisable(winToSkip);
394 void wxWindowDisabler::DoDisable(wxWindow *winToSkip)
396 // remember the top level windows which were already disabled, so that we
397 // don't reenable them later
398 m_winDisabled = NULL;
400 wxWindowList::compatibility_iterator node;
401 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
403 wxWindow *winTop = node->GetData();
404 if ( winTop == winToSkip )
407 // we don't need to disable the hidden or already disabled windows
408 if ( winTop->IsEnabled() && winTop->IsShown() )
414 if ( !m_winDisabled )
416 m_winDisabled = new wxWindowList;
419 m_winDisabled->Append(winTop);
423 m_modalEventLoop = (wxEventLoop*)wxEventLoopBase::GetActive();
424 if (m_modalEventLoop)
425 m_modalEventLoop->BeginModalSession(winToSkip);
428 wxWindowDisabler::~wxWindowDisabler()
433 if (m_modalEventLoop)
434 m_modalEventLoop->EndModalSession();
436 wxWindowList::compatibility_iterator node;
437 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
439 wxWindow *winTop = node->GetData();
440 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
444 //else: had been already disabled, don't reenable
447 delete m_winDisabled;