]>
Commit | Line | Data |
---|---|---|
fb896a32 DE |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: cocoa/app.mm | |
3 | // Purpose: wxApp | |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2002/11/27 | |
d89d4059 | 7 | // RCS-ID: $Id$ |
fb896a32 | 8 | // Copyright: (c) David Elliott |
d89d4059 | 9 | // Licence: wxWindows licence |
fb896a32 DE |
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" | |
891d0563 | 26 | #include "wx/dc.h" |
fb896a32 DE |
27 | #include "wx/intl.h" |
28 | #include "wx/log.h" | |
fb896a32 DE |
29 | #endif |
30 | ||
11c08416 DE |
31 | #include "wx/module.h" |
32 | ||
fb45bb1f DE |
33 | #include "wx/cocoa/ObjcPose.h" |
34 | ||
fb896a32 DE |
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> | |
47f1ad6a | 42 | #import <Foundation/NSAutoreleasePool.h> |
fb896a32 DE |
43 | |
44 | // ---------------------------------------------------------------------------- | |
45 | // globals | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
fb896a32 DE |
48 | wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL; |
49 | ||
50 | @interface wxPoserNSApplication : NSApplication | |
51 | { | |
52 | } | |
53 | ||
54 | - (void)doIdle: (id)data; | |
fb896a32 DE |
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 | ||
fb896a32 DE |
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 | ||
47f1ad6a DE |
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 | ||
fb896a32 DE |
115 | // ============================================================================ |
116 | // functions | |
117 | // ============================================================================ | |
118 | ||
e2478fde | 119 | void wxApp::Exit() |
fb896a32 | 120 | { |
fb896a32 | 121 | wxApp::CleanUp(); |
e2478fde VZ |
122 | |
123 | wxAppConsole::Exit(); | |
fb896a32 DE |
124 | } |
125 | ||
126 | // ============================================================================ | |
127 | // wxApp implementation | |
128 | // ============================================================================ | |
129 | ||
130 | // ---------------------------------------------------------------------------- | |
131 | // wxApp Static member initialization | |
132 | // ---------------------------------------------------------------------------- | |
fb896a32 DE |
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 | // ---------------------------------------------------------------------------- | |
05e2b077 | 144 | // wxApp initialization/cleanup |
fb896a32 | 145 | // ---------------------------------------------------------------------------- |
94826170 | 146 | |
05e2b077 | 147 | bool wxApp::Initialize(int& argc, wxChar **argv) |
fb896a32 | 148 | { |
47f1ad6a | 149 | wxAutoNSAutoreleasePool pool; |
05e2b077 VZ |
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 | ||
28ce3086 DE |
164 | // Posing must be completed before any instances of the Objective-C |
165 | // classes being posed as are created. | |
fb896a32 | 166 | wxPoseAsInitializer::InitializePosers(); |
fb896a32 | 167 | |
94826170 | 168 | return wxAppBase::Initialize(argc, argv); |
fb896a32 DE |
169 | } |
170 | ||
94826170 | 171 | void wxApp::CleanUp() |
fb896a32 | 172 | { |
891d0563 | 173 | wxDC::CocoaShutdownTextSystem(); |
94826170 VZ |
174 | |
175 | wxAppBase::CleanUp(); | |
fb896a32 DE |
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 | wxLogDebug("wxApp::CocoaInstallIdleHandler"); | |
200 | m_isIdle = false; | |
201 | // Call doIdle for EVERYTHING dammit | |
202 | // We'd need Foundation/NSConnection.h for this next constant, do we need it? | |
203 | [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ]; | |
204 | } | |
205 | ||
206 | bool wxApp::OnInitGui() | |
207 | { | |
47f1ad6a | 208 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
209 | if(!wxAppBase::OnInitGui()) |
210 | return FALSE; | |
211 | ||
212 | // Create the app using the sharedApplication method | |
213 | m_cocoaApp = [NSApplication sharedApplication]; | |
891d0563 | 214 | wxDC::CocoaInitializeTextSystem(); |
fb896a32 DE |
215 | // [ m_cocoaApp setDelegate:m_cocoaApp ]; |
216 | #if 0 | |
217 | wxLogDebug("Just for kicks"); | |
218 | [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ]; | |
219 | wxLogDebug("okay.. done now"); | |
220 | #endif | |
221 | return TRUE; | |
222 | } | |
223 | ||
47f1ad6a DE |
224 | bool wxApp::CallOnInit() |
225 | { | |
226 | wxAutoNSAutoreleasePool pool; | |
227 | return OnInit(); | |
228 | } | |
229 | ||
fb896a32 DE |
230 | bool wxApp::OnInit() |
231 | { | |
232 | if(!wxAppBase::OnInit()) | |
233 | return FALSE; | |
234 | ||
235 | return TRUE; | |
236 | } | |
237 | ||
238 | bool wxApp::Initialized() | |
239 | { | |
240 | if (GetTopWindow()) | |
241 | return TRUE; | |
242 | else | |
243 | return FALSE; | |
244 | } | |
245 | ||
246 | int wxApp::MainLoop() | |
247 | { | |
248 | [m_cocoaApp run]; | |
249 | return 0; | |
250 | } | |
251 | ||
252 | // Returns TRUE if more time is needed. | |
253 | bool wxApp::ProcessIdle() | |
254 | { | |
255 | wxIdleEvent event; | |
256 | event.SetEventObject(this); | |
257 | ProcessEvent(event); | |
258 | ||
0b30bb0b JS |
259 | wxUpdateUIEvent::ResetUpdateTime(); |
260 | ||
fb896a32 DE |
261 | return event.MoreRequested(); |
262 | } | |
263 | ||
264 | void wxApp::ExitMainLoop() | |
265 | { | |
266 | wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]); | |
267 | // CocoaInstallRequestedIdleHandler(); | |
268 | // if(m_isIdle) | |
269 | // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ]; | |
270 | // actually.. we WANT the idle event | |
271 | // or not | |
272 | #if 0 | |
273 | if(!m_isIdle) | |
274 | [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL]; | |
275 | #endif | |
276 | [m_cocoaApp terminate: m_cocoaApp]; | |
277 | } | |
278 | ||
279 | // Is a message/event pending? | |
280 | bool wxApp::Pending() | |
281 | { | |
282 | return 0; | |
283 | } | |
284 | ||
285 | // Dispatch a message. | |
286 | void wxApp::Dispatch() | |
287 | { | |
288 | } | |
289 | ||
290 | void wxApp::OnIdle(wxIdleEvent& event) | |
291 | { | |
292 | wxLogDebug("wxApp::OnIdle"); | |
293 | static bool s_inOnIdle = FALSE; | |
294 | ||
295 | // Avoid recursion (via ProcessEvent default case) | |
296 | if ( s_inOnIdle ) | |
297 | return; | |
298 | s_inOnIdle = TRUE; | |
299 | ||
300 | ||
301 | DeletePendingObjects(); | |
302 | ||
303 | // flush the logged messages if any | |
304 | wxLog *pLog = wxLog::GetActiveTarget(); | |
305 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
306 | pLog->Flush(); | |
307 | ||
308 | // Send OnIdle events to all windows | |
309 | bool needMore = SendIdleEvents(); | |
310 | ||
311 | if (needMore) | |
312 | event.RequestMore(TRUE); | |
313 | ||
314 | s_inOnIdle = FALSE; | |
315 | } | |
316 | ||
317 | // Send idle event to all top-level windows | |
318 | bool wxApp::SendIdleEvents() | |
319 | { | |
320 | bool needMore = FALSE; | |
321 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
322 | while (node) | |
323 | { | |
324 | wxWindow* win = node->GetData(); | |
325 | if (SendIdleEvents(win)) | |
326 | needMore = TRUE; | |
327 | ||
328 | node = node->GetNext(); | |
329 | } | |
330 | return needMore; | |
331 | } | |
332 | ||
333 | // Send idle event to window and all subwindows | |
334 | bool wxApp::SendIdleEvents(wxWindow* win) | |
335 | { | |
336 | // wxLogDebug("SendIdleEvents win=%p",win); | |
337 | bool needMore = FALSE; | |
338 | ||
339 | wxIdleEvent event; | |
340 | event.SetEventObject(win); | |
341 | win->ProcessEvent(event); | |
342 | ||
343 | if (event.MoreRequested()) | |
344 | needMore = TRUE; | |
345 | ||
346 | wxWindowList::Node* node = win->GetChildren().GetFirst(); | |
347 | while (node) | |
348 | { | |
349 | // wxLogDebug("child=%p",node->Data()); | |
350 | wxWindow* win = node->GetData(); | |
351 | if (SendIdleEvents(win)) | |
352 | needMore = TRUE; | |
353 | ||
354 | node = node->GetNext(); | |
355 | } | |
356 | return needMore; | |
357 | } | |
358 | ||
359 | // Yield to other processes | |
360 | ||
361 | bool wxApp::Yield(bool onlyIfNeeded) | |
362 | { | |
363 | // MT-FIXME | |
364 | static bool s_inYield = false; | |
365 | ||
366 | #if wxUSE_LOG | |
367 | // disable log flushing from here because a call to wxYield() shouldn't | |
368 | // normally result in message boxes popping up &c | |
369 | wxLog::Suspend(); | |
370 | #endif // wxUSE_LOG | |
371 | ||
372 | if (s_inYield) | |
373 | { | |
374 | if ( !onlyIfNeeded ) | |
375 | { | |
376 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
377 | } | |
378 | ||
379 | return false; | |
380 | } | |
381 | ||
382 | s_inYield = true; | |
383 | ||
384 | wxLogDebug("WARNING: SUPPOSED to have yielded!"); | |
385 | // FIXME: Do something! | |
386 | ||
387 | #if wxUSE_LOG | |
388 | // let the logs be flashed again | |
389 | wxLog::Resume(); | |
390 | #endif // wxUSE_LOG | |
391 | ||
392 | s_inYield = false; | |
393 | ||
394 | return true; | |
395 | } | |
396 |