1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Elliott
8 // Copyright: (c) David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/dialog.h"
23 #include "wx/module.h"
25 #include "wx/cocoa/ObjcPose.h"
26 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/mbarman.h"
29 #if wxUSE_WX_RESOURCES
30 # include "wx/resource.h"
33 #import <AppKit/NSApplication.h>
34 #import <Foundation/NSRunLoop.h>
35 #import <Foundation/NSArray.h>
36 #import <Foundation/NSAutoreleasePool.h>
37 #import <Foundation/NSThread.h>
38 #import <AppKit/NSEvent.h>
40 // ========================================================================
41 // wxPoseAsInitializer
42 // ========================================================================
43 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
45 // ========================================================================
46 // wxPoserNSApplication
47 // ========================================================================
48 @interface wxPoserNSApplication : NSApplication
52 - (void)doIdle: (id)data;
53 - (void)sendEvent: (NSEvent*)anEvent;
54 @end // wxPoserNSApplication
56 WX_IMPLEMENT_POSER(wxPoserNSApplication);
58 @implementation wxPoserNSApplication : NSApplication
60 - (void)doIdle: (id)data
63 wxASSERT(wxMenuBarManager::GetInstance());
64 wxMenuBarManager::GetInstance()->CocoaInternalIdle();
65 wxLogDebug("doIdle called");
67 if(wxTheApp->IsInAssert())
69 wxLogDebug("Idle events ignored durring assertion dialog");
74 NSRunLoop *rl = [NSRunLoop currentRunLoop];
75 // runMode: beforeDate returns YES if something was done
76 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
78 wxLogDebug("Looping for idle events");
80 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
82 wxLogDebug("Found actual work to do");
88 wxLogDebug("Idle processing complete, requesting next idle event");
89 // Add ourself back into the run loop (on next event) if necessary
90 wxTheApp->CocoaRequestIdle();
93 - (void)sendEvent: (NSEvent*)anEvent
95 wxLogDebug("SendEvent");
96 wxTheApp->CocoaInstallRequestedIdleHandler();
97 [super sendEvent: anEvent];
100 @end // wxPoserNSApplication
102 // ========================================================================
103 // wxNSApplicationDelegate
104 // ========================================================================
105 @interface wxNSApplicationDelegate : NSObject
109 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
110 - (void)applicationWillBecomeActive:(NSNotification *)notification;
111 - (void)applicationDidBecomeActive:(NSNotification *)notification;
112 - (void)applicationWillResignActive:(NSNotification *)notification;
113 - (void)applicationDidResignActive:(NSNotification *)notification;
114 @end // interface wxNSApplicationDelegate : NSObject
116 @implementation wxNSApplicationDelegate : NSObject
118 // NOTE: Terminate means that the event loop does NOT return and thus
119 // cleanup code doesn't properly execute. Furthermore, wxWindows has its
120 // own exit on frame delete mechanism.
121 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
126 - (void)applicationWillBecomeActive:(NSNotification *)notification
128 wxTheApp->CocoaDelegate_applicationWillBecomeActive();
131 - (void)applicationDidBecomeActive:(NSNotification *)notification
133 wxTheApp->CocoaDelegate_applicationDidBecomeActive();
136 - (void)applicationWillResignActive:(NSNotification *)notification
138 wxTheApp->CocoaDelegate_applicationWillResignActive();
141 - (void)applicationDidResignActive:(NSNotification *)notification
143 wxTheApp->CocoaDelegate_applicationDidResignActive();
146 @end // implementation wxNSApplicationDelegate : NSObject
148 // ========================================================================
150 // ========================================================================
152 // ----------------------------------------------------------------------------
153 // wxApp Static member initialization
154 // ----------------------------------------------------------------------------
155 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
156 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
157 EVT_IDLE(wxAppBase::OnIdle)
158 // EVT_END_SESSION(wxApp::OnEndSession)
159 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
162 // ----------------------------------------------------------------------------
163 // wxApp initialization/cleanup
164 // ----------------------------------------------------------------------------
165 bool wxApp::Initialize(int& argc, wxChar **argv)
167 wxAutoNSAutoreleasePool pool;
168 m_cocoaMainThread = [NSThread currentThread];
169 // Mac OS X passes a process serial number command line argument when
170 // the application is launched from the Finder. This argument must be
171 // removed from the command line arguments before being handled by the
172 // application (otherwise applications would need to handle it)
175 static const wxChar *ARG_PSN = _T("-psn_");
176 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
178 // remove this argument
179 memmove(argv, argv + 1, argc--);
183 // Posing must be completed before any instances of the Objective-C
184 // classes being posed as are created.
185 wxPoseAsInitializer::InitializePosers();
187 return wxAppBase::Initialize(argc, argv);
190 void wxApp::CleanUp()
192 wxAutoNSAutoreleasePool pool;
194 wxDC::CocoaShutdownTextSystem();
195 wxMenuBarManager::DestroyInstance();
197 [m_cocoaApp setDelegate:nil];
198 [m_cocoaAppDelegate release];
199 m_cocoaAppDelegate = NULL;
201 wxAppBase::CleanUp();
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
212 #if WXWIN_COMPATIBILITY_2_2
213 m_wantDebugOutput = TRUE;
216 m_isInAssert = FALSE;
217 #endif // __WXDEBUG__
222 m_cocoaAppDelegate = NULL;
225 void wxApp::CocoaInstallIdleHandler()
227 // If we're not the main thread, don't install the idle handler
228 if(m_cocoaMainThread != [NSThread currentThread])
230 wxLogDebug("Attempt to install idle handler from secondary thread");
233 // If we're supposed to be stopping, don't add more idle events
234 if(![m_cocoaApp isRunning])
236 wxLogDebug("wxApp::CocoaInstallIdleHandler");
238 // Call doIdle for EVERYTHING dammit
239 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
240 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
243 void wxApp::CocoaDelegate_applicationWillBecomeActive()
247 void wxApp::CocoaDelegate_applicationDidBecomeActive()
251 void wxApp::CocoaDelegate_applicationWillResignActive()
255 void wxApp::CocoaDelegate_applicationDidResignActive()
259 bool wxApp::OnInitGui()
261 wxAutoNSAutoreleasePool pool;
262 if(!wxAppBase::OnInitGui())
265 // Create the app using the sharedApplication method
266 m_cocoaApp = [NSApplication sharedApplication];
267 m_cocoaAppDelegate = [[wxNSApplicationDelegate alloc] init];
268 [m_cocoaApp setDelegate:m_cocoaAppDelegate];
270 wxMenuBarManager::CreateInstance();
272 wxDC::CocoaInitializeTextSystem();
273 // [ m_cocoaApp setDelegate:m_cocoaApp ];
275 wxLogDebug("Just for kicks");
276 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
277 wxLogDebug("okay.. done now");
282 bool wxApp::CallOnInit()
284 // wxAutoNSAutoreleasePool pool;
290 if(!wxAppBase::OnInit())
300 wxAppConsole::Exit();
303 // Yield to other processes
304 bool wxApp::Yield(bool onlyIfNeeded)
307 static bool s_inYield = false;
310 // disable log flushing from here because a call to wxYield() shouldn't
311 // normally result in message boxes popping up &c
319 wxFAIL_MSG( wxT("wxYield called recursively" ) );
327 // Run the event loop until it is out of events
328 while(NSEvent *event = [GetNSApplication()
329 nextEventMatchingMask:NSAnyEventMask
330 untilDate:[NSDate distantPast]
331 inMode:NSDefaultRunLoopMode
334 [GetNSApplication() sendEvent: event];
338 // let the logs be flashed again
348 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
351 wxAppBase::OnAssert(file, line, cond, msg);
352 m_isInAssert = FALSE;
354 #endif // __WXDEBUG__