+bool wxApp::OnInitGui()
+{
+ if ( !wxAppBase::OnInitGui() )
+ return false;
+
+#ifdef __WXDEBUG__
+ // MGL redirects stdout and stderr to physical console, so lets redirect
+ // it to file in debug build. Do it only when WXSTDERR environment variable is set
+ wxString redirect;
+ if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
+ freopen(redirect.mb_str(), "wt", stderr);
+#endif // __WXDEBUG__
+
+ wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
+ if ( oldLog ) delete oldLog;
+
+ return true;
+}
+
+bool wxApp::Initialize(int& argc, wxChar **argv)
+{
+#ifdef __DJGPP__
+ // VS: disable long filenames under DJGPP as the very first thing,
+ // since SciTech MGL doesn't like them much...
+ wxSetEnv(wxT("LFN"), wxT("N"));
+#endif
+
+ // intialize MGL before creating wxFontsManager since it uses MGL funcs
+ if ( MGL_init(".", NULL) == 0 )
+ {
+ wxLogError(_("Cannot initialize SciTech MGL!"));
+ return false;
+ }
+
+ // must do it before calling wxAppBase::Initialize(), because fonts are
+ // needed by stock lists which are created there
+ wxTheFontsManager = new wxFontsManager;
+
+ if ( !wxAppBase::Initialize(argc, argv) )
+ {
+ delete wxTheFontsManager;
+ wxTheFontsManager = NULL;
+ MGL_exit();
+ return false;
+ }
+
+#if wxUSE_INTL
+ wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
+#endif
+
+ return true;
+}
+
+// Modules are cleaned up after wxApp::CleanUp(), and some modules may
+// require MGL to still be alive, e.g. the stock fonts need the fonts
+// manager. So append this module last minute in wxApp::CleanUp() to close
+// down MGL after all the other modules have been cleaned up.
+//
+struct wxMGLFinalCleanup: public wxModule
+{
+ bool OnInit() { return true; }
+
+ void OnExit()
+ {
+ delete wxTheFontsManager;
+ wxTheFontsManager = (wxFontsManager*) NULL;
+
+ wxDestroyMGL_WM();
+ MGL_exit();
+ }
+};
+
+void wxApp::CleanUp()
+{
+ delete gs_rootWindow;
+
+ wxAppBase::CleanUp();
+
+ wxModule::RegisterModule(new wxMGLFinalCleanup);
+}