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