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