1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Elliott
8 // Copyright: (c) David Elliott
9 // Licence: wxWindows licence
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)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)sendEvent: (NSEvent*)anEvent
85 wxLogDebug("SendEvent");
86 wxTheApp->CocoaInstallRequestedIdleHandler();
87 [super sendEvent: anEvent];
90 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
92 BOOL ret = wxTheApp->GetExitOnFrameDelete();
93 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
97 @end // wxPoserNSApplication
98 WX_IMPLEMENT_POSER(wxPoserNSApplication);
100 class wxAutoNSAutoreleasePool
103 wxAutoNSAutoreleasePool()
105 m_pool = [[NSAutoreleasePool alloc] init];
107 ~wxAutoNSAutoreleasePool()
112 NSAutoreleasePool *m_pool;
115 // ============================================================================
117 // ============================================================================
123 wxAppConsole::Exit();
126 // ============================================================================
127 // wxApp implementation
128 // ============================================================================
130 // ----------------------------------------------------------------------------
131 // wxApp Static member initialization
132 // ----------------------------------------------------------------------------
134 #if !USE_SHARED_LIBRARY
135 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
136 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
137 EVT_IDLE(wxApp::OnIdle)
138 // EVT_END_SESSION(wxApp::OnEndSession)
139 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
143 // ----------------------------------------------------------------------------
144 // wxApp initialization/cleanup
145 // ----------------------------------------------------------------------------
147 bool wxApp::Initialize(int& argc, wxChar **argv)
149 wxAutoNSAutoreleasePool pool;
150 // Mac OS X passes a process serial number command line argument when
151 // the application is launched from the Finder. This argument must be
152 // removed from the command line arguments before being handled by the
153 // application (otherwise applications would need to handle it)
156 static const wxChar *ARG_PSN = _T("-psn_");
157 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
159 // remove this argument
160 memmove(argv, argv + 1, argc--);
164 // Posing must be completed before any instances of the Objective-C
165 // classes being posed as are created.
166 wxPoseAsInitializer::InitializePosers();
168 return wxAppBase::Initialize(argc, argv);
171 void wxApp::CleanUp()
173 wxDC::CocoaShutdownTextSystem();
175 wxAppBase::CleanUp();
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
188 #if WXWIN_COMPATIBILITY_2_2
189 m_wantDebugOutput = TRUE;
197 void wxApp::CocoaInstallIdleHandler()
199 wxLogDebug("wxApp::CocoaInstallIdleHandler");
201 // Call doIdle for EVERYTHING dammit
202 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
203 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
206 bool wxApp::OnInitGui()
208 wxAutoNSAutoreleasePool pool;
209 if(!wxAppBase::OnInitGui())
212 // Create the app using the sharedApplication method
213 m_cocoaApp = [NSApplication sharedApplication];
214 wxDC::CocoaInitializeTextSystem();
215 // [ m_cocoaApp setDelegate:m_cocoaApp ];
217 wxLogDebug("Just for kicks");
218 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
219 wxLogDebug("okay.. done now");
224 bool wxApp::CallOnInit()
226 wxAutoNSAutoreleasePool pool;
232 if(!wxAppBase::OnInit())
238 bool wxApp::Initialized()
246 int wxApp::MainLoop()
252 // Returns TRUE if more time is needed.
253 bool wxApp::ProcessIdle()
256 event.SetEventObject(this);
259 wxUpdateUIEvent::ResetUpdateTime();
261 return event.MoreRequested();
264 void wxApp::ExitMainLoop()
266 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
267 // CocoaInstallRequestedIdleHandler();
269 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
270 // actually.. we WANT the idle event
274 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
276 [m_cocoaApp terminate: m_cocoaApp];
279 // Is a message/event pending?
280 bool wxApp::Pending()
285 // Dispatch a message.
286 void wxApp::Dispatch()
290 void wxApp::OnIdle(wxIdleEvent& event)
292 wxLogDebug("wxApp::OnIdle");
293 static bool s_inOnIdle = FALSE;
295 // Avoid recursion (via ProcessEvent default case)
301 DeletePendingObjects();
303 // flush the logged messages if any
304 wxLog *pLog = wxLog::GetActiveTarget();
305 if ( pLog != NULL && pLog->HasPendingMessages() )
308 // Send OnIdle events to all windows
309 bool needMore = SendIdleEvents();
312 event.RequestMore(TRUE);
317 // Send idle event to all top-level windows
318 bool wxApp::SendIdleEvents()
320 bool needMore = FALSE;
321 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
324 wxWindow* win = node->GetData();
325 if (SendIdleEvents(win))
328 node = node->GetNext();
333 // Send idle event to window and all subwindows
334 bool wxApp::SendIdleEvents(wxWindow* win)
336 // wxLogDebug("SendIdleEvents win=%p",win);
337 bool needMore = FALSE;
340 event.SetEventObject(win);
341 win->ProcessEvent(event);
343 if (event.MoreRequested())
346 wxWindowList::Node* node = win->GetChildren().GetFirst();
349 // wxLogDebug("child=%p",node->Data());
350 wxWindow* win = node->GetData();
351 if (SendIdleEvents(win))
354 node = node->GetNext();
359 // Yield to other processes
361 bool wxApp::Yield(bool onlyIfNeeded)
364 static bool s_inYield = false;
367 // disable log flushing from here because a call to wxYield() shouldn't
368 // normally result in message boxes popping up &c
376 wxFAIL_MSG( wxT("wxYield called recursively" ) );
384 wxLogDebug("WARNING: SUPPOSED to have yielded!");
385 // FIXME: Do something!
388 // let the logs be flashed again