]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/app.mm
wxCocoa: Added NSThread
[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 33#include "wx/cocoa/ObjcPose.h"
493902ac 34#include "wx/cocoa/autorelease.h"
fb45bb1f 35
fb896a32
DE
36#if wxUSE_WX_RESOURCES
37# include "wx/resource.h"
38#endif
39
40#import <AppKit/NSApplication.h>
41#import <Foundation/NSRunLoop.h>
42#import <Foundation/NSArray.h>
47f1ad6a 43#import <Foundation/NSAutoreleasePool.h>
fb896a32
DE
44
45// ----------------------------------------------------------------------------
46// globals
47// ----------------------------------------------------------------------------
48
fb896a32
DE
49wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
50
51@interface wxPoserNSApplication : NSApplication
52{
53}
54
55- (void)doIdle: (id)data;
fb896a32
DE
56- (void)sendEvent: (NSEvent*)anEvent;
57- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
58@end // wxPoserNSApplication
59
60@implementation wxPoserNSApplication : NSApplication
61
62- (void)doIdle: (id)data
63{
64 wxASSERT(wxTheApp);
65 wxLogDebug("doIdle called");
66 NSRunLoop *rl = [NSRunLoop currentRunLoop];
67 // runMode: beforeDate returns YES if something was done
68 while(wxTheApp->ProcessIdle()) // FIXME: AND NO EVENTS ARE PENDING
69 {
70 wxLogDebug("Looping for idle events");
71 #if 1
72 if( [rl runMode:[rl currentMode] beforeDate:[NSDate distantPast]])
73 {
74 wxLogDebug("Found actual work to do");
75 break;
76 }
77 #endif
78 }
79 wxLogDebug("Idle processing complete, requesting next idle event");
80 // Add ourself back into the run loop (on next event) if necessary
81 wxTheApp->CocoaRequestIdle();
82}
83
fb896a32
DE
84- (void)sendEvent: (NSEvent*)anEvent
85{
86 wxLogDebug("SendEvent");
87 wxTheApp->CocoaInstallRequestedIdleHandler();
88 [super sendEvent: anEvent];
89}
90
91- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
92{
93 BOOL ret = wxTheApp->GetExitOnFrameDelete();
94 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
95 return ret;
96}
97
98@end // wxPoserNSApplication
99WX_IMPLEMENT_POSER(wxPoserNSApplication);
100
101// ============================================================================
102// functions
103// ============================================================================
104
e2478fde 105void wxApp::Exit()
fb896a32 106{
fb896a32 107 wxApp::CleanUp();
e2478fde
VZ
108
109 wxAppConsole::Exit();
fb896a32
DE
110}
111
112// ============================================================================
113// wxApp implementation
114// ============================================================================
115
116// ----------------------------------------------------------------------------
117// wxApp Static member initialization
118// ----------------------------------------------------------------------------
fb896a32
DE
119
120#if !USE_SHARED_LIBRARY
121IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
122BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 123 EVT_IDLE(wxAppBase::OnIdle)
fb896a32
DE
124// EVT_END_SESSION(wxApp::OnEndSession)
125// EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
126END_EVENT_TABLE()
127#endif
128
129// ----------------------------------------------------------------------------
05e2b077 130// wxApp initialization/cleanup
fb896a32 131// ----------------------------------------------------------------------------
94826170 132
05e2b077 133bool wxApp::Initialize(int& argc, wxChar **argv)
fb896a32 134{
47f1ad6a 135 wxAutoNSAutoreleasePool pool;
05e2b077
VZ
136 // Mac OS X passes a process serial number command line argument when
137 // the application is launched from the Finder. This argument must be
138 // removed from the command line arguments before being handled by the
139 // application (otherwise applications would need to handle it)
140 if ( argc > 1 )
141 {
142 static const wxChar *ARG_PSN = _T("-psn_");
143 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
144 {
145 // remove this argument
146 memmove(argv, argv + 1, argc--);
147 }
148 }
149
28ce3086
DE
150 // Posing must be completed before any instances of the Objective-C
151 // classes being posed as are created.
fb896a32 152 wxPoseAsInitializer::InitializePosers();
fb896a32 153
94826170 154 return wxAppBase::Initialize(argc, argv);
fb896a32
DE
155}
156
94826170 157void wxApp::CleanUp()
fb896a32 158{
891d0563 159 wxDC::CocoaShutdownTextSystem();
94826170
VZ
160
161 wxAppBase::CleanUp();
fb896a32
DE
162}
163
164// ----------------------------------------------------------------------------
165// wxApp creation
166// ----------------------------------------------------------------------------
167
168wxApp::wxApp()
169{
170 m_topWindow = NULL;
fb896a32
DE
171
172 m_isIdle = true;
173#if WXWIN_COMPATIBILITY_2_2
174 m_wantDebugOutput = TRUE;
175#endif
176
177 argc = 0;
178 argv = NULL;
179 m_cocoaApp = NULL;
180}
181
182void wxApp::CocoaInstallIdleHandler()
183{
bc34fa26
DE
184 // If we're supposed to be stopping, don't add more idle events
185 if(![m_cocoaApp isRunning])
186 return;
fb896a32
DE
187 wxLogDebug("wxApp::CocoaInstallIdleHandler");
188 m_isIdle = false;
189 // Call doIdle for EVERYTHING dammit
190// We'd need Foundation/NSConnection.h for this next constant, do we need it?
191 [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode,*/ NSModalPanelRunLoopMode, /**/NSEventTrackingRunLoopMode,/**/ nil] ];
192}
193
194bool wxApp::OnInitGui()
195{
47f1ad6a 196 wxAutoNSAutoreleasePool pool;
fb896a32
DE
197 if(!wxAppBase::OnInitGui())
198 return FALSE;
199
200 // Create the app using the sharedApplication method
201 m_cocoaApp = [NSApplication sharedApplication];
891d0563 202 wxDC::CocoaInitializeTextSystem();
fb896a32
DE
203// [ m_cocoaApp setDelegate:m_cocoaApp ];
204 #if 0
205 wxLogDebug("Just for kicks");
206 [ m_cocoaApp performSelector:@selector(doIdle:) withObject:NULL ];
207 wxLogDebug("okay.. done now");
208 #endif
209 return TRUE;
210}
211
47f1ad6a
DE
212bool wxApp::CallOnInit()
213{
bd3e8827 214// wxAutoNSAutoreleasePool pool;
47f1ad6a
DE
215 return OnInit();
216}
217
fb896a32
DE
218bool wxApp::OnInit()
219{
220 if(!wxAppBase::OnInit())
221 return FALSE;
222
223 return TRUE;
224}
225
226bool wxApp::Initialized()
227{
228 if (GetTopWindow())
229 return TRUE;
230 else
231 return FALSE;
232}
233
234int wxApp::MainLoop()
235{
236 [m_cocoaApp run];
237 return 0;
238}
239
fb896a32
DE
240void wxApp::ExitMainLoop()
241{
242 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
243// CocoaInstallRequestedIdleHandler();
244// if(m_isIdle)
245// [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
246// actually.. we WANT the idle event
247// or not
248#if 0
249 if(!m_isIdle)
250 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
251#endif
bc34fa26 252 [m_cocoaApp stop: m_cocoaApp];
fb896a32
DE
253}
254
255// Is a message/event pending?
256bool wxApp::Pending()
257{
258 return 0;
259}
260
261// Dispatch a message.
262void wxApp::Dispatch()
263{
264}
265
fb896a32
DE
266// Yield to other processes
267
268bool wxApp::Yield(bool onlyIfNeeded)
269{
270 // MT-FIXME
271 static bool s_inYield = false;
272
273#if wxUSE_LOG
274 // disable log flushing from here because a call to wxYield() shouldn't
275 // normally result in message boxes popping up &c
276 wxLog::Suspend();
277#endif // wxUSE_LOG
278
279 if (s_inYield)
280 {
281 if ( !onlyIfNeeded )
282 {
283 wxFAIL_MSG( wxT("wxYield called recursively" ) );
284 }
285
286 return false;
287 }
288
289 s_inYield = true;
290
291 wxLogDebug("WARNING: SUPPOSED to have yielded!");
292 // FIXME: Do something!
293
294#if wxUSE_LOG
295 // let the logs be flashed again
296 wxLog::Resume();
297#endif // wxUSE_LOG
298
299 s_inYield = false;
300
301 return true;
302}
303