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