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