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];
259 NSString* mode = [[NSRunLoop mainRunLoop] currentMode];
261 // when already in a mouse event handler, don't add higher level event
262 // if ( cevent != nil && [cevent type] <= NSMouseMoved && )
263 if ( [NSEventTrackingRunLoopMode isEqualToString:mode] )
265 // NSLog(@"event for wakeup %@ in mode %@",cevent,mode);
266 wxCFEventLoop::WakeUp();
270 wxMacAutoreleasePool autoreleasepool;
271 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
272 location:NSMakePoint(0.0, 0.0)
277 subtype:0 data1:0 data2:0];
278 [NSApp postEvent:event atStart:FALSE];
282 CFRunLoopRef wxGUIEventLoop::CFGetCurrentRunLoop() const
284 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
285 return [nsloop getCFRunLoop];
289 // TODO move into a evtloop_osx.cpp
291 wxModalEventLoop::wxModalEventLoop(wxWindow *modalWindow)
293 m_modalWindow = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
294 wxASSERT_MSG( m_modalWindow != NULL, "must pass in a toplevel window for modal event loop" );
295 m_modalNativeWindow = m_modalWindow->GetWXWindow();
298 wxModalEventLoop::wxModalEventLoop(WXWindow modalNativeWindow)
300 m_modalWindow = NULL;
301 wxASSERT_MSG( modalNativeWindow != NULL, "must pass in a toplevel window for modal event loop" );
302 m_modalNativeWindow = modalNativeWindow;
305 // END move into a evtloop_osx.cpp
307 void wxModalEventLoop::DoRun()
309 wxMacAutoreleasePool pool;
311 // If the app hasn't started, flush the event queue
312 // If we don't do this, the Dock doesn't get the message that
313 // the app has started so will refuse to activate it.
314 [NSApplication sharedApplication];
315 if (![NSApp isRunning])
317 while(NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES])
319 [NSApp sendEvent:event];
323 [NSApp runModalForWindow:m_modalNativeWindow];
326 void wxModalEventLoop::DoStop()
331 void wxGUIEventLoop::BeginModalSession( wxWindow* modalWindow )
333 WXWindow nsnow = nil;
335 if ( m_modalNestedLevel > 0 )
337 wxASSERT_MSG( m_modalWindow == modalWindow, "Nested Modal Sessions must be based on same window");
338 m_modalNestedLevel++;
342 m_modalWindow = modalWindow;
343 m_modalNestedLevel = 1;
347 // we must show now, otherwise beginModalSessionForWindow does it but it
348 // also would do a centering of the window before overriding all our position
349 if ( !modalWindow->IsShownOnScreen() )
352 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (modalWindow);
353 wxASSERT_MSG( now != NULL, "must pass in a toplevel window for modal event loop" );
354 nsnow = now ? now->GetWXWindow() : nil;
358 NSRect r = NSMakeRect(10, 10, 0, 0);
359 nsnow = [NSPanel alloc];
360 [nsnow initWithContentRect:r
361 styleMask:NSBorderlessWindowMask
362 backing:NSBackingStoreBuffered
365 [nsnow orderOut:nil];
366 m_dummyWindow = nsnow;
368 m_modalSession = [NSApp beginModalSessionForWindow:nsnow];
369 wxASSERT_MSG(m_modalSession != NULL, "modal session couldn't be started");
372 void wxGUIEventLoop::EndModalSession()
374 wxASSERT_MSG(m_modalSession != NULL, "no modal session active");
376 wxASSERT_MSG(m_modalNestedLevel > 0, "incorrect modal nesting level");
378 if ( --m_modalNestedLevel == 0 )
380 [NSApp endModalSession:(NSModalSession)m_modalSession];
381 m_modalSession = nil;
384 [m_dummyWindow release];
394 wxWindowDisabler::wxWindowDisabler(bool disable)
396 m_modalEventLoop = NULL;
397 m_disabled = disable;
402 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
405 DoDisable(winToSkip);
408 void wxWindowDisabler::DoDisable(wxWindow *winToSkip)
410 // remember the top level windows which were already disabled, so that we
411 // don't reenable them later
412 m_winDisabled = NULL;
414 wxWindowList::compatibility_iterator node;
415 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
417 wxWindow *winTop = node->GetData();
418 if ( winTop == winToSkip )
421 // we don't need to disable the hidden or already disabled windows
422 if ( winTop->IsEnabled() && winTop->IsShown() )
428 if ( !m_winDisabled )
430 m_winDisabled = new wxWindowList;
433 m_winDisabled->Append(winTop);
437 m_modalEventLoop = (wxEventLoop*)wxEventLoopBase::GetActive();
438 if (m_modalEventLoop)
439 m_modalEventLoop->BeginModalSession(winToSkip);
442 wxWindowDisabler::~wxWindowDisabler()
447 if (m_modalEventLoop)
448 m_modalEventLoop->EndModalSession();
450 wxWindowList::compatibility_iterator node;
451 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
453 wxWindow *winTop = node->GetData();
454 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
458 //else: had been already disabled, don't reenable
461 delete m_winDisabled;