+bool wxApp::SetDisplayMode(const wxVideoMode& mode)
+{
+ if ( !mode.IsOk() )
+ {
+ return false;
+ }
+ if ( g_displayDC != NULL )
+ {
+ // FIXME_MGL -- we currently don't allow to switch video mode
+ // more than once. This can hopefully be changed...
+ wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
+ return false;
+ }
+
+ if ( !wxCreateMGL_WM(mode) )
+ return false;
+ gs_rootWindow = new wxRootWindow;
+
+ m_displayMode = mode;
+
+ return true;
+}
+
+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;
+ }
+
+ if ( !wxAppBase::Initialize(argc, argv) )
+ {
+ 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()
+ {
+ wxFontsManager::CleanUp();
+
+ wxDestroyMGL_WM();
+ MGL_exit();
+ }
+};
+
+void wxApp::CleanUp()
+{
+ delete gs_rootWindow;
+
+ wxAppBase::CleanUp();
+
+ wxModule::RegisterModule(new wxMGLFinalCleanup);
+}