Rework idle handling so that NSApplication does not need to be subclassed or posed as.
[wxWidgets.git] / src / cocoa / app.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/app.mm
3 // Purpose:     wxApp
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2002/11/27
7 // RCS-ID:      $Id$
8 // Copyright:   (c) David Elliott
9 //              Software 2000 Ltd.
10 // Licence:     wxWidgets licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #include "wx/wxprec.h"
14
15 #include "wx/app.h"
16
17 #ifndef WX_PRECOMP
18     #include "wx/dc.h"
19     #include "wx/intl.h"
20     #include "wx/log.h"
21     #include "wx/module.h"
22 #endif
23
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"
29
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>
37
38 // wxNSApplicationObserver singleton.
39 static wxObjcAutoRefFromAlloc<wxNSApplicationObserver*> sg_cocoaAppObserver = [[wxNSApplicationObserver alloc] init];
40
41 // ========================================================================
42 // wxPoseAsInitializer
43 // ========================================================================
44 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
45
46 // ========================================================================
47 // wxNSApplicationDelegate
48 // ========================================================================
49 @implementation wxNSApplicationDelegate : NSObject
50
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
55 {
56     return NO;
57 }
58
59 @end // implementation wxNSApplicationDelegate : NSObject
60
61 // ========================================================================
62 // wxNSApplicationObserver
63 // ========================================================================
64 @implementation wxNSApplicationObserver : NSObject
65
66 - (void)applicationWillBecomeActive:(NSNotification *)notification
67 {
68     wxTheApp->CocoaDelegate_applicationWillBecomeActive();
69 }
70
71 - (void)applicationDidBecomeActive:(NSNotification *)notification
72 {
73     wxTheApp->CocoaDelegate_applicationDidBecomeActive();
74 }
75
76 - (void)applicationWillResignActive:(NSNotification *)notification
77 {
78     wxTheApp->CocoaDelegate_applicationWillResignActive();
79 }
80
81 - (void)applicationDidResignActive:(NSNotification *)notification
82 {
83     wxTheApp->CocoaDelegate_applicationDidResignActive();
84 }
85
86 - (void)applicationWillUpdate:(NSNotification *)notification;
87 {
88     wxTheApp->CocoaDelegate_applicationWillUpdate();
89 }
90
91 - (void)controlTintChanged:(NSNotification *)notification
92 {
93     wxLogDebug(wxT("TODO: send EVT_SYS_COLOUR_CHANGED as appropriate"));
94 }
95
96 @end // implementation wxNSApplicationObserver : NSObject
97
98 // ========================================================================
99 // wxApp
100 // ========================================================================
101
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)
110 END_EVENT_TABLE()
111
112 // ----------------------------------------------------------------------------
113 // wxApp initialization/cleanup
114 // ----------------------------------------------------------------------------
115 bool wxApp::Initialize(int& argc, wxChar **argv)
116 {
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)
123     if ( argc > 1 )
124     {
125         static const wxChar *ARG_PSN = _T("-psn_");
126         if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
127         {
128             // remove this argument
129             --argc;
130             memmove(argv + 1, argv + 2, argc * sizeof(wxChar *));
131         }
132     }
133
134     // Posing must be completed before any instances of the Objective-C
135     // classes being posed as are created.
136     wxPoseAsInitializer::InitializePosers();
137
138     return wxAppBase::Initialize(argc, argv);
139 }
140
141 void wxApp::CleanUp()
142 {
143     wxAutoNSAutoreleasePool pool;
144
145     wxDC::CocoaShutdownTextSystem();
146     wxMenuBarManager::DestroyInstance();
147
148     [m_cocoaApp setDelegate:nil];
149     [[NSNotificationCenter defaultCenter] removeObserver:m_cocoaAppDelegate];
150     [m_cocoaAppDelegate release];
151     m_cocoaAppDelegate = NULL;
152
153     wxAppBase::CleanUp();
154 }
155
156 // ----------------------------------------------------------------------------
157 // wxApp creation
158 // ----------------------------------------------------------------------------
159 wxApp::wxApp()
160 {
161     m_topWindow = NULL;
162
163 #ifdef __WXDEBUG__
164     m_isInAssert = false;
165 #endif // __WXDEBUG__
166
167     argc = 0;
168     argv = NULL;
169     m_cocoaApp = NULL;
170     m_cocoaAppDelegate = NULL;
171 }
172
173 void wxApp::CocoaDelegate_applicationWillBecomeActive()
174 {
175 }
176
177 void wxApp::CocoaDelegate_applicationDidBecomeActive()
178 {
179 }
180
181 void wxApp::CocoaDelegate_applicationWillResignActive()
182 {
183     wxTopLevelWindowCocoa::DeactivatePendingWindow();
184 }
185
186 void wxApp::CocoaDelegate_applicationDidResignActive()
187 {
188 }
189
190 bool wxApp::OnInitGui()
191 {
192     wxAutoNSAutoreleasePool pool;
193     if(!wxAppBase::OnInitGui())
194         return false;
195
196     // Create the app using the sharedApplication method
197     m_cocoaApp = [NSApplication sharedApplication];
198
199     // Enable response to application delegate messages
200     m_cocoaAppDelegate = [[wxNSApplicationDelegate alloc] init];
201     [m_cocoaApp setDelegate:m_cocoaAppDelegate];
202
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];
207
208     [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
209         selector:@selector(applicationDidBecomeActive:)
210         name:NSApplicationDidBecomeActiveNotification object:nil];
211
212     [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
213         selector:@selector(applicationWillResignActive:)
214         name:NSApplicationWillResignActiveNotification object:nil];
215
216     [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
217         selector:@selector(applicationDidResignActive:)
218         name:NSApplicationDidResignActiveNotification object:nil];
219
220     [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
221         selector:@selector(applicationWillUpdate:)
222         name:NSApplicationWillUpdateNotification object:nil];
223
224     // Enable response to system notifications
225     [[NSNotificationCenter defaultCenter] addObserver:sg_cocoaAppObserver
226         selector:@selector(controlTintChanged:)
227         name:NSControlTintDidChangeNotification object:nil];
228
229     wxMenuBarManager::CreateInstance();
230
231     wxDC::CocoaInitializeTextSystem();
232     return true;
233 }
234
235 wxApp::~wxApp()
236 {
237     if(m_cfRunLoopIdleObserver != NULL)
238     {
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();
243     }
244 }
245
246 bool wxApp::CallOnInit()
247 {
248 //    wxAutoNSAutoreleasePool pool;
249     return OnInit();
250 }
251
252 bool wxApp::OnInit()
253 {
254     if(!wxAppBase::OnInit())
255         return false;
256
257     return true;
258 }
259
260 void wxApp::Exit()
261 {
262     wxApp::CleanUp();
263
264     wxAppConsole::Exit();
265 }
266
267 // Yield to other processes
268 bool wxApp::Yield(bool onlyIfNeeded)
269 {
270     // MT-FIXME
271     static bool s_inYield = false;
272
273 #if wxUSE_LOG
274     // disable log flushing from here because a call to wxYield() shouldn't
275     // normally result in message boxes popping up &c
276     wxLog::Suspend();
277 #endif // wxUSE_LOG
278
279     if (s_inYield)
280     {
281         if ( !onlyIfNeeded )
282         {
283             wxFAIL_MSG( wxT("wxYield called recursively" ) );
284         }
285
286         return false;
287     }
288
289     s_inYield = true;
290
291     // Run the event loop until it is out of events
292     while(1)
293     {
294         wxAutoNSAutoreleasePool pool;
295         NSEvent *event = [GetNSApplication()
296                 nextEventMatchingMask:NSAnyEventMask
297                 untilDate:[NSDate distantPast]
298                 inMode:NSDefaultRunLoopMode
299                 dequeue: YES];
300         if(!event)
301             break;
302         [GetNSApplication() sendEvent: event];
303     }
304
305 #if wxUSE_LOG
306     // let the logs be flashed again
307     wxLog::Resume();
308 #endif // wxUSE_LOG
309
310     s_inYield = false;
311
312     return true;
313 }
314
315 void wxApp::WakeUpIdle()
316 {
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];
321 }
322
323 extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info);
324 extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
325 {
326     static_cast<wxApp*>(info)->CF_ObserveMainRunLoopBeforeWaiting(observer, activity);
327 }
328
329 #if 0
330 static int sg_cApplicationWillUpdate = 0;
331 #endif
332
333 void wxApp::CocoaDelegate_applicationWillUpdate()
334 {
335     wxLogTrace(wxTRACE_COCOA,wxT("applicationWillUpdate"));
336
337 //    CFRunLoopRef cfRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
338     CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
339     wxCFRef<CFStringRef> cfRunLoopMode(CFRunLoopCopyCurrentMode(cfRunLoop));
340
341     if(m_cfRunLoopIdleObserver != NULL && m_cfObservedRunLoopMode != cfRunLoopMode)
342     {
343         CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
344         m_cfRunLoopIdleObserver.reset();
345     }
346 #if 0
347     ++sg_cApplicationWillUpdate;
348 #endif
349     if(m_cfRunLoopIdleObserver == NULL)
350     {
351         // Enable idle event handling
352         CFRunLoopObserverContext observerContext =
353         {   0
354         ,   this
355         ,   NULL
356         ,   NULL
357         ,   NULL
358         };
359         m_cfRunLoopIdleObserver.reset(CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, /*repeats*/FALSE, /*priority*/0, ObserveMainRunLoopBeforeWaiting, &observerContext));
360         m_cfObservedRunLoopMode = cfRunLoopMode;
361         CFRunLoopAddObserver(cfRunLoop, m_cfRunLoopIdleObserver, m_cfObservedRunLoopMode);
362     }
363 }
364
365 static inline bool FakeNeedMoreIdle()
366 {
367 #if 0
368 // Return true on every 10th call.
369     static int idleCount = 0;
370     return ++idleCount % 10;
371 #else
372     return false;
373 #endif
374 }
375
376 void wxApp::CF_ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, int activity)
377 {
378     // Ensure that the app knows we've been invalidated
379     m_cfRunLoopIdleObserver.reset();
380 #if 0
381     wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN (%d)"), sg_cApplicationWillUpdate);
382     sg_cApplicationWillUpdate = 0;
383 #else
384     wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN"));
385 #endif
386     if( ProcessIdle() || FakeNeedMoreIdle() )
387     {
388         wxLogTrace(wxTRACE_COCOA, wxT("Idle REQUEST MORE"));
389         [NSApp setWindowsNeedUpdate:YES];
390     }
391     else
392     {
393         wxLogTrace(wxTRACE_COCOA, wxT("Idle END"));
394     }
395 }
396
397 #ifdef __WXDEBUG__
398 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
399 {
400     m_isInAssert = true;
401     wxAppBase::OnAssert(file, line, cond, msg);
402     m_isInAssert = false;
403 }
404 #endif // __WXDEBUG__