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