]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
Initialize wxLogWindow::m_pLogFrame in ctor to avoid (rare) crash.
[wxWidgets.git] / src / mgl / app.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/mgl/app.cpp
32b8ec41 3// Author: Vaclav Slavik
7bdc1879 4// based on GTK and MSW implementations
32b8ec41 5// Id: $Id$
c41c20a5 6// Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
65571936 7// Licence: wxWindows licence
32b8ec41
VZ
8/////////////////////////////////////////////////////////////////////////////
9
7bdc1879
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
16
670f9935 17#include "wx/app.h"
2ec3892d 18
7bdc1879
VS
19#ifndef WX_PRECOMP
20 #include "wx/settings.h"
7bdc1879
VS
21 #include "wx/frame.h"
22 #include "wx/dialog.h"
2ec3892d 23 #include "wx/log.h"
7bdc1879 24 #include "wx/intl.h"
02761f6c 25 #include "wx/module.h"
7bdc1879 26#endif
32b8ec41 27
90eb13e6 28#include "wx/evtloop.h"
ef344ff8 29#include "wx/fontutil.h"
df028524
VS
30#include "wx/univ/theme.h"
31#include "wx/univ/renderer.h"
58061670 32#include "wx/univ/colschem.h"
05c9ccbe 33#include "wx/sysopt.h"
7bdc1879 34#include "wx/mgl/private.h"
d7ae4a62 35#include "wx/private/fontmgr.h"
32b8ec41
VZ
36
37//-----------------------------------------------------------------------------
505f0a85 38// wxApp::Exit()
32b8ec41
VZ
39//-----------------------------------------------------------------------------
40
e2478fde 41void wxApp::Exit()
32b8ec41 42{
7bdc1879 43 MGL_exit();
32b8ec41
VZ
44 exit(0);
45}
46
7bdc1879 47
32b8ec41
VZ
48//-----------------------------------------------------------------------------
49// wxWakeUpIdle
50//-----------------------------------------------------------------------------
51
e2478fde 52void wxApp::WakeUpIdle()
32b8ec41 53{
7bdc1879
VS
54#if wxUSE_THREADS
55 if (!wxThread::IsMain())
56 wxMutexGuiEnter();
57#endif
58
e2478fde
VZ
59 while (wxTheApp->ProcessIdle())
60 ;
7bdc1879
VS
61
62#if wxUSE_THREADS
63 if (!wxThread::IsMain())
64 wxMutexGuiLeave();
65#endif
32b8ec41
VZ
66}
67
58061670
VS
68//-----------------------------------------------------------------------------
69// Root window
70//-----------------------------------------------------------------------------
71
72class wxRootWindow : public wxWindow
73{
74 public:
0471d486 75 wxRootWindow() : wxWindow(NULL, wxID_ANY)
58061670
VS
76 {
77 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng));
78 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP));
79 }
d3c7fc99 80 virtual ~wxRootWindow()
58061670
VS
81 {
82 // we don't want to delete MGL_WM's rootWnd
7c9955d1 83 m_wnd = NULL;
58061670
VS
84 }
85
0471d486 86 virtual bool AcceptsFocus() const { return false; }
7c9955d1 87
b7d5acd0 88 DECLARE_DYNAMIC_CLASS(wxRootWindow)
58061670
VS
89};
90
b7d5acd0
VS
91IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow)
92
58061670
VS
93static wxRootWindow *gs_rootWindow = NULL;
94
95//-----------------------------------------------------------------------------
96// MGL initialization
97//-----------------------------------------------------------------------------
98
fedec981 99static bool wxCreateMGL_WM(const wxVideoMode& displayMode)
58061670
VS
100{
101 int mode;
58061670 102 int refresh = MGL_DEFAULT_REFRESH;
7c9955d1 103
58061670 104#if wxUSE_SYSTEM_OPTIONS
05c9ccbe 105 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
58061670
VS
106 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
107#endif
7c9955d1
JS
108
109 mode = MGL_findMode(displayMode.GetWidth(),
110 displayMode.GetHeight(),
634f6a1f 111 displayMode.GetDepth());
58061670
VS
112 if ( mode == -1 )
113 {
7c9955d1 114 wxLogError(_("Mode %ix%i-%i not available."),
d76048f5
VS
115 displayMode.GetWidth(),
116 displayMode.GetHeight(),
634f6a1f 117 displayMode.GetDepth());
0471d486 118 return false;
58061670
VS
119 }
120 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
121 if ( !g_displayDC->isValid() )
122 {
5276b0a5 123 wxDELETE(g_displayDC);
0471d486 124 return false;
58061670 125 }
7c9955d1 126
58061670
VS
127 g_winMng = MGL_wmCreate(g_displayDC->getDC());
128 if (!g_winMng)
0471d486 129 return false;
1f43b5c9 130
0471d486 131 return true;
58061670
VS
132}
133
134static void wxDestroyMGL_WM()
135{
136 if ( g_winMng )
137 {
138 MGL_wmDestroy(g_winMng);
139 g_winMng = NULL;
140 }
5276b0a5 141 wxDELETE(g_displayDC);
58061670
VS
142}
143
32b8ec41
VZ
144//-----------------------------------------------------------------------------
145// wxApp
146//-----------------------------------------------------------------------------
147
148IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
149
752464f9 150wxApp::wxApp()
7bdc1879
VS
151{
152}
153
154wxApp::~wxApp()
155{
156}
157
fedec981 158wxVideoMode wxGetDefaultDisplayMode()
a3e76614
VS
159{
160 wxString mode;
161 unsigned w, h, bpp;
162
7c9955d1 163 if ( !wxGetEnv(wxT("WXMODE"), &mode) ||
9a83f860 164 (wxSscanf(mode.c_str(), wxT("%ux%u-%u"), &w, &h, &bpp) != 3) )
a3e76614
VS
165 {
166 w = 640, h = 480, bpp = 16;
167 }
168
fedec981 169 return wxVideoMode(w, h, bpp);
a3e76614
VS
170}
171
fedec981 172bool wxApp::SetDisplayMode(const wxVideoMode& mode)
634f6a1f
VS
173{
174 if ( !mode.IsOk() )
175 {
0471d486 176 return false;
634f6a1f
VS
177 }
178 if ( g_displayDC != NULL )
179 {
180 // FIXME_MGL -- we currently don't allow to switch video mode
1f43b5c9 181 // more than once. This can hopefully be changed...
634f6a1f 182 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
0471d486 183 return false;
634f6a1f 184 }
1f43b5c9
VS
185
186 if ( !wxCreateMGL_WM(mode) )
0471d486 187 return false;
1f43b5c9
VS
188 gs_rootWindow = new wxRootWindow;
189
634f6a1f 190 m_displayMode = mode;
1f43b5c9 191
0471d486 192 return true;
634f6a1f
VS
193}
194
7bdc1879
VS
195bool wxApp::OnInitGui()
196{
7bdc1879 197 if ( !wxAppBase::OnInitGui() )
0471d486 198 return false;
7bdc1879 199
d76048f5 200 // MGL redirects stdout and stderr to physical console, so lets redirect
4b6a582b
VZ
201 // it to file if WXSTDERR environment variable is set to be able to see
202 // wxLogDebug() output
343e418c
VS
203 wxString redirect;
204 if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
205 freopen(redirect.mb_str(), "wt", stderr);
2ec3892d 206
d76048f5
VS
207 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
208 if ( oldLog ) delete oldLog;
209
0471d486 210 return true;
7bdc1879
VS
211}
212
05e2b077 213bool wxApp::Initialize(int& argc, wxChar **argv)
7bdc1879 214{
05e2b077
VZ
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
79481be2
MW
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
94826170 228 if ( !wxAppBase::Initialize(argc, argv) )
ddfca47f 229 {
79481be2 230 MGL_exit();
94826170
VZ
231 return false;
232 }
7bdc1879 233
7bdc1879
VS
234#if wxUSE_INTL
235 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
236#endif
237
94826170 238 return true;
32b8ec41
VZ
239}
240
bfb8eb7e
MW
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//
246struct wxMGLFinalCleanup: public wxModule
247{
248 bool OnInit() { return true; }
249
250 void OnExit()
251 {
d7ae4a62 252 wxFontsManager::CleanUp();
bfb8eb7e
MW
253
254 wxDestroyMGL_WM();
255 MGL_exit();
256 }
257};
258
7bdc1879
VS
259void wxApp::CleanUp()
260{
d76048f5
VS
261 delete gs_rootWindow;
262
94826170 263 wxAppBase::CleanUp();
7bdc1879 264
bfb8eb7e 265 wxModule::RegisterModule(new wxMGLFinalCleanup);
7bdc1879 266}