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