removed wxApp::Initialized() (replaced with a dummy version in wxApp itself); wxApp...
[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 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
102 {
103     BOOL ret = wxTheApp->GetExitOnFrameDelete();
104     wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
105     return ret;
106 }
107
108 @end // wxPoserNSApplication
109
110 // ========================================================================
111 // wxApp
112 // ========================================================================
113
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)
122 END_EVENT_TABLE()
123
124 // ----------------------------------------------------------------------------
125 // wxApp initialization/cleanup
126 // ----------------------------------------------------------------------------
127 bool wxApp::Initialize(int& argc, wxChar **argv)
128 {
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)
135     if ( argc > 1 )
136     {
137         static const wxChar *ARG_PSN = _T("-psn_");
138         if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
139         {
140             // remove this argument
141             memmove(argv, argv + 1, argc--);
142         }
143     }
144
145     // Posing must be completed before any instances of the Objective-C
146     // classes being posed as are created.
147     wxPoseAsInitializer::InitializePosers();
148
149     return wxAppBase::Initialize(argc, argv);
150 }
151
152 void wxApp::CleanUp()
153 {
154     wxDC::CocoaShutdownTextSystem();
155     wxMenuBarManager::DestroyInstance();
156
157     wxAppBase::CleanUp();
158 }
159
160 // ----------------------------------------------------------------------------
161 // wxApp creation
162 // ----------------------------------------------------------------------------
163 wxApp::wxApp()
164 {
165     m_topWindow = NULL;
166
167     m_isIdle = true;
168 #if WXWIN_COMPATIBILITY_2_2
169     m_wantDebugOutput = TRUE;
170 #endif
171 #ifdef __WXDEBUG__
172     m_isInAssert = FALSE;
173 #endif // __WXDEBUG__
174
175     argc = 0;
176     argv = NULL;
177     m_cocoaApp = NULL;
178 }
179
180 void wxApp::CocoaInstallIdleHandler()
181 {
182     // If we're not the main thread, don't install the idle handler
183     if(m_cocoaMainThread != [NSThread currentThread])
184     {
185         wxLogDebug("Attempt to install idle handler from secondary thread");
186         return;
187     }
188     // If we're supposed to be stopping, don't add more idle events
189     if(![m_cocoaApp isRunning])
190         return;
191     wxLogDebug("wxApp::CocoaInstallIdleHandler");
192     m_isIdle = false;
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] ];
196 }
197
198 bool wxApp::OnInitGui()
199 {
200     wxAutoNSAutoreleasePool pool;
201     if(!wxAppBase::OnInitGui())
202         return FALSE;
203
204     // Create the app using the sharedApplication method
205     m_cocoaApp = [NSApplication sharedApplication];
206
207     wxMenuBarManager::CreateInstance();
208
209     wxDC::CocoaInitializeTextSystem();
210 //    [ m_cocoaApp setDelegate:m_cocoaApp ];
211     #if 0
212     wxLogDebug("Just for kicks");
213     [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
214     wxLogDebug("okay.. done now");
215     #endif
216     return TRUE;
217 }
218
219 bool wxApp::CallOnInit()
220 {
221 //    wxAutoNSAutoreleasePool pool;
222     return OnInit();
223 }
224
225 bool wxApp::OnInit()
226 {
227     if(!wxAppBase::OnInit())
228         return FALSE;
229
230     return TRUE;
231 }
232
233 void wxApp::Exit()
234 {
235     wxApp::CleanUp();
236
237     wxAppConsole::Exit();
238 }
239
240 int wxApp::MainLoop()
241 {
242     [m_cocoaApp run];
243     return 0;
244 }
245
246 void wxApp::ExitMainLoop()
247 {
248     wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
249 //    CocoaInstallRequestedIdleHandler();
250 //    if(m_isIdle)
251 //        [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
252 // actually.. we WANT the idle event
253 // or not
254 #if 0
255     if(!m_isIdle)
256         [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
257 #endif
258     [m_cocoaApp stop: m_cocoaApp];
259 }
260
261 // Is a message/event pending?
262 bool wxApp::Pending()
263 {
264     return 0;
265 }
266
267 // Dispatch a message.
268 bool wxApp::Dispatch()
269 {
270     return true;
271 }
272
273 // Yield to other processes
274 bool wxApp::Yield(bool onlyIfNeeded)
275 {
276     // MT-FIXME
277     static bool s_inYield = false;
278
279 #if wxUSE_LOG
280     // disable log flushing from here because a call to wxYield() shouldn't
281     // normally result in message boxes popping up &c
282     wxLog::Suspend();
283 #endif // wxUSE_LOG
284
285     if (s_inYield)
286     {
287         if ( !onlyIfNeeded )
288         {
289             wxFAIL_MSG( wxT("wxYield called recursively" ) );
290         }
291
292         return false;
293     }
294
295     s_inYield = true;
296
297     // Run the event loop until it is out of events
298     while(NSEvent *event = [GetNSApplication()
299                 nextEventMatchingMask:NSAnyEventMask
300                 untilDate:[NSDate distantPast]
301                 inMode:NSDefaultRunLoopMode
302                 dequeue: YES])
303     {
304         [GetNSApplication() sendEvent: event];
305     }
306
307 #if wxUSE_LOG
308     // let the logs be flashed again
309     wxLog::Resume();
310 #endif // wxUSE_LOG
311
312     s_inYield = false;
313
314     return true;
315 }
316
317 #ifdef __WXDEBUG__
318 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
319 {
320     m_isInAssert = TRUE;
321     wxAppBase::OnAssert(file, line, cond, msg);
322     m_isInAssert = FALSE;
323 }
324 #endif // __WXDEBUG__
325