]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/app.mm
71704b5e2823b693c4d412568aa145adc7a6341d
[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_IDLE(wxApp::OnIdle)
138 // EVT_END_SESSION(wxApp::OnEndSession)
139 // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
140 END_EVENT_TABLE()
141 #endif
142
143 // ----------------------------------------------------------------------------
144 // wxApp initialization/cleanup
145 // ----------------------------------------------------------------------------
146
147 bool wxApp::Initialize(int& argc, wxChar **argv)
148 {
149 wxAutoNSAutoreleasePool pool;
150 // Mac OS X passes a process serial number command line argument when
151 // the application is launched from the Finder. This argument must be
152 // removed from the command line arguments before being handled by the
153 // application (otherwise applications would need to handle it)
154 if ( argc > 1 )
155 {
156 static const wxChar *ARG_PSN = _T("-psn_");
157 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
158 {
159 // remove this argument
160 memmove(argv, argv + 1, argc--);
161 }
162 }
163
164 // Posing must be completed before any instances of the Objective-C
165 // classes being posed as are created.
166 wxPoseAsInitializer::InitializePosers();
167
168 return wxAppBase::Initialize(argc, argv);
169 }
170
171 void wxApp::CleanUp()
172 {
173 wxDC::CocoaShutdownTextSystem();
174
175 wxAppBase::CleanUp();
176 }
177
178 // ----------------------------------------------------------------------------
179 // wxApp creation
180 // ----------------------------------------------------------------------------
181
182 wxApp::wxApp()
183 {
184 m_topWindow = NULL;
185 wxTheApp = this;
186
187 m_isIdle = true;
188 #if WXWIN_COMPATIBILITY_2_2
189 m_wantDebugOutput = TRUE;
190 #endif
191
192 argc = 0;
193 argv = NULL;
194 m_cocoaApp = NULL;
195 }
196
197 void wxApp::CocoaInstallIdleHandler()
198 {
199 // If we're supposed to be stopping, don't add more idle events
200 if(![m_cocoaApp isRunning])
201 return;
202 wxLogDebug("wxApp::CocoaInstallIdleHandler");
203 m_isIdle = false;
204 // Call doIdle for EVERYTHING dammit
205 // We'd need Foundation/NSConnection.h for this next constant, do we need it?
206 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
207 }
208
209 bool wxApp::OnInitGui()
210 {
211 wxAutoNSAutoreleasePool pool;
212 if(!wxAppBase::OnInitGui())
213 return FALSE;
214
215 // Create the app using the sharedApplication method
216 m_cocoaApp = [NSApplication sharedApplication];
217 wxDC::CocoaInitializeTextSystem();
218 // [ m_cocoaApp setDelegate:m_cocoaApp ];
219 #if 0
220 wxLogDebug("Just for kicks");
221 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
222 wxLogDebug("okay.. done now");
223 #endif
224 return TRUE;
225 }
226
227 bool wxApp::CallOnInit()
228 {
229 wxAutoNSAutoreleasePool pool;
230 return OnInit();
231 }
232
233 bool wxApp::OnInit()
234 {
235 if(!wxAppBase::OnInit())
236 return FALSE;
237
238 return TRUE;
239 }
240
241 bool wxApp::Initialized()
242 {
243 if (GetTopWindow())
244 return TRUE;
245 else
246 return FALSE;
247 }
248
249 int wxApp::MainLoop()
250 {
251 [m_cocoaApp run];
252 return 0;
253 }
254
255 void wxApp::ExitMainLoop()
256 {
257 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
258 // CocoaInstallRequestedIdleHandler();
259 // if(m_isIdle)
260 // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
261 // actually.. we WANT the idle event
262 // or not
263 #if 0
264 if(!m_isIdle)
265 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
266 #endif
267 [m_cocoaApp stop: m_cocoaApp];
268 }
269
270 // Is a message/event pending?
271 bool wxApp::Pending()
272 {
273 return 0;
274 }
275
276 // Dispatch a message.
277 void wxApp::Dispatch()
278 {
279 }
280
281 void wxApp::OnIdle(wxIdleEvent& event)
282 {
283 wxLogDebug("wxApp::OnIdle");
284 #if 0
285 static bool s_inOnIdle = FALSE;
286
287 // Avoid recursion (via ProcessEvent default case)
288 if ( s_inOnIdle )
289 return;
290 s_inOnIdle = TRUE;
291 #endif
292
293
294 DeletePendingObjects();
295
296 // flush the logged messages if any
297 wxLog *pLog = wxLog::GetActiveTarget();
298 if ( pLog != NULL && pLog->HasPendingMessages() )
299 pLog->Flush();
300
301 #if 0
302 // Send OnIdle events to all windows
303 bool needMore = SendIdleEvents();
304
305 if (needMore)
306 event.RequestMore(TRUE);
307
308 s_inOnIdle = FALSE;
309 #endif
310 }
311
312 // Yield to other processes
313
314 bool wxApp::Yield(bool onlyIfNeeded)
315 {
316 // MT-FIXME
317 static bool s_inYield = false;
318
319 #if wxUSE_LOG
320 // disable log flushing from here because a call to wxYield() shouldn't
321 // normally result in message boxes popping up &c
322 wxLog::Suspend();
323 #endif // wxUSE_LOG
324
325 if (s_inYield)
326 {
327 if ( !onlyIfNeeded )
328 {
329 wxFAIL_MSG( wxT("wxYield called recursively" ) );
330 }
331
332 return false;
333 }
334
335 s_inYield = true;
336
337 wxLogDebug("WARNING: SUPPOSED to have yielded!");
338 // FIXME: Do something!
339
340 #if wxUSE_LOG
341 // let the logs be flashed again
342 wxLog::Resume();
343 #endif // wxUSE_LOG
344
345 s_inYield = false;
346
347 return true;
348 }
349