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"
35 #include "wx/cocoa/mbarman.h"
37 #if wxUSE_WX_RESOURCES
38 # include "wx/resource.h"
41 #import <AppKit/NSApplication.h>
42 #import <Foundation/NSRunLoop.h>
43 #import <Foundation/NSArray.h>
44 #import <Foundation/NSAutoreleasePool.h>
45 #import <Foundation/NSThread.h>
46 #import <AppKit/NSEvent.h>
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
54 @interface wxPoserNSApplication : NSApplication
58 - (void)doIdle: (id)data;
59 - (void)sendEvent: (NSEvent*)anEvent;
60 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
61 @end // wxPoserNSApplication
63 @implementation wxPoserNSApplication : NSApplication
65 - (void)doIdle: (id)data
68 wxASSERT(wxMenuBarManager::GetInstance());
69 wxMenuBarManager::GetInstance()->CocoaInternalIdle();
70 wxLogDebug("doIdle called");
72 if(wxTheApp->IsInAssert())
74 wxLogDebug("Idle events ignored durring assertion dialog");
79 NSRunLoop *rl = [NSRunLoop currentRunLoop];
80 // runMode: beforeDate returns YES if something was done
81 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
83 wxLogDebug("Looping for idle events");
85 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
87 wxLogDebug("Found actual work to do");
93 wxLogDebug("Idle processing complete, requesting next idle event");
94 // Add ourself back into the run loop (on next event) if necessary
95 wxTheApp->CocoaRequestIdle();
98 - (void)sendEvent: (NSEvent*)anEvent
100 wxLogDebug("SendEvent");
101 wxTheApp->CocoaInstallRequestedIdleHandler();
102 [super sendEvent: anEvent];
105 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
107 BOOL ret = wxTheApp->GetExitOnFrameDelete();
108 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
112 @end // wxPoserNSApplication
113 WX_IMPLEMENT_POSER(wxPoserNSApplication);
115 // ============================================================================
117 // ============================================================================
123 wxAppConsole::Exit();
126 // ============================================================================
127 // wxApp implementation
128 // ============================================================================
130 // ----------------------------------------------------------------------------
131 // wxApp Static member initialization
132 // ----------------------------------------------------------------------------
134 #if !USE_SHARED_LIBRARY
135 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
136 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
137 EVT_IDLE(wxAppBase::OnIdle)
138 // EVT_END_SESSION(wxApp::OnEndSession)
139 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
143 // ----------------------------------------------------------------------------
144 // wxApp initialization/cleanup
145 // ----------------------------------------------------------------------------
147 bool wxApp::Initialize(int& argc, wxChar **argv)
149 wxAutoNSAutoreleasePool pool;
150 m_cocoaMainThread = [NSThread currentThread];
151 // Mac OS X passes a process serial number command line argument when
152 // the application is launched from the Finder. This argument must be
153 // removed from the command line arguments before being handled by the
154 // application (otherwise applications would need to handle it)
157 static const wxChar *ARG_PSN = _T("-psn_");
158 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
160 // remove this argument
161 memmove(argv, argv + 1, argc--);
165 // Posing must be completed before any instances of the Objective-C
166 // classes being posed as are created.
167 wxPoseAsInitializer::InitializePosers();
169 return wxAppBase::Initialize(argc, argv);
172 void wxApp::CleanUp()
174 wxDC::CocoaShutdownTextSystem();
175 wxMenuBarManager::DestroyInstance();
177 wxAppBase::CleanUp();
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
189 #if WXWIN_COMPATIBILITY_2_2
190 m_wantDebugOutput = TRUE;
193 m_isInAssert = FALSE;
194 #endif // __WXDEBUG__
201 void wxApp::CocoaInstallIdleHandler()
203 // If we're not the main thread, don't install the idle handler
204 if(m_cocoaMainThread != [NSThread currentThread])
206 wxLogDebug("Attempt to install idle handler from secondary thread");
209 // If we're supposed to be stopping, don't add more idle events
210 if(![m_cocoaApp isRunning])
212 wxLogDebug("wxApp::CocoaInstallIdleHandler");
214 // Call doIdle for EVERYTHING dammit
215 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
216 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
219 bool wxApp::OnInitGui()
221 wxAutoNSAutoreleasePool pool;
222 if(!wxAppBase::OnInitGui())
225 // Create the app using the sharedApplication method
226 m_cocoaApp = [NSApplication sharedApplication];
228 wxMenuBarManager::CreateInstance();
230 wxDC::CocoaInitializeTextSystem();
231 // [ m_cocoaApp setDelegate:m_cocoaApp ];
233 wxLogDebug("Just for kicks");
234 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
235 wxLogDebug("okay.. done now");
240 bool wxApp::CallOnInit()
242 // wxAutoNSAutoreleasePool pool;
248 if(!wxAppBase::OnInit())
254 bool wxApp::Initialized()
262 int wxApp::MainLoop()
268 void wxApp::ExitMainLoop()
270 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
271 // CocoaInstallRequestedIdleHandler();
273 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
274 // actually.. we WANT the idle event
278 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
280 [m_cocoaApp stop: m_cocoaApp];
283 // Is a message/event pending?
284 bool wxApp::Pending()
289 // Dispatch a message.
290 void wxApp::Dispatch()
294 // Yield to other processes
296 bool wxApp::Yield(bool onlyIfNeeded)
299 static bool s_inYield = false;
302 // disable log flushing from here because a call to wxYield() shouldn't
303 // normally result in message boxes popping up &c
311 wxFAIL_MSG( wxT("wxYield called recursively" ) );
319 // Run the event loop until it is out of events
320 while(NSEvent *event = [GetNSApplication()
321 nextEventMatchingMask:NSAnyEventMask
322 untilDate:[NSDate distantPast]
323 inMode:NSDefaultRunLoopMode
326 [GetNSApplication() sendEvent: event];
330 // let the logs be flashed again
340 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
343 wxAppBase::OnAssert(file, line, cond, msg);
344 m_isInAssert = FALSE;
346 #endif // __WXDEBUG__