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 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
55 @end // wxPoserNSApplication
57 WX_IMPLEMENT_POSER(wxPoserNSApplication);
59 @implementation wxPoserNSApplication : NSApplication
61 - (void)doIdle: (id)data
64 wxASSERT(wxMenuBarManager::GetInstance());
65 wxMenuBarManager::GetInstance()->CocoaInternalIdle();
66 wxLogDebug("doIdle called");
68 if(wxTheApp->IsInAssert())
70 wxLogDebug("Idle events ignored durring assertion dialog");
75 NSRunLoop *rl = [NSRunLoop currentRunLoop];
76 // runMode: beforeDate returns YES if something was done
77 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
79 wxLogDebug("Looping for idle events");
81 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
83 wxLogDebug("Found actual work to do");
89 wxLogDebug("Idle processing complete, requesting next idle event");
90 // Add ourself back into the run loop (on next event) if necessary
91 wxTheApp->CocoaRequestIdle();
94 - (void)sendEvent: (NSEvent*)anEvent
96 wxLogDebug("SendEvent");
97 wxTheApp->CocoaInstallRequestedIdleHandler();
98 [super sendEvent: anEvent];
101 // NOTE: Terminate means that the event loop does NOT return and thus
102 // cleanup code doesn't properly execute. Furthermore, wxWindows has its
103 // own exit on frame delete mechanism.
104 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
109 @end // wxPoserNSApplication
111 // ========================================================================
113 // ========================================================================
115 // ----------------------------------------------------------------------------
116 // wxApp Static member initialization
117 // ----------------------------------------------------------------------------
118 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
119 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
120 EVT_IDLE(wxAppBase::OnIdle)
121 // EVT_END_SESSION(wxApp::OnEndSession)
122 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
125 // ----------------------------------------------------------------------------
126 // wxApp initialization/cleanup
127 // ----------------------------------------------------------------------------
128 bool wxApp::Initialize(int& argc, wxChar **argv)
130 wxAutoNSAutoreleasePool pool;
131 m_cocoaMainThread = [NSThread currentThread];
132 // Mac OS X passes a process serial number command line argument when
133 // the application is launched from the Finder. This argument must be
134 // removed from the command line arguments before being handled by the
135 // application (otherwise applications would need to handle it)
138 static const wxChar *ARG_PSN = _T("-psn_");
139 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
141 // remove this argument
142 memmove(argv, argv + 1, argc--);
146 // Posing must be completed before any instances of the Objective-C
147 // classes being posed as are created.
148 wxPoseAsInitializer::InitializePosers();
150 return wxAppBase::Initialize(argc, argv);
153 void wxApp::CleanUp()
155 wxDC::CocoaShutdownTextSystem();
156 wxMenuBarManager::DestroyInstance();
158 wxAppBase::CleanUp();
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
169 #if WXWIN_COMPATIBILITY_2_2
170 m_wantDebugOutput = TRUE;
173 m_isInAssert = FALSE;
174 #endif // __WXDEBUG__
181 void wxApp::CocoaInstallIdleHandler()
183 // If we're not the main thread, don't install the idle handler
184 if(m_cocoaMainThread != [NSThread currentThread])
186 wxLogDebug("Attempt to install idle handler from secondary thread");
189 // If we're supposed to be stopping, don't add more idle events
190 if(![m_cocoaApp isRunning])
192 wxLogDebug("wxApp::CocoaInstallIdleHandler");
194 // Call doIdle for EVERYTHING dammit
195 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
196 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
199 bool wxApp::OnInitGui()
201 wxAutoNSAutoreleasePool pool;
202 if(!wxAppBase::OnInitGui())
205 // Create the app using the sharedApplication method
206 m_cocoaApp = [NSApplication sharedApplication];
208 wxMenuBarManager::CreateInstance();
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())
238 wxAppConsole::Exit();
241 // Yield to other processes
242 bool wxApp::Yield(bool onlyIfNeeded)
245 static bool s_inYield = false;
248 // disable log flushing from here because a call to wxYield() shouldn't
249 // normally result in message boxes popping up &c
257 wxFAIL_MSG( wxT("wxYield called recursively" ) );
265 // Run the event loop until it is out of events
266 while(NSEvent *event = [GetNSApplication()
267 nextEventMatchingMask:NSAnyEventMask
268 untilDate:[NSDate distantPast]
269 inMode:NSDefaultRunLoopMode
272 [GetNSApplication() sendEvent: event];
276 // let the logs be flashed again
286 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
289 wxAppBase::OnAssert(file, line, cond, msg);
290 m_isInAssert = FALSE;
292 #endif // __WXDEBUG__