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