]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: app.cpp | |
3 | // Author: Vaclav Slavik | |
4 | // based on GTK and MSW implementations | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "app.h" | |
12 | #endif | |
13 | ||
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 | ||
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" | |
28 | #include "wx/log.h" | |
29 | #include "wx/intl.h" | |
30 | #include "wx/resource.h" | |
31 | #endif | |
32 | ||
33 | #include "wx/app.h" | |
34 | #include "wx/fontutil.h" | |
35 | #include "wx/univ/theme.h" | |
36 | #include "wx/univ/renderer.h" | |
37 | #include "wx/univ/colschem.h" | |
38 | #include "wx/sysopt.h" | |
39 | #include "wx/mgl/private.h" | |
40 | ||
41 | //----------------------------------------------------------------------------- | |
42 | // Global data | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | wxApp *wxTheApp = NULL; | |
46 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; | |
47 | ||
48 | ||
49 | //----------------------------------------------------------------------------- | |
50 | // wxExit | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | void wxExit() | |
54 | { | |
55 | MGL_exit(); | |
56 | exit(0); | |
57 | } | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // wxYield | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | static bool gs_inYield = FALSE; | |
64 | ||
65 | bool wxApp::Yield(bool onlyIfNeeded) | |
66 | { | |
67 | if ( gs_inYield ) | |
68 | { | |
69 | if ( !onlyIfNeeded ) | |
70 | { | |
71 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
72 | } | |
73 | ||
74 | return FALSE; | |
75 | } | |
76 | ||
77 | #if wxUSE_THREADS | |
78 | if ( !wxThread::IsMain() ) | |
79 | { | |
80 | // can't process events from other threads, MGL is thread-unsafe | |
81 | return TRUE; | |
82 | } | |
83 | #endif // wxUSE_THREADS | |
84 | ||
85 | gs_inYield = TRUE; | |
86 | ||
87 | wxLog::Suspend(); | |
88 | ||
89 | if ( wxEventLoop::GetActive() ) | |
90 | { | |
91 | while (wxEventLoop::GetActive()->Pending()) | |
92 | wxEventLoop::GetActive()->Dispatch(); | |
93 | } | |
94 | ||
95 | /* it's necessary to call ProcessIdle() to update the frames sizes which | |
96 | might have been changed (it also will update other things set from | |
97 | OnUpdateUI() which is a nice (and desired) side effect) */ | |
98 | while (wxTheApp->ProcessIdle()) { } | |
99 | ||
100 | wxLog::Resume(); | |
101 | ||
102 | gs_inYield = FALSE; | |
103 | ||
104 | return TRUE; | |
105 | } | |
106 | ||
107 | ||
108 | //----------------------------------------------------------------------------- | |
109 | // wxWakeUpIdle | |
110 | //----------------------------------------------------------------------------- | |
111 | ||
112 | void wxWakeUpIdle() | |
113 | { | |
114 | #if wxUSE_THREADS | |
115 | if (!wxThread::IsMain()) | |
116 | wxMutexGuiEnter(); | |
117 | #endif | |
118 | ||
119 | while (wxTheApp->ProcessIdle()) {} | |
120 | ||
121 | #if wxUSE_THREADS | |
122 | if (!wxThread::IsMain()) | |
123 | wxMutexGuiLeave(); | |
124 | #endif | |
125 | } | |
126 | ||
127 | //----------------------------------------------------------------------------- | |
128 | // Root window | |
129 | //----------------------------------------------------------------------------- | |
130 | ||
131 | class wxRootWindow : public wxWindow | |
132 | { | |
133 | public: | |
134 | wxRootWindow() : wxWindow(NULL, -1) | |
135 | { | |
136 | SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng)); | |
137 | SetBackgroundColour(wxTHEME_COLOUR(DESKTOP)); | |
138 | } | |
139 | ~wxRootWindow() | |
140 | { | |
141 | // we don't want to delete MGL_WM's rootWnd | |
142 | m_wnd = NULL; | |
143 | } | |
144 | ||
145 | virtual bool AcceptsFocus() const { return FALSE; } | |
146 | ||
147 | DECLARE_DYNAMIC_CLASS(wxRootWindow) | |
148 | }; | |
149 | ||
150 | IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow) | |
151 | ||
152 | static wxRootWindow *gs_rootWindow = NULL; | |
153 | ||
154 | //----------------------------------------------------------------------------- | |
155 | // MGL initialization | |
156 | //----------------------------------------------------------------------------- | |
157 | ||
158 | static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode) | |
159 | { | |
160 | int mode; | |
161 | int refresh = MGL_DEFAULT_REFRESH; | |
162 | ||
163 | #if wxUSE_SYSTEM_OPTIONS | |
164 | if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) ) | |
165 | refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh")); | |
166 | #endif | |
167 | ||
168 | mode = MGL_findMode(displayMode.GetWidth(), | |
169 | displayMode.GetHeight(), | |
170 | displayMode.GetDepth()); | |
171 | if ( mode == -1 ) | |
172 | { | |
173 | wxLogError(_("Mode %ix%i-%i not available."), | |
174 | displayMode.GetWidth(), | |
175 | displayMode.GetHeight(), | |
176 | displayMode.GetDepth()); | |
177 | return FALSE; | |
178 | } | |
179 | g_displayDC = new MGLDisplayDC(mode, 1, refresh); | |
180 | if ( !g_displayDC->isValid() ) | |
181 | { | |
182 | delete g_displayDC; | |
183 | g_displayDC = NULL; | |
184 | return FALSE; | |
185 | } | |
186 | ||
187 | g_winMng = MGL_wmCreate(g_displayDC->getDC()); | |
188 | if (!g_winMng) | |
189 | return FALSE; | |
190 | ||
191 | return TRUE; | |
192 | } | |
193 | ||
194 | static void wxDestroyMGL_WM() | |
195 | { | |
196 | if ( g_winMng ) | |
197 | { | |
198 | MGL_wmDestroy(g_winMng); | |
199 | g_winMng = NULL; | |
200 | } | |
201 | if ( g_displayDC ) | |
202 | { | |
203 | delete g_displayDC; | |
204 | g_displayDC = NULL; | |
205 | } | |
206 | } | |
207 | ||
208 | //----------------------------------------------------------------------------- | |
209 | // wxApp | |
210 | //----------------------------------------------------------------------------- | |
211 | ||
212 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
213 | ||
214 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
215 | EVT_IDLE(wxApp::OnIdle) | |
216 | END_EVENT_TABLE() | |
217 | ||
218 | ||
219 | wxApp::wxApp() : m_mainLoop(NULL) | |
220 | { | |
221 | } | |
222 | ||
223 | wxApp::~wxApp() | |
224 | { | |
225 | } | |
226 | ||
227 | wxDisplayModeInfo wxGetDefaultDisplayMode() | |
228 | { | |
229 | wxString mode; | |
230 | unsigned w, h, bpp; | |
231 | ||
232 | if ( !wxGetEnv(wxT("WXMODE"), &mode) || | |
233 | (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) ) | |
234 | { | |
235 | w = 640, h = 480, bpp = 16; | |
236 | } | |
237 | ||
238 | return wxDisplayModeInfo(w, h, bpp); | |
239 | } | |
240 | ||
241 | bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode) | |
242 | { | |
243 | if ( !mode.IsOk() ) | |
244 | { | |
245 | return FALSE; | |
246 | } | |
247 | if ( g_displayDC != NULL ) | |
248 | { | |
249 | // FIXME_MGL -- we currently don't allow to switch video mode | |
250 | // more than once. This can hopefully be changed... | |
251 | wxFAIL_MSG(wxT("Can't change display mode after intialization!")); | |
252 | return FALSE; | |
253 | } | |
254 | ||
255 | if ( !wxCreateMGL_WM(mode) ) | |
256 | return FALSE; | |
257 | gs_rootWindow = new wxRootWindow; | |
258 | ||
259 | m_displayMode = mode; | |
260 | ||
261 | return TRUE; | |
262 | } | |
263 | ||
264 | bool wxApp::OnInitGui() | |
265 | { | |
266 | if ( !wxAppBase::OnInitGui() ) | |
267 | return FALSE; | |
268 | ||
269 | #ifdef __WXDEBUG__ | |
270 | // MGL redirects stdout and stderr to physical console, so lets redirect | |
271 | // it to file. Do it only when WXDEBUG environment variable is set | |
272 | wxString redirect; | |
273 | if ( wxGetEnv(wxT("WXSTDERR"), &redirect) ) | |
274 | freopen(redirect.mb_str(), "wt", stderr); | |
275 | #endif | |
276 | ||
277 | wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui); | |
278 | if ( oldLog ) delete oldLog; | |
279 | ||
280 | return TRUE; | |
281 | } | |
282 | ||
283 | bool wxApp::ProcessIdle() | |
284 | { | |
285 | wxIdleEvent event; | |
286 | event.SetEventObject(this); | |
287 | ProcessEvent(event); | |
288 | ||
289 | return event.MoreRequested(); | |
290 | } | |
291 | ||
292 | void wxApp::OnIdle(wxIdleEvent &event) | |
293 | { | |
294 | static bool s_inOnIdle = FALSE; | |
295 | ||
296 | /* Avoid recursion (via ProcessEvent default case) */ | |
297 | if (s_inOnIdle) | |
298 | return; | |
299 | ||
300 | s_inOnIdle = TRUE; | |
301 | ||
302 | /* Resend in the main thread events which have been prepared in other | |
303 | threads */ | |
304 | ProcessPendingEvents(); | |
305 | ||
306 | // 'Garbage' collection of windows deleted with Close(). | |
307 | DeletePendingObjects(); | |
308 | ||
309 | #if wxUSE_LOG | |
310 | // flush the logged messages if any | |
311 | wxLog::FlushActive(); | |
312 | #endif // wxUSE_LOG | |
313 | ||
314 | // Send OnIdle events to all windows | |
315 | if ( SendIdleEvents() ) | |
316 | event.RequestMore(TRUE); | |
317 | ||
318 | s_inOnIdle = FALSE; | |
319 | } | |
320 | ||
321 | bool wxApp::SendIdleEvents() | |
322 | { | |
323 | bool needMore = FALSE; | |
324 | ||
325 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
326 | while (node) | |
327 | { | |
328 | wxWindow* win = node->GetData(); | |
329 | if ( SendIdleEvents(win) ) | |
330 | needMore = TRUE; | |
331 | node = node->GetNext(); | |
332 | } | |
333 | ||
334 | return needMore; | |
335 | } | |
336 | ||
337 | bool wxApp::SendIdleEvents(wxWindow* win) | |
338 | { | |
339 | bool needMore = FALSE; | |
340 | ||
341 | wxIdleEvent event; | |
342 | event.SetEventObject(win); | |
343 | ||
344 | win->GetEventHandler()->ProcessEvent(event); | |
345 | ||
346 | if ( event.MoreRequested() ) | |
347 | needMore = TRUE; | |
348 | ||
349 | wxNode* node = win->GetChildren().First(); | |
350 | while (node) | |
351 | { | |
352 | wxWindow* win = (wxWindow*) node->Data(); | |
353 | if ( SendIdleEvents(win) ) | |
354 | needMore = TRUE; | |
355 | ||
356 | node = node->Next(); | |
357 | } | |
358 | return needMore; | |
359 | } | |
360 | ||
361 | int wxApp::MainLoop() | |
362 | { | |
363 | int rt; | |
364 | m_mainLoop = new wxEventLoop; | |
365 | ||
366 | rt = m_mainLoop->Run(); | |
367 | ||
368 | delete m_mainLoop; | |
369 | m_mainLoop = NULL; | |
370 | return rt; | |
371 | } | |
372 | ||
373 | void wxApp::ExitMainLoop() | |
374 | { | |
375 | if ( m_mainLoop ) | |
376 | m_mainLoop->Exit(0); | |
377 | } | |
378 | ||
379 | bool wxApp::Initialized() | |
380 | { | |
381 | return (wxTopLevelWindows.GetCount() != 0); | |
382 | } | |
383 | ||
384 | bool wxApp::Pending() | |
385 | { | |
386 | return wxEventLoop::GetActive()->Pending(); | |
387 | } | |
388 | ||
389 | void wxApp::Dispatch() | |
390 | { | |
391 | wxEventLoop::GetActive()->Dispatch(); | |
392 | } | |
393 | ||
394 | void wxApp::DeletePendingObjects() | |
395 | { | |
396 | wxNode *node = wxPendingDelete.First(); | |
397 | while (node) | |
398 | { | |
399 | wxObject *obj = (wxObject *)node->Data(); | |
400 | ||
401 | delete obj; | |
402 | ||
403 | if ( wxPendingDelete.Find(obj) ) | |
404 | delete node; | |
405 | ||
406 | node = wxPendingDelete.First(); | |
407 | } | |
408 | } | |
409 | ||
410 | bool wxApp::Initialize() | |
411 | { | |
412 | if ( MGL_init(".", NULL) == 0 ) | |
413 | return FALSE; | |
414 | ||
415 | wxBuffer = new wxChar[BUFSIZ + 512]; | |
416 | ||
417 | wxClassInfo::InitializeClasses(); | |
418 | ||
419 | #if wxUSE_INTL | |
420 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
421 | #endif | |
422 | ||
423 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
424 | // create a module for that as it's part of the core. | |
425 | #if wxUSE_THREADS | |
426 | wxPendingEvents = new wxList; | |
427 | wxPendingEventsLocker = new wxCriticalSection; | |
428 | #endif | |
429 | ||
430 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
431 | wxTheColourDatabase->Initialize(); | |
432 | ||
433 | // Can't do this in wxModule, because fonts are needed by stock lists | |
434 | wxTheFontsManager = new wxFontsManager; | |
435 | ||
436 | wxInitializeStockLists(); | |
437 | wxInitializeStockObjects(); | |
438 | ||
439 | #if wxUSE_WX_RESOURCES | |
440 | wxInitializeResourceSystem(); | |
441 | #endif | |
442 | ||
443 | wxModule::RegisterModules(); | |
444 | if (!wxModule::InitializeModules()) return FALSE; | |
445 | ||
446 | return TRUE; | |
447 | } | |
448 | ||
449 | wxIcon wxApp::GetStdIcon(int which) const | |
450 | { | |
451 | return wxTheme::Get()->GetRenderer()->GetStdIcon(which); | |
452 | } | |
453 | ||
454 | void wxApp::CleanUp() | |
455 | { | |
456 | #if wxUSE_LOG | |
457 | // continuing to use user defined log target is unsafe from now on because | |
458 | // some resources may be already unavailable, so replace it by something | |
459 | // more safe | |
460 | wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr); | |
461 | if ( oldlog ) | |
462 | delete oldlog; | |
463 | #endif // wxUSE_LOG | |
464 | ||
465 | delete gs_rootWindow; | |
466 | ||
467 | wxModule::CleanUpModules(); | |
468 | ||
469 | #if wxUSE_WX_RESOURCES | |
470 | wxCleanUpResourceSystem(); | |
471 | #endif | |
472 | ||
473 | if (wxTheColourDatabase) | |
474 | delete wxTheColourDatabase; | |
475 | ||
476 | wxTheColourDatabase = (wxColourDatabase*) NULL; | |
477 | ||
478 | wxDeleteStockObjects(); | |
479 | wxDeleteStockLists(); | |
480 | ||
481 | delete wxTheApp; | |
482 | wxTheApp = (wxApp*) NULL; | |
483 | ||
484 | ||
485 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
486 | // create a module for that as it's part of the core. | |
487 | #if wxUSE_THREADS | |
488 | delete wxPendingEvents; | |
489 | delete wxPendingEventsLocker; | |
490 | #endif | |
491 | ||
492 | wxClassInfo::CleanUpClasses(); | |
493 | ||
494 | // Can't do this in wxModule, because fonts are needed by stock lists | |
495 | // (do it after deleting wxTheApp and cleaning modules up, since somebody | |
496 | // may be deleting fonts that lately) | |
497 | delete wxTheFontsManager; | |
498 | wxTheFontsManager = (wxFontsManager*) NULL; | |
499 | ||
500 | delete[] wxBuffer; | |
501 | ||
502 | // check for memory leaks | |
503 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
504 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
505 | { | |
506 | wxLogDebug(wxT("There were memory leaks.\n")); | |
507 | wxDebugContext::Dump(); | |
508 | wxDebugContext::PrintStatistics(); | |
509 | } | |
510 | #endif // Debug | |
511 | ||
512 | #if wxUSE_LOG | |
513 | // do this as the very last thing because everything else can log messages | |
514 | wxLog::DontCreateOnDemand(); | |
515 | ||
516 | wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL ); | |
517 | if (oldLog) | |
518 | delete oldLog; | |
519 | #endif // wxUSE_LOG | |
520 | ||
521 | wxDestroyMGL_WM(); | |
522 | MGL_exit(); | |
523 | } | |
524 | ||
525 | ||
526 | int wxEntryStart(int argc, char *argv[]) | |
527 | { | |
528 | return wxApp::Initialize() ? 0 : -1; | |
529 | } | |
530 | ||
531 | ||
532 | int wxEntryInitGui() | |
533 | { | |
534 | return wxTheApp->OnInitGui() ? 0 : -1; | |
535 | } | |
536 | ||
537 | ||
538 | void wxEntryCleanup() | |
539 | { | |
540 | wxApp::CleanUp(); | |
541 | } | |
542 | ||
543 | ||
544 | ||
545 | int wxEntry(int argc, char *argv[]) | |
546 | { | |
547 | #ifdef __DJGPP__ | |
548 | // VS: disable long filenames under DJGPP as the very first thing, | |
549 | // since SciTech MGL doesn't like them much... | |
550 | wxSetEnv(wxT("LFN"), wxT("N")); | |
551 | #endif | |
552 | ||
553 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
554 | // This seems to be necessary since there are 'rogue' | |
555 | // objects present at this point (perhaps global objects?) | |
556 | // Setting a checkpoint will ignore them as far as the | |
557 | // memory checking facility is concerned. | |
558 | // Of course you may argue that memory allocated in globals should be | |
559 | // checked, but this is a reasonable compromise. | |
560 | wxDebugContext::SetCheckpoint(); | |
561 | #endif | |
562 | int err = wxEntryStart(argc, argv); | |
563 | if ( err ) | |
564 | return err; | |
565 | ||
566 | if ( !wxTheApp ) | |
567 | { | |
568 | wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, | |
569 | wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); | |
570 | ||
571 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); | |
572 | ||
573 | wxObject *test_app = app_ini(); | |
574 | ||
575 | wxTheApp = (wxApp*) test_app; | |
576 | } | |
577 | ||
578 | wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); | |
579 | ||
580 | wxTheApp->argc = argc; | |
581 | #if wxUSE_UNICODE | |
582 | wxTheApp->argv = new wxChar*[argc+1]; | |
583 | int mb_argc = 0; | |
584 | while (mb_argc < argc) | |
585 | { | |
586 | wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); | |
587 | mb_argc++; | |
588 | } | |
589 | wxTheApp->argv[mb_argc] = (wxChar *)NULL; | |
590 | #else | |
591 | wxTheApp->argv = argv; | |
592 | #endif | |
593 | ||
594 | wxString name(wxFileNameFromPath(argv[0])); | |
595 | wxStripExtension(name); | |
596 | wxTheApp->SetAppName(name); | |
597 | ||
598 | int retValue; | |
599 | retValue = wxEntryInitGui(); | |
600 | ||
601 | // Here frames insert themselves automatically into wxTopLevelWindows by | |
602 | // getting created in OnInit(). | |
603 | if ( retValue == 0 ) | |
604 | { | |
605 | if ( !wxTheApp->OnInit() ) | |
606 | retValue = -1; | |
607 | } | |
608 | ||
609 | if ( retValue == 0 ) | |
610 | { | |
611 | /* delete pending toplevel windows (typically a single | |
612 | dialog) so that, if there isn't any left, we don't | |
613 | call OnRun() */ | |
614 | wxTheApp->DeletePendingObjects(); | |
615 | ||
616 | if ( wxTheApp->Initialized() ) | |
617 | { | |
618 | wxTheApp->OnRun(); | |
619 | ||
620 | wxWindow *topWindow = wxTheApp->GetTopWindow(); | |
621 | if ( topWindow ) | |
622 | { | |
623 | /* Forcibly delete the window. */ | |
624 | if (topWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
625 | topWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
626 | { | |
627 | topWindow->Close(TRUE); | |
628 | wxTheApp->DeletePendingObjects(); | |
629 | } | |
630 | else | |
631 | { | |
632 | delete topWindow; | |
633 | wxTheApp->SetTopWindow((wxWindow*) NULL); | |
634 | } | |
635 | } | |
636 | ||
637 | #if wxUSE_LOG | |
638 | // flush the logged messages if any | |
639 | wxLog *log = wxLog::GetActiveTarget(); | |
640 | if (log != NULL && log->HasPendingMessages()) | |
641 | log->Flush(); | |
642 | #endif // wxUSE_LOG | |
643 | retValue = wxTheApp->OnExit(); | |
644 | } | |
645 | } | |
646 | ||
647 | wxEntryCleanup(); | |
648 | ||
649 | return retValue; | |
650 | } |