]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "app.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 #include "wx/settings.h"
24 #include "wx/module.h"
25 #include "wx/evtloop.h"
27 #include "wx/dialog.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"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static bool gs_inYield
= FALSE
;
56 bool wxApp::Yield(bool onlyIfNeeded
)
62 wxFAIL_MSG( wxT("wxYield called recursively" ) );
69 if ( !wxThread::IsMain() )
71 // can't process events from other threads, MGL is thread-unsafe
74 #endif // wxUSE_THREADS
80 if ( wxEventLoop::GetActive() )
82 while (wxEventLoop::GetActive()->Pending())
83 wxEventLoop::GetActive()->Dispatch();
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()) { }
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 void wxApp::WakeUpIdle()
106 if (!wxThread::IsMain())
110 while (wxTheApp
->ProcessIdle())
114 if (!wxThread::IsMain())
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 class wxRootWindow
: public wxWindow
126 wxRootWindow() : wxWindow(NULL
, -1)
128 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
129 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
133 // we don't want to delete MGL_WM's rootWnd
137 virtual bool AcceptsFocus() const { return FALSE
; }
139 DECLARE_DYNAMIC_CLASS(wxRootWindow
)
142 IMPLEMENT_DYNAMIC_CLASS(wxRootWindow
, wxWindow
)
144 static wxRootWindow
*gs_rootWindow
= NULL
;
146 //-----------------------------------------------------------------------------
147 // MGL initialization
148 //-----------------------------------------------------------------------------
150 static bool wxCreateMGL_WM(const wxDisplayModeInfo
& displayMode
)
153 int refresh
= MGL_DEFAULT_REFRESH
;
155 #if wxUSE_SYSTEM_OPTIONS
156 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
157 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
160 mode
= MGL_findMode(displayMode
.GetWidth(),
161 displayMode
.GetHeight(),
162 displayMode
.GetDepth());
165 wxLogError(_("Mode %ix%i-%i not available."),
166 displayMode
.GetWidth(),
167 displayMode
.GetHeight(),
168 displayMode
.GetDepth());
171 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
172 if ( !g_displayDC
->isValid() )
179 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
186 static void wxDestroyMGL_WM()
190 MGL_wmDestroy(g_winMng
);
200 //-----------------------------------------------------------------------------
202 //-----------------------------------------------------------------------------
204 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
206 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
207 EVT_IDLE(wxApp::OnIdle
)
211 wxApp::wxApp() : m_mainLoop(NULL
)
219 wxDisplayModeInfo
wxGetDefaultDisplayMode()
224 if ( !wxGetEnv(wxT("WXMODE"), &mode
) ||
225 (wxSscanf(mode
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3) )
227 w
= 640, h
= 480, bpp
= 16;
230 return wxDisplayModeInfo(w
, h
, bpp
);
233 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
239 if ( g_displayDC
!= NULL
)
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!"));
247 if ( !wxCreateMGL_WM(mode
) )
249 gs_rootWindow
= new wxRootWindow
;
251 m_displayMode
= mode
;
256 bool wxApp::OnInitGui()
258 if ( !wxAppBase::OnInitGui() )
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
265 if ( wxGetEnv(wxT("WXSTDERR"), &redirect
) )
266 freopen(redirect
.mb_str(), "wt", stderr
);
269 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
270 if ( oldLog
) delete oldLog
;
275 bool wxApp::ProcessIdle()
278 event
.SetEventObject(this);
281 wxUpdateUIEvent::ResetUpdateTime();
283 return event
.MoreRequested();
286 void wxApp::OnIdle(wxIdleEvent
&event
)
288 static bool s_inOnIdle
= FALSE
;
290 /* Avoid recursion (via ProcessEvent default case) */
296 /* Resend in the main thread events which have been prepared in other
298 ProcessPendingEvents();
300 // 'Garbage' collection of windows deleted with Close().
301 DeletePendingObjects();
304 // flush the logged messages if any
305 wxLog::FlushActive();
308 // Send OnIdle events to all windows
309 if ( SendIdleEvents() )
310 event
.RequestMore(TRUE
);
315 bool wxApp::SendIdleEvents()
317 bool needMore
= FALSE
;
319 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
322 wxWindow
* win
= node
->GetData();
323 if ( SendIdleEvents(win
) )
325 node
= node
->GetNext();
331 bool wxApp::SendIdleEvents(wxWindow
* win
)
333 bool needMore
= FALSE
;
336 event
.SetEventObject(win
);
338 win
->GetEventHandler()->ProcessEvent(event
);
340 if ( event
.MoreRequested() )
343 wxNode
* node
= win
->GetChildren().First();
346 wxWindow
* win
= (wxWindow
*) node
->Data();
347 if ( SendIdleEvents(win
) )
355 int wxApp::MainLoop()
358 m_mainLoop
= new wxEventLoop
;
360 rt
= m_mainLoop
->Run();
367 void wxApp::ExitMainLoop()
373 bool wxApp::Initialized()
375 return (wxTopLevelWindows
.GetCount() != 0);
378 bool wxApp::Pending()
380 return wxEventLoop::GetActive()->Pending();
383 void wxApp::Dispatch()
385 wxEventLoop::GetActive()->Dispatch();
388 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
391 // VS: disable long filenames under DJGPP as the very first thing,
392 // since SciTech MGL doesn't like them much...
393 wxSetEnv(wxT("LFN"), wxT("N"));
396 // must do it before calling wxAppBase::Initialize(), because fonts are
397 // needed by stock lists which are created there
398 wxTheFontsManager
= new wxFontsManager
;
400 if ( !wxAppBase::Initialize(argc
, argv
) )
403 if ( MGL_init(".", NULL
) == 0 )
405 wxLogError(_("Cannot initialize SciTech MGL!"));
407 wxAppBase::CleanUp();
413 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
419 void wxApp::CleanUp()
421 delete gs_rootWindow
;
423 wxAppBase::CleanUp();
425 // must do this after calling base class CleanUp()
426 delete wxTheFontsManager
;
427 wxTheFontsManager
= (wxFontsManager
*) NULL
;