]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
11 | #pragma implementation "app.h" | |
12 | #endif | |
13 | ||
14 | // For compilers that support precompilation, includes "wx.h". | |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/settings.h" | |
24 | #include "wx/module.h" | |
25 | #include "wx/evtloop.h" | |
26 | #include "wx/frame.h" | |
27 | #include "wx/dialog.h" | |
28 | #include "wx/log.h" | |
29 | #include "wx/intl.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/app.h" | |
33 | #include "wx/fontutil.h" | |
34 | #include "wx/univ/theme.h" | |
35 | #include "wx/univ/renderer.h" | |
36 | #include "wx/univ/colschem.h" | |
37 | #include "wx/sysopt.h" | |
38 | #include "wx/mgl/private.h" | |
39 | ||
40 | //----------------------------------------------------------------------------- | |
41 | // wxApp::Exit() | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | void wxApp::Exit() | |
45 | { | |
46 | MGL_exit(); | |
47 | exit(0); | |
48 | } | |
49 | ||
50 | //----------------------------------------------------------------------------- | |
51 | // wxYield | |
52 | //----------------------------------------------------------------------------- | |
53 | ||
54 | static bool gs_inYield = FALSE; | |
55 | ||
56 | bool wxApp::Yield(bool onlyIfNeeded) | |
57 | { | |
58 | if ( gs_inYield ) | |
59 | { | |
60 | if ( !onlyIfNeeded ) | |
61 | { | |
62 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
63 | } | |
64 | ||
65 | return FALSE; | |
66 | } | |
67 | ||
68 | #if wxUSE_THREADS | |
69 | if ( !wxThread::IsMain() ) | |
70 | { | |
71 | // can't process events from other threads, MGL is thread-unsafe | |
72 | return TRUE; | |
73 | } | |
74 | #endif // wxUSE_THREADS | |
75 | ||
76 | gs_inYield = TRUE; | |
77 | ||
78 | wxLog::Suspend(); | |
79 | ||
80 | if ( wxEventLoop::GetActive() ) | |
81 | { | |
82 | while (wxEventLoop::GetActive()->Pending()) | |
83 | wxEventLoop::GetActive()->Dispatch(); | |
84 | } | |
85 | ||
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 | ||
93 | gs_inYield = FALSE; | |
94 | ||
95 | return TRUE; | |
96 | } | |
97 | ||
98 | ||
99 | //----------------------------------------------------------------------------- | |
100 | // wxWakeUpIdle | |
101 | //----------------------------------------------------------------------------- | |
102 | ||
103 | void wxApp::WakeUpIdle() | |
104 | { | |
105 | #if wxUSE_THREADS | |
106 | if (!wxThread::IsMain()) | |
107 | wxMutexGuiEnter(); | |
108 | #endif | |
109 | ||
110 | while (wxTheApp->ProcessIdle()) | |
111 | ; | |
112 | ||
113 | #if wxUSE_THREADS | |
114 | if (!wxThread::IsMain()) | |
115 | wxMutexGuiLeave(); | |
116 | #endif | |
117 | } | |
118 | ||
119 | //----------------------------------------------------------------------------- | |
120 | // Root window | |
121 | //----------------------------------------------------------------------------- | |
122 | ||
123 | class wxRootWindow : public wxWindow | |
124 | { | |
125 | public: | |
126 | wxRootWindow() : wxWindow(NULL, -1) | |
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 | |
134 | m_wnd = NULL; | |
135 | } | |
136 | ||
137 | virtual bool AcceptsFocus() const { return FALSE; } | |
138 | ||
139 | DECLARE_DYNAMIC_CLASS(wxRootWindow) | |
140 | }; | |
141 | ||
142 | IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow) | |
143 | ||
144 | static wxRootWindow *gs_rootWindow = NULL; | |
145 | ||
146 | //----------------------------------------------------------------------------- | |
147 | // MGL initialization | |
148 | //----------------------------------------------------------------------------- | |
149 | ||
150 | static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode) | |
151 | { | |
152 | int mode; | |
153 | int refresh = MGL_DEFAULT_REFRESH; | |
154 | ||
155 | #if wxUSE_SYSTEM_OPTIONS | |
156 | if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) ) | |
157 | refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh")); | |
158 | #endif | |
159 | ||
160 | mode = MGL_findMode(displayMode.GetWidth(), | |
161 | displayMode.GetHeight(), | |
162 | displayMode.GetDepth()); | |
163 | if ( mode == -1 ) | |
164 | { | |
165 | wxLogError(_("Mode %ix%i-%i not available."), | |
166 | displayMode.GetWidth(), | |
167 | displayMode.GetHeight(), | |
168 | displayMode.GetDepth()); | |
169 | return FALSE; | |
170 | } | |
171 | g_displayDC = new MGLDisplayDC(mode, 1, refresh); | |
172 | if ( !g_displayDC->isValid() ) | |
173 | { | |
174 | delete g_displayDC; | |
175 | g_displayDC = NULL; | |
176 | return FALSE; | |
177 | } | |
178 | ||
179 | g_winMng = MGL_wmCreate(g_displayDC->getDC()); | |
180 | if (!g_winMng) | |
181 | return FALSE; | |
182 | ||
183 | return TRUE; | |
184 | } | |
185 | ||
186 | static 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 | ||
200 | //----------------------------------------------------------------------------- | |
201 | // wxApp | |
202 | //----------------------------------------------------------------------------- | |
203 | ||
204 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
205 | ||
206 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
207 | EVT_IDLE(wxAppBase::OnIdle) | |
208 | END_EVENT_TABLE() | |
209 | ||
210 | ||
211 | wxApp::wxApp() : m_mainLoop(NULL) | |
212 | { | |
213 | } | |
214 | ||
215 | wxApp::~wxApp() | |
216 | { | |
217 | } | |
218 | ||
219 | wxDisplayModeInfo wxGetDefaultDisplayMode() | |
220 | { | |
221 | wxString mode; | |
222 | unsigned w, h, bpp; | |
223 | ||
224 | if ( !wxGetEnv(wxT("WXMODE"), &mode) || | |
225 | (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) ) | |
226 | { | |
227 | w = 640, h = 480, bpp = 16; | |
228 | } | |
229 | ||
230 | return wxDisplayModeInfo(w, h, bpp); | |
231 | } | |
232 | ||
233 | bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode) | |
234 | { | |
235 | if ( !mode.IsOk() ) | |
236 | { | |
237 | return FALSE; | |
238 | } | |
239 | if ( g_displayDC != NULL ) | |
240 | { | |
241 | // FIXME_MGL -- we currently don't allow to switch video mode | |
242 | // more than once. This can hopefully be changed... | |
243 | wxFAIL_MSG(wxT("Can't change display mode after intialization!")); | |
244 | return FALSE; | |
245 | } | |
246 | ||
247 | if ( !wxCreateMGL_WM(mode) ) | |
248 | return FALSE; | |
249 | gs_rootWindow = new wxRootWindow; | |
250 | ||
251 | m_displayMode = mode; | |
252 | ||
253 | return TRUE; | |
254 | } | |
255 | ||
256 | bool wxApp::OnInitGui() | |
257 | { | |
258 | if ( !wxAppBase::OnInitGui() ) | |
259 | return FALSE; | |
260 | ||
261 | #ifdef __WXDEBUG__ | |
262 | // MGL redirects stdout and stderr to physical console, so lets redirect | |
263 | // it to file. Do it only when WXDEBUG environment variable is set | |
264 | wxString redirect; | |
265 | if ( wxGetEnv(wxT("WXSTDERR"), &redirect) ) | |
266 | freopen(redirect.mb_str(), "wt", stderr); | |
267 | #endif | |
268 | ||
269 | wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui); | |
270 | if ( oldLog ) delete oldLog; | |
271 | ||
272 | return TRUE; | |
273 | } | |
274 | ||
275 | int wxApp::MainLoop() | |
276 | { | |
277 | int rt; | |
278 | m_mainLoop = new wxEventLoop; | |
279 | ||
280 | rt = m_mainLoop->Run(); | |
281 | ||
282 | delete m_mainLoop; | |
283 | m_mainLoop = NULL; | |
284 | return rt; | |
285 | } | |
286 | ||
287 | void wxApp::ExitMainLoop() | |
288 | { | |
289 | if ( m_mainLoop ) | |
290 | m_mainLoop->Exit(0); | |
291 | } | |
292 | ||
293 | bool wxApp::Initialized() | |
294 | { | |
295 | return (wxTopLevelWindows.GetCount() != 0); | |
296 | } | |
297 | ||
298 | bool wxApp::Pending() | |
299 | { | |
300 | return wxEventLoop::GetActive()->Pending(); | |
301 | } | |
302 | ||
303 | void wxApp::Dispatch() | |
304 | { | |
305 | wxEventLoop::GetActive()->Dispatch(); | |
306 | } | |
307 | ||
308 | bool wxApp::Initialize(int& argc, wxChar **argv) | |
309 | { | |
310 | #ifdef __DJGPP__ | |
311 | // VS: disable long filenames under DJGPP as the very first thing, | |
312 | // since SciTech MGL doesn't like them much... | |
313 | wxSetEnv(wxT("LFN"), wxT("N")); | |
314 | #endif | |
315 | ||
316 | // must do it before calling wxAppBase::Initialize(), because fonts are | |
317 | // needed by stock lists which are created there | |
318 | wxTheFontsManager = new wxFontsManager; | |
319 | ||
320 | if ( !wxAppBase::Initialize(argc, argv) ) | |
321 | return false; | |
322 | ||
323 | if ( MGL_init(".", NULL) == 0 ) | |
324 | { | |
325 | wxLogError(_("Cannot initialize SciTech MGL!")); | |
326 | ||
327 | wxAppBase::CleanUp(); | |
328 | ||
329 | return false; | |
330 | } | |
331 | ||
332 | #if wxUSE_INTL | |
333 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
334 | #endif | |
335 | ||
336 | return true; | |
337 | } | |
338 | ||
339 | void wxApp::CleanUp() | |
340 | { | |
341 | delete gs_rootWindow; | |
342 | ||
343 | wxAppBase::CleanUp(); | |
344 | ||
345 | // must do this after calling base class CleanUp() | |
346 | delete wxTheFontsManager; | |
347 | wxTheFontsManager = (wxFontsManager*) NULL; | |
348 | ||
349 | wxDestroyMGL_WM(); | |
350 | MGL_exit(); | |
351 | } | |
352 |