| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/mgl/app.cpp |
| 3 | // Author: Vaclav Slavik |
| 4 | // based on GTK and MSW implementations |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx.h". |
| 11 | #include "wx/wxprec.h" |
| 12 | |
| 13 | #ifdef __BORLANDC__ |
| 14 | #pragma hdrstop |
| 15 | #endif |
| 16 | |
| 17 | #include "wx/app.h" |
| 18 | |
| 19 | #ifndef WX_PRECOMP |
| 20 | #include "wx/settings.h" |
| 21 | #include "wx/frame.h" |
| 22 | #include "wx/dialog.h" |
| 23 | #include "wx/log.h" |
| 24 | #include "wx/intl.h" |
| 25 | #include "wx/module.h" |
| 26 | #endif |
| 27 | |
| 28 | #include "wx/evtloop.h" |
| 29 | #include "wx/fontutil.h" |
| 30 | #include "wx/univ/theme.h" |
| 31 | #include "wx/univ/renderer.h" |
| 32 | #include "wx/univ/colschem.h" |
| 33 | #include "wx/sysopt.h" |
| 34 | #include "wx/mgl/private.h" |
| 35 | #include "wx/private/fontmgr.h" |
| 36 | |
| 37 | //----------------------------------------------------------------------------- |
| 38 | // wxApp::Exit() |
| 39 | //----------------------------------------------------------------------------- |
| 40 | |
| 41 | void wxApp::Exit() |
| 42 | { |
| 43 | MGL_exit(); |
| 44 | exit(0); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | //----------------------------------------------------------------------------- |
| 49 | // wxWakeUpIdle |
| 50 | //----------------------------------------------------------------------------- |
| 51 | |
| 52 | void wxApp::WakeUpIdle() |
| 53 | { |
| 54 | #if wxUSE_THREADS |
| 55 | if (!wxThread::IsMain()) |
| 56 | wxMutexGuiEnter(); |
| 57 | #endif |
| 58 | |
| 59 | while (wxTheApp->ProcessIdle()) |
| 60 | ; |
| 61 | |
| 62 | #if wxUSE_THREADS |
| 63 | if (!wxThread::IsMain()) |
| 64 | wxMutexGuiLeave(); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | //----------------------------------------------------------------------------- |
| 69 | // Root window |
| 70 | //----------------------------------------------------------------------------- |
| 71 | |
| 72 | class wxRootWindow : public wxWindow |
| 73 | { |
| 74 | public: |
| 75 | wxRootWindow() : wxWindow(NULL, wxID_ANY) |
| 76 | { |
| 77 | SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng)); |
| 78 | SetBackgroundColour(wxTHEME_COLOUR(DESKTOP)); |
| 79 | } |
| 80 | virtual ~wxRootWindow() |
| 81 | { |
| 82 | // we don't want to delete MGL_WM's rootWnd |
| 83 | m_wnd = NULL; |
| 84 | } |
| 85 | |
| 86 | virtual bool AcceptsFocus() const { return false; } |
| 87 | |
| 88 | DECLARE_DYNAMIC_CLASS(wxRootWindow) |
| 89 | }; |
| 90 | |
| 91 | IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow) |
| 92 | |
| 93 | static wxRootWindow *gs_rootWindow = NULL; |
| 94 | |
| 95 | //----------------------------------------------------------------------------- |
| 96 | // MGL initialization |
| 97 | //----------------------------------------------------------------------------- |
| 98 | |
| 99 | static bool wxCreateMGL_WM(const wxVideoMode& displayMode) |
| 100 | { |
| 101 | int mode; |
| 102 | int refresh = MGL_DEFAULT_REFRESH; |
| 103 | |
| 104 | #if wxUSE_SYSTEM_OPTIONS |
| 105 | if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) ) |
| 106 | refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh")); |
| 107 | #endif |
| 108 | |
| 109 | mode = MGL_findMode(displayMode.GetWidth(), |
| 110 | displayMode.GetHeight(), |
| 111 | displayMode.GetDepth()); |
| 112 | if ( mode == -1 ) |
| 113 | { |
| 114 | wxLogError(_("Mode %ix%i-%i not available."), |
| 115 | displayMode.GetWidth(), |
| 116 | displayMode.GetHeight(), |
| 117 | displayMode.GetDepth()); |
| 118 | return false; |
| 119 | } |
| 120 | g_displayDC = new MGLDisplayDC(mode, 1, refresh); |
| 121 | if ( !g_displayDC->isValid() ) |
| 122 | { |
| 123 | wxDELETE(g_displayDC); |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | g_winMng = MGL_wmCreate(g_displayDC->getDC()); |
| 128 | if (!g_winMng) |
| 129 | return false; |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | static void wxDestroyMGL_WM() |
| 135 | { |
| 136 | if ( g_winMng ) |
| 137 | { |
| 138 | MGL_wmDestroy(g_winMng); |
| 139 | g_winMng = NULL; |
| 140 | } |
| 141 | wxDELETE(g_displayDC); |
| 142 | } |
| 143 | |
| 144 | //----------------------------------------------------------------------------- |
| 145 | // wxApp |
| 146 | //----------------------------------------------------------------------------- |
| 147 | |
| 148 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) |
| 149 | |
| 150 | wxApp::wxApp() |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | wxApp::~wxApp() |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | wxVideoMode wxGetDefaultDisplayMode() |
| 159 | { |
| 160 | wxString mode; |
| 161 | unsigned w, h, bpp; |
| 162 | |
| 163 | if ( !wxGetEnv(wxT("WXMODE"), &mode) || |
| 164 | (wxSscanf(mode.c_str(), wxT("%ux%u-%u"), &w, &h, &bpp) != 3) ) |
| 165 | { |
| 166 | w = 640, h = 480, bpp = 16; |
| 167 | } |
| 168 | |
| 169 | return wxVideoMode(w, h, bpp); |
| 170 | } |
| 171 | |
| 172 | bool wxApp::SetDisplayMode(const wxVideoMode& mode) |
| 173 | { |
| 174 | if ( !mode.IsOk() ) |
| 175 | { |
| 176 | return false; |
| 177 | } |
| 178 | if ( g_displayDC != NULL ) |
| 179 | { |
| 180 | // FIXME_MGL -- we currently don't allow to switch video mode |
| 181 | // more than once. This can hopefully be changed... |
| 182 | wxFAIL_MSG(wxT("Can't change display mode after intialization!")); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | if ( !wxCreateMGL_WM(mode) ) |
| 187 | return false; |
| 188 | gs_rootWindow = new wxRootWindow; |
| 189 | |
| 190 | m_displayMode = mode; |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | bool wxApp::OnInitGui() |
| 196 | { |
| 197 | if ( !wxAppBase::OnInitGui() ) |
| 198 | return false; |
| 199 | |
| 200 | // MGL redirects stdout and stderr to physical console, so lets redirect |
| 201 | // it to file if WXSTDERR environment variable is set to be able to see |
| 202 | // wxLogDebug() output |
| 203 | wxString redirect; |
| 204 | if ( wxGetEnv(wxT("WXSTDERR"), &redirect) ) |
| 205 | freopen(redirect.mb_str(), "wt", stderr); |
| 206 | |
| 207 | wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui); |
| 208 | if ( oldLog ) delete oldLog; |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | bool wxApp::Initialize(int& argc, wxChar **argv) |
| 214 | { |
| 215 | #ifdef __DJGPP__ |
| 216 | // VS: disable long filenames under DJGPP as the very first thing, |
| 217 | // since SciTech MGL doesn't like them much... |
| 218 | wxSetEnv(wxT("LFN"), wxT("N")); |
| 219 | #endif |
| 220 | |
| 221 | // intialize MGL before creating wxFontsManager since it uses MGL funcs |
| 222 | if ( MGL_init(".", NULL) == 0 ) |
| 223 | { |
| 224 | wxLogError(_("Cannot initialize SciTech MGL!")); |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | if ( !wxAppBase::Initialize(argc, argv) ) |
| 229 | { |
| 230 | MGL_exit(); |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | #if wxUSE_INTL |
| 235 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); |
| 236 | #endif |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | // Modules are cleaned up after wxApp::CleanUp(), and some modules may |
| 242 | // require MGL to still be alive, e.g. the stock fonts need the fonts |
| 243 | // manager. So append this module last minute in wxApp::CleanUp() to close |
| 244 | // down MGL after all the other modules have been cleaned up. |
| 245 | // |
| 246 | struct wxMGLFinalCleanup: public wxModule |
| 247 | { |
| 248 | bool OnInit() { return true; } |
| 249 | |
| 250 | void OnExit() |
| 251 | { |
| 252 | wxFontsManager::CleanUp(); |
| 253 | |
| 254 | wxDestroyMGL_WM(); |
| 255 | MGL_exit(); |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | void wxApp::CleanUp() |
| 260 | { |
| 261 | delete gs_rootWindow; |
| 262 | |
| 263 | wxAppBase::CleanUp(); |
| 264 | |
| 265 | wxModule::RegisterModule(new wxMGLFinalCleanup); |
| 266 | } |