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>
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
51 @interface wxPoserNSApplication : NSApplication
55 - (void)doIdle: (id)data;
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)sendEvent: (NSEvent*)anEvent
86 wxLogDebug("SendEvent");
87 wxTheApp->CocoaInstallRequestedIdleHandler();
88 [super sendEvent: anEvent];
91 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
93 BOOL ret = wxTheApp->GetExitOnFrameDelete();
94 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
98 @end // wxPoserNSApplication
99 WX_IMPLEMENT_POSER(wxPoserNSApplication);
101 // ============================================================================
103 // ============================================================================
109 wxAppConsole::Exit();
112 // ============================================================================
113 // wxApp implementation
114 // ============================================================================
116 // ----------------------------------------------------------------------------
117 // wxApp Static member initialization
118 // ----------------------------------------------------------------------------
120 #if !USE_SHARED_LIBRARY
121 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
122 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
123 EVT_IDLE(wxAppBase::OnIdle)
124 // EVT_END_SESSION(wxApp::OnEndSession)
125 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
129 // ----------------------------------------------------------------------------
130 // wxApp initialization/cleanup
131 // ----------------------------------------------------------------------------
133 bool wxApp::Initialize(int& argc, wxChar **argv)
135 wxAutoNSAutoreleasePool pool;
136 // Mac OS X passes a process serial number command line argument when
137 // the application is launched from the Finder. This argument must be
138 // removed from the command line arguments before being handled by the
139 // application (otherwise applications would need to handle it)
142 static const wxChar *ARG_PSN = _T("-psn_");
143 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
145 // remove this argument
146 memmove(argv, argv + 1, argc--);
150 // Posing must be completed before any instances of the Objective-C
151 // classes being posed as are created.
152 wxPoseAsInitializer::InitializePosers();
154 return wxAppBase::Initialize(argc, argv);
157 void wxApp::CleanUp()
159 wxDC::CocoaShutdownTextSystem();
161 wxAppBase::CleanUp();
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
174 #if WXWIN_COMPATIBILITY_2_2
175 m_wantDebugOutput = TRUE;
183 void wxApp::CocoaInstallIdleHandler()
185 // If we're supposed to be stopping, don't add more idle events
186 if(![m_cocoaApp isRunning])
188 wxLogDebug("wxApp::CocoaInstallIdleHandler");
190 // Call doIdle for EVERYTHING dammit
191 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
192 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
195 bool wxApp::OnInitGui()
197 wxAutoNSAutoreleasePool pool;
198 if(!wxAppBase::OnInitGui())
201 // Create the app using the sharedApplication method
202 m_cocoaApp = [NSApplication sharedApplication];
203 wxDC::CocoaInitializeTextSystem();
204 // [ m_cocoaApp setDelegate:m_cocoaApp ];
206 wxLogDebug("Just for kicks");
207 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
208 wxLogDebug("okay.. done now");
213 bool wxApp::CallOnInit()
215 // wxAutoNSAutoreleasePool pool;
221 if(!wxAppBase::OnInit())
227 bool wxApp::Initialized()
235 int wxApp::MainLoop()
241 void wxApp::ExitMainLoop()
243 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
244 // CocoaInstallRequestedIdleHandler();
246 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
247 // actually.. we WANT the idle event
251 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
253 [m_cocoaApp stop: m_cocoaApp];
256 // Is a message/event pending?
257 bool wxApp::Pending()
262 // Dispatch a message.
263 void wxApp::Dispatch()
267 // Yield to other processes
269 bool wxApp::Yield(bool onlyIfNeeded)
272 static bool s_inYield = false;
275 // disable log flushing from here because a call to wxYield() shouldn't
276 // normally result in message boxes popping up &c
284 wxFAIL_MSG( wxT("wxYield called recursively" ) );
292 wxLogDebug("WARNING: SUPPOSED to have yielded!");
293 // FIXME: Do something!
296 // let the logs be flashed again