]>
Commit | Line | Data |
---|---|---|
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 licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
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" | |
18 | #include "wx/dc.h" | |
19 | #include "wx/intl.h" | |
20 | #include "wx/log.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/module.h" | |
24 | ||
25 | #include "wx/cocoa/ObjcPose.h" | |
26 | #include "wx/cocoa/autorelease.h" | |
27 | #include "wx/cocoa/mbarman.h" | |
28 | ||
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> | |
36 | #import <Foundation/NSAutoreleasePool.h> | |
37 | #import <Foundation/NSThread.h> | |
38 | #import <AppKit/NSEvent.h> | |
39 | ||
40 | // ======================================================================== | |
41 | // wxPoseAsInitializer | |
42 | // ======================================================================== | |
43 | wxPoseAsInitializer *wxPoseAsInitializer::sm_first = NULL; | |
44 | ||
45 | // ======================================================================== | |
46 | // wxPoserNSApplication | |
47 | // ======================================================================== | |
48 | @interface wxPoserNSApplication : NSApplication | |
49 | { | |
50 | } | |
51 | ||
52 | - (void)doIdle: (id)data; | |
53 | - (void)sendEvent: (NSEvent*)anEvent; | |
54 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication; | |
55 | @end // wxPoserNSApplication | |
56 | ||
57 | WX_IMPLEMENT_POSER(wxPoserNSApplication); | |
58 | ||
59 | @implementation wxPoserNSApplication : NSApplication | |
60 | ||
61 | - (void)doIdle: (id)data | |
62 | { | |
63 | wxASSERT(wxTheApp); | |
64 | wxASSERT(wxMenuBarManager::GetInstance()); | |
65 | wxMenuBarManager::GetInstance()->CocoaInternalIdle(); | |
66 | wxLogDebug("doIdle called"); | |
67 | #ifdef __WXDEBUG__ | |
68 | if(wxTheApp->IsInAssert()) | |
69 | { | |
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 | |
78 | { | |
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 | |
87 | } | |
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 | ||
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 | |
109 | ||
110 | // ======================================================================== | |
111 | // wxApp | |
112 | // ======================================================================== | |
113 | ||
114 | // ---------------------------------------------------------------------------- | |
115 | // wxApp Static member initialization | |
116 | // ---------------------------------------------------------------------------- | |
117 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
118 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
119 | EVT_IDLE(wxAppBase::OnIdle) | |
120 | // EVT_END_SESSION(wxApp::OnEndSession) | |
121 | // EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
122 | END_EVENT_TABLE() | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // wxApp initialization/cleanup | |
126 | // ---------------------------------------------------------------------------- | |
127 | bool wxApp::Initialize(int& argc, wxChar **argv) | |
128 | { | |
129 | wxAutoNSAutoreleasePool pool; | |
130 | m_cocoaMainThread = [NSThread currentThread]; | |
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 | ||
145 | // Posing must be completed before any instances of the Objective-C | |
146 | // classes being posed as are created. | |
147 | wxPoseAsInitializer::InitializePosers(); | |
148 | ||
149 | return wxAppBase::Initialize(argc, argv); | |
150 | } | |
151 | ||
152 | void wxApp::CleanUp() | |
153 | { | |
154 | wxDC::CocoaShutdownTextSystem(); | |
155 | wxMenuBarManager::DestroyInstance(); | |
156 | ||
157 | wxAppBase::CleanUp(); | |
158 | } | |
159 | ||
160 | // ---------------------------------------------------------------------------- | |
161 | // wxApp creation | |
162 | // ---------------------------------------------------------------------------- | |
163 | wxApp::wxApp() | |
164 | { | |
165 | m_topWindow = NULL; | |
166 | ||
167 | m_isIdle = true; | |
168 | #if WXWIN_COMPATIBILITY_2_2 | |
169 | m_wantDebugOutput = TRUE; | |
170 | #endif | |
171 | #ifdef __WXDEBUG__ | |
172 | m_isInAssert = FALSE; | |
173 | #endif // __WXDEBUG__ | |
174 | ||
175 | argc = 0; | |
176 | argv = NULL; | |
177 | m_cocoaApp = NULL; | |
178 | } | |
179 | ||
180 | void wxApp::CocoaInstallIdleHandler() | |
181 | { | |
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 | } | |
188 | // If we're supposed to be stopping, don't add more idle events | |
189 | if(![m_cocoaApp isRunning]) | |
190 | return; | |
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 | ||
198 | bool wxApp::OnInitGui() | |
199 | { | |
200 | wxAutoNSAutoreleasePool pool; | |
201 | if(!wxAppBase::OnInitGui()) | |
202 | return FALSE; | |
203 | ||
204 | // Create the app using the sharedApplication method | |
205 | m_cocoaApp = [NSApplication sharedApplication]; | |
206 | ||
207 | wxMenuBarManager::CreateInstance(); | |
208 | ||
209 | wxDC::CocoaInitializeTextSystem(); | |
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 | ||
219 | bool wxApp::CallOnInit() | |
220 | { | |
221 | // wxAutoNSAutoreleasePool pool; | |
222 | return OnInit(); | |
223 | } | |
224 | ||
225 | bool wxApp::OnInit() | |
226 | { | |
227 | if(!wxAppBase::OnInit()) | |
228 | return FALSE; | |
229 | ||
230 | return TRUE; | |
231 | } | |
232 | ||
233 | void wxApp::Exit() | |
234 | { | |
235 | wxApp::CleanUp(); | |
236 | ||
237 | wxAppConsole::Exit(); | |
238 | } | |
239 | ||
240 | // Yield to other processes | |
241 | bool 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 | ||
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 | } | |
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 | ||
284 | #ifdef __WXDEBUG__ | |
285 | void 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 |