]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/app.mm
don't hang forever in Dispatch() if there is no event loop
[wxWidgets.git] / src / cocoa / app.mm
CommitLineData
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
fb896a32
DE
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"
891d0563 18 #include "wx/dc.h"
fb896a32
DE
19 #include "wx/intl.h"
20 #include "wx/log.h"
fb896a32
DE
21#endif
22
11c08416
DE
23#include "wx/module.h"
24
fb45bb1f 25#include "wx/cocoa/ObjcPose.h"
493902ac 26#include "wx/cocoa/autorelease.h"
af367f46 27#include "wx/cocoa/mbarman.h"
fb45bb1f 28
fb896a32
DE
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>
47f1ad6a 36#import <Foundation/NSAutoreleasePool.h>
14fc7eb4 37#import <Foundation/NSThread.h>
aaa5ab05 38#import <AppKit/NSEvent.h>
fb896a32 39
70fb935a
DE
40// ========================================================================
41// wxPoseAsInitializer
42// ========================================================================
fb896a32
DE
43wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
44
70fb935a
DE
45// ========================================================================
46// wxPoserNSApplication
47// ========================================================================
fb896a32
DE
48@interface wxPoserNSApplication : NSApplication
49{
50}
51
52- (void)doIdle: (id)data;
fb896a32
DE
53- (void)sendEvent: (NSEvent*)anEvent;
54- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
55@end // wxPoserNSApplication
56
70fb935a
DE
57WX_IMPLEMENT_POSER(wxPoserNSApplication);
58
fb896a32
DE
59@implementation wxPoserNSApplication : NSApplication
60
61- (void)doIdle: (id)data
62{
63 wxASSERT(wxTheApp);
af367f46
DE
64 wxASSERT(wxMenuBarManager::GetInstance());
65 wxMenuBarManager::GetInstance()->CocoaInternalIdle();
fb896a32 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
e2478fde 109
70fb935a
DE
110// ========================================================================
111// wxApp
112// ========================================================================
fb896a32
DE
113
114// ----------------------------------------------------------------------------
115// wxApp Static member initialization
116// ----------------------------------------------------------------------------
fb896a32
DE
117IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
118BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 119 EVT_IDLE(wxAppBase::OnIdle)
fb896a32
DE
120// EVT_END_SESSION(wxApp::OnEndSession)
121// EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
122END_EVENT_TABLE()
fb896a32
DE
123
124// ----------------------------------------------------------------------------
05e2b077 125// wxApp initialization/cleanup
fb896a32 126// ----------------------------------------------------------------------------
05e2b077 127bool wxApp::Initialize(int& argc, wxChar **argv)
fb896a32 128{
47f1ad6a 129 wxAutoNSAutoreleasePool pool;
14fc7eb4 130 m_cocoaMainThread = [NSThread currentThread];
05e2b077
VZ
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
28ce3086
DE
145 // Posing must be completed before any instances of the Objective-C
146 // classes being posed as are created.
fb896a32 147 wxPoseAsInitializer::InitializePosers();
fb896a32 148
94826170 149 return wxAppBase::Initialize(argc, argv);
fb896a32
DE
150}
151
94826170 152void wxApp::CleanUp()
fb896a32 153{
891d0563 154 wxDC::CocoaShutdownTextSystem();
af367f46 155 wxMenuBarManager::DestroyInstance();
94826170
VZ
156
157 wxAppBase::CleanUp();
fb896a32
DE
158}
159
160// ----------------------------------------------------------------------------
161// wxApp creation
162// ----------------------------------------------------------------------------
fb896a32
DE
163wxApp::wxApp()
164{
165 m_topWindow = NULL;
fb896a32
DE
166
167 m_isIdle = true;
168#if WXWIN_COMPATIBILITY_2_2
169 m_wantDebugOutput = TRUE;
170#endif
b93d8cc4
DE
171#ifdef __WXDEBUG__
172 m_isInAssert = FALSE;
173#endif // __WXDEBUG__
174
fb896a32
DE
175 argc = 0;
176 argv = NULL;
177 m_cocoaApp = NULL;
178}
179
180void wxApp::CocoaInstallIdleHandler()
181{
14fc7eb4
DE
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 }
bc34fa26
DE
188 // If we're supposed to be stopping, don't add more idle events
189 if(![m_cocoaApp isRunning])
190 return;
fb896a32
DE
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
198bool wxApp::OnInitGui()
199{
47f1ad6a 200 wxAutoNSAutoreleasePool pool;
fb896a32
DE
201 if(!wxAppBase::OnInitGui())
202 return FALSE;
203
204 // Create the app using the sharedApplication method
205 m_cocoaApp = [NSApplication sharedApplication];
af367f46
DE
206
207 wxMenuBarManager::CreateInstance();
208
891d0563 209 wxDC::CocoaInitializeTextSystem();
fb896a32
DE
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
47f1ad6a
DE
219bool wxApp::CallOnInit()
220{
bd3e8827 221// wxAutoNSAutoreleasePool pool;
47f1ad6a
DE
222 return OnInit();
223}
224
fb896a32
DE
225bool wxApp::OnInit()
226{
227 if(!wxAppBase::OnInit())
228 return FALSE;
229
230 return TRUE;
231}
232
233bool wxApp::Initialized()
234{
235 if (GetTopWindow())
236 return TRUE;
237 else
238 return FALSE;
239}
240
70fb935a
DE
241void wxApp::Exit()
242{
243 wxApp::CleanUp();
244
245 wxAppConsole::Exit();
246}
247
fb896a32
DE
248int wxApp::MainLoop()
249{
250 [m_cocoaApp run];
251 return 0;
252}
253
fb896a32
DE
254void 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
bc34fa26 266 [m_cocoaApp stop: m_cocoaApp];
fb896a32
DE
267}
268
269// Is a message/event pending?
270bool wxApp::Pending()
271{
272 return 0;
273}
274
275// Dispatch a message.
1bf77ee5 276bool wxApp::Dispatch()
fb896a32 277{
1bf77ee5 278 return true;
fb896a32
DE
279}
280
fb896a32 281// Yield to other processes
fb896a32
DE
282bool 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
aaa5ab05
DE
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 }
fb896a32
DE
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__
326void 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