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"
34 #include "wx/cocoa/autorelease.h"
36 #if wxUSE_WX_RESOURCES
37 # include "wx/resource.h"
40 #import <AppKit/NSApplication.h>
41 #import <Foundation/NSRunLoop.h>
42 #import <Foundation/NSArray.h>
43 #import <Foundation/NSAutoreleasePool.h>
44 #import <Foundation/NSThread.h>
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
52 @interface wxPoserNSApplication : NSApplication
56 - (void)doIdle: (id)data;
57 - (void)sendEvent: (NSEvent*)anEvent;
58 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
59 @end // wxPoserNSApplication
61 @implementation wxPoserNSApplication : NSApplication
63 - (void)doIdle: (id)data
66 wxLogDebug("doIdle called");
68 if(wxTheApp->IsInAssert())
70 wxLogDebug("Idle events ignored durring assertion dialog");
75 NSRunLoop *rl = [NSRunLoop currentRunLoop];
76 // runMode: beforeDate returns YES if something was done
77 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
79 wxLogDebug("Looping for idle events");
81 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
83 wxLogDebug("Found actual work to do");
89 wxLogDebug("Idle processing complete, requesting next idle event");
90 // Add ourself back into the run loop (on next event) if necessary
91 wxTheApp->CocoaRequestIdle();
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 // ============================================================================
113 // ============================================================================
119 wxAppConsole::Exit();
122 // ============================================================================
123 // wxApp implementation
124 // ============================================================================
126 // ----------------------------------------------------------------------------
127 // wxApp Static member initialization
128 // ----------------------------------------------------------------------------
130 #if !USE_SHARED_LIBRARY
131 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
132 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
133 EVT_IDLE(wxAppBase::OnIdle)
134 // EVT_END_SESSION(wxApp::OnEndSession)
135 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
139 // ----------------------------------------------------------------------------
140 // wxApp initialization/cleanup
141 // ----------------------------------------------------------------------------
143 bool wxApp::Initialize(int& argc, wxChar **argv)
145 wxAutoNSAutoreleasePool pool;
146 m_cocoaMainThread = [NSThread currentThread];
147 // Mac OS X passes a process serial number command line argument when
148 // the application is launched from the Finder. This argument must be
149 // removed from the command line arguments before being handled by the
150 // application (otherwise applications would need to handle it)
153 static const wxChar *ARG_PSN = _T("-psn_");
154 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
156 // remove this argument
157 memmove(argv, argv + 1, argc--);
161 // Posing must be completed before any instances of the Objective-C
162 // classes being posed as are created.
163 wxPoseAsInitializer::InitializePosers();
165 return wxAppBase::Initialize(argc, argv);
168 void wxApp::CleanUp()
170 wxDC::CocoaShutdownTextSystem();
172 wxAppBase::CleanUp();
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
184 #if WXWIN_COMPATIBILITY_2_2
185 m_wantDebugOutput = TRUE;
188 m_isInAssert = FALSE;
189 #endif // __WXDEBUG__
197 void wxApp::CocoaInstallIdleHandler()
199 // If we're not the main thread, don't install the idle handler
200 if(m_cocoaMainThread != [NSThread currentThread])
202 wxLogDebug("Attempt to install idle handler from secondary thread");
205 // If we're supposed to be stopping, don't add more idle events
206 if(![m_cocoaApp isRunning])
208 wxLogDebug("wxApp::CocoaInstallIdleHandler");
210 // Call doIdle for EVERYTHING dammit
211 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
212 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
215 bool wxApp::OnInitGui()
217 wxAutoNSAutoreleasePool pool;
218 if(!wxAppBase::OnInitGui())
221 // Create the app using the sharedApplication method
222 m_cocoaApp = [NSApplication sharedApplication];
223 wxDC::CocoaInitializeTextSystem();
224 // [ m_cocoaApp setDelegate:m_cocoaApp ];
226 wxLogDebug("Just for kicks");
227 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
228 wxLogDebug("okay.. done now");
233 bool wxApp::CallOnInit()
235 // wxAutoNSAutoreleasePool pool;
241 if(!wxAppBase::OnInit())
247 bool wxApp::Initialized()
255 int wxApp::MainLoop()
261 void wxApp::ExitMainLoop()
263 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
264 // CocoaInstallRequestedIdleHandler();
266 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
267 // actually.. we WANT the idle event
271 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
273 [m_cocoaApp stop: m_cocoaApp];
276 // Is a message/event pending?
277 bool wxApp::Pending()
282 // Dispatch a message.
283 void wxApp::Dispatch()
287 // Yield to other processes
289 bool wxApp::Yield(bool onlyIfNeeded)
292 static bool s_inYield = false;
295 // disable log flushing from here because a call to wxYield() shouldn't
296 // normally result in message boxes popping up &c
304 wxFAIL_MSG( wxT("wxYield called recursively" ) );
312 wxLogDebug("WARNING: SUPPOSED to have yielded!");
313 // FIXME: Do something!
316 // let the logs be flashed again
326 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
329 wxAppBase::OnAssert(file, line, cond, msg);
330 m_isInAssert = FALSE;
332 #endif // __WXDEBUG__