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()
250 // only calling stop: is not enough when called from a runloop-observer,
251 // therefore add a dummy event, to make sure the runloop gets another round
252 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
253 location:NSMakePoint(0.0, 0.0)
258 subtype:0 data1:0 data2:0];
259 [NSApp postEvent:event atStart:FALSE];
262 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
264 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
265 return [nsloop getCFRunLoop];
269 // TODO move into a evtloop_osx.cpp
271 wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow)
273 m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
274 wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" );
275 m_modalNativeWindow = m_modalWindow->GetWXWindow();
278 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow)
280 m_modalWindow = NULL;
281 wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" );
282 m_modalNativeWindow = modalNativeWindow;
285 // END move into a evtloop_osx.cpp
287 void wxModalEventLoop::DoRun()
289 wxMacAutoreleasePool pool;
291 // If the app hasn't started, flush the event queue
292 // If we don't do this, the Dock doesn't get the message that
293 // the app has started so will refuse to activate it.
294 [NSApplication sharedApplication];
295 if (![NSApp isRunning])
297 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
299 [NSApp sendEvent:event];
303 [NSApp runModalForWindow:m_modalNativeWindow];
306 void wxModalEventLoop::DoStop()
311 void wxGUIEventLoop::BeginModalSession( wxWindow* modalWindow )
313 WXWindow nsnow = nil;
315 if ( m_modalNestedLevel > 0 )
317 wxASSERT_MSG( m_modalWindow == modalWindow, "Nested Modal Sessions must be based on same window");
318 m_modalNestedLevel++;
322 m_modalWindow = modalWindow;
323 m_modalNestedLevel = 1;
327 // we must show now, otherwise beginModalSessionForWindow does it but it
328 // also would do a centering of the window before overriding all our position
329 if ( !modalWindow->IsShownOnScreen() )
332 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
333 wxASSERT_MSG( now != NULL, "must pass in a toplevel window for modal event loop" );
334 nsnow = now ? now->GetWXWindow() : nil;
338 NSRect r = NSMakeRect(10, 10, 0, 0);
339 nsnow = [NSPanel alloc];
340 [nsnow initWithContentRect:r
341 styleMask:NSBorderlessWindowMask
342 backing:NSBackingStoreBuffered
345 [nsnow orderOut:nil];
346 m_dummyWindow = nsnow;
348 m_modalSession = [NSApp beginModalSessionForWindow:nsnow];
349 wxASSERT_MSG(m_modalSession != NULL, "modal session couldn't be started");
352 void wxGUIEventLoop::EndModalSession()
354 wxASSERT_MSG(m_modalSession != NULL, "no modal session active");
356 wxASSERT_MSG(m_modalNestedLevel > 0, "incorrect modal nesting level");
358 if ( --m_modalNestedLevel == 0 )
360 [NSApp endModalSession:(NSModalSession)m_modalSession];
361 m_modalSession = nil;
364 [m_dummyWindow release];
374 wxWindowDisabler::wxWindowDisabler(bool disable)
376 m_modalEventLoop = NULL;
377 m_disabled = disable;
382 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
385 DoDisable(winToSkip);
388 void wxWindowDisabler::DoDisable(wxWindow *winToSkip)
390 // remember the top level windows which were already disabled, so that we
391 // don't reenable them later
392 m_winDisabled = NULL;
394 wxWindowList::compatibility_iterator node;
395 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
397 wxWindow *winTop = node->GetData();
398 if ( winTop == winToSkip )
401 // we don't need to disable the hidden or already disabled windows
402 if ( winTop->IsEnabled() && winTop->IsShown() )
408 if ( !m_winDisabled )
410 m_winDisabled = new wxWindowList;
413 m_winDisabled->Append(winTop);
417 m_modalEventLoop = (wxEventLoop*)wxEventLoopBase::GetActive();
418 m_modalEventLoop->BeginModalSession(winToSkip);
421 wxWindowDisabler::~wxWindowDisabler()
426 m_modalEventLoop->EndModalSession();
428 wxWindowList::compatibility_iterator node;
429 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
431 wxWindow *winTop = node->GetData();
432 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
436 //else: had been already disabled, don't reenable
439 delete m_winDisabled;