1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Elliott
8 // Copyright: (c) David Elliott
9 // Licence: wxWindows license
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"
35 #if wxUSE_WX_RESOURCES
36 # include "wx/resource.h"
39 #import <AppKit/NSApplication.h>
40 #import <Foundation/NSRunLoop.h>
41 #import <Foundation/NSArray.h>
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
49 @interface wxPoserNSApplication : NSApplication
53 - (void)doIdle: (id)data;
54 - (void)finishLaunching;
55 - (void)sendEvent: (NSEvent*)anEvent;
56 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
57 @end // wxPoserNSApplication
59 @implementation wxPoserNSApplication : NSApplication
61 - (void)doIdle: (id)data
64 wxLogDebug("doIdle called");
65 NSRunLoop *rl = [NSRunLoop currentRunLoop];
66 // runMode: beforeDate returns YES if something was done
67 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
69 wxLogDebug("Looping for idle events");
71 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
73 wxLogDebug("Found actual work to do");
78 wxLogDebug("Idle processing complete, requesting next idle event");
79 // Add ourself back into the run loop (on next event) if necessary
80 wxTheApp->CocoaRequestIdle();
83 - (void)finishLaunching
85 wxLogDebug("finishLaunching");
86 bool initsuccess = wxTheApp->OnInit();
90 [super finishLaunching];
93 - (void)sendEvent: (NSEvent*)anEvent
95 wxLogDebug("SendEvent");
96 wxTheApp->CocoaInstallRequestedIdleHandler();
97 [super sendEvent: anEvent];
100 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
102 BOOL ret = wxTheApp->GetExitOnFrameDelete();
103 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
107 @end // wxPoserNSApplication
108 WX_IMPLEMENT_POSER(wxPoserNSApplication);
110 // ============================================================================
112 // ============================================================================
118 wxAppConsole::Exit();
121 // ============================================================================
122 // wxApp implementation
123 // ============================================================================
125 // ----------------------------------------------------------------------------
126 // wxApp Static member initialization
127 // ----------------------------------------------------------------------------
129 #if !USE_SHARED_LIBRARY
130 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
131 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
132 EVT_IDLE(wxApp::OnIdle)
133 // EVT_END_SESSION(wxApp::OnEndSession)
134 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
138 // ----------------------------------------------------------------------------
139 // wxApp initialization/cleanup
140 // ----------------------------------------------------------------------------
142 bool wxApp::Initialize(int& argc, wxChar **argv)
144 // Mac OS X passes a process serial number command line argument when
145 // the application is launched from the Finder. This argument must be
146 // removed from the command line arguments before being handled by the
147 // application (otherwise applications would need to handle it)
150 static const wxChar *ARG_PSN = _T("-psn_");
151 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
153 // remove this argument
154 memmove(argv, argv + 1, argc--);
158 // Posing must be completed before any instances of the Objective-C
159 // classes being posed as are created.
160 wxPoseAsInitializer::InitializePosers();
162 return wxAppBase::Initialize(argc, argv);
165 void wxApp::CleanUp()
167 wxDC::CocoaShutdownTextSystem();
169 wxAppBase::CleanUp();
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
182 #if WXWIN_COMPATIBILITY_2_2
183 m_wantDebugOutput = TRUE;
191 void wxApp::CocoaInstallIdleHandler()
193 wxLogDebug("wxApp::CocoaInstallIdleHandler");
195 // Call doIdle for EVERYTHING dammit
196 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
197 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
200 bool wxApp::OnInitGui()
202 if(!wxAppBase::OnInitGui())
205 // Create the app using the sharedApplication method
206 m_cocoaApp = [NSApplication sharedApplication];
207 wxDC::CocoaInitializeTextSystem();
208 // [ m_cocoaApp setDelegate:m_cocoaApp ];
210 wxLogDebug("Just for kicks");
211 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
212 wxLogDebug("okay.. done now");
219 if(!wxAppBase::OnInit())
225 bool wxApp::Initialized()
233 int wxApp::MainLoop()
239 // Returns TRUE if more time is needed.
240 bool wxApp::ProcessIdle()
243 event.SetEventObject(this);
246 return event.MoreRequested();
249 void wxApp::ExitMainLoop()
251 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
252 // CocoaInstallRequestedIdleHandler();
254 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
255 // actually.. we WANT the idle event
259 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
261 [m_cocoaApp terminate: m_cocoaApp];
264 // Is a message/event pending?
265 bool wxApp::Pending()
270 // Dispatch a message.
271 void wxApp::Dispatch()
275 void wxApp::OnIdle(wxIdleEvent& event)
277 wxLogDebug("wxApp::OnIdle");
278 static bool s_inOnIdle = FALSE;
280 // Avoid recursion (via ProcessEvent default case)
286 DeletePendingObjects();
288 // flush the logged messages if any
289 wxLog *pLog = wxLog::GetActiveTarget();
290 if ( pLog != NULL && pLog->HasPendingMessages() )
293 // Send OnIdle events to all windows
294 bool needMore = SendIdleEvents();
297 event.RequestMore(TRUE);
302 // Send idle event to all top-level windows
303 bool wxApp::SendIdleEvents()
305 bool needMore = FALSE;
306 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
309 wxWindow* win = node->GetData();
310 if (SendIdleEvents(win))
313 node = node->GetNext();
318 // Send idle event to window and all subwindows
319 bool wxApp::SendIdleEvents(wxWindow* win)
321 // wxLogDebug("SendIdleEvents win=%p",win);
322 bool needMore = FALSE;
325 event.SetEventObject(win);
326 win->ProcessEvent(event);
328 if (event.MoreRequested())
331 wxWindowList::Node* node = win->GetChildren().GetFirst();
334 // wxLogDebug("child=%p",node->Data());
335 wxWindow* win = node->GetData();
336 if (SendIdleEvents(win))
339 node = node->GetNext();
344 // Yield to other processes
346 bool wxApp::Yield(bool onlyIfNeeded)
349 static bool s_inYield = false;
352 // disable log flushing from here because a call to wxYield() shouldn't
353 // normally result in message boxes popping up &c
361 wxFAIL_MSG( wxT("wxYield called recursively" ) );
369 wxLogDebug("WARNING: SUPPOSED to have yielded!");
370 // FIXME: Do something!
373 // let the logs be flashed again