]>
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 | 33 | #include "wx/cocoa/ObjcPose.h" |
493902ac | 34 | #include "wx/cocoa/autorelease.h" |
fb45bb1f | 35 | |
fb896a32 DE |
36 | #if wxUSE_WX_RESOURCES |
37 | # include "wx/resource.h" | |
38 | #endif | |
39 | ||
40 | #import <AppKit/NSApplication.h> | |
41 | #import <Foundation/NSRunLoop.h> | |
42 | #import <Foundation/NSArray.h> | |
47f1ad6a | 43 | #import <Foundation/NSAutoreleasePool.h> |
14fc7eb4 | 44 | #import <Foundation/NSThread.h> |
fb896a32 DE |
45 | |
46 | // ---------------------------------------------------------------------------- | |
47 | // globals | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
fb896a32 DE |
50 | wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL; |
51 | ||
52 | @interface wxPoserNSApplication : NSApplication | |
53 | { | |
54 | } | |
55 | ||
56 | - (void)doIdle: (id)data; | |
fb896a32 DE |
57 | - (void)sendEvent: (NSEvent*)anEvent; |
58 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication; | |
59 | @end // wxPoserNSApplication | |
60 | ||
61 | @implementation wxPoserNSApplication : NSApplication | |
62 | ||
63 | - (void)doIdle: (id)data | |
64 | { | |
65 | wxASSERT(wxTheApp); | |
66 | wxLogDebug("doIdle called"); | |
b93d8cc4 DE |
67 | #ifdef __WXDEBUG__ |
68 | if(wxTheApp->IsInAssert()) | |
fb896a32 | 69 | { |
b93d8cc4 DE |
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 | |
fb896a32 | 78 | { |
b93d8cc4 DE |
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 | |
fb896a32 | 87 | } |
fb896a32 DE |
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 | ||
fb896a32 DE |
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 | WX_IMPLEMENT_POSER(wxPoserNSApplication); | |
110 | ||
111 | // ============================================================================ | |
112 | // functions | |
113 | // ============================================================================ | |
114 | ||
e2478fde | 115 | void wxApp::Exit() |
fb896a32 | 116 | { |
fb896a32 | 117 | wxApp::CleanUp(); |
e2478fde VZ |
118 | |
119 | wxAppConsole::Exit(); | |
fb896a32 DE |
120 | } |
121 | ||
122 | // ============================================================================ | |
123 | // wxApp implementation | |
124 | // ============================================================================ | |
125 | ||
126 | // ---------------------------------------------------------------------------- | |
127 | // wxApp Static member initialization | |
128 | // ---------------------------------------------------------------------------- | |
fb896a32 DE |
129 | |
130 | #if !USE_SHARED_LIBRARY | |
131 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
132 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
955a9197 | 133 | EVT_IDLE(wxAppBase::OnIdle) |
fb896a32 DE |
134 | // EVT_END_SESSION(wxApp::OnEndSession) |
135 | // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
136 | END_EVENT_TABLE() | |
137 | #endif | |
138 | ||
139 | // ---------------------------------------------------------------------------- | |
05e2b077 | 140 | // wxApp initialization/cleanup |
fb896a32 | 141 | // ---------------------------------------------------------------------------- |
94826170 | 142 | |
05e2b077 | 143 | bool wxApp::Initialize(int& argc, wxChar **argv) |
fb896a32 | 144 | { |
47f1ad6a | 145 | wxAutoNSAutoreleasePool pool; |
14fc7eb4 | 146 | m_cocoaMainThread = [NSThread currentThread]; |
05e2b077 VZ |
147 | // Mac OS X passes a process serial number command line argument when |
148 | // the application is launched from the Finder. This argument must be | |
149 | // removed from the command line arguments before being handled by the | |
150 | // application (otherwise applications would need to handle it) | |
151 | if ( argc > 1 ) | |
152 | { | |
153 | static const wxChar *ARG_PSN = _T("-psn_"); | |
154 | if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 ) | |
155 | { | |
156 | // remove this argument | |
157 | memmove(argv, argv + 1, argc--); | |
158 | } | |
159 | } | |
160 | ||
28ce3086 DE |
161 | // Posing must be completed before any instances of the Objective-C |
162 | // classes being posed as are created. | |
fb896a32 | 163 | wxPoseAsInitializer::InitializePosers(); |
fb896a32 | 164 | |
94826170 | 165 | return wxAppBase::Initialize(argc, argv); |
fb896a32 DE |
166 | } |
167 | ||
94826170 | 168 | void wxApp::CleanUp() |
fb896a32 | 169 | { |
891d0563 | 170 | wxDC::CocoaShutdownTextSystem(); |
94826170 VZ |
171 | |
172 | wxAppBase::CleanUp(); | |
fb896a32 DE |
173 | } |
174 | ||
175 | // ---------------------------------------------------------------------------- | |
176 | // wxApp creation | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
179 | wxApp::wxApp() | |
180 | { | |
181 | m_topWindow = NULL; | |
fb896a32 DE |
182 | |
183 | m_isIdle = true; | |
184 | #if WXWIN_COMPATIBILITY_2_2 | |
185 | m_wantDebugOutput = TRUE; | |
186 | #endif | |
b93d8cc4 DE |
187 | #ifdef __WXDEBUG__ |
188 | m_isInAssert = FALSE; | |
189 | #endif // __WXDEBUG__ | |
190 | ||
fb896a32 DE |
191 | |
192 | argc = 0; | |
193 | argv = NULL; | |
194 | m_cocoaApp = NULL; | |
195 | } | |
196 | ||
197 | void wxApp::CocoaInstallIdleHandler() | |
198 | { | |
14fc7eb4 DE |
199 | // If we're not the main thread, don't install the idle handler |
200 | if(m_cocoaMainThread != [NSThread currentThread]) | |
201 | { | |
202 | wxLogDebug("Attempt to install idle handler from secondary thread"); | |
203 | return; | |
204 | } | |
bc34fa26 DE |
205 | // If we're supposed to be stopping, don't add more idle events |
206 | if(![m_cocoaApp isRunning]) | |
207 | return; | |
fb896a32 DE |
208 | wxLogDebug("wxApp::CocoaInstallIdleHandler"); |
209 | m_isIdle = false; | |
210 | // Call doIdle for EVERYTHING dammit | |
211 | // We'd need Foundation/NSConnection.h for this next constant, do we need it? | |
212 | [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ]; | |
213 | } | |
214 | ||
215 | bool wxApp::OnInitGui() | |
216 | { | |
47f1ad6a | 217 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
218 | if(!wxAppBase::OnInitGui()) |
219 | return FALSE; | |
220 | ||
221 | // Create the app using the sharedApplication method | |
222 | m_cocoaApp = [NSApplication sharedApplication]; | |
891d0563 | 223 | wxDC::CocoaInitializeTextSystem(); |
fb896a32 DE |
224 | // [ m_cocoaApp setDelegate:m_cocoaApp ]; |
225 | #if 0 | |
226 | wxLogDebug("Just for kicks"); | |
227 | [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ]; | |
228 | wxLogDebug("okay.. done now"); | |
229 | #endif | |
230 | return TRUE; | |
231 | } | |
232 | ||
47f1ad6a DE |
233 | bool wxApp::CallOnInit() |
234 | { | |
bd3e8827 | 235 | // wxAutoNSAutoreleasePool pool; |
47f1ad6a DE |
236 | return OnInit(); |
237 | } | |
238 | ||
fb896a32 DE |
239 | bool wxApp::OnInit() |
240 | { | |
241 | if(!wxAppBase::OnInit()) | |
242 | return FALSE; | |
243 | ||
244 | return TRUE; | |
245 | } | |
246 | ||
247 | bool wxApp::Initialized() | |
248 | { | |
249 | if (GetTopWindow()) | |
250 | return TRUE; | |
251 | else | |
252 | return FALSE; | |
253 | } | |
254 | ||
255 | int wxApp::MainLoop() | |
256 | { | |
257 | [m_cocoaApp run]; | |
258 | return 0; | |
259 | } | |
260 | ||
fb896a32 DE |
261 | void wxApp::ExitMainLoop() |
262 | { | |
263 | wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]); | |
264 | // CocoaInstallRequestedIdleHandler(); | |
265 | // if(m_isIdle) | |
266 | // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ]; | |
267 | // actually.. we WANT the idle event | |
268 | // or not | |
269 | #if 0 | |
270 | if(!m_isIdle) | |
271 | [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL]; | |
272 | #endif | |
bc34fa26 | 273 | [m_cocoaApp stop: m_cocoaApp]; |
fb896a32 DE |
274 | } |
275 | ||
276 | // Is a message/event pending? | |
277 | bool wxApp::Pending() | |
278 | { | |
279 | return 0; | |
280 | } | |
281 | ||
282 | // Dispatch a message. | |
283 | void wxApp::Dispatch() | |
284 | { | |
285 | } | |
286 | ||
fb896a32 DE |
287 | // Yield to other processes |
288 | ||
289 | bool wxApp::Yield(bool onlyIfNeeded) | |
290 | { | |
291 | // MT-FIXME | |
292 | static bool s_inYield = false; | |
293 | ||
294 | #if wxUSE_LOG | |
295 | // disable log flushing from here because a call to wxYield() shouldn't | |
296 | // normally result in message boxes popping up &c | |
297 | wxLog::Suspend(); | |
298 | #endif // wxUSE_LOG | |
299 | ||
300 | if (s_inYield) | |
301 | { | |
302 | if ( !onlyIfNeeded ) | |
303 | { | |
304 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
305 | } | |
306 | ||
307 | return false; | |
308 | } | |
309 | ||
310 | s_inYield = true; | |
311 | ||
312 | wxLogDebug("WARNING: SUPPOSED to have yielded!"); | |
313 | // FIXME: Do something! | |
314 | ||
315 | #if wxUSE_LOG | |
316 | // let the logs be flashed again | |
317 | wxLog::Resume(); | |
318 | #endif // wxUSE_LOG | |
319 | ||
320 | s_inYield = false; | |
321 | ||
322 | return true; | |
323 | } | |
324 | ||
b93d8cc4 DE |
325 | #ifdef __WXDEBUG__ |
326 | void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg) | |
327 | { | |
328 | m_isInAssert = TRUE; | |
329 | wxAppBase::OnAssert(file, line, cond, msg); | |
330 | m_isInAssert = FALSE; | |
331 | } | |
332 | #endif // __WXDEBUG__ | |
333 |