]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/app.mm
Removed conflicting wxEntry prototype
[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
7// RCS-ID: $Id:
8// Copyright: (c) David Elliott
9// Licence: wxWindows license
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>
42
43// ----------------------------------------------------------------------------
44// globals
45// ----------------------------------------------------------------------------
46
fb896a32
DE
47wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL;
48
49@interface wxPoserNSApplication : NSApplication
50{
51}
52
53- (void)doIdle: (id)data;
54- (void)finishLaunching;
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
83- (void)finishLaunching
84{
85 wxLogDebug("finishLaunching");
86 bool initsuccess = wxTheApp->OnInit();
87 if(!initsuccess)
88 [super stop: NULL];
89
90 [super finishLaunching];
91}
92
93- (void)sendEvent: (NSEvent*)anEvent
94{
95 wxLogDebug("SendEvent");
96 wxTheApp->CocoaInstallRequestedIdleHandler();
97 [super sendEvent: anEvent];
98}
99
100- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
101{
102 BOOL ret = wxTheApp->GetExitOnFrameDelete();
103 wxLogDebug("applicationShouldTermintaeAfterLastWindowClosed=%d",ret);
104 return ret;
105}
106
107@end // wxPoserNSApplication
108WX_IMPLEMENT_POSER(wxPoserNSApplication);
109
110// ============================================================================
111// functions
112// ============================================================================
113
e2478fde 114void wxApp::Exit()
fb896a32 115{
fb896a32 116 wxApp::CleanUp();
e2478fde
VZ
117
118 wxAppConsole::Exit();
fb896a32
DE
119}
120
121// ============================================================================
122// wxApp implementation
123// ============================================================================
124
125// ----------------------------------------------------------------------------
126// wxApp Static member initialization
127// ----------------------------------------------------------------------------
fb896a32
DE
128
129#if !USE_SHARED_LIBRARY
130IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
131BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
132 EVT_IDLE(wxApp::OnIdle)
133// EVT_END_SESSION(wxApp::OnEndSession)
134// EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
135END_EVENT_TABLE()
136#endif
137
138// ----------------------------------------------------------------------------
05e2b077 139// wxApp initialization/cleanup
fb896a32 140// ----------------------------------------------------------------------------
94826170 141
05e2b077 142bool wxApp::Initialize(int& argc, wxChar **argv)
fb896a32 143{
05e2b077
VZ
144 // Mac OS X passes a process serial number command line argument when
145 // the application is launched from the Finder. This argument must be
146 // removed from the command line arguments before being handled by the
147 // application (otherwise applications would need to handle it)
148 if ( argc > 1 )
149 {
150 static const wxChar *ARG_PSN = _T("-psn_");
151 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
152 {
153 // remove this argument
154 memmove(argv, argv + 1, argc--);
155 }
156 }
157
94826170
VZ
158 // VZ: apparently this needs to be done a.s.a.p., right? it is done after
159 // wxClassInfo::InitializeClasses() now but usd to be done before, I
160 // hope it's not a problem -- if it is, please let me know, David (if
161 // it isn't, just remove this comment :-)
fb896a32 162 wxPoseAsInitializer::InitializePosers();
fb896a32 163
94826170 164 return wxAppBase::Initialize(argc, argv);
fb896a32
DE
165}
166
94826170 167void wxApp::CleanUp()
fb896a32 168{
891d0563 169 wxDC::CocoaShutdownTextSystem();
94826170
VZ
170
171 wxAppBase::CleanUp();
fb896a32
DE
172}
173
174// ----------------------------------------------------------------------------
175// wxApp creation
176// ----------------------------------------------------------------------------
177
178wxApp::wxApp()
179{
180 m_topWindow = NULL;
181 wxTheApp = this;
182
183 m_isIdle = true;
184#if WXWIN_COMPATIBILITY_2_2
185 m_wantDebugOutput = TRUE;
186#endif
187
188 argc = 0;
189 argv = NULL;
190 m_cocoaApp = NULL;
191}
192
193void wxApp::CocoaInstallIdleHandler()
194{
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{
204 if(!wxAppBase::OnInitGui())
205 return FALSE;
206
207 // Create the app using the sharedApplication method
208 m_cocoaApp = [NSApplication sharedApplication];
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
219bool wxApp::OnInit()
220{
221 if(!wxAppBase::OnInit())
222 return FALSE;
223
224 return TRUE;
225}
226
227bool wxApp::Initialized()
228{
229 if (GetTopWindow())
230 return TRUE;
231 else
232 return FALSE;
233}
234
235int wxApp::MainLoop()
236{
237 [m_cocoaApp run];
238 return 0;
239}
240
241// Returns TRUE if more time is needed.
242bool wxApp::ProcessIdle()
243{
244 wxIdleEvent event;
245 event.SetEventObject(this);
246 ProcessEvent(event);
247
248 return event.MoreRequested();
249}
250
251void wxApp::ExitMainLoop()
252{
253 wxLogDebug("wxApp::ExitMailLoop m_isIdle=%d, isRunning=%d",(int)m_isIdle,(int)[m_cocoaApp isRunning]);
254// CocoaInstallRequestedIdleHandler();
255// if(m_isIdle)
256// [[ NSRunLoop currentRunLoop ] performSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL order:0 modes:[NSArray arrayWithObjects:NSDefaultRunLoopMode, /* NSConnectionReplyRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode,*/ nil] ];
257// actually.. we WANT the idle event
258// or not
259#if 0
260 if(!m_isIdle)
261 [[ NSRunLoop currentRunLoop ] cancelPerformSelector:@selector(doIdle:) target:m_cocoaApp argument:NULL];
262#endif
263 [m_cocoaApp terminate: m_cocoaApp];
264}
265
266// Is a message/event pending?
267bool wxApp::Pending()
268{
269 return 0;
270}
271
272// Dispatch a message.
273void wxApp::Dispatch()
274{
275}
276
277void wxApp::OnIdle(wxIdleEvent& event)
278{
279 wxLogDebug("wxApp::OnIdle");
280 static bool s_inOnIdle = FALSE;
281
282 // Avoid recursion (via ProcessEvent default case)
283 if ( s_inOnIdle )
284 return;
285 s_inOnIdle = TRUE;
286
287
288 DeletePendingObjects();
289
290 // flush the logged messages if any
291 wxLog *pLog = wxLog::GetActiveTarget();
292 if ( pLog != NULL && pLog->HasPendingMessages() )
293 pLog->Flush();
294
295 // Send OnIdle events to all windows
296 bool needMore = SendIdleEvents();
297
298 if (needMore)
299 event.RequestMore(TRUE);
300
301 s_inOnIdle = FALSE;
302}
303
304// Send idle event to all top-level windows
305bool wxApp::SendIdleEvents()
306{
307 bool needMore = FALSE;
308 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
309 while (node)
310 {
311 wxWindow* win = node->GetData();
312 if (SendIdleEvents(win))
313 needMore = TRUE;
314
315 node = node->GetNext();
316 }
317 return needMore;
318}
319
320// Send idle event to window and all subwindows
321bool wxApp::SendIdleEvents(wxWindow* win)
322{
323// wxLogDebug("SendIdleEvents win=%p",win);
324 bool needMore = FALSE;
325
326 wxIdleEvent event;
327 event.SetEventObject(win);
328 win->ProcessEvent(event);
329
330 if (event.MoreRequested())
331 needMore = TRUE;
332
333 wxWindowList::Node* node = win->GetChildren().GetFirst();
334 while (node)
335 {
336// wxLogDebug("child=%p",node->Data());
337 wxWindow* win = node->GetData();
338 if (SendIdleEvents(win))
339 needMore = TRUE;
340
341 node = node->GetNext();
342 }
343 return needMore;
344}
345
346// Yield to other processes
347
348bool wxApp::Yield(bool onlyIfNeeded)
349{
350 // MT-FIXME
351 static bool s_inYield = false;
352
353#if wxUSE_LOG
354 // disable log flushing from here because a call to wxYield() shouldn't
355 // normally result in message boxes popping up &c
356 wxLog::Suspend();
357#endif // wxUSE_LOG
358
359 if (s_inYield)
360 {
361 if ( !onlyIfNeeded )
362 {
363 wxFAIL_MSG( wxT("wxYield called recursively" ) );
364 }
365
366 return false;
367 }
368
369 s_inYield = true;
370
371 wxLogDebug("WARNING: SUPPOSED to have yielded!");
372 // FIXME: Do something!
373
374#if wxUSE_LOG
375 // let the logs be flashed again
376 wxLog::Resume();
377#endif // wxUSE_LOG
378
379 s_inYield = false;
380
381 return true;
382}
383