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/ObjcPose.h"
26 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/mbarman.h"
28 #include "wx/cocoa/NSApplication.h"
30 #import <AppKit/NSApplication.h>
31 #import <Foundation/NSRunLoop.h>
32 #import <Foundation/NSThread.h>
33 #import <AppKit/NSEvent.h>
34 #import <Foundation/NSString.h>
35 #import <Foundation/NSNotification.h>
36 #import <AppKit/NSCell.h>
38 // wxNSApplicationObserver singleton.
39 static wxObjcAutoRefFromAlloc<wxNSApplicationObserver*> sg_cocoaAppObserver = [[wxNSApplicationObserver alloc] init];
41 // ========================================================================
42 // wxPoseAsInitializer
43 // ========================================================================
44 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
46 // ========================================================================
47 // wxNSApplicationDelegate
48 // ========================================================================
49 @implementation wxNSApplicationDelegate : NSObject
51 // NOTE: Terminate means that the event loop does NOT return and thus
52 // cleanup code doesn't properly execute. Furthermore, wxWidgets has its
53 // own exit on frame delete mechanism.
54 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
59 @end // implementation wxNSApplicationDelegate : NSObject
61 // ========================================================================
62 // wxNSApplicationObserver
63 // ========================================================================
64 @implementation wxNSApplicationObserver : NSObject
66 - (void)applicationWillBecomeActive:(NSNotification *)notification
68 wxTheApp->CocoaDelegate_applicationWillBecomeActive();
71 - (void)applicationDidBecomeActive:(NSNotification *)notification
73 wxTheApp->CocoaDelegate_applicationDidBecomeActive();
76 - (void)applicationWillResignActive:(NSNotification *)notification
78 wxTheApp->CocoaDelegate_applicationWillResignActive();
81 - (void)applicationDidResignActive:(NSNotification *)notification
83 wxTheApp->CocoaDelegate_applicationDidResignActive();
86 - (void)applicationWillUpdate:(NSNotification *)notification;
88 wxTheApp->CocoaDelegate_applicationWillUpdate();
91 - (void)controlTintChanged:(NSNotification *)notification
93 wxLogDebug(wxT("TODO: send EVT_SYS_COLOUR_CHANGED as appropriate"));
96 @end // implementation wxNSApplicationObserver : NSObject
98 // ========================================================================
100 // ========================================================================
102 // ----------------------------------------------------------------------------
103 // wxApp Static member initialization
104 // ----------------------------------------------------------------------------
105 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
106 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
107 EVT_IDLE(wxAppBase::OnIdle)
108 // EVT_END_SESSION(wxApp::OnEndSession)
109 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
112 // ----------------------------------------------------------------------------
113 // wxApp initialization/cleanup
114 // ----------------------------------------------------------------------------
115 bool wxApp::Initialize(int& argc, wxChar **argv)
117 wxAutoNSAutoreleasePool pool;
118 m_cocoaMainThread = [NSThread currentThread];
119 // Mac OS X passes a process serial number command line argument when
120 // the application is launched from the Finder. This argument must be
121 // removed from the command line arguments before being handled by the
122 // application (otherwise applications would need to handle it)
125 static const wxChar *ARG_PSN = _T("-psn_");
126 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
128 // remove this argument
130 memmove(argv + 1, argv + 2, argc * sizeof(wxChar *));
134 // Posing must be completed before any instances of the Objective-C
135 // classes being posed as are created.
136 wxPoseAsInitializer::InitializePosers();
138 return wxAppBase::Initialize(argc, argv);
141 void wxApp::CleanUp()
143 wxAutoNSAutoreleasePool pool;
145 wxDC::CocoaShutdownTextSystem();
146 wxMenuBarManager::DestroyInstance();
148 [m_cocoaApp setDelegate:nil];
149 [[NSNotificationCenter defaultCenter] removeObserver:m_cocoaAppDelegate];
150 [m_cocoaAppDelegate release];
151 m_cocoaAppDelegate = NULL;
153 wxAppBase::CleanUp();
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
164 m_isInAssert = false;
165 #endif // __WXDEBUG__
170 m_cocoaAppDelegate = NULL;
173 void wxApp::CocoaDelegate_applicationWillBecomeActive()
177 void wxApp::CocoaDelegate_applicationDidBecomeActive()
181 void wxApp::CocoaDelegate_applicationWillResignActive()
183 wxTopLevelWindowCocoa::DeactivatePendingWindow();
186 void wxApp::CocoaDelegate_applicationDidResignActive()
190 bool wxApp::OnInitGui()
192 wxAutoNSAutoreleasePool pool;
193 if(!wxAppBase::OnInitGui())
196 // Create the app using the sharedApplication method
197 m_cocoaApp = [NSApplication sharedApplication];
199 // Enable response to application delegate messages
200 m_cocoaAppDelegate = [[wxNSApplicationDelegate alloc] init];
201 [m_cocoaApp setDelegate:m_cocoaAppDelegate];
203 // Enable response to "delegate" messages on the notification observer
204 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
205 selector:@selector(applicationWillBecomeActive:)
206 name:NSApplicationWillBecomeActiveNotification object:nil];
208 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
209 selector:@selector(applicationDidBecomeActive:)
210 name:NSApplicationDidBecomeActiveNotification object:nil];
212 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
213 selector:@selector(applicationWillResignActive:)
214 name:NSApplicationWillResignActiveNotification object:nil];
216 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
217 selector:@selector(applicationDidResignActive:)
218 name:NSApplicationDidResignActiveNotification object:nil];
220 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
221 selector:@selector(applicationWillUpdate:)
222 name:NSApplicationWillUpdateNotification object:nil];
224 // Enable response to system notifications
225 [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
226 selector:@selector(controlTintChanged:)
227 name:NSControlTintDidChangeNotification object:nil];
229 wxMenuBarManager::CreateInstance();
231 wxDC::CocoaInitializeTextSystem();
237 if(m_cfRunLoopIdleObserver != NULL)
239 // Invalidate the observer which also removes it from the run loop.
240 CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
241 // Release the ref as we don't need it anymore.
242 m_cfRunLoopIdleObserver.reset();
246 bool wxApp::CallOnInit()
248 // wxAutoNSAutoreleasePool pool;
254 if(!wxAppBase::OnInit())
264 wxAppConsole::Exit();
267 // Yield to other processes
268 bool wxApp::Yield(bool onlyIfNeeded)
271 static bool s_inYield = false;
274 // disable log flushing from here because a call to wxYield() shouldn't
275 // normally result in message boxes popping up &c
283 wxFAIL_MSG( wxT("wxYield called recursively" ) );
291 // Run the event loop until it is out of events
294 wxAutoNSAutoreleasePool pool;
295 NSEvent *event = [GetNSApplication()
296 nextEventMatchingMask:NSAnyEventMask
297 untilDate:[NSDate distantPast]
298 inMode:NSDefaultRunLoopMode
302 [GetNSApplication() sendEvent: event];
306 // let the logs be flashed again
315 void wxApp::WakeUpIdle()
317 [m_cocoaApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
318 location:NSZeroPoint modifierFlags:NSAnyEventMask
319 timestamp:0 windowNumber:0 context:nil
320 subtype:0 data1:0 data2:0] atStart:NO];
323 extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info);
324 extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
326 static_cast<wxApp*>(info)->CF_ObserveMainRunLoopBeforeWaiting(observer, activity);
330 static int sg_cApplicationWillUpdate = 0;
333 void wxApp::CocoaDelegate_applicationWillUpdate()
335 wxLogTrace(wxTRACE_COCOA,wxT("applicationWillUpdate"));
337 // CFRunLoopRef cfRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
338 CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
339 wxCFRef<CFStringRef> cfRunLoopMode(CFRunLoopCopyCurrentMode(cfRunLoop));
341 if(m_cfRunLoopIdleObserver != NULL && m_cfObservedRunLoopMode != cfRunLoopMode)
343 CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
344 m_cfRunLoopIdleObserver.reset();
347 ++sg_cApplicationWillUpdate;
349 if(m_cfRunLoopIdleObserver == NULL)
351 // Enable idle event handling
352 CFRunLoopObserverContext observerContext =
359 m_cfRunLoopIdleObserver.reset(CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, /*repeats*/FALSE, /*priority*/0, ObserveMainRunLoopBeforeWaiting, &observerContext));
360 m_cfObservedRunLoopMode = cfRunLoopMode;
361 CFRunLoopAddObserver(cfRunLoop, m_cfRunLoopIdleObserver, m_cfObservedRunLoopMode);
365 static inline bool FakeNeedMoreIdle()
368 // Return true on every 10th call.
369 static int idleCount = 0;
370 return ++idleCount % 10;
376 void wxApp::CF_ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, int activity)
378 // Ensure that the app knows we've been invalidated
379 m_cfRunLoopIdleObserver.reset();
381 wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN (%d)"), sg_cApplicationWillUpdate);
382 sg_cApplicationWillUpdate = 0;
384 wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN"));
386 if( ProcessIdle() || FakeNeedMoreIdle() )
388 wxLogTrace(wxTRACE_COCOA, wxT("Idle REQUEST MORE"));
389 [NSApp setWindowsNeedUpdate:YES];
393 wxLogTrace(wxTRACE_COCOA, wxT("Idle END"));
398 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
401 wxAppBase::OnAssert(file, line, cond, msg);
402 m_isInAssert = false;
404 #endif // __WXDEBUG__