1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Elliott
8 // Copyright: (c) David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
25 #include "wx/dialog.h"
31 #include "wx/module.h"
33 #include "wx/cocoa/ObjcPose.h"
35 #if wxUSE_WX_RESOURCES
36 # include "wx/resource.h"
39 #import <AppKit/NSApplication.h>
40 #import <Foundation/NSRunLoop.h>
41 #import <Foundation/NSArray.h>
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
49 @interface wxPoserNSApplication : NSApplication
53 - (void)doIdle: (id)data;
54 - (void)finishLaunching;
55 - (void)sendEvent: (NSEvent*)anEvent;
56 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
57 @end // wxPoserNSApplication
59 @implementation wxPoserNSApplication : NSApplication
61 - (void)doIdle: (id)data
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
69 wxLogDebug("Looping for idle events");
71 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
73 wxLogDebug("Found actual work to do");
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();
83 - (void)finishLaunching
85 wxLogDebug("finishLaunching");
86 bool initsuccess = wxTheApp->OnInit();
90 [super finishLaunching];
93 - (void)sendEvent: (NSEvent*)anEvent
95 wxLogDebug("SendEvent");
96 wxTheApp->CocoaInstallRequestedIdleHandler();
97 [super sendEvent: anEvent];
100 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
102 BOOL ret = wxTheApp->GetExitOnFrameDelete();
103 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
107 @end // wxPoserNSApplication
108 WX_IMPLEMENT_POSER(wxPoserNSApplication);
110 // ============================================================================
112 // ============================================================================
114 //----------------------------------------------------------------------
116 //----------------------------------------------------------------------
118 int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
120 return wxApp::Initialize();
123 int WXDLLEXPORT wxEntryInitGui()
125 return wxTheApp->OnInitGui();
128 void WXDLLEXPORT wxEntryCleanup()
133 int wxEntry( int argc, char *argv[])
135 if (!wxEntryStart(argc, argv)) {
138 wxLogDebug("Creating application");
139 // create the application object or ensure that one already exists
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.") );
148 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
151 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
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)
160 strncpy(theArg, argv[1], 5);
162 if (strcmp(theArg, "-psn_") == 0) {
163 // assume the argument is always the only one and remove it
168 wxTheApp->argc = argc;
169 wxTheApp->argv = argv;
171 wxLogDebug("initializing gui");
172 // GUI-specific initialization, such as creating an app context.
175 // Here frames insert themselves automatically
176 // into wxTopLevelWindows by getting created
181 wxLogDebug("Time to run");
182 retValue = wxTheApp->OnRun();
184 wxWindow *topWindow = wxTheApp->GetTopWindow();
187 // Forcibly delete the window.
188 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
189 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
191 topWindow->Close(TRUE);
196 wxTheApp->SetTopWindow(NULL);
211 wxAppConsole::Exit();
214 // ============================================================================
215 // wxApp implementation
216 // ============================================================================
218 // ----------------------------------------------------------------------------
219 // wxApp Static member initialization
220 // ----------------------------------------------------------------------------
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)
231 // ----------------------------------------------------------------------------
232 // wxApp static functions
233 // ----------------------------------------------------------------------------
235 bool wxApp::Initialize(int argc, wxChar **argv)
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();
243 return wxAppBase::Initialize(argc, argv);
246 void wxApp::CleanUp()
248 wxDC::CocoaShutdownTextSystem();
250 wxAppBase::CleanUp();
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
263 #if WXWIN_COMPATIBILITY_2_2
264 m_wantDebugOutput = TRUE;
272 void wxApp::CocoaInstallIdleHandler()
274 wxLogDebug("wxApp::CocoaInstallIdleHandler");
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] ];
281 bool wxApp::OnInitGui()
283 if(!wxAppBase::OnInitGui())
286 // Create the app using the sharedApplication method
287 m_cocoaApp = [NSApplication sharedApplication];
288 wxDC::CocoaInitializeTextSystem();
289 // [ m_cocoaApp setDelegate:m_cocoaApp ];
291 wxLogDebug("Just for kicks");
292 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
293 wxLogDebug("okay.. done now");
300 if(!wxAppBase::OnInit())
306 bool wxApp::Initialized()
314 int wxApp::MainLoop()
320 // Returns TRUE if more time is needed.
321 bool wxApp::ProcessIdle()
324 event.SetEventObject(this);
327 return event.MoreRequested();
330 void wxApp::ExitMainLoop()
332 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
333 // CocoaInstallRequestedIdleHandler();
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
340 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
342 [m_cocoaApp terminate: m_cocoaApp];
345 // Is a message/event pending?
346 bool wxApp::Pending()
351 // Dispatch a message.
352 void wxApp::Dispatch()
356 void wxApp::OnIdle(wxIdleEvent& event)
358 wxLogDebug("wxApp::OnIdle");
359 static bool s_inOnIdle = FALSE;
361 // Avoid recursion (via ProcessEvent default case)
367 DeletePendingObjects();
369 // flush the logged messages if any
370 wxLog *pLog = wxLog::GetActiveTarget();
371 if ( pLog != NULL && pLog->HasPendingMessages() )
374 // Send OnIdle events to all windows
375 bool needMore = SendIdleEvents();
378 event.RequestMore(TRUE);
383 // Send idle event to all top-level windows
384 bool wxApp::SendIdleEvents()
386 bool needMore = FALSE;
387 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
390 wxWindow* win = node->GetData();
391 if (SendIdleEvents(win))
394 node = node->GetNext();
399 // Send idle event to window and all subwindows
400 bool wxApp::SendIdleEvents(wxWindow* win)
402 // wxLogDebug("SendIdleEvents win=%p",win);
403 bool needMore = FALSE;
406 event.SetEventObject(win);
407 win->ProcessEvent(event);
409 if (event.MoreRequested())
412 wxWindowList::Node* node = win->GetChildren().GetFirst();
415 // wxLogDebug("child=%p",node->Data());
416 wxWindow* win = node->GetData();
417 if (SendIdleEvents(win))
420 node = node->GetNext();
425 // Yield to other processes
427 bool wxApp::Yield(bool onlyIfNeeded)
430 static bool s_inYield = false;
433 // disable log flushing from here because a call to wxYield() shouldn't
434 // normally result in message boxes popping up &c
442 wxFAIL_MSG( wxT("wxYield called recursively" ) );
450 wxLogDebug("WARNING: SUPPOSED to have yielded!");
451 // FIXME: Do something!
454 // let the logs be flashed again