]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/app.mm
Compilation fix.
[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
70fb935a
DE
233void wxApp::Exit()
234{
235 wxApp::CleanUp();
236
237 wxAppConsole::Exit();
238}
239
fb896a32 240// Yield to other processes
fb896a32
DE
241bool wxApp::Yield(bool onlyIfNeeded)
242{
243 // MT-FIXME
244 static bool s_inYield = false;
245
246#if wxUSE_LOG
247 // disable log flushing from here because a call to wxYield() shouldn't
248 // normally result in message boxes popping up &c
249 wxLog::Suspend();
250#endif // wxUSE_LOG
251
252 if (s_inYield)
253 {
254 if ( !onlyIfNeeded )
255 {
256 wxFAIL_MSG( wxT("wxYield called recursively" ) );
257 }
258
259 return false;
260 }
261
262 s_inYield = true;
263
aaa5ab05
DE
264 // Run the event loop until it is out of events
265 while(NSEvent *event = [GetNSApplication()
266 nextEventMatchingMask:NSAnyEventMask
267 untilDate:[NSDate distantPast]
268 inMode:NSDefaultRunLoopMode
269 dequeue: YES])
270 {
271 [GetNSApplication() sendEvent: event];
272 }
fb896a32
DE
273
274#if wxUSE_LOG
275 // let the logs be flashed again
276 wxLog::Resume();
277#endif // wxUSE_LOG
278
279 s_inYield = false;
280
281 return true;
282}
283
b93d8cc4
DE
284#ifdef __WXDEBUG__
285void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
286{
287 m_isInAssert = TRUE;
288 wxAppBase::OnAssert(file, line, cond, msg);
289 m_isInAssert = FALSE;
290}
291#endif // __WXDEBUG__
292