1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/app.cpp
3 // Author: Vaclav Slavik
4 // based on GTK and MSW implementations
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
20 #include "wx/settings.h"
22 #include "wx/dialog.h"
25 #include "wx/module.h"
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"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 bool wxApp::DoYield(bool onlyIfNeeded
, long eventsToProcess
)
53 if ( m_isInsideYield
)
57 wxFAIL_MSG( wxT("wxYield called recursively" ) );
64 if ( !wxThread::IsMain() )
66 // can't process events from other threads, MGL is thread-unsafe
69 #endif // wxUSE_THREADS
71 m_isInsideYield
= true;
72 m_eventsToProcessInsideYield
= eventsToProcess
;
76 wxEventLoopBase
* const eventLoop
= wxEventLoop::GetActive();
79 // TODO: implement event filtering using the eventsToProcess mask
81 while (eventLoop
->Pending())
82 eventLoop
->Dispatch();
85 /* it's necessary to call ProcessIdle() to update the frames sizes which
86 might have been changed (it also will update other things set from
87 OnUpdateUI() which is a nice (and desired) side effect) */
88 while (wxTheApp
->ProcessIdle()) { }
92 m_isInsideYield
= false;
98 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
102 void wxApp::WakeUpIdle()
105 if (!wxThread::IsMain())
109 while (wxTheApp
->ProcessIdle())
113 if (!wxThread::IsMain())
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 class wxRootWindow
: public wxWindow
125 wxRootWindow() : wxWindow(NULL
, wxID_ANY
)
127 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
128 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
130 virtual ~wxRootWindow()
132 // we don't want to delete MGL_WM's rootWnd
136 virtual bool AcceptsFocus() const { return false; }
138 DECLARE_DYNAMIC_CLASS(wxRootWindow
)
141 IMPLEMENT_DYNAMIC_CLASS(wxRootWindow
, wxWindow
)
143 static wxRootWindow
*gs_rootWindow
= NULL
;
145 //-----------------------------------------------------------------------------
146 // MGL initialization
147 //-----------------------------------------------------------------------------
149 static bool wxCreateMGL_WM(const wxVideoMode
& displayMode
)
152 int refresh
= MGL_DEFAULT_REFRESH
;
154 #if wxUSE_SYSTEM_OPTIONS
155 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
156 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
159 mode
= MGL_findMode(displayMode
.GetWidth(),
160 displayMode
.GetHeight(),
161 displayMode
.GetDepth());
164 wxLogError(_("Mode %ix%i-%i not available."),
165 displayMode
.GetWidth(),
166 displayMode
.GetHeight(),
167 displayMode
.GetDepth());
170 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
171 if ( !g_displayDC
->isValid() )
178 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
185 static void wxDestroyMGL_WM()
189 MGL_wmDestroy(g_winMng
);
199 //-----------------------------------------------------------------------------
201 //-----------------------------------------------------------------------------
203 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
213 wxVideoMode
wxGetDefaultDisplayMode()
218 if ( !wxGetEnv(wxT("WXMODE"), &mode
) ||
219 (wxSscanf(mode
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3) )
221 w
= 640, h
= 480, bpp
= 16;
224 return wxVideoMode(w
, h
, bpp
);
227 bool wxApp::SetDisplayMode(const wxVideoMode
& mode
)
233 if ( g_displayDC
!= NULL
)
235 // FIXME_MGL -- we currently don't allow to switch video mode
236 // more than once. This can hopefully be changed...
237 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
241 if ( !wxCreateMGL_WM(mode
) )
243 gs_rootWindow
= new wxRootWindow
;
245 m_displayMode
= mode
;
250 bool wxApp::OnInitGui()
252 if ( !wxAppBase::OnInitGui() )
256 // MGL redirects stdout and stderr to physical console, so lets redirect
257 // it to file in debug build. Do it only when WXSTDERR environment variable is set
259 if ( wxGetEnv(wxT("WXSTDERR"), &redirect
) )
260 freopen(redirect
.mb_str(), "wt", stderr
);
261 #endif // __WXDEBUG__
263 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
264 if ( oldLog
) delete oldLog
;
269 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
272 // VS: disable long filenames under DJGPP as the very first thing,
273 // since SciTech MGL doesn't like them much...
274 wxSetEnv(wxT("LFN"), wxT("N"));
277 // intialize MGL before creating wxFontsManager since it uses MGL funcs
278 if ( MGL_init(".", NULL
) == 0 )
280 wxLogError(_("Cannot initialize SciTech MGL!"));
284 if ( !wxAppBase::Initialize(argc
, argv
) )
291 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
297 // Modules are cleaned up after wxApp::CleanUp(), and some modules may
298 // require MGL to still be alive, e.g. the stock fonts need the fonts
299 // manager. So append this module last minute in wxApp::CleanUp() to close
300 // down MGL after all the other modules have been cleaned up.
302 struct wxMGLFinalCleanup
: public wxModule
304 bool OnInit() { return true; }
308 wxFontsManager::CleanUp();
315 void wxApp::CleanUp()
317 delete gs_rootWindow
;
319 wxAppBase::CleanUp();
321 wxModule::RegisterModule(new wxMGLFinalCleanup
);