]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/app.cpp
Various compilation fixes.
[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 //-----------------------------------------------------------------------------
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 delete g_displayDC;
124 g_displayDC = NULL;
125 return false;
126 }
127
128 g_winMng = MGL_wmCreate(g_displayDC->getDC());
129 if (!g_winMng)
130 return false;
131
132 return true;
133 }
134
135 static void wxDestroyMGL_WM()
136 {
137 if ( g_winMng )
138 {
139 MGL_wmDestroy(g_winMng);
140 g_winMng = NULL;
141 }
142 if ( g_displayDC )
143 {
144 delete g_displayDC;
145 g_displayDC = NULL;
146 }
147 }
148
149 //-----------------------------------------------------------------------------
150 // wxApp
151 //-----------------------------------------------------------------------------
152
153 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
154
155 wxApp::wxApp()
156 {
157 }
158
159 wxApp::~wxApp()
160 {
161 }
162
163 wxVideoMode wxGetDefaultDisplayMode()
164 {
165 wxString mode;
166 unsigned w, h, bpp;
167
168 if ( !wxGetEnv(wxT("WXMODE"), &mode) ||
169 (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) )
170 {
171 w = 640, h = 480, bpp = 16;
172 }
173
174 return wxVideoMode(w, h, bpp);
175 }
176
177 bool wxApp::SetDisplayMode(const wxVideoMode& mode)
178 {
179 if ( !mode.IsOk() )
180 {
181 return false;
182 }
183 if ( g_displayDC != NULL )
184 {
185 // FIXME_MGL -- we currently don't allow to switch video mode
186 // more than once. This can hopefully be changed...
187 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
188 return false;
189 }
190
191 if ( !wxCreateMGL_WM(mode) )
192 return false;
193 gs_rootWindow = new wxRootWindow;
194
195 m_displayMode = mode;
196
197 return true;
198 }
199
200 bool wxApp::OnInitGui()
201 {
202 if ( !wxAppBase::OnInitGui() )
203 return false;
204
205 #ifdef __WXDEBUG__
206 // MGL redirects stdout and stderr to physical console, so lets redirect
207 // it to file in debug build. Do it only when WXSTDERR environment variable is set
208 wxString redirect;
209 if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
210 freopen(redirect.mb_str(), "wt", stderr);
211 #endif // __WXDEBUG__
212
213 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
214 if ( oldLog ) delete oldLog;
215
216 return true;
217 }
218
219 bool wxApp::Initialize(int& argc, wxChar **argv)
220 {
221 #ifdef __DJGPP__
222 // VS: disable long filenames under DJGPP as the very first thing,
223 // since SciTech MGL doesn't like them much...
224 wxSetEnv(wxT("LFN"), wxT("N"));
225 #endif
226
227 // intialize MGL before creating wxFontsManager since it uses MGL funcs
228 if ( MGL_init(".", NULL) == 0 )
229 {
230 wxLogError(_("Cannot initialize SciTech MGL!"));
231 return false;
232 }
233
234 if ( !wxAppBase::Initialize(argc, argv) )
235 {
236 MGL_exit();
237 return false;
238 }
239
240 #if wxUSE_INTL
241 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
242 #endif
243
244 return true;
245 }
246
247 // Modules are cleaned up after wxApp::CleanUp(), and some modules may
248 // require MGL to still be alive, e.g. the stock fonts need the fonts
249 // manager. So append this module last minute in wxApp::CleanUp() to close
250 // down MGL after all the other modules have been cleaned up.
251 //
252 struct wxMGLFinalCleanup: public wxModule
253 {
254 bool OnInit() { return true; }
255
256 void OnExit()
257 {
258 wxFontsManager::CleanUp();
259
260 wxDestroyMGL_WM();
261 MGL_exit();
262 }
263 };
264
265 void wxApp::CleanUp()
266 {
267 delete gs_rootWindow;
268
269 wxAppBase::CleanUp();
270
271 wxModule::RegisterModule(new wxMGLFinalCleanup);
272 }