]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/app.mm
implemented multiple selection
[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 wxApp *wxTheApp = NULL;
48 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
49
50 @interface wxPoserNSApplication : NSApplication
51 {
52 }
53
54 - (void)doIdle: (id)data;
55 - (void)finishLaunching;
56 - (void)sendEvent: (NSEvent*)anEvent;
57 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
58 @end // wxPoserNSApplication
59
60 @implementation wxPoserNSApplication : NSApplication
61
62 - (void)doIdle: (id)data
63 {
64 wxASSERT(wxTheApp);
65 wxLogDebug("doIdle called");
66 NSRunLoop *rl = [NSRunLoop currentRunLoop];
67 // runMode: beforeDate returns YES if something was done
68 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
69 {
70 wxLogDebug("Looping for idle events");
71 #if 1
72 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
73 {
74 wxLogDebug("Found actual work to do");
75 break;
76 }
77 #endif
78 }
79 wxLogDebug("Idle processing complete, requesting next idle event");
80 // Add ourself back into the run loop (on next event) if necessary
81 wxTheApp->CocoaRequestIdle();
82 }
83
84 - (void)finishLaunching
85 {
86 wxLogDebug("finishLaunching");
87 bool initsuccess = wxTheApp->OnInit();
88 if(!initsuccess)
89 [super stop: NULL];
90
91 [super finishLaunching];
92 }
93
94 - (void)sendEvent: (NSEvent*)anEvent
95 {
96 wxLogDebug("SendEvent");
97 wxTheApp->CocoaInstallRequestedIdleHandler();
98 [super sendEvent: anEvent];
99 }
100
101 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
102 {
103 BOOL ret = wxTheApp->GetExitOnFrameDelete();
104 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
105 return ret;
106 }
107
108 @end // wxPoserNSApplication
109 WX_IMPLEMENT_POSER(wxPoserNSApplication);
110
111 // ============================================================================
112 // functions
113 // ============================================================================
114
115 //----------------------------------------------------------------------
116 // wxEntry
117 //----------------------------------------------------------------------
118
119 int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
120 {
121 return wxApp::Initialize();
122 }
123
124 int WXDLLEXPORT wxEntryInitGui()
125 {
126 return wxTheApp->OnInitGui();
127 }
128
129 void WXDLLEXPORT wxEntryCleanup()
130 {
131 wxApp::CleanUp();
132 }
133
134 int wxEntry( int argc, char *argv[])
135 {
136 if (!wxEntryStart(argc, argv)) {
137 return 0;
138 }
139 wxLogDebug("Creating application");
140 // create the application object or ensure that one already exists
141 if (!wxTheApp)
142 {
143 // The app may have declared a global application object, but we recommend
144 // the IMPLEMENT_APP macro is used instead, which sets an initializer
145 // function for delayed, dynamic app object construction.
146 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
147 wxT("No initializer - use IMPLEMENT_APP macro.") );
148
149 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
150 }
151
152 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
153
154 // Mac OS X passes a process serial number command line argument when
155 // the application is launched from the Finder. This argument must be
156 // removed from the command line arguments before being handled by the
157 // application (otherwise applications would need to handle it)
158
159 if (argc > 1) {
160 char theArg[6] = "";
161 strncpy(theArg, argv[1], 5);
162
163 if (strcmp(theArg, "-psn_") == 0) {
164 // assume the argument is always the only one and remove it
165 --argc;
166 }
167 }
168
169 wxTheApp->argc = argc;
170 wxTheApp->argv = argv;
171
172 wxLogDebug("initializing gui");
173 // GUI-specific initialization, such as creating an app context.
174 wxEntryInitGui();
175
176 // Here frames insert themselves automatically
177 // into wxTopLevelWindows by getting created
178 // in OnInit().
179
180 int retValue = 0;
181
182 wxLogDebug("Time to run");
183 retValue = wxTheApp->OnRun();
184
185 wxWindow *topWindow = wxTheApp->GetTopWindow();
186 if ( topWindow )
187 {
188 // Forcibly delete the window.
189 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
190 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
191 {
192 topWindow->Close(TRUE);
193 }
194 else
195 {
196 delete topWindow;
197 wxTheApp->SetTopWindow(NULL);
198 }
199 }
200
201 wxTheApp->OnExit();
202
203 wxEntryCleanup();
204
205 return retValue;
206 }
207
208 // ----------------------------------------------------------------------------
209 // other functions
210 // ----------------------------------------------------------------------------
211 void wxWakeUpIdle()
212 {
213 wxTheApp->CocoaRequestIdle();
214 }
215
216 void wxExit()
217 {
218 wxLogError(_("Fatal error: exiting"));
219
220 wxApp::CleanUp();
221 exit(1);
222 }
223
224 // ============================================================================
225 // wxApp implementation
226 // ============================================================================
227
228 // ----------------------------------------------------------------------------
229 // wxApp Static member initialization
230 // ----------------------------------------------------------------------------
231 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
232
233 #if !USE_SHARED_LIBRARY
234 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
235 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
236 EVT_IDLE(wxApp::OnIdle)
237 // EVT_END_SESSION(wxApp::OnEndSession)
238 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
239 END_EVENT_TABLE()
240 #endif
241
242 // ----------------------------------------------------------------------------
243 // wxApp static functions
244 // ----------------------------------------------------------------------------
245 /*static*/ bool wxApp::Initialize()
246 {
247 wxPoseAsInitializer::InitializePosers();
248 wxClassInfo::InitializeClasses();
249
250 #if wxUSE_THREADS
251 wxPendingEventsLocker = new wxCriticalSection;
252 #endif
253
254 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
255 wxTheColourDatabase->Initialize();
256
257 wxInitializeStockLists();
258 wxInitializeStockObjects();
259
260 #if wxUSE_WX_RESOURCES
261 wxInitializeResourceSystem();
262 #endif
263
264 wxBitmap::InitStandardHandlers();
265
266 wxModule::RegisterModules();
267 if (!wxModule::InitializeModules()) {
268 return FALSE;
269 }
270 return TRUE;
271 }
272
273 /*static*/ void wxApp::CleanUp()
274 {
275 wxModule::CleanUpModules();
276
277 #if wxUSE_WX_RESOURCES
278 wxCleanUpResourceSystem();
279 #endif
280
281 wxDeleteStockObjects() ;
282
283 // Destroy all GDI lists, etc.
284 wxDeleteStockLists();
285
286 delete wxTheColourDatabase;
287 wxTheColourDatabase = NULL;
288
289 wxBitmap::CleanUpHandlers();
290
291 delete wxPendingEvents;
292
293 #if wxUSE_THREADS
294 delete wxPendingEventsLocker;
295 // If we don't do the following, we get an apparent memory leak.
296 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
297 #endif
298
299 wxClassInfo::CleanUpClasses();
300
301 delete wxTheApp;
302 wxTheApp = NULL;
303
304 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
305 // At this point we want to check if there are any memory
306 // blocks that aren't part of the wxDebugContext itself,
307 // as a special case. Then when dumping we need to ignore
308 // wxDebugContext, too.
309 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
310 {
311 wxLogDebug(wxT("There were memory leaks."));
312 wxDebugContext::Dump();
313 wxDebugContext::PrintStatistics();
314 }
315 // wxDebugContext::SetStream(NULL, NULL);
316 #endif
317
318 wxDC::CocoaShutdownTextSystem();
319 #if wxUSE_LOG
320 // do it as the very last thing because everything else can log messages
321 delete wxLog::SetActiveTarget(NULL);
322 #endif // wxUSE_LOG
323 }
324
325 // ----------------------------------------------------------------------------
326 // wxApp creation
327 // ----------------------------------------------------------------------------
328
329 wxApp::wxApp()
330 {
331 m_topWindow = NULL;
332 wxTheApp = this;
333
334 m_isIdle = true;
335 #if WXWIN_COMPATIBILITY_2_2
336 m_wantDebugOutput = TRUE;
337 #endif
338
339 argc = 0;
340 argv = NULL;
341 m_cocoaApp = NULL;
342 }
343
344 void wxApp::CocoaInstallIdleHandler()
345 {
346 wxLogDebug("wxApp::CocoaInstallIdleHandler");
347 m_isIdle = false;
348 // Call doIdle for EVERYTHING dammit
349 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
350 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
351 }
352
353 bool wxApp::OnInitGui()
354 {
355 if(!wxAppBase::OnInitGui())
356 return FALSE;
357
358 // Create the app using the sharedApplication method
359 m_cocoaApp = [NSApplication sharedApplication];
360 wxDC::CocoaInitializeTextSystem();
361 // [ m_cocoaApp setDelegate:m_cocoaApp ];
362 #if 0
363 wxLogDebug("Just for kicks");
364 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
365 wxLogDebug("okay.. done now");
366 #endif
367 return TRUE;
368 }
369
370 bool wxApp::OnInit()
371 {
372 if(!wxAppBase::OnInit())
373 return FALSE;
374
375 return TRUE;
376 }
377
378 bool wxApp::Initialized()
379 {
380 if (GetTopWindow())
381 return TRUE;
382 else
383 return FALSE;
384 }
385
386 int wxApp::MainLoop()
387 {
388 [m_cocoaApp run];
389 return 0;
390 }
391
392 // Returns TRUE if more time is needed.
393 bool wxApp::ProcessIdle()
394 {
395 wxIdleEvent event;
396 event.SetEventObject(this);
397 ProcessEvent(event);
398
399 return event.MoreRequested();
400 }
401
402 void wxApp::ExitMainLoop()
403 {
404 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
405 // CocoaInstallRequestedIdleHandler();
406 // if(m_isIdle)
407 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
408 // actually.. we WANT the idle event
409 // or not
410 #if 0
411 if(!m_isIdle)
412 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
413 #endif
414 [m_cocoaApp terminate: m_cocoaApp];
415 }
416
417 // Is a message/event pending?
418 bool wxApp::Pending()
419 {
420 return 0;
421 }
422
423 // Dispatch a message.
424 void wxApp::Dispatch()
425 {
426 }
427
428 void wxApp::OnIdle(wxIdleEvent& event)
429 {
430 wxLogDebug("wxApp::OnIdle");
431 static bool s_inOnIdle = FALSE;
432
433 // Avoid recursion (via ProcessEvent default case)
434 if ( s_inOnIdle )
435 return;
436 s_inOnIdle = TRUE;
437
438
439 DeletePendingObjects();
440
441 // flush the logged messages if any
442 wxLog *pLog = wxLog::GetActiveTarget();
443 if ( pLog != NULL && pLog->HasPendingMessages() )
444 pLog->Flush();
445
446 // Send OnIdle events to all windows
447 bool needMore = SendIdleEvents();
448
449 if (needMore)
450 event.RequestMore(TRUE);
451
452 s_inOnIdle = FALSE;
453 }
454
455 // Send idle event to all top-level windows
456 bool wxApp::SendIdleEvents()
457 {
458 bool needMore = FALSE;
459 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
460 while (node)
461 {
462 wxWindow* win = node->GetData();
463 if (SendIdleEvents(win))
464 needMore = TRUE;
465
466 node = node->GetNext();
467 }
468 return needMore;
469 }
470
471 // Send idle event to window and all subwindows
472 bool wxApp::SendIdleEvents(wxWindow* win)
473 {
474 // wxLogDebug("SendIdleEvents win=%p",win);
475 bool needMore = FALSE;
476
477 wxIdleEvent event;
478 event.SetEventObject(win);
479 win->ProcessEvent(event);
480
481 if (event.MoreRequested())
482 needMore = TRUE;
483
484 wxWindowList::Node* node = win->GetChildren().GetFirst();
485 while (node)
486 {
487 // wxLogDebug("child=%p",node->Data());
488 wxWindow* win = node->GetData();
489 if (SendIdleEvents(win))
490 needMore = TRUE;
491
492 node = node->GetNext();
493 }
494 return needMore;
495 }
496
497 // Yield to other processes
498
499 bool wxApp::Yield(bool onlyIfNeeded)
500 {
501 // MT-FIXME
502 static bool s_inYield = false;
503
504 #if wxUSE_LOG
505 // disable log flushing from here because a call to wxYield() shouldn't
506 // normally result in message boxes popping up &c
507 wxLog::Suspend();
508 #endif // wxUSE_LOG
509
510 if (s_inYield)
511 {
512 if ( !onlyIfNeeded )
513 {
514 wxFAIL_MSG( wxT("wxYield called recursively" ) );
515 }
516
517 return false;
518 }
519
520 s_inYield = true;
521
522 wxLogDebug("WARNING: SUPPOSED to have yielded!");
523 // FIXME: Do something!
524
525 #if wxUSE_LOG
526 // let the logs be flashed again
527 wxLog::Resume();
528 #endif // wxUSE_LOG
529
530 s_inYield = false;
531
532 return true;
533 }
534
535 void wxApp::DeletePendingObjects()
536 {
537 wxNode *node = wxPendingDelete.GetFirst();
538 while (node)
539 {
540 wxObject *obj = (wxObject *)node->GetData();
541
542 delete obj;
543
544 if (wxPendingDelete.Find(obj))
545 delete node;
546
547 node = wxPendingDelete.GetFirst();
548 }
549 }
550
551 // platform specifics
552