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