]>
Commit | Line | Data |
---|---|---|
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 | bool wxApp::Yield(bool onlyIfNeeded) | |
52 | { | |
53 | if ( m_isInsideYield ) | |
54 | { | |
55 | if ( !onlyIfNeeded ) | |
56 | { | |
57 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
58 | } | |
59 | ||
60 | return false; | |
61 | } | |
62 | ||
63 | #if wxUSE_THREADS | |
64 | if ( !wxThread::IsMain() ) | |
65 | { | |
66 | // can't process events from other threads, MGL is thread-unsafe | |
67 | return true; | |
68 | } | |
69 | #endif // wxUSE_THREADS | |
70 | ||
71 | m_isInsideYield = true; | |
72 | ||
73 | wxLog::Suspend(); | |
74 | ||
75 | wxEventLoopBase * const eventLoop = wxEventLoop::GetActive(); | |
76 | if ( eventLoop ) | |
77 | { | |
78 | while (eventLoop->Pending()) | |
79 | eventLoop->Dispatch(); | |
80 | } | |
81 | ||
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 | ||
89 | m_isInsideYield = false; | |
90 | ||
91 | return true; | |
92 | } | |
93 | ||
94 | ||
95 | //----------------------------------------------------------------------------- | |
96 | // wxWakeUpIdle | |
97 | //----------------------------------------------------------------------------- | |
98 | ||
99 | void wxApp::WakeUpIdle() | |
100 | { | |
101 | #if wxUSE_THREADS | |
102 | if (!wxThread::IsMain()) | |
103 | wxMutexGuiEnter(); | |
104 | #endif | |
105 | ||
106 | while (wxTheApp->ProcessIdle()) | |
107 | ; | |
108 | ||
109 | #if wxUSE_THREADS | |
110 | if (!wxThread::IsMain()) | |
111 | wxMutexGuiLeave(); | |
112 | #endif | |
113 | } | |
114 | ||
115 | //----------------------------------------------------------------------------- | |
116 | // Root window | |
117 | //----------------------------------------------------------------------------- | |
118 | ||
119 | class wxRootWindow : public wxWindow | |
120 | { | |
121 | public: | |
122 | wxRootWindow() : wxWindow(NULL, wxID_ANY) | |
123 | { | |
124 | SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng)); | |
125 | SetBackgroundColour(wxTHEME_COLOUR(DESKTOP)); | |
126 | } | |
127 | virtual ~wxRootWindow() | |
128 | { | |
129 | // we don't want to delete MGL_WM's rootWnd | |
130 | m_wnd = NULL; | |
131 | } | |
132 | ||
133 | virtual bool AcceptsFocus() const { return false; } | |
134 | ||
135 | DECLARE_DYNAMIC_CLASS(wxRootWindow) | |
136 | }; | |
137 | ||
138 | IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow) | |
139 | ||
140 | static wxRootWindow *gs_rootWindow = NULL; | |
141 | ||
142 | //----------------------------------------------------------------------------- | |
143 | // MGL initialization | |
144 | //----------------------------------------------------------------------------- | |
145 | ||
146 | static bool wxCreateMGL_WM(const wxVideoMode& displayMode) | |
147 | { | |
148 | int mode; | |
149 | int refresh = MGL_DEFAULT_REFRESH; | |
150 | ||
151 | #if wxUSE_SYSTEM_OPTIONS | |
152 | if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) ) | |
153 | refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh")); | |
154 | #endif | |
155 | ||
156 | mode = MGL_findMode(displayMode.GetWidth(), | |
157 | displayMode.GetHeight(), | |
158 | displayMode.GetDepth()); | |
159 | if ( mode == -1 ) | |
160 | { | |
161 | wxLogError(_("Mode %ix%i-%i not available."), | |
162 | displayMode.GetWidth(), | |
163 | displayMode.GetHeight(), | |
164 | displayMode.GetDepth()); | |
165 | return false; | |
166 | } | |
167 | g_displayDC = new MGLDisplayDC(mode, 1, refresh); | |
168 | if ( !g_displayDC->isValid() ) | |
169 | { | |
170 | delete g_displayDC; | |
171 | g_displayDC = NULL; | |
172 | return false; | |
173 | } | |
174 | ||
175 | g_winMng = MGL_wmCreate(g_displayDC->getDC()); | |
176 | if (!g_winMng) | |
177 | return false; | |
178 | ||
179 | return true; | |
180 | } | |
181 | ||
182 | static 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 | ||
196 | //----------------------------------------------------------------------------- | |
197 | // wxApp | |
198 | //----------------------------------------------------------------------------- | |
199 | ||
200 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
201 | ||
202 | wxApp::wxApp() | |
203 | { | |
204 | } | |
205 | ||
206 | wxApp::~wxApp() | |
207 | { | |
208 | } | |
209 | ||
210 | wxVideoMode wxGetDefaultDisplayMode() | |
211 | { | |
212 | wxString mode; | |
213 | unsigned w, h, bpp; | |
214 | ||
215 | if ( !wxGetEnv(wxT("WXMODE"), &mode) || | |
216 | (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) ) | |
217 | { | |
218 | w = 640, h = 480, bpp = 16; | |
219 | } | |
220 | ||
221 | return wxVideoMode(w, h, bpp); | |
222 | } | |
223 | ||
224 | bool wxApp::SetDisplayMode(const wxVideoMode& mode) | |
225 | { | |
226 | if ( !mode.IsOk() ) | |
227 | { | |
228 | return false; | |
229 | } | |
230 | if ( g_displayDC != NULL ) | |
231 | { | |
232 | // FIXME_MGL -- we currently don't allow to switch video mode | |
233 | // more than once. This can hopefully be changed... | |
234 | wxFAIL_MSG(wxT("Can't change display mode after intialization!")); | |
235 | return false; | |
236 | } | |
237 | ||
238 | if ( !wxCreateMGL_WM(mode) ) | |
239 | return false; | |
240 | gs_rootWindow = new wxRootWindow; | |
241 | ||
242 | m_displayMode = mode; | |
243 | ||
244 | return true; | |
245 | } | |
246 | ||
247 | bool wxApp::OnInitGui() | |
248 | { | |
249 | if ( !wxAppBase::OnInitGui() ) | |
250 | return false; | |
251 | ||
252 | #ifdef __WXDEBUG__ | |
253 | // MGL redirects stdout and stderr to physical console, so lets redirect | |
254 | // it to file in debug build. Do it only when WXSTDERR environment variable is set | |
255 | wxString redirect; | |
256 | if ( wxGetEnv(wxT("WXSTDERR"), &redirect) ) | |
257 | freopen(redirect.mb_str(), "wt", stderr); | |
258 | #endif // __WXDEBUG__ | |
259 | ||
260 | wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui); | |
261 | if ( oldLog ) delete oldLog; | |
262 | ||
263 | return true; | |
264 | } | |
265 | ||
266 | bool wxApp::Initialize(int& argc, wxChar **argv) | |
267 | { | |
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 | ||
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 | ||
281 | if ( !wxAppBase::Initialize(argc, argv) ) | |
282 | { | |
283 | MGL_exit(); | |
284 | return false; | |
285 | } | |
286 | ||
287 | #if wxUSE_INTL | |
288 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
289 | #endif | |
290 | ||
291 | return true; | |
292 | } | |
293 | ||
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 | // | |
299 | struct wxMGLFinalCleanup: public wxModule | |
300 | { | |
301 | bool OnInit() { return true; } | |
302 | ||
303 | void OnExit() | |
304 | { | |
305 | wxFontsManager::CleanUp(); | |
306 | ||
307 | wxDestroyMGL_WM(); | |
308 | MGL_exit(); | |
309 | } | |
310 | }; | |
311 | ||
312 | void wxApp::CleanUp() | |
313 | { | |
314 | delete gs_rootWindow; | |
315 | ||
316 | wxAppBase::CleanUp(); | |
317 | ||
318 | wxModule::RegisterModule(new wxMGLFinalCleanup); | |
319 | } |