]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/app.mm
In destructor, pass the NSView ownership to the base class
[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
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
DE
33#include "wx/cocoa/ObjcPose.h"
34
fb896a32
DE
35#if wxUSE_WX_RESOURCES
36# include "wx/resource.h"
37#endif
38
39#import <AppKit/NSApplication.h>
40#import <Foundation/NSRunLoop.h>
41#import <Foundation/NSArray.h>
47f1ad6a 42#import <Foundation/NSAutoreleasePool.h>
fb896a32
DE
43
44// ----------------------------------------------------------------------------
45// globals
46// ----------------------------------------------------------------------------
47
fb896a32
DE
48wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
49
50@interface wxPoserNSApplication : NSApplication
51{
52}
53
54- (void)doIdle: (id)data;
fb896a32
DE
55- (void)sendEvent: (NSEvent*)anEvent;
56- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
57@end // wxPoserNSApplication
58
59@implementation wxPoserNSApplication : NSApplication
60
61- (void)doIdle: (id)data
62{
63 wxASSERT(wxTheApp);
64 wxLogDebug("doIdle called");
65 NSRunLoop *rl = [NSRunLoop currentRunLoop];
66 // runMode: beforeDate returns YES if something was done
67 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
68 {
69 wxLogDebug("Looping for idle events");
70 #if 1
71 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
72 {
73 wxLogDebug("Found actual work to do");
74 break;
75 }
76 #endif
77 }
78 wxLogDebug("Idle processing complete, requesting next idle event");
79 // Add ourself back into the run loop (on next event) if necessary
80 wxTheApp->CocoaRequestIdle();
81}
82
fb896a32
DE
83- (void)sendEvent: (NSEvent*)anEvent
84{
85 wxLogDebug("SendEvent");
86 wxTheApp->CocoaInstallRequestedIdleHandler();
87 [super sendEvent: anEvent];
88}
89
90- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
91{
92 BOOL ret = wxTheApp->GetExitOnFrameDelete();
93 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
94 return ret;
95}
96
97@end // wxPoserNSApplication
98WX_IMPLEMENT_POSER(wxPoserNSApplication);
99
47f1ad6a
DE
100class wxAutoNSAutoreleasePool
101{
102public:
103 wxAutoNSAutoreleasePool()
104 {
105 m_pool = [[NSAutoreleasePool alloc] init];
106 }
107 ~wxAutoNSAutoreleasePool()
108 {
109 [m_pool release];
110 }
111protected:
112 NSAutoreleasePool *m_pool;
113};
114
fb896a32
DE
115// ============================================================================
116// functions
117// ============================================================================
118
e2478fde 119void 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
135IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
136BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
fb896a32
DE
137// EVT_END_SESSION(wxApp::OnEndSession)
138// EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
139END_EVENT_TABLE()
140#endif
141
142// ----------------------------------------------------------------------------
05e2b077 143// wxApp initialization/cleanup
fb896a32 144// ----------------------------------------------------------------------------
94826170 145
05e2b077 146bool wxApp::Initialize(int& argc, wxChar **argv)
fb896a32 147{
47f1ad6a 148 wxAutoNSAutoreleasePool pool;
05e2b077
VZ
149 // Mac OS X passes a process serial number command line argument when
150 // the application is launched from the Finder. This argument must be
151 // removed from the command line arguments before being handled by the
152 // application (otherwise applications would need to handle it)
153 if ( argc > 1 )
154 {
155 static const wxChar *ARG_PSN = _T("-psn_");
156 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
157 {
158 // remove this argument
159 memmove(argv, argv + 1, argc--);
160 }
161 }
162
28ce3086
DE
163 // Posing must be completed before any instances of the Objective-C
164 // classes being posed as are created.
fb896a32 165 wxPoseAsInitializer::InitializePosers();
fb896a32 166
94826170 167 return wxAppBase::Initialize(argc, argv);
fb896a32
DE
168}
169
94826170 170void wxApp::CleanUp()
fb896a32 171{
891d0563 172 wxDC::CocoaShutdownTextSystem();
94826170
VZ
173
174 wxAppBase::CleanUp();
fb896a32
DE
175}
176
177// ----------------------------------------------------------------------------
178// wxApp creation
179// ----------------------------------------------------------------------------
180
181wxApp::wxApp()
182{
183 m_topWindow = NULL;
184 wxTheApp = this;
185
186 m_isIdle = true;
187#if WXWIN_COMPATIBILITY_2_2
188 m_wantDebugOutput = TRUE;
189#endif
190
191 argc = 0;
192 argv = NULL;
193 m_cocoaApp = NULL;
194}
195
196void wxApp::CocoaInstallIdleHandler()
197{
bc34fa26
DE
198 // If we're supposed to be stopping, don't add more idle events
199 if(![m_cocoaApp isRunning])
200 return;
fb896a32
DE
201 wxLogDebug("wxApp::CocoaInstallIdleHandler");
202 m_isIdle = false;
203 // Call doIdle for EVERYTHING dammit
204// We'd need Foundation/NSConnection.h for this next constant, do we need it?
205 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
206}
207
208bool wxApp::OnInitGui()
209{
47f1ad6a 210 wxAutoNSAutoreleasePool pool;
fb896a32
DE
211 if(!wxAppBase::OnInitGui())
212 return FALSE;
213
214 // Create the app using the sharedApplication method
215 m_cocoaApp = [NSApplication sharedApplication];
891d0563 216 wxDC::CocoaInitializeTextSystem();
fb896a32
DE
217// [ m_cocoaApp setDelegate:m_cocoaApp ];
218 #if 0
219 wxLogDebug("Just for kicks");
220 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
221 wxLogDebug("okay.. done now");
222 #endif
223 return TRUE;
224}
225
47f1ad6a
DE
226bool wxApp::CallOnInit()
227{
228 wxAutoNSAutoreleasePool pool;
229 return OnInit();
230}
231
fb896a32
DE
232bool wxApp::OnInit()
233{
234 if(!wxAppBase::OnInit())
235 return FALSE;
236
237 return TRUE;
238}
239
240bool wxApp::Initialized()
241{
242 if (GetTopWindow())
243 return TRUE;
244 else
245 return FALSE;
246}
247
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.
276void wxApp::Dispatch()
277{
278}
279
fb896a32
DE
280// Yield to other processes
281
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
305 wxLogDebug("WARNING: SUPPOSED to have yielded!");
306 // FIXME: Do something!
307
308#if wxUSE_LOG
309 // let the logs be flashed again
310 wxLog::Resume();
311#endif // wxUSE_LOG
312
313 s_inYield = false;
314
315 return true;
316}
317