]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
Call MGL_init earlier in wxApp::Initialize to avoid crash
[wxWidgets.git] / src / mgl / app.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
32b8ec41
VZ
11 #pragma implementation "app.h"
12#endif
13
7bdc1879
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
2ec3892d 21
7bdc1879
VS
22#ifndef WX_PRECOMP
23 #include "wx/settings.h"
24 #include "wx/module.h"
7bdc1879
VS
25 #include "wx/frame.h"
26 #include "wx/dialog.h"
2ec3892d 27 #include "wx/log.h"
7bdc1879
VS
28 #include "wx/intl.h"
29#endif
32b8ec41 30
7bdc1879 31#include "wx/app.h"
90eb13e6 32#include "wx/evtloop.h"
ef344ff8 33#include "wx/fontutil.h"
df028524
VS
34#include "wx/univ/theme.h"
35#include "wx/univ/renderer.h"
58061670 36#include "wx/univ/colschem.h"
05c9ccbe 37#include "wx/sysopt.h"
7bdc1879 38#include "wx/mgl/private.h"
32b8ec41
VZ
39
40//-----------------------------------------------------------------------------
505f0a85 41// wxApp::Exit()
32b8ec41
VZ
42//-----------------------------------------------------------------------------
43
e2478fde 44void wxApp::Exit()
32b8ec41 45{
7bdc1879 46 MGL_exit();
32b8ec41
VZ
47 exit(0);
48}
49
50//-----------------------------------------------------------------------------
51// wxYield
52//-----------------------------------------------------------------------------
53
0471d486 54static bool gs_inYield = false;
7bdc1879 55
e1218bd6 56bool wxApp::Yield(bool onlyIfNeeded)
32b8ec41 57{
e1218bd6
VS
58 if ( gs_inYield )
59 {
60 if ( !onlyIfNeeded )
61 {
62 wxFAIL_MSG( wxT("wxYield called recursively" ) );
63 }
64
0471d486 65 return false;
e1218bd6
VS
66 }
67
7bdc1879
VS
68#if wxUSE_THREADS
69 if ( !wxThread::IsMain() )
70 {
71 // can't process events from other threads, MGL is thread-unsafe
0471d486 72 return true;
7bdc1879
VS
73 }
74#endif // wxUSE_THREADS
75
0471d486 76 gs_inYield = true;
7bdc1879
VS
77
78 wxLog::Suspend();
79
ef344ff8
VS
80 if ( wxEventLoop::GetActive() )
81 {
82 while (wxEventLoop::GetActive()->Pending())
83 wxEventLoop::GetActive()->Dispatch();
84 }
7c9955d1 85
7bdc1879
VS
86 /* it's necessary to call ProcessIdle() to update the frames sizes which
87 might have been changed (it also will update other things set from
88 OnUpdateUI() which is a nice (and desired) side effect) */
89 while (wxTheApp->ProcessIdle()) { }
90
91 wxLog::Resume();
92
0471d486 93 gs_inYield = false;
7bdc1879 94
0471d486 95 return true;
32b8ec41
VZ
96}
97
7bdc1879 98
32b8ec41
VZ
99//-----------------------------------------------------------------------------
100// wxWakeUpIdle
101//-----------------------------------------------------------------------------
102
e2478fde 103void wxApp::WakeUpIdle()
32b8ec41 104{
7bdc1879
VS
105#if wxUSE_THREADS
106 if (!wxThread::IsMain())
107 wxMutexGuiEnter();
108#endif
109
e2478fde
VZ
110 while (wxTheApp->ProcessIdle())
111 ;
7bdc1879
VS
112
113#if wxUSE_THREADS
114 if (!wxThread::IsMain())
115 wxMutexGuiLeave();
116#endif
32b8ec41
VZ
117}
118
58061670
VS
119//-----------------------------------------------------------------------------
120// Root window
121//-----------------------------------------------------------------------------
122
123class wxRootWindow : public wxWindow
124{
125 public:
0471d486 126 wxRootWindow() : wxWindow(NULL, wxID_ANY)
58061670
VS
127 {
128 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng));
129 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP));
130 }
131 ~wxRootWindow()
132 {
133 // we don't want to delete MGL_WM's rootWnd
7c9955d1 134 m_wnd = NULL;
58061670
VS
135 }
136
0471d486 137 virtual bool AcceptsFocus() const { return false; }
7c9955d1 138
b7d5acd0 139 DECLARE_DYNAMIC_CLASS(wxRootWindow)
58061670
VS
140};
141
b7d5acd0
VS
142IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow)
143
58061670
VS
144static wxRootWindow *gs_rootWindow = NULL;
145
146//-----------------------------------------------------------------------------
147// MGL initialization
148//-----------------------------------------------------------------------------
149
fedec981 150static bool wxCreateMGL_WM(const wxVideoMode& displayMode)
58061670
VS
151{
152 int mode;
58061670 153 int refresh = MGL_DEFAULT_REFRESH;
7c9955d1 154
58061670 155#if wxUSE_SYSTEM_OPTIONS
05c9ccbe 156 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
58061670
VS
157 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
158#endif
7c9955d1
JS
159
160 mode = MGL_findMode(displayMode.GetWidth(),
161 displayMode.GetHeight(),
634f6a1f 162 displayMode.GetDepth());
58061670
VS
163 if ( mode == -1 )
164 {
7c9955d1 165 wxLogError(_("Mode %ix%i-%i not available."),
d76048f5
VS
166 displayMode.GetWidth(),
167 displayMode.GetHeight(),
634f6a1f 168 displayMode.GetDepth());
0471d486 169 return false;
58061670
VS
170 }
171 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
172 if ( !g_displayDC->isValid() )
173 {
174 delete g_displayDC;
175 g_displayDC = NULL;
0471d486 176 return false;
58061670 177 }
7c9955d1 178
58061670
VS
179 g_winMng = MGL_wmCreate(g_displayDC->getDC());
180 if (!g_winMng)
0471d486 181 return false;
1f43b5c9 182
0471d486 183 return true;
58061670
VS
184}
185
186static void wxDestroyMGL_WM()
187{
188 if ( g_winMng )
189 {
190 MGL_wmDestroy(g_winMng);
191 g_winMng = NULL;
192 }
193 if ( g_displayDC )
194 {
195 delete g_displayDC;
196 g_displayDC = NULL;
197 }
198}
199
32b8ec41
VZ
200//-----------------------------------------------------------------------------
201// wxApp
202//-----------------------------------------------------------------------------
203
204IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
205
206BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 207 EVT_IDLE(wxAppBase::OnIdle)
32b8ec41
VZ
208END_EVENT_TABLE()
209
210
752464f9 211wxApp::wxApp()
7bdc1879
VS
212{
213}
214
215wxApp::~wxApp()
216{
217}
218
fedec981 219wxVideoMode wxGetDefaultDisplayMode()
a3e76614
VS
220{
221 wxString mode;
222 unsigned w, h, bpp;
223
7c9955d1 224 if ( !wxGetEnv(wxT("WXMODE"), &mode) ||
a3e76614
VS
225 (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) )
226 {
227 w = 640, h = 480, bpp = 16;
228 }
229
fedec981 230 return wxVideoMode(w, h, bpp);
a3e76614
VS
231}
232
fedec981 233bool wxApp::SetDisplayMode(const wxVideoMode& mode)
634f6a1f
VS
234{
235 if ( !mode.IsOk() )
236 {
0471d486 237 return false;
634f6a1f
VS
238 }
239 if ( g_displayDC != NULL )
240 {
241 // FIXME_MGL -- we currently don't allow to switch video mode
1f43b5c9 242 // more than once. This can hopefully be changed...
634f6a1f 243 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
0471d486 244 return false;
634f6a1f 245 }
1f43b5c9
VS
246
247 if ( !wxCreateMGL_WM(mode) )
0471d486 248 return false;
1f43b5c9
VS
249 gs_rootWindow = new wxRootWindow;
250
634f6a1f 251 m_displayMode = mode;
1f43b5c9 252
0471d486 253 return true;
634f6a1f
VS
254}
255
7bdc1879
VS
256bool wxApp::OnInitGui()
257{
7bdc1879 258 if ( !wxAppBase::OnInitGui() )
0471d486 259 return false;
7bdc1879 260
d76048f5
VS
261#ifdef __WXDEBUG__
262 // MGL redirects stdout and stderr to physical console, so lets redirect
0471d486 263 // it to file in debug build. Do it only when WXSTDERR environment variable is set
343e418c
VS
264 wxString redirect;
265 if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
266 freopen(redirect.mb_str(), "wt", stderr);
0471d486 267#endif // __WXDEBUG__
2ec3892d 268
d76048f5
VS
269 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
270 if ( oldLog ) delete oldLog;
271
0471d486 272 return true;
7bdc1879
VS
273}
274
05e2b077 275bool wxApp::Initialize(int& argc, wxChar **argv)
7bdc1879 276{
05e2b077
VZ
277#ifdef __DJGPP__
278 // VS: disable long filenames under DJGPP as the very first thing,
279 // since SciTech MGL doesn't like them much...
280 wxSetEnv(wxT("LFN"), wxT("N"));
281#endif
282
79481be2
MW
283 // intialize MGL before creating wxFontsManager since it uses MGL funcs
284 if ( MGL_init(".", NULL) == 0 )
285 {
286 wxLogError(_("Cannot initialize SciTech MGL!"));
287 return false;
288 }
289
94826170
VZ
290 // must do it before calling wxAppBase::Initialize(), because fonts are
291 // needed by stock lists which are created there
292 wxTheFontsManager = new wxFontsManager;
32b8ec41 293
94826170 294 if ( !wxAppBase::Initialize(argc, argv) )
ddfca47f 295 {
79481be2
MW
296 delete wxTheFontsManager;
297 wxTheFontsManager = NULL;
298 MGL_exit();
94826170
VZ
299 return false;
300 }
7bdc1879 301
7bdc1879
VS
302#if wxUSE_INTL
303 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
304#endif
305
94826170 306 return true;
32b8ec41
VZ
307}
308
7bdc1879
VS
309void wxApp::CleanUp()
310{
d76048f5
VS
311 delete gs_rootWindow;
312
94826170 313 wxAppBase::CleanUp();
7bdc1879 314
94826170 315 // must do this after calling base class CleanUp()
df16a53e
VS
316 delete wxTheFontsManager;
317 wxTheFontsManager = (wxFontsManager*) NULL;
318
7bdc1879
VS
319 wxDestroyMGL_WM();
320 MGL_exit();
321}
322