]>
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 | |
7 | // RCS-ID: $Id: | |
8 | // Copyright: (c) David Elliott | |
9 | // Licence: wxWindows license | |
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> | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // globals | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
fb896a32 DE |
47 | wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL; |
48 | ||
49 | @interface wxPoserNSApplication : NSApplication | |
50 | { | |
51 | } | |
52 | ||
53 | - (void)doIdle: (id)data; | |
54 | - (void)finishLaunching; | |
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)finishLaunching | |
84 | { | |
85 | wxLogDebug("finishLaunching"); | |
86 | bool initsuccess = wxTheApp->OnInit(); | |
87 | if(!initsuccess) | |
88 | [super stop: NULL]; | |
89 | ||
90 | [super finishLaunching]; | |
91 | } | |
92 | ||
93 | - (void)sendEvent: (NSEvent*)anEvent | |
94 | { | |
95 | wxLogDebug("SendEvent"); | |
96 | wxTheApp->CocoaInstallRequestedIdleHandler(); | |
97 | [super sendEvent: anEvent]; | |
98 | } | |
99 | ||
100 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication | |
101 | { | |
102 | BOOL ret = wxTheApp->GetExitOnFrameDelete(); | |
103 | wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret); | |
104 | return ret; | |
105 | } | |
106 | ||
107 | @end // wxPoserNSApplication | |
108 | WX_IMPLEMENT_POSER(wxPoserNSApplication); | |
109 | ||
110 | // ============================================================================ | |
111 | // functions | |
112 | // ============================================================================ | |
113 | ||
e2478fde | 114 | void wxApp::Exit() |
fb896a32 | 115 | { |
fb896a32 | 116 | wxApp::CleanUp(); |
e2478fde VZ |
117 | |
118 | wxAppConsole::Exit(); | |
fb896a32 DE |
119 | } |
120 | ||
121 | // ============================================================================ | |
122 | // wxApp implementation | |
123 | // ============================================================================ | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // wxApp Static member initialization | |
127 | // ---------------------------------------------------------------------------- | |
fb896a32 DE |
128 | |
129 | #if !USE_SHARED_LIBRARY | |
130 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
131 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
132 | EVT_IDLE(wxApp::OnIdle) | |
133 | // EVT_END_SESSION(wxApp::OnEndSession) | |
134 | // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
135 | END_EVENT_TABLE() | |
136 | #endif | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
05e2b077 | 139 | // wxApp initialization/cleanup |
fb896a32 | 140 | // ---------------------------------------------------------------------------- |
94826170 | 141 | |
05e2b077 | 142 | bool wxApp::Initialize(int& argc, wxChar **argv) |
fb896a32 | 143 | { |
05e2b077 VZ |
144 | // Mac OS X passes a process serial number command line argument when |
145 | // the application is launched from the Finder. This argument must be | |
146 | // removed from the command line arguments before being handled by the | |
147 | // application (otherwise applications would need to handle it) | |
148 | if ( argc > 1 ) | |
149 | { | |
150 | static const wxChar *ARG_PSN = _T("-psn_"); | |
151 | if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 ) | |
152 | { | |
153 | // remove this argument | |
154 | memmove(argv, argv + 1, argc--); | |
155 | } | |
156 | } | |
157 | ||
28ce3086 DE |
158 | // Posing must be completed before any instances of the Objective-C |
159 | // classes being posed as are created. | |
fb896a32 | 160 | wxPoseAsInitializer::InitializePosers(); |
fb896a32 | 161 | |
94826170 | 162 | return wxAppBase::Initialize(argc, argv); |
fb896a32 DE |
163 | } |
164 | ||
94826170 | 165 | void wxApp::CleanUp() |
fb896a32 | 166 | { |
891d0563 | 167 | wxDC::CocoaShutdownTextSystem(); |
94826170 VZ |
168 | |
169 | wxAppBase::CleanUp(); | |
fb896a32 DE |
170 | } |
171 | ||
172 | // ---------------------------------------------------------------------------- | |
173 | // wxApp creation | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
176 | wxApp::wxApp() | |
177 | { | |
178 | m_topWindow = NULL; | |
179 | wxTheApp = this; | |
180 | ||
181 | m_isIdle = true; | |
182 | #if WXWIN_COMPATIBILITY_2_2 | |
183 | m_wantDebugOutput = TRUE; | |
184 | #endif | |
185 | ||
186 | argc = 0; | |
187 | argv = NULL; | |
188 | m_cocoaApp = NULL; | |
189 | } | |
190 | ||
191 | void wxApp::CocoaInstallIdleHandler() | |
192 | { | |
193 | wxLogDebug("wxApp::CocoaInstallIdleHandler"); | |
194 | m_isIdle = false; | |
195 | // Call doIdle for EVERYTHING dammit | |
196 | // We'd need Foundation/NSConnection.h for this next constant, do we need it? | |
197 | [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ]; | |
198 | } | |
199 | ||
200 | bool wxApp::OnInitGui() | |
201 | { | |
202 | if(!wxAppBase::OnInitGui()) | |
203 | return FALSE; | |
204 | ||
205 | // Create the app using the sharedApplication method | |
206 | m_cocoaApp = [NSApplication sharedApplication]; | |
891d0563 | 207 | wxDC::CocoaInitializeTextSystem(); |
fb896a32 DE |
208 | // [ m_cocoaApp setDelegate:m_cocoaApp ]; |
209 | #if 0 | |
210 | wxLogDebug("Just for kicks"); | |
211 | [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ]; | |
212 | wxLogDebug("okay.. done now"); | |
213 | #endif | |
214 | return TRUE; | |
215 | } | |
216 | ||
217 | bool wxApp::OnInit() | |
218 | { | |
219 | if(!wxAppBase::OnInit()) | |
220 | return FALSE; | |
221 | ||
222 | return TRUE; | |
223 | } | |
224 | ||
225 | bool wxApp::Initialized() | |
226 | { | |
227 | if (GetTopWindow()) | |
228 | return TRUE; | |
229 | else | |
230 | return FALSE; | |
231 | } | |
232 | ||
233 | int wxApp::MainLoop() | |
234 | { | |
235 | [m_cocoaApp run]; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | // Returns TRUE if more time is needed. | |
240 | bool wxApp::ProcessIdle() | |
241 | { | |
242 | wxIdleEvent event; | |
243 | event.SetEventObject(this); | |
244 | ProcessEvent(event); | |
245 | ||
246 | return event.MoreRequested(); | |
247 | } | |
248 | ||
249 | void wxApp::ExitMainLoop() | |
250 | { | |
251 | wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]); | |
252 | // CocoaInstallRequestedIdleHandler(); | |
253 | // if(m_isIdle) | |
254 | // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ]; | |
255 | // actually.. we WANT the idle event | |
256 | // or not | |
257 | #if 0 | |
258 | if(!m_isIdle) | |
259 | [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL]; | |
260 | #endif | |
261 | [m_cocoaApp terminate: m_cocoaApp]; | |
262 | } | |
263 | ||
264 | // Is a message/event pending? | |
265 | bool wxApp::Pending() | |
266 | { | |
267 | return 0; | |
268 | } | |
269 | ||
270 | // Dispatch a message. | |
271 | void wxApp::Dispatch() | |
272 | { | |
273 | } | |
274 | ||
275 | void wxApp::OnIdle(wxIdleEvent& event) | |
276 | { | |
277 | wxLogDebug("wxApp::OnIdle"); | |
278 | static bool s_inOnIdle = FALSE; | |
279 | ||
280 | // Avoid recursion (via ProcessEvent default case) | |
281 | if ( s_inOnIdle ) | |
282 | return; | |
283 | s_inOnIdle = TRUE; | |
284 | ||
285 | ||
286 | DeletePendingObjects(); | |
287 | ||
288 | // flush the logged messages if any | |
289 | wxLog *pLog = wxLog::GetActiveTarget(); | |
290 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
291 | pLog->Flush(); | |
292 | ||
293 | // Send OnIdle events to all windows | |
294 | bool needMore = SendIdleEvents(); | |
295 | ||
296 | if (needMore) | |
297 | event.RequestMore(TRUE); | |
298 | ||
299 | s_inOnIdle = FALSE; | |
300 | } | |
301 | ||
302 | // Send idle event to all top-level windows | |
303 | bool wxApp::SendIdleEvents() | |
304 | { | |
305 | bool needMore = FALSE; | |
306 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
307 | while (node) | |
308 | { | |
309 | wxWindow* win = node->GetData(); | |
310 | if (SendIdleEvents(win)) | |
311 | needMore = TRUE; | |
312 | ||
313 | node = node->GetNext(); | |
314 | } | |
315 | return needMore; | |
316 | } | |
317 | ||
318 | // Send idle event to window and all subwindows | |
319 | bool wxApp::SendIdleEvents(wxWindow* win) | |
320 | { | |
321 | // wxLogDebug("SendIdleEvents win=%p",win); | |
322 | bool needMore = FALSE; | |
323 | ||
324 | wxIdleEvent event; | |
325 | event.SetEventObject(win); | |
326 | win->ProcessEvent(event); | |
327 | ||
328 | if (event.MoreRequested()) | |
329 | needMore = TRUE; | |
330 | ||
331 | wxWindowList::Node* node = win->GetChildren().GetFirst(); | |
332 | while (node) | |
333 | { | |
334 | // wxLogDebug("child=%p",node->Data()); | |
335 | wxWindow* win = node->GetData(); | |
336 | if (SendIdleEvents(win)) | |
337 | needMore = TRUE; | |
338 | ||
339 | node = node->GetNext(); | |
340 | } | |
341 | return needMore; | |
342 | } | |
343 | ||
344 | // Yield to other processes | |
345 | ||
346 | bool wxApp::Yield(bool onlyIfNeeded) | |
347 | { | |
348 | // MT-FIXME | |
349 | static bool s_inYield = false; | |
350 | ||
351 | #if wxUSE_LOG | |
352 | // disable log flushing from here because a call to wxYield() shouldn't | |
353 | // normally result in message boxes popping up &c | |
354 | wxLog::Suspend(); | |
355 | #endif // wxUSE_LOG | |
356 | ||
357 | if (s_inYield) | |
358 | { | |
359 | if ( !onlyIfNeeded ) | |
360 | { | |
361 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
362 | } | |
363 | ||
364 | return false; | |
365 | } | |
366 | ||
367 | s_inYield = true; | |
368 | ||
369 | wxLogDebug("WARNING: SUPPOSED to have yielded!"); | |
370 | // FIXME: Do something! | |
371 | ||
372 | #if wxUSE_LOG | |
373 | // let the logs be flashed again | |
374 | wxLog::Resume(); | |
375 | #endif // wxUSE_LOG | |
376 | ||
377 | s_inYield = false; | |
378 | ||
379 | return true; | |
380 | } | |
381 |