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>
39 #import <Foundation/NSString.h>
41 // ========================================================================
42 // wxPoseAsInitializer
43 // ========================================================================
44 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
46 static bool sg_needIdle = true;
48 // ========================================================================
49 // wxPoserNSApplication
50 // ========================================================================
51 @interface wxPoserNSApplication : NSApplication
55 - (NSEvent *)nextEventMatchingMask:(unsigned int)mask untilDate:(NSDate *)expiration inMode:(NSString *)mode dequeue:(BOOL)flag;
56 - (void)sendEvent: (NSEvent*)anEvent;
57 @end // wxPoserNSApplication
59 WX_IMPLEMENT_POSER(wxPoserNSApplication);
61 @implementation wxPoserNSApplication : NSApplication
63 /* NOTE: The old method of idle event handling added the handler using the
64 [NSRunLoop -performSelector:target:argument:order:modes] which caused
65 the invocation to occur at the begining of [NSApplication
66 -nextEventMatchingMask:untilDate:expiration:inMode:dequeue:]. However,
67 the code would be scheduled for invocation with every iteration of
68 the event loop. This new method simply overrides the method. The
69 same caveats apply. In particular, by the time the event loop has
70 called this method, it usually expects to receive an event. If you
71 plan on stopping the event loop, it is wise to send an event through
72 the queue to ensure this method will return.
73 See wxEventLoop::Exit() for more information.
75 RN: We used to use nil as the untilDate in previous versions since nil
76 is a shorter and more concise way of specifying an infinite amount of
77 time than [NSDate distantPast]. However, Apple neglects to mention in
78 their documentation that nil is not handled correctly in OSX 10.2
79 (and possibly lower) and when the call is reached the system comes to
80 a screeching halt, therefore we need to specify [NSDate distantPast]
81 explicitly so that wxCocoa will work correctly in OSX 10.2.
84 - (NSEvent *)nextEventMatchingMask:(unsigned int)mask untilDate:(NSDate *)expiration inMode:(NSString *)mode dequeue:(BOOL)flag
86 // Get the same events except don't block
87 NSEvent *event = [super nextEventMatchingMask:mask untilDate:[NSDate distantPast] inMode:mode dequeue:flag];
88 // If we got one, simply return it
91 // No events, try doing some idle stuff
92 if(sg_needIdle /*&& !wxTheApp->IsInAssert()*/ && ([NSDefaultRunLoopMode isEqualToString:mode] || [NSModalPanelRunLoopMode isEqualToString:mode]))
95 wxLogDebug(wxT("Processing idle events"));
96 while(wxTheApp->ProcessIdle())
98 // Get the same events except don't block
99 NSEvent *event = [super nextEventMatchingMask:mask untilDate:[NSDate distantPast]/* equivalent to [NSDate distantPast] */ inMode:mode dequeue:flag];
100 // If we got one, simply return it
103 // we didn't get one, do some idle work
104 wxLogDebug(wxT("Looping idle events"));
106 // No more idle work requested, block
107 wxLogDebug(wxT("Finished idle processing"));
110 wxLogDebug(wxT("Avoiding idle processing sg_needIdle=%d"),sg_needIdle);
111 return [super nextEventMatchingMask:mask untilDate:expiration inMode:mode dequeue:flag];
114 - (void)sendEvent: (NSEvent*)anEvent
116 wxLogDebug(wxT("SendEvent"));
118 [super sendEvent: anEvent];
121 @end // wxPoserNSApplication
123 // ========================================================================
124 // wxNSApplicationDelegate
125 // ========================================================================
126 @interface wxNSApplicationDelegate : NSObject
131 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
132 - (void)applicationWillBecomeActive:(NSNotification *)notification;
133 - (void)applicationDidBecomeActive:(NSNotification *)notification;
134 - (void)applicationWillResignActive:(NSNotification *)notification;
135 - (void)applicationDidResignActive:(NSNotification *)notification;
136 @end // interface wxNSApplicationDelegate : NSObject
138 @implementation wxNSApplicationDelegate : NSObject
140 // NOTE: Terminate means that the event loop does NOT return and thus
141 // cleanup code doesn't properly execute. Furthermore, wxWindows has its
142 // own exit on frame delete mechanism.
143 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
148 - (void)applicationWillBecomeActive:(NSNotification *)notification
150 wxTheApp->CocoaDelegate_applicationWillBecomeActive();
153 - (void)applicationDidBecomeActive:(NSNotification *)notification
155 wxTheApp->CocoaDelegate_applicationDidBecomeActive();
158 - (void)applicationWillResignActive:(NSNotification *)notification
160 wxTheApp->CocoaDelegate_applicationWillResignActive();
163 - (void)applicationDidResignActive:(NSNotification *)notification
165 wxTheApp->CocoaDelegate_applicationDidResignActive();
168 @end // implementation wxNSApplicationDelegate : NSObject
170 // ========================================================================
172 // ========================================================================
174 // ----------------------------------------------------------------------------
175 // wxApp Static member initialization
176 // ----------------------------------------------------------------------------
177 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
178 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
179 EVT_IDLE(wxAppBase::OnIdle)
180 // EVT_END_SESSION(wxApp::OnEndSession)
181 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
184 // ----------------------------------------------------------------------------
185 // wxApp initialization/cleanup
186 // ----------------------------------------------------------------------------
187 bool wxApp::Initialize(int& argc, wxChar **argv)
189 wxAutoNSAutoreleasePool pool;
190 m_cocoaMainThread = [NSThread currentThread];
191 // Mac OS X passes a process serial number command line argument when
192 // the application is launched from the Finder. This argument must be
193 // removed from the command line arguments before being handled by the
194 // application (otherwise applications would need to handle it)
197 static const wxChar *ARG_PSN = _T("-psn_");
198 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
200 // remove this argument
202 memmove(argv + 1, argv + 2, argc * sizeof(wxChar *));
206 // Posing must be completed before any instances of the Objective-C
207 // classes being posed as are created.
208 wxPoseAsInitializer::InitializePosers();
210 return wxAppBase::Initialize(argc, argv);
213 void wxApp::CleanUp()
215 wxAutoNSAutoreleasePool pool;
217 wxDC::CocoaShutdownTextSystem();
218 wxMenuBarManager::DestroyInstance();
220 [m_cocoaApp setDelegate:nil];
221 [m_cocoaAppDelegate release];
222 m_cocoaAppDelegate = NULL;
224 wxAppBase::CleanUp();
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
234 #if WXWIN_COMPATIBILITY_2_2
235 m_wantDebugOutput = TRUE;
238 m_isInAssert = FALSE;
239 #endif // __WXDEBUG__
244 m_cocoaAppDelegate = NULL;
247 void wxApp::CocoaDelegate_applicationWillBecomeActive()
251 void wxApp::CocoaDelegate_applicationDidBecomeActive()
255 void wxApp::CocoaDelegate_applicationWillResignActive()
257 wxTopLevelWindowCocoa::DeactivatePendingWindow();
260 void wxApp::CocoaDelegate_applicationDidResignActive()
264 bool wxApp::OnInitGui()
266 wxAutoNSAutoreleasePool pool;
267 if(!wxAppBase::OnInitGui())
270 // Create the app using the sharedApplication method
271 m_cocoaApp = [NSApplication sharedApplication];
272 m_cocoaAppDelegate = [[wxNSApplicationDelegate alloc] init];
273 [m_cocoaApp setDelegate:m_cocoaAppDelegate];
275 wxMenuBarManager::CreateInstance();
277 wxDC::CocoaInitializeTextSystem();
278 // [ m_cocoaApp setDelegate:m_cocoaApp ];
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
347 void wxApp::WakeUpIdle()
349 [m_cocoaApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
350 location:NSZeroPoint modifierFlags:NSAnyEventMask
351 timestamp:0 windowNumber:0 context:nil
352 subtype:0 data1:0 data2:0] atStart:NO];
356 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
359 wxAppBase::OnAssert(file, line, cond, msg);
360 m_isInAssert = FALSE;
362 #endif // __WXDEBUG__