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 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
103 BOOL ret = wxTheApp->GetExitOnFrameDelete();
104 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
108 @end // wxPoserNSApplication
110 // ========================================================================
112 // ========================================================================
114 // ----------------------------------------------------------------------------
115 // wxApp Static member initialization
116 // ----------------------------------------------------------------------------
117 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
118 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
119 EVT_IDLE(wxAppBase::OnIdle)
120 // EVT_END_SESSION(wxApp::OnEndSession)
121 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
124 // ----------------------------------------------------------------------------
125 // wxApp initialization/cleanup
126 // ----------------------------------------------------------------------------
127 bool wxApp::Initialize(int& argc, wxChar **argv)
129 wxAutoNSAutoreleasePool pool;
130 m_cocoaMainThread = [NSThread currentThread];
131 // Mac OS X passes a process serial number command line argument when
132 // the application is launched from the Finder. This argument must be
133 // removed from the command line arguments before being handled by the
134 // application (otherwise applications would need to handle it)
137 static const wxChar *ARG_PSN = _T("-psn_");
138 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
140 // remove this argument
141 memmove(argv, argv + 1, argc--);
145 // Posing must be completed before any instances of the Objective-C
146 // classes being posed as are created.
147 wxPoseAsInitializer::InitializePosers();
149 return wxAppBase::Initialize(argc, argv);
152 void wxApp::CleanUp()
154 wxDC::CocoaShutdownTextSystem();
155 wxMenuBarManager::DestroyInstance();
157 wxAppBase::CleanUp();
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
168 #if WXWIN_COMPATIBILITY_2_2
169 m_wantDebugOutput = TRUE;
172 m_isInAssert = FALSE;
173 #endif // __WXDEBUG__
180 void wxApp::CocoaInstallIdleHandler()
182 // If we're not the main thread, don't install the idle handler
183 if(m_cocoaMainThread != [NSThread currentThread])
185 wxLogDebug("Attempt to install idle handler from secondary thread");
188 // If we're supposed to be stopping, don't add more idle events
189 if(![m_cocoaApp isRunning])
191 wxLogDebug("wxApp::CocoaInstallIdleHandler");
193 // Call doIdle for EVERYTHING dammit
194 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
195 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
198 bool wxApp::OnInitGui()
200 wxAutoNSAutoreleasePool pool;
201 if(!wxAppBase::OnInitGui())
204 // Create the app using the sharedApplication method
205 m_cocoaApp = [NSApplication sharedApplication];
207 wxMenuBarManager::CreateInstance();
209 wxDC::CocoaInitializeTextSystem();
210 // [ m_cocoaApp setDelegate:m_cocoaApp ];
212 wxLogDebug("Just for kicks");
213 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
214 wxLogDebug("okay.. done now");
219 bool wxApp::CallOnInit()
221 // wxAutoNSAutoreleasePool pool;
227 if(!wxAppBase::OnInit())
233 bool wxApp::Initialized()
245 wxAppConsole::Exit();
248 int wxApp::MainLoop()
254 void wxApp::ExitMainLoop()
256 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
257 // CocoaInstallRequestedIdleHandler();
259 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
260 // actually.. we WANT the idle event
264 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
266 [m_cocoaApp stop: m_cocoaApp];
269 // Is a message/event pending?
270 bool wxApp::Pending()
275 // Dispatch a message.
276 void wxApp::Dispatch()
280 // Yield to other processes
281 bool wxApp::Yield(bool onlyIfNeeded)
284 static bool s_inYield = false;
287 // disable log flushing from here because a call to wxYield() shouldn't
288 // normally result in message boxes popping up &c
296 wxFAIL_MSG( wxT("wxYield called recursively" ) );
304 // Run the event loop until it is out of events
305 while(NSEvent *event = [GetNSApplication()
306 nextEventMatchingMask:NSAnyEventMask
307 untilDate:[NSDate distantPast]
308 inMode:NSDefaultRunLoopMode
311 [GetNSApplication() sendEvent: event];
315 // let the logs be flashed again
325 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
328 wxAppBase::OnAssert(file, line, cond, msg);
329 m_isInAssert = FALSE;
331 #endif // __WXDEBUG__