Always return NO for applicationShouldTerminateAfterLastWindowClosed
[wxWidgets.git] / src / cocoa / app.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        cocoa/app.mm
3 // Purpose:     wxApp
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2002/11/27
7 // RCS-ID:      $Id$
8 // Copyright:   (c) David Elliott
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14     #include "wx/defs.h"
15     #include "wx/app.h"
16     #include "wx/frame.h"
17     #include "wx/dialog.h"
18     #include "wx/dc.h"
19     #include "wx/intl.h"
20     #include "wx/log.h"
21 #endif
22
23 #include "wx/module.h"
24
25 #include "wx/cocoa/ObjcPose.h"
26 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/mbarman.h"
28
29 #if wxUSE_WX_RESOURCES
30 #  include "wx/resource.h"
31 #endif
32
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
40 // ========================================================================
41 // wxPoseAsInitializer
42 // ========================================================================
43 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
44
45 // ========================================================================
46 // wxPoserNSApplication
47 // ========================================================================
48 @interface wxPoserNSApplication : NSApplication
49 {
50 }
51
52 - (void)doIdle: (id)data;
53 - (void)sendEvent: (NSEvent*)anEvent;
54 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
55 @end // wxPoserNSApplication
56
57 WX_IMPLEMENT_POSER(wxPoserNSApplication);
58
59 @implementation wxPoserNSApplication : NSApplication
60
61 - (void)doIdle: (id)data
62 {
63     wxASSERT(wxTheApp);
64     wxASSERT(wxMenuBarManager::GetInstance());
65     wxMenuBarManager::GetInstance()->CocoaInternalIdle();
66     wxLogDebug("doIdle called");
67 #ifdef __WXDEBUG__
68     if(wxTheApp->IsInAssert())
69     {
70         wxLogDebug("Idle events ignored durring assertion dialog");
71     }
72     else
73 #endif
74     {
75         NSRunLoop *rl = [NSRunLoop currentRunLoop];
76         // runMode: beforeDate returns YES if something was done
77         while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
78         {
79             wxLogDebug("Looping for idle events");
80             #if 1
81             if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
82             {
83                 wxLogDebug("Found actual work to do");
84                 break;
85             }
86             #endif
87         }
88     }
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();
92 }
93
94 - (void)sendEvent: (NSEvent*)anEvent
95 {
96     wxLogDebug("SendEvent");
97     wxTheApp->CocoaInstallRequestedIdleHandler();
98     [super sendEvent: anEvent];
99 }
100
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
105 {
106     return NO;
107 }
108
109 @end // wxPoserNSApplication
110
111 // ========================================================================
112 // wxApp
113 // ========================================================================
114
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)
123 END_EVENT_TABLE()
124
125 // ----------------------------------------------------------------------------
126 // wxApp initialization/cleanup
127 // ----------------------------------------------------------------------------
128 bool wxApp::Initialize(int& argc, wxChar **argv)
129 {
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)
136     if ( argc > 1 )
137     {
138         static const wxChar *ARG_PSN = _T("-psn_");
139         if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
140         {
141             // remove this argument
142             memmove(argv, argv + 1, argc--);
143         }
144     }
145
146     // Posing must be completed before any instances of the Objective-C
147     // classes being posed as are created.
148     wxPoseAsInitializer::InitializePosers();
149
150     return wxAppBase::Initialize(argc, argv);
151 }
152
153 void wxApp::CleanUp()
154 {
155     wxDC::CocoaShutdownTextSystem();
156     wxMenuBarManager::DestroyInstance();
157
158     wxAppBase::CleanUp();
159 }
160
161 // ----------------------------------------------------------------------------
162 // wxApp creation
163 // ----------------------------------------------------------------------------
164 wxApp::wxApp()
165 {
166     m_topWindow = NULL;
167
168     m_isIdle = true;
169 #if WXWIN_COMPATIBILITY_2_2
170     m_wantDebugOutput = TRUE;
171 #endif
172 #ifdef __WXDEBUG__
173     m_isInAssert = FALSE;
174 #endif // __WXDEBUG__
175
176     argc = 0;
177     argv = NULL;
178     m_cocoaApp = NULL;
179 }
180
181 void wxApp::CocoaInstallIdleHandler()
182 {
183     // If we're not the main thread, don't install the idle handler
184     if(m_cocoaMainThread != [NSThread currentThread])
185     {
186         wxLogDebug("Attempt to install idle handler from secondary thread");
187         return;
188     }
189     // If we're supposed to be stopping, don't add more idle events
190     if(![m_cocoaApp isRunning])
191         return;
192     wxLogDebug("wxApp::CocoaInstallIdleHandler");
193     m_isIdle = false;
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] ];
197 }
198
199 bool wxApp::OnInitGui()
200 {
201     wxAutoNSAutoreleasePool pool;
202     if(!wxAppBase::OnInitGui())
203         return FALSE;
204
205     // Create the app using the sharedApplication method
206     m_cocoaApp = [NSApplication sharedApplication];
207
208     wxMenuBarManager::CreateInstance();
209
210     wxDC::CocoaInitializeTextSystem();
211 //    [ m_cocoaApp setDelegate:m_cocoaApp ];
212     #if 0
213     wxLogDebug("Just for kicks");
214     [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
215     wxLogDebug("okay.. done now");
216     #endif
217     return TRUE;
218 }
219
220 bool wxApp::CallOnInit()
221 {
222 //    wxAutoNSAutoreleasePool pool;
223     return OnInit();
224 }
225
226 bool wxApp::OnInit()
227 {
228     if(!wxAppBase::OnInit())
229         return FALSE;
230
231     return TRUE;
232 }
233
234 void wxApp::Exit()
235 {
236     wxApp::CleanUp();
237
238     wxAppConsole::Exit();
239 }
240
241 // Yield to other processes
242 bool wxApp::Yield(bool onlyIfNeeded)
243 {
244     // MT-FIXME
245     static bool s_inYield = false;
246
247 #if wxUSE_LOG
248     // disable log flushing from here because a call to wxYield() shouldn't
249     // normally result in message boxes popping up &c
250     wxLog::Suspend();
251 #endif // wxUSE_LOG
252
253     if (s_inYield)
254     {
255         if ( !onlyIfNeeded )
256         {
257             wxFAIL_MSG( wxT("wxYield called recursively" ) );
258         }
259
260         return false;
261     }
262
263     s_inYield = true;
264
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
270                 dequeue: YES])
271     {
272         [GetNSApplication() sendEvent: event];
273     }
274
275 #if wxUSE_LOG
276     // let the logs be flashed again
277     wxLog::Resume();
278 #endif // wxUSE_LOG
279
280     s_inYield = false;
281
282     return true;
283 }
284
285 #ifdef __WXDEBUG__
286 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
287 {
288     m_isInAssert = TRUE;
289     wxAppBase::OnAssert(file, line, cond, msg);
290     m_isInAssert = FALSE;
291 }
292 #endif // __WXDEBUG__
293