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.
76 - (NSEvent *)nextEventMatchingMask:(unsigned int)mask untilDate:(NSDate *)expiration inMode:(NSString *)mode dequeue:(BOOL)flag
78 // Get the same events except don't block
79 NSEvent *event = [super nextEventMatchingMask:mask untilDate:nil/* equivalent to [NSDate distantPast] */ inMode:mode dequeue:flag];
80 // If we got one, simply return it
83 // No events, try doing some idle stuff
86 && !wxTheApp->IsInAssert()
88 && ([NSDefaultRunLoopMode isEqualToString:mode] || [NSModalPanelRunLoopMode isEqualToString:mode]))
91 wxLogDebug(wxT("Processing idle events"));
92 while(wxTheApp->ProcessIdle())
94 // Get the same events except don't block
95 NSEvent *event = [super nextEventMatchingMask:mask untilDate:nil/* equivalent to [NSDate distantPast] */ inMode:mode dequeue:flag];
96 // If we got one, simply return it
99 // we didn't get one, do some idle work
100 wxLogDebug(wxT("Looping idle events"));
102 // No more idle work requested, block
103 wxLogDebug(wxT("Finished idle processing"));
106 wxLogDebug(wxT("Avoiding idle processing sg_needIdle=%d"),sg_needIdle);
107 return [super nextEventMatchingMask:mask untilDate:expiration inMode:mode dequeue:flag];
110 - (void)sendEvent: (NSEvent*)anEvent
112 wxLogDebug(wxT("SendEvent"));
114 [super sendEvent: anEvent];
117 @end // wxPoserNSApplication
119 // ========================================================================
120 // wxNSApplicationDelegate
121 // ========================================================================
122 @interface wxNSApplicationDelegate : NSObject
127 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
128 - (void)applicationWillBecomeActive:(NSNotification *)notification;
129 - (void)applicationDidBecomeActive:(NSNotification *)notification;
130 - (void)applicationWillResignActive:(NSNotification *)notification;
131 - (void)applicationDidResignActive:(NSNotification *)notification;
132 @end // interface wxNSApplicationDelegate : NSObject
134 @implementation wxNSApplicationDelegate : NSObject
136 // NOTE: Terminate means that the event loop does NOT return and thus
137 // cleanup code doesn't properly execute. Furthermore, wxWindows has its
138 // own exit on frame delete mechanism.
139 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
144 - (void)applicationWillBecomeActive:(NSNotification *)notification
146 wxTheApp->CocoaDelegate_applicationWillBecomeActive();
149 - (void)applicationDidBecomeActive:(NSNotification *)notification
151 wxTheApp->CocoaDelegate_applicationDidBecomeActive();
154 - (void)applicationWillResignActive:(NSNotification *)notification
156 wxTheApp->CocoaDelegate_applicationWillResignActive();
159 - (void)applicationDidResignActive:(NSNotification *)notification
161 wxTheApp->CocoaDelegate_applicationDidResignActive();
164 @end // implementation wxNSApplicationDelegate : NSObject
166 // ========================================================================
168 // ========================================================================
170 // ----------------------------------------------------------------------------
171 // wxApp Static member initialization
172 // ----------------------------------------------------------------------------
173 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
174 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
175 EVT_IDLE(wxAppBase::OnIdle)
176 // EVT_END_SESSION(wxApp::OnEndSession)
177 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
180 // ----------------------------------------------------------------------------
181 // wxApp initialization/cleanup
182 // ----------------------------------------------------------------------------
183 bool wxApp::Initialize(int& argc, wxChar **argv)
185 wxAutoNSAutoreleasePool pool;
186 m_cocoaMainThread = [NSThread currentThread];
187 // Mac OS X passes a process serial number command line argument when
188 // the application is launched from the Finder. This argument must be
189 // removed from the command line arguments before being handled by the
190 // application (otherwise applications would need to handle it)
193 static const wxChar *ARG_PSN = _T("-psn_");
194 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
196 // remove this argument
198 memmove(argv + 1, argv + 2, argc * sizeof(wxChar *));
202 // Posing must be completed before any instances of the Objective-C
203 // classes being posed as are created.
204 wxPoseAsInitializer::InitializePosers();
206 return wxAppBase::Initialize(argc, argv);
209 void wxApp::CleanUp()
211 wxAutoNSAutoreleasePool pool;
213 wxDC::CocoaShutdownTextSystem();
214 wxMenuBarManager::DestroyInstance();
216 [m_cocoaApp setDelegate:nil];
217 [m_cocoaAppDelegate release];
218 m_cocoaAppDelegate = NULL;
220 wxAppBase::CleanUp();
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
230 #if WXWIN_COMPATIBILITY_2_2
231 m_wantDebugOutput = TRUE;
234 m_isInAssert = FALSE;
235 #endif // __WXDEBUG__
240 m_cocoaAppDelegate = NULL;
243 void wxApp::CocoaDelegate_applicationWillBecomeActive()
247 void wxApp::CocoaDelegate_applicationDidBecomeActive()
251 void wxApp::CocoaDelegate_applicationWillResignActive()
253 wxTopLevelWindowCocoa::DeactivatePendingWindow();
256 void wxApp::CocoaDelegate_applicationDidResignActive()
260 bool wxApp::OnInitGui()
262 wxAutoNSAutoreleasePool pool;
263 if(!wxAppBase::OnInitGui())
266 // Create the app using the sharedApplication method
267 m_cocoaApp = [NSApplication sharedApplication];
268 m_cocoaAppDelegate = [[wxNSApplicationDelegate alloc] init];
269 [m_cocoaApp setDelegate:m_cocoaAppDelegate];
271 wxMenuBarManager::CreateInstance();
273 wxDC::CocoaInitializeTextSystem();
274 // [ m_cocoaApp setDelegate:m_cocoaApp ];
278 bool wxApp::CallOnInit()
280 // wxAutoNSAutoreleasePool pool;
286 if(!wxAppBase::OnInit())
296 wxAppConsole::Exit();
299 // Yield to other processes
300 bool wxApp::Yield(bool onlyIfNeeded)
303 static bool s_inYield = false;
306 // disable log flushing from here because a call to wxYield() shouldn't
307 // normally result in message boxes popping up &c
315 wxFAIL_MSG( wxT("wxYield called recursively" ) );
323 // Run the event loop until it is out of events
324 while(NSEvent *event = [GetNSApplication()
325 nextEventMatchingMask:NSAnyEventMask
326 untilDate:[NSDate distantPast]
327 inMode:NSDefaultRunLoopMode
330 [GetNSApplication() sendEvent: event];
334 // let the logs be flashed again
343 void wxApp::WakeUpIdle()
345 [m_cocoaApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
346 location:NSZeroPoint modifierFlags:NSAnyEventMask
347 timestamp:0 windowNumber:0 context:nil
348 subtype:0 data1:0 data2:0] atStart:NO];
352 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
355 wxAppBase::OnAssert(file, line, cond, msg);
356 m_isInAssert = FALSE;
358 #endif // __WXDEBUG__