]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/app.mm
2ee2c6bab8f69abeca77e7b9fa5738fdbb90e60b
[wxWidgets.git] / src / cocoa / app.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #include "wx/defs.h"
15 #include "wx/app.h"
16 #include "wx/frame.h"
17 #include "wx/dialog.h"
18 #include "wx/dc.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #endif
22
23 #include "wx/module.h"
24
25 #include "wx/cocoa/ObjcPose.h"
26 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/mbarman.h"
28
29 #if wxUSE_WX_RESOURCES
30 # include "wx/resource.h"
31 #endif
32
33 #import <AppKit/NSApplication.h>
34 #import <Foundation/NSRunLoop.h>
35 #import <Foundation/NSArray.h>
36 #import <Foundation/NSAutoreleasePool.h>
37 #import <Foundation/NSThread.h>
38 #import <AppKit/NSEvent.h>
39
40 // ========================================================================
41 // wxPoseAsInitializer
42 // ========================================================================
43 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
44
45 // ========================================================================
46 // wxPoserNSApplication
47 // ========================================================================
48 @interface wxPoserNSApplication : NSApplication
49 {
50 }
51
52 - (void)doIdle: (id)data;
53 - (void)sendEvent: (NSEvent*)anEvent;
54 @end // wxPoserNSApplication
55
56 WX_IMPLEMENT_POSER(wxPoserNSApplication);
57
58 @implementation wxPoserNSApplication : NSApplication
59
60 - (void)doIdle: (id)data
61 {
62 wxASSERT(wxTheApp);
63 wxASSERT(wxMenuBarManager::GetInstance());
64 wxMenuBarManager::GetInstance()->CocoaInternalIdle();
65 wxLogDebug("doIdle called");
66 #ifdef __WXDEBUG__
67 if(wxTheApp->IsInAssert())
68 {
69 wxLogDebug("Idle events ignored durring assertion dialog");
70 }
71 else
72 #endif
73 {
74 NSRunLoop *rl = [NSRunLoop currentRunLoop];
75 // runMode: beforeDate returns YES if something was done
76 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
77 {
78 wxLogDebug("Looping for idle events");
79 #if 1
80 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
81 {
82 wxLogDebug("Found actual work to do");
83 break;
84 }
85 #endif
86 }
87 }
88 wxLogDebug("Idle processing complete, requesting next idle event");
89 // Add ourself back into the run loop (on next event) if necessary
90 wxTheApp->CocoaRequestIdle();
91 }
92
93 - (void)sendEvent: (NSEvent*)anEvent
94 {
95 wxLogDebug("SendEvent");
96 wxTheApp->CocoaInstallRequestedIdleHandler();
97 [super sendEvent: anEvent];
98 }
99
100 @end // wxPoserNSApplication
101
102 // ========================================================================
103 // wxNSApplicationDelegate
104 // ========================================================================
105 @interface wxNSApplicationDelegate : NSObject
106 {
107 }
108
109 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
110 - (void)applicationWillBecomeActive:(NSNotification *)notification;
111 - (void)applicationDidBecomeActive:(NSNotification *)notification;
112 - (void)applicationWillResignActive:(NSNotification *)notification;
113 - (void)applicationDidResignActive:(NSNotification *)notification;
114 @end // interface wxNSApplicationDelegate : NSObject
115
116 @implementation wxNSApplicationDelegate : NSObject
117
118 // NOTE: Terminate means that the event loop does NOT return and thus
119 // cleanup code doesn't properly execute. Furthermore, wxWindows has its
120 // own exit on frame delete mechanism.
121 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
122 {
123 return NO;
124 }
125
126 - (void)applicationWillBecomeActive:(NSNotification *)notification
127 {
128 wxTheApp->CocoaDelegate_applicationWillBecomeActive();
129 }
130
131 - (void)applicationDidBecomeActive:(NSNotification *)notification
132 {
133 wxTheApp->CocoaDelegate_applicationDidBecomeActive();
134 }
135
136 - (void)applicationWillResignActive:(NSNotification *)notification
137 {
138 wxTheApp->CocoaDelegate_applicationWillResignActive();
139 }
140
141 - (void)applicationDidResignActive:(NSNotification *)notification
142 {
143 wxTheApp->CocoaDelegate_applicationDidResignActive();
144 }
145
146 @end // implementation wxNSApplicationDelegate : NSObject
147
148 // ========================================================================
149 // wxApp
150 // ========================================================================
151
152 // ----------------------------------------------------------------------------
153 // wxApp Static member initialization
154 // ----------------------------------------------------------------------------
155 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
156 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
157 EVT_IDLE(wxAppBase::OnIdle)
158 // EVT_END_SESSION(wxApp::OnEndSession)
159 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
160 END_EVENT_TABLE()
161
162 // ----------------------------------------------------------------------------
163 // wxApp initialization/cleanup
164 // ----------------------------------------------------------------------------
165 bool wxApp::Initialize(int& argc, wxChar **argv)
166 {
167 wxAutoNSAutoreleasePool pool;
168 m_cocoaMainThread = [NSThread currentThread];
169 // Mac OS X passes a process serial number command line argument when
170 // the application is launched from the Finder. This argument must be
171 // removed from the command line arguments before being handled by the
172 // application (otherwise applications would need to handle it)
173 if ( argc > 1 )
174 {
175 static const wxChar *ARG_PSN = _T("-psn_");
176 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
177 {
178 // remove this argument
179 memmove(argv, argv + 1, argc--);
180 }
181 }
182
183 // Posing must be completed before any instances of the Objective-C
184 // classes being posed as are created.
185 wxPoseAsInitializer::InitializePosers();
186
187 return wxAppBase::Initialize(argc, argv);
188 }
189
190 void wxApp::CleanUp()
191 {
192 wxAutoNSAutoreleasePool pool;
193
194 wxDC::CocoaShutdownTextSystem();
195 wxMenuBarManager::DestroyInstance();
196
197 [m_cocoaApp setDelegate:nil];
198 [m_cocoaAppDelegate release];
199 m_cocoaAppDelegate = NULL;
200
201 wxAppBase::CleanUp();
202 }
203
204 // ----------------------------------------------------------------------------
205 // wxApp creation
206 // ----------------------------------------------------------------------------
207 wxApp::wxApp()
208 {
209 m_topWindow = NULL;
210
211 m_isIdle = true;
212 #if WXWIN_COMPATIBILITY_2_2
213 m_wantDebugOutput = TRUE;
214 #endif
215 #ifdef __WXDEBUG__
216 m_isInAssert = FALSE;
217 #endif // __WXDEBUG__
218
219 argc = 0;
220 argv = NULL;
221 m_cocoaApp = NULL;
222 m_cocoaAppDelegate = NULL;
223 }
224
225 void wxApp::CocoaInstallIdleHandler()
226 {
227 // If we're not the main thread, don't install the idle handler
228 if(m_cocoaMainThread != [NSThread currentThread])
229 {
230 wxLogDebug("Attempt to install idle handler from secondary thread");
231 return;
232 }
233 // If we're supposed to be stopping, don't add more idle events
234 if(![m_cocoaApp isRunning])
235 return;
236 wxLogDebug("wxApp::CocoaInstallIdleHandler");
237 m_isIdle = false;
238 // Call doIdle for EVERYTHING dammit
239 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
240 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
241 }
242
243 void wxApp::CocoaDelegate_applicationWillBecomeActive()
244 {
245 }
246
247 void wxApp::CocoaDelegate_applicationDidBecomeActive()
248 {
249 }
250
251 void wxApp::CocoaDelegate_applicationWillResignActive()
252 {
253 }
254
255 void wxApp::CocoaDelegate_applicationDidResignActive()
256 {
257 }
258
259 bool wxApp::OnInitGui()
260 {
261 wxAutoNSAutoreleasePool pool;
262 if(!wxAppBase::OnInitGui())
263 return FALSE;
264
265 // Create the app using the sharedApplication method
266 m_cocoaApp = [NSApplication sharedApplication];
267 m_cocoaAppDelegate = [[wxNSApplicationDelegate alloc] init];
268 [m_cocoaApp setDelegate:m_cocoaAppDelegate];
269
270 wxMenuBarManager::CreateInstance();
271
272 wxDC::CocoaInitializeTextSystem();
273 // [ m_cocoaApp setDelegate:m_cocoaApp ];
274 #if 0
275 wxLogDebug("Just for kicks");
276 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
277 wxLogDebug("okay.. done now");
278 #endif
279 return TRUE;
280 }
281
282 bool wxApp::CallOnInit()
283 {
284 // wxAutoNSAutoreleasePool pool;
285 return OnInit();
286 }
287
288 bool wxApp::OnInit()
289 {
290 if(!wxAppBase::OnInit())
291 return FALSE;
292
293 return TRUE;
294 }
295
296 void wxApp::Exit()
297 {
298 wxApp::CleanUp();
299
300 wxAppConsole::Exit();
301 }
302
303 // Yield to other processes
304 bool wxApp::Yield(bool onlyIfNeeded)
305 {
306 // MT-FIXME
307 static bool s_inYield = false;
308
309 #if wxUSE_LOG
310 // disable log flushing from here because a call to wxYield() shouldn't
311 // normally result in message boxes popping up &c
312 wxLog::Suspend();
313 #endif // wxUSE_LOG
314
315 if (s_inYield)
316 {
317 if ( !onlyIfNeeded )
318 {
319 wxFAIL_MSG( wxT("wxYield called recursively" ) );
320 }
321
322 return false;
323 }
324
325 s_inYield = true;
326
327 // Run the event loop until it is out of events
328 while(NSEvent *event = [GetNSApplication()
329 nextEventMatchingMask:NSAnyEventMask
330 untilDate:[NSDate distantPast]
331 inMode:NSDefaultRunLoopMode
332 dequeue: YES])
333 {
334 [GetNSApplication() sendEvent: event];
335 }
336
337 #if wxUSE_LOG
338 // let the logs be flashed again
339 wxLog::Resume();
340 #endif // wxUSE_LOG
341
342 s_inYield = false;
343
344 return true;
345 }
346
347 #ifdef __WXDEBUG__
348 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
349 {
350 m_isInAssert = TRUE;
351 wxAppBase::OnAssert(file, line, cond, msg);
352 m_isInAssert = FALSE;
353 }
354 #endif // __WXDEBUG__
355