]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/app.mm
8532a12f941933b42343f14907fd109d22438faa
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22 #include "wx/defs.h"
23 #include "wx/app.h"
24 #include "wx/frame.h"
25 #include "wx/dialog.h"
26 #include "wx/dc.h"
27 #include "wx/intl.h"
28 #include "wx/log.h"
29 #endif
30
31 #include "wx/module.h"
32
33 #include "wx/cocoa/ObjcPose.h"
34
35 #if wxUSE_WX_RESOURCES
36 # include "wx/resource.h"
37 #endif
38
39 #import <AppKit/NSApplication.h>
40 #import <Foundation/NSRunLoop.h>
41 #import <Foundation/NSArray.h>
42
43 // ----------------------------------------------------------------------------
44 // globals
45 // ----------------------------------------------------------------------------
46
47 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
48
49 @interface wxPoserNSApplication : NSApplication
50 {
51 }
52
53 - (void)doIdle: (id)data;
54 - (void)finishLaunching;
55 - (void)sendEvent: (NSEvent*)anEvent;
56 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
57 @end // wxPoserNSApplication
58
59 @implementation wxPoserNSApplication : NSApplication
60
61 - (void)doIdle: (id)data
62 {
63 wxASSERT(wxTheApp);
64 wxLogDebug("doIdle called");
65 NSRunLoop *rl = [NSRunLoop currentRunLoop];
66 // runMode: beforeDate returns YES if something was done
67 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
68 {
69 wxLogDebug("Looping for idle events");
70 #if 1
71 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
72 {
73 wxLogDebug("Found actual work to do");
74 break;
75 }
76 #endif
77 }
78 wxLogDebug("Idle processing complete, requesting next idle event");
79 // Add ourself back into the run loop (on next event) if necessary
80 wxTheApp->CocoaRequestIdle();
81 }
82
83 - (void)finishLaunching
84 {
85 wxLogDebug("finishLaunching");
86 bool initsuccess = wxTheApp->OnInit();
87 if(!initsuccess)
88 [super stop: NULL];
89
90 [super finishLaunching];
91 }
92
93 - (void)sendEvent: (NSEvent*)anEvent
94 {
95 wxLogDebug("SendEvent");
96 wxTheApp->CocoaInstallRequestedIdleHandler();
97 [super sendEvent: anEvent];
98 }
99
100 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
101 {
102 BOOL ret = wxTheApp->GetExitOnFrameDelete();
103 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
104 return ret;
105 }
106
107 @end // wxPoserNSApplication
108 WX_IMPLEMENT_POSER(wxPoserNSApplication);
109
110 // ============================================================================
111 // functions
112 // ============================================================================
113
114 //----------------------------------------------------------------------
115 // wxEntry
116 //----------------------------------------------------------------------
117
118 int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
119 {
120 return wxApp::Initialize();
121 }
122
123 int WXDLLEXPORT wxEntryInitGui()
124 {
125 return wxTheApp->OnInitGui();
126 }
127
128 void WXDLLEXPORT wxEntryCleanup()
129 {
130 wxApp::CleanUp();
131 }
132
133 int wxEntry( int argc, char *argv[])
134 {
135 if (!wxEntryStart(argc, argv)) {
136 return 0;
137 }
138 wxLogDebug("Creating application");
139 // create the application object or ensure that one already exists
140 if (!wxTheApp)
141 {
142 // The app may have declared a global application object, but we recommend
143 // the IMPLEMENT_APP macro is used instead, which sets an initializer
144 // function for delayed, dynamic app object construction.
145 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
146 wxT("No initializer - use IMPLEMENT_APP macro.") );
147
148 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
149 }
150
151 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
152
153 // Mac OS X passes a process serial number command line argument when
154 // the application is launched from the Finder. This argument must be
155 // removed from the command line arguments before being handled by the
156 // application (otherwise applications would need to handle it)
157
158 if (argc > 1) {
159 char theArg[6] = "";
160 strncpy(theArg, argv[1], 5);
161
162 if (strcmp(theArg, "-psn_") == 0) {
163 // assume the argument is always the only one and remove it
164 --argc;
165 }
166 }
167
168 wxTheApp->argc = argc;
169 wxTheApp->argv = argv;
170
171 wxLogDebug("initializing gui");
172 // GUI-specific initialization, such as creating an app context.
173 wxEntryInitGui();
174
175 // Here frames insert themselves automatically
176 // into wxTopLevelWindows by getting created
177 // in OnInit().
178
179 int retValue = 0;
180
181 wxLogDebug("Time to run");
182 retValue = wxTheApp->OnRun();
183
184 wxWindow *topWindow = wxTheApp->GetTopWindow();
185 if ( topWindow )
186 {
187 // Forcibly delete the window.
188 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
189 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
190 {
191 topWindow->Close(TRUE);
192 }
193 else
194 {
195 delete topWindow;
196 wxTheApp->SetTopWindow(NULL);
197 }
198 }
199
200 wxTheApp->OnExit();
201
202 wxEntryCleanup();
203
204 return retValue;
205 }
206
207 void wxApp::Exit()
208 {
209 wxApp::CleanUp();
210
211 wxAppConsole::Exit();
212 }
213
214 // ============================================================================
215 // wxApp implementation
216 // ============================================================================
217
218 // ----------------------------------------------------------------------------
219 // wxApp Static member initialization
220 // ----------------------------------------------------------------------------
221
222 #if !USE_SHARED_LIBRARY
223 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
224 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
225 EVT_IDLE(wxApp::OnIdle)
226 // EVT_END_SESSION(wxApp::OnEndSession)
227 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
228 END_EVENT_TABLE()
229 #endif
230
231 // ----------------------------------------------------------------------------
232 // wxApp static functions
233 // ----------------------------------------------------------------------------
234
235 bool wxApp::Initialize(int argc, wxChar **argv)
236 {
237 // VZ: apparently this needs to be done a.s.a.p., right? it is done after
238 // wxClassInfo::InitializeClasses() now but usd to be done before, I
239 // hope it's not a problem -- if it is, please let me know, David (if
240 // it isn't, just remove this comment :-)
241 wxPoseAsInitializer::InitializePosers();
242
243 return wxAppBase::Initialize(argc, argv);
244 }
245
246 void wxApp::CleanUp()
247 {
248 wxDC::CocoaShutdownTextSystem();
249
250 wxAppBase::CleanUp();
251 }
252
253 // ----------------------------------------------------------------------------
254 // wxApp creation
255 // ----------------------------------------------------------------------------
256
257 wxApp::wxApp()
258 {
259 m_topWindow = NULL;
260 wxTheApp = this;
261
262 m_isIdle = true;
263 #if WXWIN_COMPATIBILITY_2_2
264 m_wantDebugOutput = TRUE;
265 #endif
266
267 argc = 0;
268 argv = NULL;
269 m_cocoaApp = NULL;
270 }
271
272 void wxApp::CocoaInstallIdleHandler()
273 {
274 wxLogDebug("wxApp::CocoaInstallIdleHandler");
275 m_isIdle = false;
276 // Call doIdle for EVERYTHING dammit
277 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
278 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
279 }
280
281 bool wxApp::OnInitGui()
282 {
283 if(!wxAppBase::OnInitGui())
284 return FALSE;
285
286 // Create the app using the sharedApplication method
287 m_cocoaApp = [NSApplication sharedApplication];
288 wxDC::CocoaInitializeTextSystem();
289 // [ m_cocoaApp setDelegate:m_cocoaApp ];
290 #if 0
291 wxLogDebug("Just for kicks");
292 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
293 wxLogDebug("okay.. done now");
294 #endif
295 return TRUE;
296 }
297
298 bool wxApp::OnInit()
299 {
300 if(!wxAppBase::OnInit())
301 return FALSE;
302
303 return TRUE;
304 }
305
306 bool wxApp::Initialized()
307 {
308 if (GetTopWindow())
309 return TRUE;
310 else
311 return FALSE;
312 }
313
314 int wxApp::MainLoop()
315 {
316 [m_cocoaApp run];
317 return 0;
318 }
319
320 // Returns TRUE if more time is needed.
321 bool wxApp::ProcessIdle()
322 {
323 wxIdleEvent event;
324 event.SetEventObject(this);
325 ProcessEvent(event);
326
327 return event.MoreRequested();
328 }
329
330 void wxApp::ExitMainLoop()
331 {
332 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
333 // CocoaInstallRequestedIdleHandler();
334 // if(m_isIdle)
335 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
336 // actually.. we WANT the idle event
337 // or not
338 #if 0
339 if(!m_isIdle)
340 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
341 #endif
342 [m_cocoaApp terminate: m_cocoaApp];
343 }
344
345 // Is a message/event pending?
346 bool wxApp::Pending()
347 {
348 return 0;
349 }
350
351 // Dispatch a message.
352 void wxApp::Dispatch()
353 {
354 }
355
356 void wxApp::OnIdle(wxIdleEvent& event)
357 {
358 wxLogDebug("wxApp::OnIdle");
359 static bool s_inOnIdle = FALSE;
360
361 // Avoid recursion (via ProcessEvent default case)
362 if ( s_inOnIdle )
363 return;
364 s_inOnIdle = TRUE;
365
366
367 DeletePendingObjects();
368
369 // flush the logged messages if any
370 wxLog *pLog = wxLog::GetActiveTarget();
371 if ( pLog != NULL && pLog->HasPendingMessages() )
372 pLog->Flush();
373
374 // Send OnIdle events to all windows
375 bool needMore = SendIdleEvents();
376
377 if (needMore)
378 event.RequestMore(TRUE);
379
380 s_inOnIdle = FALSE;
381 }
382
383 // Send idle event to all top-level windows
384 bool wxApp::SendIdleEvents()
385 {
386 bool needMore = FALSE;
387 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
388 while (node)
389 {
390 wxWindow* win = node->GetData();
391 if (SendIdleEvents(win))
392 needMore = TRUE;
393
394 node = node->GetNext();
395 }
396 return needMore;
397 }
398
399 // Send idle event to window and all subwindows
400 bool wxApp::SendIdleEvents(wxWindow* win)
401 {
402 // wxLogDebug("SendIdleEvents win=%p",win);
403 bool needMore = FALSE;
404
405 wxIdleEvent event;
406 event.SetEventObject(win);
407 win->ProcessEvent(event);
408
409 if (event.MoreRequested())
410 needMore = TRUE;
411
412 wxWindowList::Node* node = win->GetChildren().GetFirst();
413 while (node)
414 {
415 // wxLogDebug("child=%p",node->Data());
416 wxWindow* win = node->GetData();
417 if (SendIdleEvents(win))
418 needMore = TRUE;
419
420 node = node->GetNext();
421 }
422 return needMore;
423 }
424
425 // Yield to other processes
426
427 bool wxApp::Yield(bool onlyIfNeeded)
428 {
429 // MT-FIXME
430 static bool s_inYield = false;
431
432 #if wxUSE_LOG
433 // disable log flushing from here because a call to wxYield() shouldn't
434 // normally result in message boxes popping up &c
435 wxLog::Suspend();
436 #endif // wxUSE_LOG
437
438 if (s_inYield)
439 {
440 if ( !onlyIfNeeded )
441 {
442 wxFAIL_MSG( wxT("wxYield called recursively" ) );
443 }
444
445 return false;
446 }
447
448 s_inYield = true;
449
450 wxLogDebug("WARNING: SUPPOSED to have yielded!");
451 // FIXME: Do something!
452
453 #if wxUSE_LOG
454 // let the logs be flashed again
455 wxLog::Resume();
456 #endif // wxUSE_LOG
457
458 s_inYield = false;
459
460 return true;
461 }
462