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
147 bool wxGUIEventLoop::Dispatch()
152 wxMacAutoreleasePool autoreleasepool;
154 if(NSEvent *event = [NSApp
155 nextEventMatchingMask:NSAnyEventMask
156 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
157 inMode:NSDefaultRunLoopMode
160 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
161 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
164 wxTheApp->MacSetCurrentEvent(event, NULL);
166 [NSApp sendEvent: event];
169 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
174 wxTheApp->ProcessPendingEvents();
176 if ( wxTheApp->ProcessIdle() )
194 int wxGUIEventLoop::DoDispatchTimeout(unsigned long timeout)
196 wxMacAutoreleasePool autoreleasepool;
198 if ( m_modalSession )
200 NSInteger response = [NSApp runModalSession:(NSModalSession)m_modalSession];
204 case NSRunContinuesResponse:
206 if ( [[NSApplication sharedApplication]
207 nextEventMatchingMask: NSAnyEventMask
209 inMode: NSDefaultRunLoopMode
210 dequeue: NO] != nil )
216 case NSRunStoppedResponse:
217 case NSRunAbortedResponse:
220 wxFAIL_MSG("unknown response code");
227 NSEvent *event = [NSApp
228 nextEventMatchingMask:NSAnyEventMask
229 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
230 inMode:NSDefaultRunLoopMode
236 [NSApp sendEvent: event];
242 void wxGUIEventLoop::DoRun()
244 wxMacAutoreleasePool autoreleasepool;
248 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
256 void wxGUIEventLoop::WakeUp()
258 NSEvent* cevent = [NSApp currentEvent];
260 // when already in a mouse event handler, don't add higher level event
261 if ( cevent != nil && [cevent type] < NSKeyDown )
263 wxCFEventLoop::WakeUp();
267 wxMacAutoreleasePool autoreleasepool;
268 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
269 location:NSMakePoint(0.0, 0.0)
274 subtype:0 data1:0 data2:0];
275 [NSApp postEvent:event atStart:FALSE];
279 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
281 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
282 return [nsloop getCFRunLoop];
286 // TODO move into a evtloop_osx.cpp
288 wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow)
290 m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
291 wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" );
292 m_modalNativeWindow = m_modalWindow->GetWXWindow();
295 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow)
297 m_modalWindow = NULL;
298 wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" );
299 m_modalNativeWindow = modalNativeWindow;
302 // END move into a evtloop_osx.cpp
304 void wxModalEventLoop::DoRun()
306 wxMacAutoreleasePool pool;
308 // If the app hasn't started, flush the event queue
309 // If we don't do this, the Dock doesn't get the message that
310 // the app has started so will refuse to activate it.
311 [NSApplication sharedApplication];
312 if (![NSApp isRunning])
314 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
316 [NSApp sendEvent:event];
320 [NSApp runModalForWindow:m_modalNativeWindow];
323 void wxModalEventLoop::DoStop()
328 void wxGUIEventLoop::BeginModalSession( wxWindow* modalWindow )
330 WXWindow nsnow = nil;
332 if ( m_modalNestedLevel > 0 )
334 wxASSERT_MSG( m_modalWindow == modalWindow, "Nested Modal Sessions must be based on same window");
335 m_modalNestedLevel++;
339 m_modalWindow = modalWindow;
340 m_modalNestedLevel = 1;
344 // we must show now, otherwise beginModalSessionForWindow does it but it
345 // also would do a centering of the window before overriding all our position
346 if ( !modalWindow->IsShownOnScreen() )
349 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
350 wxASSERT_MSG( now != NULL, "must pass in a toplevel window for modal event loop" );
351 nsnow = now ? now->GetWXWindow() : nil;
355 NSRect r = NSMakeRect(10, 10, 0, 0);
356 nsnow = [NSPanel alloc];
357 [nsnow initWithContentRect:r
358 styleMask:NSBorderlessWindowMask
359 backing:NSBackingStoreBuffered
362 [nsnow orderOut:nil];
363 m_dummyWindow = nsnow;
365 m_modalSession = [NSApp beginModalSessionForWindow:nsnow];
366 wxASSERT_MSG(m_modalSession != NULL, "modal session couldn't be started");
369 void wxGUIEventLoop::EndModalSession()
371 wxASSERT_MSG(m_modalSession != NULL, "no modal session active");
373 wxASSERT_MSG(m_modalNestedLevel > 0, "incorrect modal nesting level");
375 if ( --m_modalNestedLevel == 0 )
377 [NSApp endModalSession:(NSModalSession)m_modalSession];
378 m_modalSession = nil;
381 [m_dummyWindow release];
391 wxWindowDisabler::wxWindowDisabler(bool disable)
393 m_modalEventLoop = NULL;
394 m_disabled = disable;
399 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
402 DoDisable(winToSkip);
405 void wxWindowDisabler::DoDisable(wxWindow *winToSkip)
407 // remember the top level windows which were already disabled, so that we
408 // don't reenable them later
409 m_winDisabled = NULL;
411 wxWindowList::compatibility_iterator node;
412 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
414 wxWindow *winTop = node->GetData();
415 if ( winTop == winToSkip )
418 // we don't need to disable the hidden or already disabled windows
419 if ( winTop->IsEnabled() && winTop->IsShown() )
425 if ( !m_winDisabled )
427 m_winDisabled = new wxWindowList;
430 m_winDisabled->Append(winTop);
434 m_modalEventLoop = (wxEventLoop*)wxEventLoopBase::GetActive();
435 if (m_modalEventLoop)
436 m_modalEventLoop->BeginModalSession(winToSkip);
439 wxWindowDisabler::~wxWindowDisabler()
444 if (m_modalEventLoop)
445 m_modalEventLoop->EndModalSession();
447 wxWindowList::compatibility_iterator node;
448 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
450 wxWindow *winTop = node->GetData();
451 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
455 //else: had been already disabled, don't reenable
458 delete m_winDisabled;