]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/app.mm
compilation fixes for some cases
[wxWidgets.git] / src / cocoa / app.mm
CommitLineData
fb896a32 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/cocoa/app.mm
fb896a32
DE
3// Purpose: wxApp
4// Author: David Elliott
5// Modified by:
6// Created: 2002/11/27
d89d4059 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) David Elliott
047c1182 9// Software 2000 Ltd.
bb27b372 10// Licence: wxWidgets licence
fb896a32
DE
11/////////////////////////////////////////////////////////////////////////////
12
fb896a32 13#include "wx/wxprec.h"
8898456d 14
da80ae71
WS
15#include "wx/app.h"
16
fb896a32 17#ifndef WX_PRECOMP
891d0563 18 #include "wx/dc.h"
fb896a32
DE
19 #include "wx/intl.h"
20 #include "wx/log.h"
02761f6c 21 #include "wx/module.h"
fb896a32
DE
22#endif
23
047c1182 24#include "wx/cocoa/ObjcRef.h"
493902ac 25#include "wx/cocoa/autorelease.h"
af367f46 26#include "wx/cocoa/mbarman.h"
bb27b372 27#include "wx/cocoa/NSApplication.h"
fb45bb1f 28
fb896a32
DE
29#import <AppKit/NSApplication.h>
30#import <Foundation/NSRunLoop.h>
14fc7eb4 31#import <Foundation/NSThread.h>
aaa5ab05 32#import <AppKit/NSEvent.h>
eb537cfb 33#import <Foundation/NSString.h>
bb27b372
RN
34#import <Foundation/NSNotification.h>
35#import <AppKit/NSCell.h>
fb896a32 36
879180b6
DE
37bool wxApp::sm_isEmbedded = false; // Normally we're not a plugin
38
047c1182 39// wxNSApplicationObserver singleton.
a24aa427 40static wxObjcAutoRefFromAlloc<wxNSApplicationObserver*> sg_cocoaAppObserver = [[WX_GET_OBJC_CLASS(wxNSApplicationObserver) alloc] init];
047c1182 41
ba808e24
DE
42// ========================================================================
43// wxNSApplicationDelegate
44// ========================================================================
ba808e24
DE
45@implementation wxNSApplicationDelegate : NSObject
46
2990ec97 47// NOTE: Terminate means that the event loop does NOT return and thus
bb27b372 48// cleanup code doesn't properly execute. Furthermore, wxWidgets has its
2990ec97 49// own exit on frame delete mechanism.
fb896a32
DE
50- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
51{
2990ec97 52 return NO;
fb896a32
DE
53}
54
047c1182 55@end // implementation wxNSApplicationDelegate : NSObject
a24aa427 56WX_IMPLEMENT_GET_OBJC_CLASS(wxNSApplicationDelegate,NSObject)
047c1182
DE
57
58// ========================================================================
59// wxNSApplicationObserver
60// ========================================================================
61@implementation wxNSApplicationObserver : NSObject
62
0187ddb4
DE
63- (void)applicationWillBecomeActive:(NSNotification *)notification
64{
65 wxTheApp->CocoaDelegate_applicationWillBecomeActive();
66}
67
68- (void)applicationDidBecomeActive:(NSNotification *)notification
69{
70 wxTheApp->CocoaDelegate_applicationDidBecomeActive();
71}
72
73- (void)applicationWillResignActive:(NSNotification *)notification
74{
75 wxTheApp->CocoaDelegate_applicationWillResignActive();
76}
77
78- (void)applicationDidResignActive:(NSNotification *)notification
79{
80 wxTheApp->CocoaDelegate_applicationDidResignActive();
81}
82
047c1182
DE
83- (void)applicationWillUpdate:(NSNotification *)notification;
84{
85 wxTheApp->CocoaDelegate_applicationWillUpdate();
86}
87
bb27b372
RN
88- (void)controlTintChanged:(NSNotification *)notification
89{
422d306c 90 wxLogDebug(wxT("TODO: send EVT_SYS_COLOUR_CHANGED as appropriate"));
bb27b372
RN
91}
92
047c1182 93@end // implementation wxNSApplicationObserver : NSObject
a24aa427 94WX_IMPLEMENT_GET_OBJC_CLASS(wxNSApplicationObserver,NSObject)
e2478fde 95
70fb935a
DE
96// ========================================================================
97// wxApp
98// ========================================================================
fb896a32
DE
99
100// ----------------------------------------------------------------------------
101// wxApp Static member initialization
102// ----------------------------------------------------------------------------
fb896a32
DE
103IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
104BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 105 EVT_IDLE(wxAppBase::OnIdle)
fb896a32
DE
106// EVT_END_SESSION(wxApp::OnEndSession)
107// EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
108END_EVENT_TABLE()
fb896a32
DE
109
110// ----------------------------------------------------------------------------
05e2b077 111// wxApp initialization/cleanup
fb896a32 112// ----------------------------------------------------------------------------
05e2b077 113bool wxApp::Initialize(int& argc, wxChar **argv)
fb896a32 114{
47f1ad6a 115 wxAutoNSAutoreleasePool pool;
14fc7eb4 116 m_cocoaMainThread = [NSThread currentThread];
05e2b077
VZ
117 // Mac OS X passes a process serial number command line argument when
118 // the application is launched from the Finder. This argument must be
119 // removed from the command line arguments before being handled by the
120 // application (otherwise applications would need to handle it)
121 if ( argc > 1 )
122 {
123 static const wxChar *ARG_PSN = _T("-psn_");
b0c0a393 124 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
05e2b077
VZ
125 {
126 // remove this argument
33f39af3 127 --argc;
b0c0a393 128 memmove(argv + 1, argv + 2, argc * sizeof(wxChar *));
05e2b077
VZ
129 }
130 }
131
94826170 132 return wxAppBase::Initialize(argc, argv);
fb896a32
DE
133}
134
94826170 135void wxApp::CleanUp()
fb896a32 136{
0187ddb4
DE
137 wxAutoNSAutoreleasePool pool;
138
891d0563 139 wxDC::CocoaShutdownTextSystem();
af367f46 140 wxMenuBarManager::DestroyInstance();
94826170 141
879180b6
DE
142 [[NSNotificationCenter defaultCenter] removeObserver:sg_cocoaAppObserver];
143 if(!sm_isEmbedded)
144 {
145 [m_cocoaApp setDelegate:nil];
146 [m_cocoaAppDelegate release];
147 m_cocoaAppDelegate = NULL;
148 }
0187ddb4 149
94826170 150 wxAppBase::CleanUp();
fb896a32
DE
151}
152
153// ----------------------------------------------------------------------------
154// wxApp creation
155// ----------------------------------------------------------------------------
fb896a32
DE
156wxApp::wxApp()
157{
158 m_topWindow = NULL;
fb896a32 159
b93d8cc4 160#ifdef __WXDEBUG__
8898456d 161 m_isInAssert = false;
b93d8cc4
DE
162#endif // __WXDEBUG__
163
fb896a32
DE
164 argc = 0;
165 argv = NULL;
166 m_cocoaApp = NULL;
0187ddb4 167 m_cocoaAppDelegate = NULL;
fb896a32
DE
168}
169
0187ddb4
DE
170void wxApp::CocoaDelegate_applicationWillBecomeActive()
171{
172}
173
174void wxApp::CocoaDelegate_applicationDidBecomeActive()
175{
176}
177
178void wxApp::CocoaDelegate_applicationWillResignActive()
179{
39050120 180 wxTopLevelWindowCocoa::DeactivatePendingWindow();
0187ddb4
DE
181}
182
183void wxApp::CocoaDelegate_applicationDidResignActive()
184{
185}
186
fb896a32
DE
187bool wxApp::OnInitGui()
188{
47f1ad6a 189 wxAutoNSAutoreleasePool pool;
fb896a32 190 if(!wxAppBase::OnInitGui())
8898456d 191 return false;
fb896a32
DE
192
193 // Create the app using the sharedApplication method
194 m_cocoaApp = [NSApplication sharedApplication];
047c1182 195
879180b6
DE
196 if(!sm_isEmbedded)
197 {
198 // Enable response to application delegate messages
a24aa427 199 m_cocoaAppDelegate = [[WX_GET_OBJC_CLASS(wxNSApplicationDelegate) alloc] init];
879180b6
DE
200 [m_cocoaApp setDelegate:m_cocoaAppDelegate];
201 }
047c1182
DE
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
bb27b372
RN
226 selector:@selector(controlTintChanged:)
227 name:NSControlTintDidChangeNotification object:nil];
af367f46 228
879180b6
DE
229 if(!sm_isEmbedded)
230 wxMenuBarManager::CreateInstance();
af367f46 231
891d0563 232 wxDC::CocoaInitializeTextSystem();
8898456d 233 return true;
fb896a32
DE
234}
235
047c1182
DE
236wxApp::~wxApp()
237{
238 if(m_cfRunLoopIdleObserver != NULL)
239 {
240 // Invalidate the observer which also removes it from the run loop.
241 CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
242 // Release the ref as we don't need it anymore.
243 m_cfRunLoopIdleObserver.reset();
244 }
245}
246
47f1ad6a
DE
247bool wxApp::CallOnInit()
248{
bd3e8827 249// wxAutoNSAutoreleasePool pool;
47f1ad6a
DE
250 return OnInit();
251}
252
fb896a32
DE
253bool wxApp::OnInit()
254{
255 if(!wxAppBase::OnInit())
8898456d 256 return false;
fb896a32 257
8898456d 258 return true;
fb896a32
DE
259}
260
70fb935a
DE
261void wxApp::Exit()
262{
263 wxApp::CleanUp();
264
265 wxAppConsole::Exit();
266}
267
fb896a32 268// Yield to other processes
fb896a32
DE
269bool wxApp::Yield(bool onlyIfNeeded)
270{
271 // MT-FIXME
272 static bool s_inYield = false;
273
274#if wxUSE_LOG
275 // disable log flushing from here because a call to wxYield() shouldn't
276 // normally result in message boxes popping up &c
277 wxLog::Suspend();
278#endif // wxUSE_LOG
279
280 if (s_inYield)
281 {
282 if ( !onlyIfNeeded )
283 {
284 wxFAIL_MSG( wxT("wxYield called recursively" ) );
285 }
286
287 return false;
288 }
289
290 s_inYield = true;
291
aaa5ab05 292 // Run the event loop until it is out of events
bb27b372
RN
293 while(1)
294 {
295 wxAutoNSAutoreleasePool pool;
296 NSEvent *event = [GetNSApplication()
aaa5ab05 297 nextEventMatchingMask:NSAnyEventMask
422d306c 298 untilDate:[NSDate distantPast]
aaa5ab05 299 inMode:NSDefaultRunLoopMode
bb27b372
RN
300 dequeue: YES];
301 if(!event)
302 break;
aaa5ab05
DE
303 [GetNSApplication() sendEvent: event];
304 }
fb896a32
DE
305
306#if wxUSE_LOG
307 // let the logs be flashed again
308 wxLog::Resume();
309#endif // wxUSE_LOG
310
311 s_inYield = false;
312
313 return true;
314}
315
eb537cfb
DE
316void wxApp::WakeUpIdle()
317{
318 [m_cocoaApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
319 location:NSZeroPoint modifierFlags:NSAnyEventMask
320 timestamp:0 windowNumber:0 context:nil
321 subtype:0 data1:0 data2:0] atStart:NO];
322}
323
047c1182
DE
324extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info);
325extern "C" static void ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
326{
327 static_cast<wxApp*>(info)->CF_ObserveMainRunLoopBeforeWaiting(observer, activity);
328}
329
330#if 0
331static int sg_cApplicationWillUpdate = 0;
332#endif
333
334void wxApp::CocoaDelegate_applicationWillUpdate()
335{
336 wxLogTrace(wxTRACE_COCOA,wxT("applicationWillUpdate"));
337
338// CFRunLoopRef cfRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
339 CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
340 wxCFRef<CFStringRef> cfRunLoopMode(CFRunLoopCopyCurrentMode(cfRunLoop));
341
342 if(m_cfRunLoopIdleObserver != NULL && m_cfObservedRunLoopMode != cfRunLoopMode)
343 {
344 CFRunLoopObserverInvalidate(m_cfRunLoopIdleObserver);
345 m_cfRunLoopIdleObserver.reset();
346 }
347#if 0
348 ++sg_cApplicationWillUpdate;
349#endif
350 if(m_cfRunLoopIdleObserver == NULL)
351 {
352 // Enable idle event handling
353 CFRunLoopObserverContext observerContext =
354 { 0
355 , this
356 , NULL
357 , NULL
358 , NULL
359 };
360 m_cfRunLoopIdleObserver.reset(CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, /*repeats*/FALSE, /*priority*/0, ObserveMainRunLoopBeforeWaiting, &observerContext));
361 m_cfObservedRunLoopMode = cfRunLoopMode;
362 CFRunLoopAddObserver(cfRunLoop, m_cfRunLoopIdleObserver, m_cfObservedRunLoopMode);
363 }
364}
365
366static inline bool FakeNeedMoreIdle()
367{
368#if 0
369// Return true on every 10th call.
370 static int idleCount = 0;
371 return ++idleCount % 10;
372#else
373 return false;
374#endif
375}
376
377void wxApp::CF_ObserveMainRunLoopBeforeWaiting(CFRunLoopObserverRef observer, int activity)
378{
379 // Ensure that the app knows we've been invalidated
380 m_cfRunLoopIdleObserver.reset();
381#if 0
382 wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN (%d)"), sg_cApplicationWillUpdate);
383 sg_cApplicationWillUpdate = 0;
384#else
385 wxLogTrace(wxTRACE_COCOA,wxT("Idle BEGIN"));
386#endif
387 if( ProcessIdle() || FakeNeedMoreIdle() )
388 {
389 wxLogTrace(wxTRACE_COCOA, wxT("Idle REQUEST MORE"));
390 [NSApp setWindowsNeedUpdate:YES];
391 }
392 else
393 {
394 wxLogTrace(wxTRACE_COCOA, wxT("Idle END"));
395 }
396}
397
b93d8cc4
DE
398#ifdef __WXDEBUG__
399void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
400{
8898456d 401 m_isInAssert = true;
b93d8cc4 402 wxAppBase::OnAssert(file, line, cond, msg);
8898456d 403 m_isInAssert = false;
b93d8cc4
DE
404}
405#endif // __WXDEBUG__