]>
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" |
af367f46 | 35 | #include "wx/cocoa/mbarman.h" |
fb45bb1f | 36 | |
fb896a32 DE |
37 | #if wxUSE_WX_RESOURCES |
38 | # include "wx/resource.h" | |
39 | #endif | |
40 | ||
41 | #import <AppKit/NSApplication.h> | |
42 | #import <Foundation/NSRunLoop.h> | |
43 | #import <Foundation/NSArray.h> | |
47f1ad6a | 44 | #import <Foundation/NSAutoreleasePool.h> |
14fc7eb4 | 45 | #import <Foundation/NSThread.h> |
aaa5ab05 | 46 | #import <AppKit/NSEvent.h> |
fb896a32 DE |
47 | |
48 | // ---------------------------------------------------------------------------- | |
49 | // globals | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
fb896a32 DE |
52 | wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL; |
53 | ||
54 | @interface wxPoserNSApplication : NSApplication | |
55 | { | |
56 | } | |
57 | ||
58 | - (void)doIdle: (id)data; | |
fb896a32 DE |
59 | - (void)sendEvent: (NSEvent*)anEvent; |
60 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication; | |
61 | @end // wxPoserNSApplication | |
62 | ||
63 | @implementation wxPoserNSApplication : NSApplication | |
64 | ||
65 | - (void)doIdle: (id)data | |
66 | { | |
67 | wxASSERT(wxTheApp); | |
af367f46 DE |
68 | wxASSERT(wxMenuBarManager::GetInstance()); |
69 | wxMenuBarManager::GetInstance()->CocoaInternalIdle(); | |
fb896a32 | 70 | wxLogDebug("doIdle called"); |
b93d8cc4 DE |
71 | #ifdef __WXDEBUG__ |
72 | if(wxTheApp->IsInAssert()) | |
fb896a32 | 73 | { |
b93d8cc4 DE |
74 | wxLogDebug("Idle events ignored durring assertion dialog"); |
75 | } | |
76 | else | |
77 | #endif | |
78 | { | |
79 | NSRunLoop *rl = [NSRunLoop currentRunLoop]; | |
80 | // runMode: beforeDate returns YES if something was done | |
81 | while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING | |
fb896a32 | 82 | { |
b93d8cc4 DE |
83 | wxLogDebug("Looping for idle events"); |
84 | #if 1 | |
85 | if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]]) | |
86 | { | |
87 | wxLogDebug("Found actual work to do"); | |
88 | break; | |
89 | } | |
90 | #endif | |
fb896a32 | 91 | } |
fb896a32 DE |
92 | } |
93 | wxLogDebug("Idle processing complete, requesting next idle event"); | |
94 | // Add ourself back into the run loop (on next event) if necessary | |
95 | wxTheApp->CocoaRequestIdle(); | |
96 | } | |
97 | ||
fb896a32 DE |
98 | - (void)sendEvent: (NSEvent*)anEvent |
99 | { | |
100 | wxLogDebug("SendEvent"); | |
101 | wxTheApp->CocoaInstallRequestedIdleHandler(); | |
102 | [super sendEvent: anEvent]; | |
103 | } | |
104 | ||
105 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication | |
106 | { | |
107 | BOOL ret = wxTheApp->GetExitOnFrameDelete(); | |
108 | wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret); | |
109 | return ret; | |
110 | } | |
111 | ||
112 | @end // wxPoserNSApplication | |
113 | WX_IMPLEMENT_POSER(wxPoserNSApplication); | |
114 | ||
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) | |
955a9197 | 137 | EVT_IDLE(wxAppBase::OnIdle) |
fb896a32 DE |
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; |
14fc7eb4 | 150 | m_cocoaMainThread = [NSThread currentThread]; |
05e2b077 VZ |
151 | // Mac OS X passes a process serial number command line argument when |
152 | // the application is launched from the Finder. This argument must be | |
153 | // removed from the command line arguments before being handled by the | |
154 | // application (otherwise applications would need to handle it) | |
155 | if ( argc > 1 ) | |
156 | { | |
157 | static const wxChar *ARG_PSN = _T("-psn_"); | |
158 | if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 ) | |
159 | { | |
160 | // remove this argument | |
161 | memmove(argv, argv + 1, argc--); | |
162 | } | |
163 | } | |
164 | ||
28ce3086 DE |
165 | // Posing must be completed before any instances of the Objective-C |
166 | // classes being posed as are created. | |
fb896a32 | 167 | wxPoseAsInitializer::InitializePosers(); |
fb896a32 | 168 | |
94826170 | 169 | return wxAppBase::Initialize(argc, argv); |
fb896a32 DE |
170 | } |
171 | ||
94826170 | 172 | void wxApp::CleanUp() |
fb896a32 | 173 | { |
891d0563 | 174 | wxDC::CocoaShutdownTextSystem(); |
af367f46 | 175 | wxMenuBarManager::DestroyInstance(); |
94826170 VZ |
176 | |
177 | wxAppBase::CleanUp(); | |
fb896a32 DE |
178 | } |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // wxApp creation | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | wxApp::wxApp() | |
185 | { | |
186 | m_topWindow = NULL; | |
fb896a32 DE |
187 | |
188 | m_isIdle = true; | |
189 | #if WXWIN_COMPATIBILITY_2_2 | |
190 | m_wantDebugOutput = TRUE; | |
191 | #endif | |
b93d8cc4 DE |
192 | #ifdef __WXDEBUG__ |
193 | m_isInAssert = FALSE; | |
194 | #endif // __WXDEBUG__ | |
195 | ||
fb896a32 DE |
196 | argc = 0; |
197 | argv = NULL; | |
198 | m_cocoaApp = NULL; | |
199 | } | |
200 | ||
201 | void wxApp::CocoaInstallIdleHandler() | |
202 | { | |
14fc7eb4 DE |
203 | // If we're not the main thread, don't install the idle handler |
204 | if(m_cocoaMainThread != [NSThread currentThread]) | |
205 | { | |
206 | wxLogDebug("Attempt to install idle handler from secondary thread"); | |
207 | return; | |
208 | } | |
bc34fa26 DE |
209 | // If we're supposed to be stopping, don't add more idle events |
210 | if(![m_cocoaApp isRunning]) | |
211 | return; | |
fb896a32 DE |
212 | wxLogDebug("wxApp::CocoaInstallIdleHandler"); |
213 | m_isIdle = false; | |
214 | // Call doIdle for EVERYTHING dammit | |
215 | // We'd need Foundation/NSConnection.h for this next constant, do we need it? | |
216 | [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ]; | |
217 | } | |
218 | ||
219 | bool wxApp::OnInitGui() | |
220 | { | |
47f1ad6a | 221 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
222 | if(!wxAppBase::OnInitGui()) |
223 | return FALSE; | |
224 | ||
225 | // Create the app using the sharedApplication method | |
226 | m_cocoaApp = [NSApplication sharedApplication]; | |
af367f46 DE |
227 | |
228 | wxMenuBarManager::CreateInstance(); | |
229 | ||
891d0563 | 230 | wxDC::CocoaInitializeTextSystem(); |
fb896a32 DE |
231 | // [ m_cocoaApp setDelegate:m_cocoaApp ]; |
232 | #if 0 | |
233 | wxLogDebug("Just for kicks"); | |
234 | [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ]; | |
235 | wxLogDebug("okay.. done now"); | |
236 | #endif | |
237 | return TRUE; | |
238 | } | |
239 | ||
47f1ad6a DE |
240 | bool wxApp::CallOnInit() |
241 | { | |
bd3e8827 | 242 | // wxAutoNSAutoreleasePool pool; |
47f1ad6a DE |
243 | return OnInit(); |
244 | } | |
245 | ||
fb896a32 DE |
246 | bool wxApp::OnInit() |
247 | { | |
248 | if(!wxAppBase::OnInit()) | |
249 | return FALSE; | |
250 | ||
251 | return TRUE; | |
252 | } | |
253 | ||
254 | bool wxApp::Initialized() | |
255 | { | |
256 | if (GetTopWindow()) | |
257 | return TRUE; | |
258 | else | |
259 | return FALSE; | |
260 | } | |
261 | ||
262 | int wxApp::MainLoop() | |
263 | { | |
264 | [m_cocoaApp run]; | |
265 | return 0; | |
266 | } | |
267 | ||
fb896a32 DE |
268 | void wxApp::ExitMainLoop() |
269 | { | |
270 | wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]); | |
271 | // CocoaInstallRequestedIdleHandler(); | |
272 | // if(m_isIdle) | |
273 | // [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ]; | |
274 | // actually.. we WANT the idle event | |
275 | // or not | |
276 | #if 0 | |
277 | if(!m_isIdle) | |
278 | [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL]; | |
279 | #endif | |
bc34fa26 | 280 | [m_cocoaApp stop: m_cocoaApp]; |
fb896a32 DE |
281 | } |
282 | ||
283 | // Is a message/event pending? | |
284 | bool wxApp::Pending() | |
285 | { | |
286 | return 0; | |
287 | } | |
288 | ||
289 | // Dispatch a message. | |
290 | void wxApp::Dispatch() | |
291 | { | |
292 | } | |
293 | ||
fb896a32 DE |
294 | // Yield to other processes |
295 | ||
296 | bool wxApp::Yield(bool onlyIfNeeded) | |
297 | { | |
298 | // MT-FIXME | |
299 | static bool s_inYield = false; | |
300 | ||
301 | #if wxUSE_LOG | |
302 | // disable log flushing from here because a call to wxYield() shouldn't | |
303 | // normally result in message boxes popping up &c | |
304 | wxLog::Suspend(); | |
305 | #endif // wxUSE_LOG | |
306 | ||
307 | if (s_inYield) | |
308 | { | |
309 | if ( !onlyIfNeeded ) | |
310 | { | |
311 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
312 | } | |
313 | ||
314 | return false; | |
315 | } | |
316 | ||
317 | s_inYield = true; | |
318 | ||
aaa5ab05 DE |
319 | // Run the event loop until it is out of events |
320 | while(NSEvent *event = [GetNSApplication() | |
321 | nextEventMatchingMask:NSAnyEventMask | |
322 | untilDate:[NSDate distantPast] | |
323 | inMode:NSDefaultRunLoopMode | |
324 | dequeue: YES]) | |
325 | { | |
326 | [GetNSApplication() sendEvent: event]; | |
327 | } | |
fb896a32 DE |
328 | |
329 | #if wxUSE_LOG | |
330 | // let the logs be flashed again | |
331 | wxLog::Resume(); | |
332 | #endif // wxUSE_LOG | |
333 | ||
334 | s_inYield = false; | |
335 | ||
336 | return true; | |
337 | } | |
338 | ||
b93d8cc4 DE |
339 | #ifdef __WXDEBUG__ |
340 | void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg) | |
341 | { | |
342 | m_isInAssert = TRUE; | |
343 | wxAppBase::OnAssert(file, line, cond, msg); | |
344 | m_isInAssert = FALSE; | |
345 | } | |
346 | #endif // __WXDEBUG__ | |
347 |