1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/app.mm
4 // Author: David Elliott
8 // Copyright: (c) David Elliott
10 // Licence: wxWidgets licence
11 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
21 #include "wx/module.h"
24 #include "wx/cocoa/ObjcRef.h"
25 #include "wx/cocoa/autorelease.h"
26 #include "wx/cocoa/mbarman.h"
27 #include "wx/cocoa/NSApplication.h"
29 #import <AppKit/NSApplication.h>
30 #import <Foundation/NSRunLoop.h>
31 #import <Foundation/NSThread.h>
32 #import <AppKit/NSEvent.h>
33 #import <Foundation/NSString.h>
34 #import <Foundation/NSNotification.h>
35 #import <AppKit/NSCell.h>
37 bool wxApp::sm_isEmbedded = false; // Normally we're not a plugin
39 // wxNSApplicationObserver singleton.
40 static wxObjcAutoRefFromAlloc<wxNSApplicationObserver*> sg_cocoaAppObserver = [[WX_GET_OBJC_CLASS(wxNSApplicationObserver) alloc] init];
42 // ========================================================================
43 // wxNSApplicationDelegate
44 // ========================================================================
45 @implementation wxNSApplicationDelegate : NSObject
47 // NOTE: Terminate means that the event loop does NOT return and thus
48 // cleanup code doesn't properly execute. Furthermore, wxWidgets has its
49 // own exit on frame delete mechanism.
50 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
55 @end // implementation wxNSApplicationDelegate : NSObject
56 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSApplicationDelegate,NSObject)
58 // ========================================================================
59 // wxNSApplicationObserver
60 // ========================================================================
61 @implementation wxNSApplicationObserver : NSObject
63 - (void)applicationWillBecomeActive:(NSNotification *)notification
65 wxTheApp->CocoaDelegate_applicationWillBecomeActive();
68 - (void)applicationDidBecomeActive:(NSNotification *)notification
70 wxTheApp->CocoaDelegate_applicationDidBecomeActive();
73 - (void)applicationWillResignActive:(NSNotification *)notification
75 wxTheApp->CocoaDelegate_applicationWillResignActive();
78 - (void)applicationDidResignActive:(NSNotification *)notification
80 wxTheApp->CocoaDelegate_applicationDidResignActive();
83 - (void)applicationWillUpdate:(NSNotification *)notification;
85 wxTheApp->CocoaDelegate_applicationWillUpdate();
88 - (void)controlTintChanged:(NSNotification *)notification
90 wxLogDebug(wxT("TODO: send EVT_SYS_COLOUR_CHANGED as appropriate"));
93 @end // implementation wxNSApplicationObserver : NSObject
94 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSApplicationObserver,NSObject)
96 // ========================================================================
98 // ========================================================================
100 // ----------------------------------------------------------------------------
101 // wxApp Static member initialization
102 // ----------------------------------------------------------------------------
103 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
105 // ----------------------------------------------------------------------------
106 // wxApp initialization/cleanup
107 // ----------------------------------------------------------------------------
108 bool wxApp::Initialize(int& argc, wxChar **argv)
110 wxAutoNSAutoreleasePool pool;
111 m_cocoaMainThread = [NSThread currentThread];
112 // Mac OS X passes a process serial number command line argument when
113 // the application is launched from the Finder. This argument must be
114 // removed from the command line arguments before being handled by the
115 // application (otherwise applications would need to handle it)
118 static const wxChar *ARG_PSN = _T("-psn_");
119 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
121 // remove this argument
123 memmove(argv + 1, argv + 2, argc * sizeof(wxChar *));
128 Cocoa supports -Key value options which set the user defaults key "Key"
129 to the value "value" Some of them are very handy for debugging like
130 -NSShowAllViews YES. Cocoa picks these up from the real argv so
131 our removal of them from the wx copy of it does not affect Cocoa's
134 We basically just assume that any "-NS" option and its following
135 argument needs to be removed from argv. We hope that user code does
136 not expect to see -NS options and indeed it's probably a safe bet
137 since most user code accepting options is probably using the
138 double-dash GNU-style syntax.
140 for(int i=1; i < argc; ++i)
142 static const wxChar *ARG_NS = wxT("-NS");
143 static const int ARG_NS_LEN = wxStrlen(ARG_NS);
144 if( wxStrncmp(argv[i], ARG_NS, ARG_NS_LEN) == 0 )
146 // Only eat this option if it has an argument
150 memmove(argv + i, argv + i + 2, argc * sizeof(wxChar*));
151 // drop back one position so the next run through the loop
152 // reprocesses the argument at our current index.
158 return wxAppBase::Initialize(argc, argv);
161 void wxApp::CleanUp()
163 wxAutoNSAutoreleasePool pool;
165 wxDC::CocoaShutdownTextSystem();
166 wxMenuBarManager::DestroyInstance();
168 [[NSNotificationCenter defaultCenter] removeObserver:sg_cocoaAppObserver];
171 [m_cocoaApp setDelegate:nil];
172 [m_cocoaAppDelegate release];
173 m_cocoaAppDelegate = NULL;
176 wxAppBase::CleanUp();
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
187 m_isInAssert = false;
188 #endif // __WXDEBUG__
193 m_cocoaAppDelegate = NULL;
196 void wxApp::CocoaDelegate_applicationWillBecomeActive()
200 void wxApp::CocoaDelegate_applicationDidBecomeActive()
204 void wxApp::CocoaDelegate_applicationWillResignActive()
206 wxTopLevelWindowCocoa::DeactivatePendingWindow();
209 void wxApp::CocoaDelegate_applicationDidResignActive()
213 bool wxApp::OnInitGui()
215 wxAutoNSAutoreleasePool pool;
216 if(!wxAppBase::OnInitGui())
219 // Create the app using the sharedApplication method
220 m_cocoaApp = [NSApplication sharedApplication];
224 // Enable response to application delegate messages
225 m_cocoaAppDelegate = [[WX_GET_OBJC_CLASS(wxNSApplicationDelegate) alloc] init];
226 [m_cocoaApp setDelegate:m_cocoaAppDelegate];
229 // Enable response to "delegate" messages on the notification observer
230 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
231 selector:@selector(applicationWillBecomeActive:)
232 name:NSApplicationWillBecomeActiveNotification object:nil];
234 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
235 selector:@selector(applicationDidBecomeActive:)
236 name:NSApplicationDidBecomeActiveNotification object:nil];
238 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
239 selector:@selector(applicationWillResignActive:)
240 name:NSApplicationWillResignActiveNotification object:nil];
242 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
243 selector:@selector(applicationDidResignActive:)
244 name:NSApplicationDidResignActiveNotification object:nil];
246 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
247 selector:@selector(applicationWillUpdate:)
248 name:NSApplicationWillUpdateNotification object:nil];
250 // Enable response to system notifications
251 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
252 selector:@selector(controlTintChanged:)
253 name:NSControlTintDidChangeNotification object:nil];
256 wxMenuBarManager::CreateInstance();
258 wxDC::CocoaInitializeTextSystem();
264 if(m_cfRunLoopIdleObserver != NULL)
266 // Invalidate the observer which also removes it from the run loop.
267 CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
268 // Release the ref as we don't need it anymore.
269 m_cfRunLoopIdleObserver.reset();
273 bool wxApp::CallOnInit()
275 // wxAutoNSAutoreleasePool pool;
281 if(!wxAppBase::OnInit())
291 wxAppConsole::Exit();
294 // Yield to other processes
295 bool wxApp::Yield(bool onlyIfNeeded)
298 static bool s_inYield = false;
301 // disable log flushing from here because a call to wxYield() shouldn't
302 // normally result in message boxes popping up &c
310 wxFAIL_MSG( wxT("wxYield called recursively" ) );
318 // Run the event loop until it is out of events
321 wxAutoNSAutoreleasePool pool;
322 /* NOTE: It may be better to use something like
323 NSEventTrackingRunLoopMode since we don't necessarily want all
324 timers/sources/observers to run, only those which would
325 run while tracking events. However, it should be noted that
326 NSEventTrackingRunLoopMode is in the common set of modes
327 so it may not effectively make much of a difference.
329 NSEvent *event = [GetNSApplication()
330 nextEventMatchingMask:NSAnyEventMask
331 untilDate:[NSDate distantPast]
332 inMode:NSDefaultRunLoopMode
336 [GetNSApplication() sendEvent: event];
340 Because we just told NSApplication to avoid blocking it will in turn
341 run the CFRunLoop with a timeout of 0 seconds. In that case, our
342 run loop observer on kCFRunLoopBeforeWaiting never fires because
343 no waiting occurs. Therefore, no idle events are sent.
345 Believe it or not, this is actually desirable because we do not want
346 to process idle from here. However, we do want to process pending
347 events because some user code expects to do work in a thread while
348 the main thread waits and then notify the main thread by posting
351 ProcessPendingEvents();
354 // let the logs be flashed again
363 void wxApp::WakeUpIdle()
365 /* When called from the main thread the NSAutoreleasePool managed by
366 the [NSApplication run] method would ordinarily be in place and so
367 one would think a pool here would be unnecessary.
369 However, when called from a different thread there is usually no
370 NSAutoreleasePool in place because wxThread has no knowledge of
371 wxCocoa. The pool here is generally only ever going to contain
372 the NSEvent we create with the factory method. As soon as we add
373 it to the main event queue with postEvent:atStart: it is retained
374 and so safe for our pool to release.
376 wxAutoNSAutoreleasePool pool;
377 /* NOTE: This is a little heavy handed. What this does is cause an
378 AppKit NSEvent to be added to NSApplication's queue (which is always
379 on the main thread). This will cause the main thread runloop to
380 exit which returns control to nextEventMatchingMask which returns
381 the event which is then sent with sendEvent: and essentially dropped
382 since it's not for a window (windowNumber 0) and NSApplication
383 certainly doesn't understand it.
385 With the exception of wxEventLoop::Exit which uses us to cause the
386 runloop to exit and return to the NSApplication event loop, most
387 callers only need wx idle to happen, or more specifically only really
388 need to ensure that ProcessPendingEvents is called which is currently
389 done without exiting the runloop.
391 Be careful if you decide to change the implementation of this method
392 as wxEventLoop::Exit depends on the current behavior.
394 [m_cocoaApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
395 location:NSZeroPoint modifierFlags:NSAnyEventMask
396 timestamp:0 windowNumber:0 context:nil
397 subtype:0 data1:0 data2:0] atStart:NO];
400 extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info);
401 extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
403 static_cast<wxApp*>(info)->CF_ObserveMainRunLoopBeforeWaiting(observer, activity);
407 static int sg_cApplicationWillUpdate = 0;
411 Invoked from the applicationWillUpdate notification observer. See the
412 NSApplication documentation for the official statement on when this
413 will be called. Since it can be hard to understand for a Cocoa newbie
414 I'll try to explain it here as it relates to wxCocoa.
416 Basically, we get called from within nextEventMatchingMask if and only
417 if any user code told the application to send the update notification
418 (sort of like a request for idle events). However, unlike wx idle events,
419 this notification is sent quite often, nearly every time through the loop
420 because nearly every control tells the application to send it.
422 Because wx idle events are only supposed to be sent when the event loop
423 is about to block we instead schedule a function to be called just
424 before the run loop waits and send the idle events from there.
426 It also has the desirable effect of only sending the wx idle events when
427 the event loop is actualy going to block. If the event loop is being
428 pumped manualy (e.g. like a PeekMessage) then the kCFRunLoopBeforeWaiting
429 observer never fires. Our Yield() method depends on this because sending
430 idle events from within Yield would be bad.
432 Normally you might think that we could just set the observer up once and
433 leave it attached. However, this is problematic because our run loop
434 observer calls user code (the idle handlers) which can actually display
435 modal dialogs. Displaying a modal dialog causes reentry of the event
436 loop, usually in a different run loop mode than the main loop (e.g. in
437 modal-dialog mode instead of default mode). Because we only register the
438 observer with the run loop mode at the time of this call, it won't be
439 called from a modal loop.
441 We want it to be called and thus we need a new observer.
443 void wxApp::CocoaDelegate_applicationWillUpdate()
445 wxLogTrace(wxTRACE_COCOA,wxT("applicationWillUpdate"));
447 // CFRunLoopRef cfRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
448 CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
449 wxCFRef<CFStringRef> cfRunLoopMode(CFRunLoopCopyCurrentMode(cfRunLoop));
451 /* If we have an observer and that observer is for the wrong run loop
452 mode then invalidate it and release it.
454 if(m_cfRunLoopIdleObserver != NULL && m_cfObservedRunLoopMode != cfRunLoopMode)
456 CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
457 m_cfRunLoopIdleObserver.reset();
460 ++sg_cApplicationWillUpdate;
462 /* This will be true either on the first call or when the above code has
463 invalidated and released the exisiting observer.
465 if(m_cfRunLoopIdleObserver == NULL)
467 // Enable idle event handling
468 CFRunLoopObserverContext observerContext =
475 /* NOTE: I can't recall why we don't just let the observer repeat
476 instead of invalidating itself each time it fires thus requiring
477 it to be recreated for each shot but there was if I remember
478 some good (but very obscure) reason for it.
480 On the other hand, I could be wrong so don't take that as gospel.
482 m_cfRunLoopIdleObserver.reset(CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, /*repeats*/FALSE, /*priority*/0, ObserveMainRunLoopBeforeWaiting, &observerContext));
483 m_cfObservedRunLoopMode = cfRunLoopMode;
484 CFRunLoopAddObserver(cfRunLoop, m_cfRunLoopIdleObserver, m_cfObservedRunLoopMode);
488 static inline bool FakeNeedMoreIdle()
491 // Return true on every 10th call.
492 static int idleCount = 0;
493 return ++idleCount % 10;
500 Called by CFRunLoop just before waiting. This is the appropriate time to
501 send idle events. Unlike other ports, we don't peek the queue for events
502 and stop idling if there is one. Instead, if the user requests more idle
503 events we tell Cocoa to send us an applicationWillUpdate notification
504 which will cause our observer of that notification to tell CFRunLoop to
505 call us before waiting which will cause us to be fired again but only
506 after exhausting the event queue.
508 The reason we do it this way is that peeking for an event causes CFRunLoop
509 to reenter and fire off its timers, observers, and sources which we're
510 better off avoiding. Doing it this way, we basically let CFRunLoop do the
511 work of peeking for the next event which is much nicer.
513 void wxApp::CF_ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, int activity)
515 // Ensure that CocoaDelegate_applicationWillUpdate will recreate us.
516 // We've already been invalidated by CFRunLoop because we are one-shot.
517 m_cfRunLoopIdleObserver.reset();
519 wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN (%d)"), sg_cApplicationWillUpdate);
520 sg_cApplicationWillUpdate = 0;
522 wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN"));
524 if( ProcessIdle() || FakeNeedMoreIdle() )
526 wxLogTrace(wxTRACE_COCOA, wxT("Idle REQUEST MORE"));
527 [NSApp setWindowsNeedUpdate:YES];
531 wxLogTrace(wxTRACE_COCOA, wxT("Idle END"));
536 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
539 wxAppBase::OnAssert(file, line, cond, msg);
540 m_isInAssert = false;
542 #endif // __WXDEBUG__
544 /* A note about Cocoa's event loops vs. run loops:
546 It's important to understand that Cocoa has a two-level event loop. The
547 outer level is run by NSApplication and can only ever happen on the main
548 thread. The nextEventMatchingMask:untilDate:inMode:dequeue: method returns
549 the next event which is then given to sendEvent: to send it. These
550 methods are defined in NSApplication and are thus part of AppKit.
552 Events (NSEvent) are only sent due to actual user actions like clicking
553 the mouse or moving the mouse or pressing a key and so on. There are no
554 paint events; there are no timer events; there are no socket events; there
557 All of those types of "events" have nothing to do with the GUI at all.
558 That is why Cocoa's AppKit doesn't implement them. Instead, they are
559 implemented in Foundation's NSRunLoop which on OS X uses CFRunLoop
560 to do the actual work.
562 How NSApplication uses NSRunLoop is rather interesting. Basically, it
563 interacts with NSRunLoop only from within the nextEventMatchingMask
564 method. It passes its inMode: argument almost directly to NSRunLoop
565 and thus CFRunLoop. The run loop then runs (e.g. loops) until it
566 is told to exit. The run loop calls the callout functions directly.
567 From within those callout functions the run loop is considered to
568 be running. Presumably, the AppKit installs a run loop source to
569 receive messages from the window server over the mach port (like a
570 socket). For some messages (e.g. need to paint) the AppKit will
571 call application code like drawRect: without exiting the run loop.
572 For other messages (ones that can be encapsulated in an NSEvent)
573 the AppKit tells the run loop to exit which returns control to
574 the nextEventMatchingMask method which then returns the NSEvent
575 object. It's important to note that once the runloop has exited
576 it is no longer considered running and thus if you ask it which
577 mode it is running in it will return nil.
579 When manually pumping the event loop care should be taken to
580 tell it to run in the correct mode. For instance, if you are
581 using it to run a modal dialog then you want to run it in
582 the modal panel run loop mode. AppKit presumably has sources
583 or timers or observers that specifically don't listen on this
584 mode. Another interesting mode is the connection reply mode.
585 This allows Cocoa to wait for a response from a distributed
586 objects message without firing off user code that may result
587 in a DO call being made thus recursing. So basically, the
588 mode is a way for Cocoa to attempt to avoid run loop recursion
589 but to allow it under certain circumstances.