removed OnIdle() which didn't compile any longer
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22     #include "wx/defs.h"
23     #include "wx/app.h"
24     #include "wx/frame.h"
25     #include "wx/dialog.h"
26     #include "wx/dc.h"
27     #include "wx/intl.h"
28     #include "wx/log.h"
29 #endif
30
31 #include "wx/module.h"
32
33 #include "wx/cocoa/ObjcPose.h"
34
35 #if wxUSE_WX_RESOURCES
36 #  include "wx/resource.h"
37 #endif
38
39 #import <AppKit/NSApplication.h>
40 #import <Foundation/NSRunLoop.h>
41 #import <Foundation/NSArray.h>
42 #import <Foundation/NSAutoreleasePool.h>
43
44 // ----------------------------------------------------------------------------
45 // globals
46 // ----------------------------------------------------------------------------
47
48 wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
49
50 @interface wxPoserNSApplication : NSApplication
51 {
52 }
53
54 - (void)doIdle: (id)data;
55 - (void)sendEvent: (NSEvent*)anEvent;
56 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
57 @end // wxPoserNSApplication
58
59 @implementation wxPoserNSApplication : NSApplication
60
61 - (void)doIdle: (id)data
62 {
63     wxASSERT(wxTheApp);
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
68     {
69         wxLogDebug("Looping for idle events");
70         #if 1
71         if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
72         {
73             wxLogDebug("Found actual work to do");
74             break;
75         }
76         #endif
77     }
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();
81 }
82
83 - (void)sendEvent: (NSEvent*)anEvent
84 {
85     wxLogDebug("SendEvent");
86     wxTheApp->CocoaInstallRequestedIdleHandler();
87     [super sendEvent: anEvent];
88 }
89
90 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
91 {
92     BOOL ret = wxTheApp->GetExitOnFrameDelete();
93     wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
94     return ret;
95 }
96
97 @end // wxPoserNSApplication
98 WX_IMPLEMENT_POSER(wxPoserNSApplication);
99
100 class wxAutoNSAutoreleasePool
101 {
102 public:
103     wxAutoNSAutoreleasePool()
104     {
105         m_pool = [[NSAutoreleasePool alloc] init];
106     }
107     ~wxAutoNSAutoreleasePool()
108     {
109         [m_pool release];
110     }
111 protected:
112     NSAutoreleasePool *m_pool;
113 };
114
115 // ============================================================================
116 // functions
117 // ============================================================================
118
119 void wxApp::Exit()
120 {
121     wxApp::CleanUp();
122
123     wxAppConsole::Exit();
124 }
125
126 // ============================================================================
127 // wxApp implementation
128 // ============================================================================
129
130 // ----------------------------------------------------------------------------
131 // wxApp Static member initialization
132 // ----------------------------------------------------------------------------
133
134 #if !USE_SHARED_LIBRARY
135 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
136 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
137 //    EVT_END_SESSION(wxApp::OnEndSession)
138 //    EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
139 END_EVENT_TABLE()
140 #endif
141
142 // ----------------------------------------------------------------------------
143 // wxApp initialization/cleanup
144 // ----------------------------------------------------------------------------
145
146 bool wxApp::Initialize(int& argc, wxChar **argv)
147 {
148     wxAutoNSAutoreleasePool pool;
149     // Mac OS X passes a process serial number command line argument when
150     // the application is launched from the Finder. This argument must be
151     // removed from the command line arguments before being handled by the
152     // application (otherwise applications would need to handle it)
153     if ( argc > 1 )
154     {
155         static const wxChar *ARG_PSN = _T("-psn_");
156         if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
157         {
158             // remove this argument
159             memmove(argv, argv + 1, argc--);
160         }
161     }
162
163     // Posing must be completed before any instances of the Objective-C
164     // classes being posed as are created.
165     wxPoseAsInitializer::InitializePosers();
166
167     return wxAppBase::Initialize(argc, argv);
168 }
169
170 void wxApp::CleanUp()
171 {
172     wxDC::CocoaShutdownTextSystem();
173
174     wxAppBase::CleanUp();
175 }
176
177 // ----------------------------------------------------------------------------
178 // wxApp creation
179 // ----------------------------------------------------------------------------
180
181 wxApp::wxApp()
182 {
183     m_topWindow = NULL;
184     wxTheApp = this;
185
186     m_isIdle = true;
187 #if WXWIN_COMPATIBILITY_2_2
188     m_wantDebugOutput = TRUE;
189 #endif
190
191     argc = 0;
192     argv = NULL;
193     m_cocoaApp = NULL;
194 }
195
196 void wxApp::CocoaInstallIdleHandler()
197 {
198     wxLogDebug("wxApp::CocoaInstallIdleHandler");
199     m_isIdle = false;
200     // Call doIdle for EVERYTHING dammit
201 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
202     [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
203 }
204
205 bool wxApp::OnInitGui()
206 {
207     wxAutoNSAutoreleasePool pool;
208     if(!wxAppBase::OnInitGui())
209         return FALSE;
210
211     // Create the app using the sharedApplication method
212     m_cocoaApp = [NSApplication sharedApplication];
213     wxDC::CocoaInitializeTextSystem();
214 //    [ m_cocoaApp setDelegate:m_cocoaApp ];
215     #if 0
216     wxLogDebug("Just for kicks");
217     [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
218     wxLogDebug("okay.. done now");
219     #endif
220     return TRUE;
221 }
222
223 bool wxApp::CallOnInit()
224 {
225     wxAutoNSAutoreleasePool pool;
226     return OnInit();
227 }
228
229 bool wxApp::OnInit()
230 {
231     if(!wxAppBase::OnInit())
232         return FALSE;
233
234     return TRUE;
235 }
236
237 bool wxApp::Initialized()
238 {
239   if (GetTopWindow())
240     return TRUE;
241   else
242     return FALSE;
243 }
244
245 int wxApp::MainLoop()
246 {
247     [m_cocoaApp run];
248     return 0;
249 }
250
251 void wxApp::ExitMainLoop()
252 {
253     wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
254 //    CocoaInstallRequestedIdleHandler();
255 //    if(m_isIdle)
256 //        [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
257 // actually.. we WANT the idle event
258 // or not
259 #if 0
260     if(!m_isIdle)
261         [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
262 #endif
263     [m_cocoaApp terminate: m_cocoaApp];
264 }
265
266 // Is a message/event pending?
267 bool wxApp::Pending()
268 {
269     return 0;
270 }
271
272 // Dispatch a message.
273 void wxApp::Dispatch()
274 {
275 }
276
277 // Yield to other processes
278
279 bool wxApp::Yield(bool onlyIfNeeded)
280 {
281     // MT-FIXME
282     static bool s_inYield = false;
283
284 #if wxUSE_LOG
285     // disable log flushing from here because a call to wxYield() shouldn't
286     // normally result in message boxes popping up &c
287     wxLog::Suspend();
288 #endif // wxUSE_LOG
289
290     if (s_inYield)
291     {
292         if ( !onlyIfNeeded )
293         {
294             wxFAIL_MSG( wxT("wxYield called recursively" ) );
295         }
296
297         return false;
298     }
299
300     s_inYield = true;
301
302     wxLogDebug("WARNING: SUPPOSED to have yielded!");
303     // FIXME: Do something!
304
305 #if wxUSE_LOG
306     // let the logs be flashed again
307     wxLog::Resume();
308 #endif // wxUSE_LOG
309
310     s_inYield = false;
311
312     return true;
313 }
314