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>
42 #import <Foundation/NSAutoreleasePool.h>
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
50 @interface wxPoserNSApplication : NSApplication
54 - (void)doIdle: (id)data;
55 - (void)finishLaunching;
56 - (void)sendEvent: (NSEvent*)anEvent;
57 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
58 @end // wxPoserNSApplication
60 @implementation wxPoserNSApplication : NSApplication
62 - (void)doIdle: (id)data
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
70 wxLogDebug("Looping for idle events");
72 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
74 wxLogDebug("Found actual work to do");
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();
84 - (void)finishLaunching
86 wxLogDebug("finishLaunching");
87 bool initsuccess = wxTheApp->OnInit();
91 [super finishLaunching];
94 - (void)sendEvent: (NSEvent*)anEvent
96 wxLogDebug("SendEvent");
97 wxTheApp->CocoaInstallRequestedIdleHandler();
98 [super sendEvent: anEvent];
101 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
103 BOOL ret = wxTheApp->GetExitOnFrameDelete();
104 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
108 @end // wxPoserNSApplication
109 WX_IMPLEMENT_POSER(wxPoserNSApplication);
111 class wxAutoNSAutoreleasePool
114 wxAutoNSAutoreleasePool()
116 m_pool = [[NSAutoreleasePool alloc] init];
118 ~wxAutoNSAutoreleasePool()
123 NSAutoreleasePool *m_pool;
126 // ============================================================================
128 // ============================================================================
134 wxAppConsole::Exit();
137 // ============================================================================
138 // wxApp implementation
139 // ============================================================================
141 // ----------------------------------------------------------------------------
142 // wxApp Static member initialization
143 // ----------------------------------------------------------------------------
145 #if !USE_SHARED_LIBRARY
146 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
147 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
148 EVT_IDLE(wxApp::OnIdle)
149 // EVT_END_SESSION(wxApp::OnEndSession)
150 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
154 // ----------------------------------------------------------------------------
155 // wxApp initialization/cleanup
156 // ----------------------------------------------------------------------------
158 bool wxApp::Initialize(int& argc, wxChar **argv)
160 wxAutoNSAutoreleasePool pool;
161 // Mac OS X passes a process serial number command line argument when
162 // the application is launched from the Finder. This argument must be
163 // removed from the command line arguments before being handled by the
164 // application (otherwise applications would need to handle it)
167 static const wxChar *ARG_PSN = _T("-psn_");
168 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
170 // remove this argument
171 memmove(argv, argv + 1, argc--);
175 // Posing must be completed before any instances of the Objective-C
176 // classes being posed as are created.
177 wxPoseAsInitializer::InitializePosers();
179 return wxAppBase::Initialize(argc, argv);
182 void wxApp::CleanUp()
184 wxDC::CocoaShutdownTextSystem();
186 wxAppBase::CleanUp();
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
199 #if WXWIN_COMPATIBILITY_2_2
200 m_wantDebugOutput = TRUE;
208 void wxApp::CocoaInstallIdleHandler()
210 wxLogDebug("wxApp::CocoaInstallIdleHandler");
212 // Call doIdle for EVERYTHING dammit
213 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
214 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
217 bool wxApp::OnInitGui()
219 wxAutoNSAutoreleasePool pool;
220 if(!wxAppBase::OnInitGui())
223 // Create the app using the sharedApplication method
224 m_cocoaApp = [NSApplication sharedApplication];
225 wxDC::CocoaInitializeTextSystem();
226 // [ m_cocoaApp setDelegate:m_cocoaApp ];
228 wxLogDebug("Just for kicks");
229 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
230 wxLogDebug("okay.. done now");
235 bool wxApp::CallOnInit()
237 wxAutoNSAutoreleasePool pool;
243 if(!wxAppBase::OnInit())
249 bool wxApp::Initialized()
257 int wxApp::MainLoop()
263 // Returns TRUE if more time is needed.
264 bool wxApp::ProcessIdle()
267 event.SetEventObject(this);
270 return event.MoreRequested();
273 void wxApp::ExitMainLoop()
275 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
276 // CocoaInstallRequestedIdleHandler();
278 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
279 // actually.. we WANT the idle event
283 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
285 [m_cocoaApp terminate: m_cocoaApp];
288 // Is a message/event pending?
289 bool wxApp::Pending()
294 // Dispatch a message.
295 void wxApp::Dispatch()
299 void wxApp::OnIdle(wxIdleEvent& event)
301 wxLogDebug("wxApp::OnIdle");
302 static bool s_inOnIdle = FALSE;
304 // Avoid recursion (via ProcessEvent default case)
310 DeletePendingObjects();
312 // flush the logged messages if any
313 wxLog *pLog = wxLog::GetActiveTarget();
314 if ( pLog != NULL && pLog->HasPendingMessages() )
317 // Send OnIdle events to all windows
318 bool needMore = SendIdleEvents();
321 event.RequestMore(TRUE);
326 // Send idle event to all top-level windows
327 bool wxApp::SendIdleEvents()
329 bool needMore = FALSE;
330 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
333 wxWindow* win = node->GetData();
334 if (SendIdleEvents(win))
337 node = node->GetNext();
342 // Send idle event to window and all subwindows
343 bool wxApp::SendIdleEvents(wxWindow* win)
345 // wxLogDebug("SendIdleEvents win=%p",win);
346 bool needMore = FALSE;
349 event.SetEventObject(win);
350 win->ProcessEvent(event);
352 if (event.MoreRequested())
355 wxWindowList::Node* node = win->GetChildren().GetFirst();
358 // wxLogDebug("child=%p",node->Data());
359 wxWindow* win = node->GetData();
360 if (SendIdleEvents(win))
363 node = node->GetNext();
368 // Yield to other processes
370 bool wxApp::Yield(bool onlyIfNeeded)
373 static bool s_inYield = false;
376 // disable log flushing from here because a call to wxYield() shouldn't
377 // normally result in message boxes popping up &c
385 wxFAIL_MSG( wxT("wxYield called recursively" ) );
393 wxLogDebug("WARNING: SUPPOSED to have yielded!");
394 // FIXME: Do something!
397 // let the logs be flashed again