]>
Commit | Line | Data |
---|---|---|
32b8ec41 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: app.cpp | |
32b8ec41 | 3 | // Author: Vaclav Slavik |
7bdc1879 | 4 | // based on GTK and MSW implementations |
32b8ec41 | 5 | // Id: $Id$ |
8f7b34a8 | 6 | // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) |
32b8ec41 VZ |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "app.h" | |
12 | #endif | |
13 | ||
7bdc1879 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
2ec3892d | 21 | |
7bdc1879 VS |
22 | #ifndef WX_PRECOMP |
23 | #include "wx/settings.h" | |
24 | #include "wx/module.h" | |
25 | #include "wx/evtloop.h" | |
26 | #include "wx/frame.h" | |
27 | #include "wx/dialog.h" | |
2ec3892d | 28 | #include "wx/log.h" |
7bdc1879 VS |
29 | #include "wx/intl.h" |
30 | #endif | |
32b8ec41 | 31 | |
7bdc1879 | 32 | #include "wx/app.h" |
ef344ff8 | 33 | #include "wx/fontutil.h" |
df028524 VS |
34 | #include "wx/univ/theme.h" |
35 | #include "wx/univ/renderer.h" | |
7bdc1879 | 36 | #include "wx/mgl/private.h" |
32b8ec41 | 37 | |
2ec3892d VS |
38 | #define MGL_DEBUG |
39 | ||
40 | #if defined(MGL_DEBUG) && !defined(__WXDEBUG__) | |
41 | #undef MGL_DEBUG | |
42 | #endif | |
43 | ||
32b8ec41 VZ |
44 | //----------------------------------------------------------------------------- |
45 | // Global data | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
7bdc1879 | 48 | wxApp *wxTheApp = NULL; |
32b8ec41 VZ |
49 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
50 | ||
32b8ec41 | 51 | |
7bdc1879 VS |
52 | //----------------------------------------------------------------------------- |
53 | // wxExit | |
54 | //----------------------------------------------------------------------------- | |
32b8ec41 VZ |
55 | |
56 | void wxExit() | |
57 | { | |
7bdc1879 | 58 | MGL_exit(); |
32b8ec41 VZ |
59 | exit(0); |
60 | } | |
61 | ||
62 | //----------------------------------------------------------------------------- | |
63 | // wxYield | |
64 | //----------------------------------------------------------------------------- | |
65 | ||
7bdc1879 VS |
66 | static bool gs_inYield = FALSE; |
67 | ||
32b8ec41 VZ |
68 | bool wxYield() |
69 | { | |
7bdc1879 VS |
70 | #if wxUSE_THREADS |
71 | if ( !wxThread::IsMain() ) | |
72 | { | |
73 | // can't process events from other threads, MGL is thread-unsafe | |
74 | return TRUE; | |
75 | } | |
76 | #endif // wxUSE_THREADS | |
77 | ||
78 | gs_inYield = TRUE; | |
79 | ||
80 | wxLog::Suspend(); | |
81 | ||
ef344ff8 VS |
82 | if ( wxEventLoop::GetActive() ) |
83 | { | |
84 | while (wxEventLoop::GetActive()->Pending()) | |
85 | wxEventLoop::GetActive()->Dispatch(); | |
86 | } | |
7bdc1879 VS |
87 | |
88 | /* it's necessary to call ProcessIdle() to update the frames sizes which | |
89 | might have been changed (it also will update other things set from | |
90 | OnUpdateUI() which is a nice (and desired) side effect) */ | |
91 | while (wxTheApp->ProcessIdle()) { } | |
92 | ||
93 | wxLog::Resume(); | |
94 | ||
95 | gs_inYield = FALSE; | |
96 | ||
32b8ec41 VZ |
97 | return TRUE; |
98 | } | |
99 | ||
7bdc1879 VS |
100 | bool wxYieldIfNeeded() |
101 | { | |
102 | if (gs_inYield) | |
103 | return FALSE; | |
104 | ||
105 | return wxYield(); | |
106 | } | |
107 | ||
108 | ||
32b8ec41 VZ |
109 | //----------------------------------------------------------------------------- |
110 | // wxWakeUpIdle | |
111 | //----------------------------------------------------------------------------- | |
112 | ||
113 | void wxWakeUpIdle() | |
114 | { | |
7bdc1879 VS |
115 | #if wxUSE_THREADS |
116 | if (!wxThread::IsMain()) | |
117 | wxMutexGuiEnter(); | |
118 | #endif | |
119 | ||
120 | while (wxTheApp->ProcessIdle()) {} | |
121 | ||
122 | #if wxUSE_THREADS | |
123 | if (!wxThread::IsMain()) | |
124 | wxMutexGuiLeave(); | |
125 | #endif | |
32b8ec41 VZ |
126 | } |
127 | ||
128 | //----------------------------------------------------------------------------- | |
129 | // wxApp | |
130 | //----------------------------------------------------------------------------- | |
131 | ||
132 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
133 | ||
134 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
135 | EVT_IDLE(wxApp::OnIdle) | |
136 | END_EVENT_TABLE() | |
137 | ||
138 | ||
ef344ff8 | 139 | wxApp::wxApp() : m_mainLoop(NULL) |
7bdc1879 VS |
140 | { |
141 | } | |
142 | ||
143 | wxApp::~wxApp() | |
144 | { | |
145 | } | |
146 | ||
147 | bool wxApp::OnInitGui() | |
148 | { | |
7bdc1879 VS |
149 | if ( !wxCreateMGL_WM() ) |
150 | return FALSE; | |
151 | ||
152 | // This has to be done *after* wxCreateMGL_WM() because it initializes | |
153 | // wxUniv's themes | |
154 | if ( !wxAppBase::OnInitGui() ) | |
155 | return FALSE; | |
156 | ||
2ec3892d VS |
157 | #ifdef MGL_DEBUG |
158 | // That damn MGL redirects stdin and stdout to physical console | |
159 | FILE *file = fopen("stderr", "wt"); | |
160 | wxLog::SetActiveTarget(new wxLogStderr(file)); | |
161 | #endif | |
162 | ||
7bdc1879 VS |
163 | return TRUE; |
164 | } | |
165 | ||
166 | bool wxApp::ProcessIdle() | |
167 | { | |
168 | wxIdleEvent event; | |
169 | event.SetEventObject(this); | |
170 | ProcessEvent(event); | |
171 | ||
172 | return event.MoreRequested(); | |
173 | } | |
174 | ||
175 | void wxApp::OnIdle(wxIdleEvent &event) | |
176 | { | |
177 | static bool s_inOnIdle = FALSE; | |
178 | ||
179 | /* Avoid recursion (via ProcessEvent default case) */ | |
180 | if (s_inOnIdle) | |
181 | return; | |
182 | ||
183 | s_inOnIdle = TRUE; | |
184 | ||
185 | /* Resend in the main thread events which have been prepared in other | |
186 | threads */ | |
187 | ProcessPendingEvents(); | |
188 | ||
189 | // 'Garbage' collection of windows deleted with Close(). | |
190 | DeletePendingObjects(); | |
191 | ||
192 | // Send OnIdle events to all windows | |
193 | if ( SendIdleEvents() ) | |
194 | event.RequestMore(TRUE); | |
195 | ||
196 | s_inOnIdle = FALSE; | |
197 | } | |
198 | ||
199 | bool wxApp::SendIdleEvents() | |
200 | { | |
201 | bool needMore = FALSE; | |
202 | ||
203 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
204 | while (node) | |
205 | { | |
206 | wxWindow* win = node->GetData(); | |
207 | if ( SendIdleEvents(win) ) | |
208 | needMore = TRUE; | |
209 | node = node->GetNext(); | |
210 | } | |
211 | ||
212 | return needMore; | |
213 | } | |
214 | ||
215 | bool wxApp::SendIdleEvents(wxWindow* win) | |
216 | { | |
217 | bool needMore = FALSE; | |
218 | ||
219 | wxIdleEvent event; | |
220 | event.SetEventObject(win); | |
221 | ||
222 | win->GetEventHandler()->ProcessEvent(event); | |
223 | ||
7bdc1879 VS |
224 | if ( event.MoreRequested() ) |
225 | needMore = TRUE; | |
7bdc1879 VS |
226 | |
227 | wxNode* node = win->GetChildren().First(); | |
228 | while (node) | |
229 | { | |
230 | wxWindow* win = (wxWindow*) node->Data(); | |
231 | if ( SendIdleEvents(win) ) | |
232 | needMore = TRUE; | |
233 | ||
234 | node = node->Next(); | |
235 | } | |
236 | return needMore; | |
237 | } | |
238 | ||
239 | int wxApp::MainLoop() | |
240 | { | |
fd495ab3 | 241 | int rt; |
ef344ff8 VS |
242 | m_mainLoop = new wxEventLoop; |
243 | ||
244 | rt = m_mainLoop->Run(); | |
245 | ||
246 | delete m_mainLoop; | |
247 | m_mainLoop = NULL; | |
fd495ab3 | 248 | return rt; |
7bdc1879 VS |
249 | } |
250 | ||
251 | void wxApp::ExitMainLoop() | |
252 | { | |
ef344ff8 VS |
253 | if ( m_mainLoop ) |
254 | m_mainLoop->Exit(0); | |
7bdc1879 VS |
255 | } |
256 | ||
257 | bool wxApp::Initialized() | |
258 | { | |
bd73ba41 | 259 | return (wxTopLevelWindows.GetCount() != 0); |
7bdc1879 VS |
260 | } |
261 | ||
262 | bool wxApp::Pending() | |
263 | { | |
ef344ff8 | 264 | return wxEventLoop::GetActive()->Pending(); |
7bdc1879 VS |
265 | } |
266 | ||
267 | void wxApp::Dispatch() | |
32b8ec41 | 268 | { |
ef344ff8 | 269 | wxEventLoop::GetActive()->Dispatch(); |
32b8ec41 VZ |
270 | } |
271 | ||
7bdc1879 VS |
272 | void wxApp::DeletePendingObjects() |
273 | { | |
274 | wxNode *node = wxPendingDelete.First(); | |
275 | while (node) | |
276 | { | |
277 | wxObject *obj = (wxObject *)node->Data(); | |
278 | ||
279 | delete obj; | |
280 | ||
281 | if ( wxPendingDelete.Find(obj) ) | |
282 | delete node; | |
32b8ec41 | 283 | |
7bdc1879 VS |
284 | node = wxPendingDelete.First(); |
285 | } | |
286 | } | |
287 | ||
288 | bool wxApp::Initialize() | |
32b8ec41 | 289 | { |
bd73ba41 VS |
290 | if ( MGL_init(".", NULL) == 0 ) |
291 | return FALSE; | |
292 | ||
32b8ec41 VZ |
293 | wxBuffer = new wxChar[BUFSIZ + 512]; |
294 | ||
295 | wxClassInfo::InitializeClasses(); | |
7bdc1879 | 296 | |
32b8ec41 | 297 | wxSystemSettings::Init(); |
7bdc1879 VS |
298 | |
299 | #if wxUSE_INTL | |
300 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
301 | #endif | |
302 | ||
303 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
304 | // create a module for that as it's part of the core. | |
305 | #if wxUSE_THREADS | |
306 | wxPendingEvents = new wxList; | |
307 | wxPendingEventsLocker = new wxCriticalSection; | |
308 | #endif | |
309 | ||
310 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
32b8ec41 | 311 | wxTheColourDatabase->Initialize(); |
ef344ff8 VS |
312 | |
313 | // Can't do this in wxModule, because fonts are needed by stock lists | |
314 | wxTheFontsManager = new wxFontsManager; | |
7bdc1879 | 315 | |
32b8ec41 VZ |
316 | wxInitializeStockLists(); |
317 | wxInitializeStockObjects(); | |
7bdc1879 VS |
318 | |
319 | #if wxUSE_WX_RESOURCES | |
320 | wxInitializeResourceSystem(); | |
321 | #endif | |
322 | ||
32b8ec41 VZ |
323 | wxModule::RegisterModules(); |
324 | if (!wxModule::InitializeModules()) return FALSE; | |
7bdc1879 | 325 | |
32b8ec41 VZ |
326 | return TRUE; |
327 | } | |
328 | ||
7bdc1879 VS |
329 | wxIcon wxApp::GetStdIcon(int which) const |
330 | { | |
df028524 | 331 | return wxTheme::Get()->GetRenderer()->GetStdIcon(which); |
7bdc1879 VS |
332 | } |
333 | ||
334 | void wxApp::CleanUp() | |
335 | { | |
336 | #if wxUSE_LOG | |
337 | // flush the logged messages if any | |
338 | wxLog *log = wxLog::GetActiveTarget(); | |
339 | if (log != NULL && log->HasPendingMessages()) | |
340 | log->Flush(); | |
341 | ||
342 | // continuing to use user defined log target is unsafe from now on because | |
343 | // some resources may be already unavailable, so replace it by something | |
344 | // more safe | |
345 | wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr); | |
346 | if ( oldlog ) | |
347 | delete oldlog; | |
348 | #endif // wxUSE_LOG | |
349 | ||
350 | wxModule::CleanUpModules(); | |
351 | ||
352 | #if wxUSE_WX_RESOURCES | |
353 | wxCleanUpResourceSystem(); | |
354 | #endif | |
355 | ||
356 | if (wxTheColourDatabase) | |
357 | delete wxTheColourDatabase; | |
358 | ||
359 | wxTheColourDatabase = (wxColourDatabase*) NULL; | |
360 | ||
361 | wxDeleteStockObjects(); | |
7bdc1879 VS |
362 | wxDeleteStockLists(); |
363 | ||
ef344ff8 VS |
364 | // Can't do this in wxModule, because fonts are needed by stock lists |
365 | delete wxTheFontsManager; | |
366 | wxTheFontsManager = (wxFontsManager*) NULL; | |
367 | ||
7bdc1879 VS |
368 | delete wxTheApp; |
369 | wxTheApp = (wxApp*) NULL; | |
370 | ||
371 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
372 | // create a module for that as it's part of the core. | |
373 | #if wxUSE_THREADS | |
374 | delete wxPendingEvents; | |
375 | delete wxPendingEventsLocker; | |
376 | #endif | |
377 | ||
378 | wxSystemSettings::Done(); | |
379 | ||
380 | delete[] wxBuffer; | |
381 | ||
382 | wxClassInfo::CleanUpClasses(); | |
383 | ||
384 | // check for memory leaks | |
385 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
386 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
387 | { | |
388 | wxLogDebug(wxT("There were memory leaks.\n")); | |
389 | wxDebugContext::Dump(); | |
390 | wxDebugContext::PrintStatistics(); | |
391 | } | |
392 | #endif // Debug | |
393 | ||
394 | #if wxUSE_LOG | |
395 | // do this as the very last thing because everything else can log messages | |
396 | wxLog::DontCreateOnDemand(); | |
397 | ||
398 | wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL ); | |
399 | if (oldLog) | |
400 | delete oldLog; | |
401 | #endif // wxUSE_LOG | |
402 | ||
403 | wxDestroyMGL_WM(); | |
404 | MGL_exit(); | |
405 | } | |
406 | ||
407 | ||
408 | int wxEntryStart(int argc, char *argv[]) | |
409 | { | |
410 | return wxApp::Initialize() ? 0 : -1; | |
411 | } | |
412 | ||
413 | ||
414 | int wxEntryInitGui() | |
415 | { | |
416 | return wxTheApp->OnInitGui() ? 0 : -1; | |
417 | } | |
418 | ||
419 | ||
420 | void wxEntryCleanup() | |
421 | { | |
422 | wxApp::CleanUp(); | |
423 | } | |
424 | ||
425 | ||
426 | ||
427 | int wxEntry(int argc, char *argv[]) | |
428 | { | |
429 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
430 | // This seems to be necessary since there are 'rogue' | |
431 | // objects present at this point (perhaps global objects?) | |
432 | // Setting a checkpoint will ignore them as far as the | |
433 | // memory checking facility is concerned. | |
434 | // Of course you may argue that memory allocated in globals should be | |
435 | // checked, but this is a reasonable compromise. | |
436 | wxDebugContext::SetCheckpoint(); | |
437 | #endif | |
438 | int err = wxEntryStart(argc, argv); | |
439 | if ( err ) | |
440 | return err; | |
441 | ||
442 | if ( !wxTheApp ) | |
443 | { | |
444 | wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, | |
445 | wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); | |
446 | ||
447 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); | |
448 | ||
449 | wxObject *test_app = app_ini(); | |
450 | ||
451 | wxTheApp = (wxApp*) test_app; | |
452 | } | |
453 | ||
454 | wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); | |
455 | ||
456 | wxTheApp->argc = argc; | |
457 | #if wxUSE_UNICODE | |
458 | wxTheApp->argv = new wxChar*[argc+1]; | |
459 | int mb_argc = 0; | |
460 | while (mb_argc < argc) | |
461 | { | |
462 | wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); | |
463 | mb_argc++; | |
464 | } | |
465 | wxTheApp->argv[mb_argc] = (wxChar *)NULL; | |
466 | #else | |
467 | wxTheApp->argv = argv; | |
468 | #endif | |
469 | ||
470 | wxString name(wxFileNameFromPath(argv[0])); | |
471 | wxStripExtension(name); | |
472 | wxTheApp->SetAppName(name); | |
473 | ||
474 | int retValue; | |
475 | retValue = wxEntryInitGui(); | |
476 | ||
477 | // Here frames insert themselves automatically into wxTopLevelWindows by | |
478 | // getting created in OnInit(). | |
479 | if ( retValue == 0 ) | |
480 | { | |
481 | if ( !wxTheApp->OnInit() ) | |
482 | retValue = -1; | |
483 | } | |
484 | ||
485 | if ( retValue == 0 ) | |
486 | { | |
487 | /* delete pending toplevel windows (typically a single | |
488 | dialog) so that, if there isn't any left, we don't | |
489 | call OnRun() */ | |
490 | wxTheApp->DeletePendingObjects(); | |
491 | ||
fd495ab3 | 492 | if ( wxTheApp->Initialized() ) |
7bdc1879 VS |
493 | { |
494 | wxTheApp->OnRun(); | |
495 | ||
496 | wxWindow *topWindow = wxTheApp->GetTopWindow(); | |
497 | if ( topWindow ) | |
498 | { | |
499 | /* Forcibly delete the window. */ | |
500 | if (topWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
501 | topWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
502 | { | |
503 | topWindow->Close(TRUE); | |
504 | wxTheApp->DeletePendingObjects(); | |
505 | } | |
506 | else | |
507 | { | |
508 | delete topWindow; | |
509 | wxTheApp->SetTopWindow((wxWindow*) NULL); | |
510 | } | |
511 | } | |
512 | ||
513 | retValue = wxTheApp->OnExit(); | |
514 | } | |
515 | } | |
516 | ||
517 | wxEntryCleanup(); | |
518 | ||
519 | return retValue; | |
520 | } |