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");
67 NSRunLoop *rl = [NSRunLoop currentRunLoop];
68 // runMode: beforeDate returns YES if something was done
69 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
71 wxLogDebug("Looping for idle events");
73 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
75 wxLogDebug("Found actual work to do");
80 wxLogDebug("Idle processing complete, requesting next idle event");
81 // Add ourself back into the run loop (on next event) if necessary
82 wxTheApp->CocoaRequestIdle();
85 - (void)sendEvent: (NSEvent*)anEvent
87 wxLogDebug("SendEvent");
88 wxTheApp->CocoaInstallRequestedIdleHandler();
89 [super sendEvent: anEvent];
92 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
94 BOOL ret = wxTheApp->GetExitOnFrameDelete();
95 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
99 @end // wxPoserNSApplication
100 WX_IMPLEMENT_POSER(wxPoserNSApplication);
102 // ============================================================================
104 // ============================================================================
110 wxAppConsole::Exit();
113 // ============================================================================
114 // wxApp implementation
115 // ============================================================================
117 // ----------------------------------------------------------------------------
118 // wxApp Static member initialization
119 // ----------------------------------------------------------------------------
121 #if !USE_SHARED_LIBRARY
122 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
123 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
124 EVT_IDLE(wxAppBase::OnIdle)
125 // EVT_END_SESSION(wxApp::OnEndSession)
126 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
130 // ----------------------------------------------------------------------------
131 // wxApp initialization/cleanup
132 // ----------------------------------------------------------------------------
134 bool wxApp::Initialize(int& argc, wxChar **argv)
136 wxAutoNSAutoreleasePool pool;
137 m_cocoaMainThread = [NSThread currentThread];
138 // Mac OS X passes a process serial number command line argument when
139 // the application is launched from the Finder. This argument must be
140 // removed from the command line arguments before being handled by the
141 // application (otherwise applications would need to handle it)
144 static const wxChar *ARG_PSN = _T("-psn_");
145 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
147 // remove this argument
148 memmove(argv, argv + 1, argc--);
152 // Posing must be completed before any instances of the Objective-C
153 // classes being posed as are created.
154 wxPoseAsInitializer::InitializePosers();
156 return wxAppBase::Initialize(argc, argv);
159 void wxApp::CleanUp()
161 wxDC::CocoaShutdownTextSystem();
163 wxAppBase::CleanUp();
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
175 #if WXWIN_COMPATIBILITY_2_2
176 m_wantDebugOutput = TRUE;
184 void wxApp::CocoaInstallIdleHandler()
186 // If we're not the main thread, don't install the idle handler
187 if(m_cocoaMainThread != [NSThread currentThread])
189 wxLogDebug("Attempt to install idle handler from secondary thread");
192 // If we're supposed to be stopping, don't add more idle events
193 if(![m_cocoaApp isRunning])
195 wxLogDebug("wxApp::CocoaInstallIdleHandler");
197 // Call doIdle for EVERYTHING dammit
198 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
199 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
202 bool wxApp::OnInitGui()
204 wxAutoNSAutoreleasePool pool;
205 if(!wxAppBase::OnInitGui())
208 // Create the app using the sharedApplication method
209 m_cocoaApp = [NSApplication sharedApplication];
210 wxDC::CocoaInitializeTextSystem();
211 // [ m_cocoaApp setDelegate:m_cocoaApp ];
213 wxLogDebug("Just for kicks");
214 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
215 wxLogDebug("okay.. done now");
220 bool wxApp::CallOnInit()
222 // wxAutoNSAutoreleasePool pool;
228 if(!wxAppBase::OnInit())
234 bool wxApp::Initialized()
242 int wxApp::MainLoop()
248 void wxApp::ExitMainLoop()
250 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
251 // CocoaInstallRequestedIdleHandler();
253 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
254 // actually.. we WANT the idle event
258 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
260 [m_cocoaApp stop: m_cocoaApp];
263 // Is a message/event pending?
264 bool wxApp::Pending()
269 // Dispatch a message.
270 void wxApp::Dispatch()
274 // Yield to other processes
276 bool wxApp::Yield(bool onlyIfNeeded)
279 static bool s_inYield = false;
282 // disable log flushing from here because a call to wxYield() shouldn't
283 // normally result in message boxes popping up &c
291 wxFAIL_MSG( wxT("wxYield called recursively" ) );
299 wxLogDebug("WARNING: SUPPOSED to have yielded!");
300 // FIXME: Do something!
303 // let the logs be flashed again