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"
30 #include "wx/resource.h"
34 #include "wx/fontutil.h"
35 #include "wx/univ/theme.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/colschem.h"
38 #include "wx/sysopt.h"
39 #include "wx/mgl/private.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 wxApp
*wxTheApp
= NULL
;
46 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 static bool gs_inYield
= FALSE
;
65 bool wxApp::Yield(bool onlyIfNeeded
)
71 wxFAIL_MSG( wxT("wxYield called recursively" ) );
78 if ( !wxThread::IsMain() )
80 // can't process events from other threads, MGL is thread-unsafe
83 #endif // wxUSE_THREADS
89 if ( wxEventLoop::GetActive() )
91 while (wxEventLoop::GetActive()->Pending())
92 wxEventLoop::GetActive()->Dispatch();
95 /* it's necessary to call ProcessIdle() to update the frames sizes which
96 might have been changed (it also will update other things set from
97 OnUpdateUI() which is a nice (and desired) side effect) */
98 while (wxTheApp
->ProcessIdle()) { }
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
115 if (!wxThread::IsMain())
119 while (wxTheApp
->ProcessIdle()) {}
122 if (!wxThread::IsMain())
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 class wxRootWindow
: public wxWindow
134 wxRootWindow() : wxWindow(NULL
, -1)
136 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
137 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
141 // we don't want to delete MGL_WM's rootWnd
145 virtual bool AcceptsFocus() const { return FALSE
; }
147 DECLARE_DYNAMIC_CLASS(wxRootWindow
)
150 IMPLEMENT_DYNAMIC_CLASS(wxRootWindow
, wxWindow
)
152 static wxRootWindow
*gs_rootWindow
= NULL
;
154 //-----------------------------------------------------------------------------
155 // MGL initialization
156 //-----------------------------------------------------------------------------
158 static bool wxCreateMGL_WM(const wxDisplayModeInfo
& displayMode
)
161 int refresh
= MGL_DEFAULT_REFRESH
;
163 #if wxUSE_SYSTEM_OPTIONS
164 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
165 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
168 mode
= MGL_findMode(displayMode
.GetWidth(),
169 displayMode
.GetHeight(),
170 displayMode
.GetDepth());
173 wxLogError(_("Mode %ix%i-%i not available."),
174 displayMode
.GetWidth(),
175 displayMode
.GetHeight(),
176 displayMode
.GetDepth());
179 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
180 if ( !g_displayDC
->isValid() )
187 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
194 static void wxDestroyMGL_WM()
198 MGL_wmDestroy(g_winMng
);
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
214 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
215 EVT_IDLE(wxApp::OnIdle
)
219 wxApp::wxApp() : m_mainLoop(NULL
)
227 wxDisplayModeInfo
wxGetDefaultDisplayMode()
232 if ( !wxGetEnv(wxT("WXMODE"), &mode
) ||
233 (wxSscanf(mode
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3) )
235 w
= 640, h
= 480, bpp
= 16;
238 return wxDisplayModeInfo(w
, h
, bpp
);
241 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
247 if ( g_displayDC
!= NULL
)
249 // FIXME_MGL -- we currently don't allow to switch video mode
250 // more than once. This can hopefully be changed...
251 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
255 if ( !wxCreateMGL_WM(mode
) )
257 gs_rootWindow
= new wxRootWindow
;
259 m_displayMode
= mode
;
264 bool wxApp::OnInitGui()
266 if ( !wxAppBase::OnInitGui() )
270 // MGL redirects stdout and stderr to physical console, so lets redirect
271 // it to file. Do it only when WXDEBUG environment variable is set
273 if ( wxGetEnv(wxT("WXSTDERR"), &redirect
) )
274 freopen(redirect
.mb_str(), "wt", stderr
);
277 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
278 if ( oldLog
) delete oldLog
;
283 bool wxApp::ProcessIdle()
286 event
.SetEventObject(this);
289 return event
.MoreRequested();
292 void wxApp::OnIdle(wxIdleEvent
&event
)
294 static bool s_inOnIdle
= FALSE
;
296 /* Avoid recursion (via ProcessEvent default case) */
302 /* Resend in the main thread events which have been prepared in other
304 ProcessPendingEvents();
306 // 'Garbage' collection of windows deleted with Close().
307 DeletePendingObjects();
310 // flush the logged messages if any
311 wxLog::FlushActive();
314 // Send OnIdle events to all windows
315 if ( SendIdleEvents() )
316 event
.RequestMore(TRUE
);
321 bool wxApp::SendIdleEvents()
323 bool needMore
= FALSE
;
325 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
328 wxWindow
* win
= node
->GetData();
329 if ( SendIdleEvents(win
) )
331 node
= node
->GetNext();
337 bool wxApp::SendIdleEvents(wxWindow
* win
)
339 bool needMore
= FALSE
;
342 event
.SetEventObject(win
);
344 win
->GetEventHandler()->ProcessEvent(event
);
346 if ( event
.MoreRequested() )
349 wxNode
* node
= win
->GetChildren().First();
352 wxWindow
* win
= (wxWindow
*) node
->Data();
353 if ( SendIdleEvents(win
) )
361 int wxApp::MainLoop()
364 m_mainLoop
= new wxEventLoop
;
366 rt
= m_mainLoop
->Run();
373 void wxApp::ExitMainLoop()
379 bool wxApp::Initialized()
381 return (wxTopLevelWindows
.GetCount() != 0);
384 bool wxApp::Pending()
386 return wxEventLoop::GetActive()->Pending();
389 void wxApp::Dispatch()
391 wxEventLoop::GetActive()->Dispatch();
394 void wxApp::DeletePendingObjects()
396 wxNode
*node
= wxPendingDelete
.First();
399 wxObject
*obj
= (wxObject
*)node
->Data();
403 if ( wxPendingDelete
.Find(obj
) )
406 node
= wxPendingDelete
.First();
410 bool wxApp::Initialize()
412 if ( MGL_init(".", NULL
) == 0 )
414 wxLogError(_("Cannot initialize SciTech MGL!"));
418 wxClassInfo::InitializeClasses();
421 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
424 // GL: I'm annoyed ... I don't know where to put this and I don't want to
425 // create a module for that as it's part of the core.
427 wxPendingEvents
= new wxList
;
428 wxPendingEventsLocker
= new wxCriticalSection
;
431 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
432 wxTheColourDatabase
->Initialize();
434 // Can't do this in wxModule, because fonts are needed by stock lists
435 wxTheFontsManager
= new wxFontsManager
;
437 wxInitializeStockLists();
438 wxInitializeStockObjects();
440 #if wxUSE_WX_RESOURCES
441 wxInitializeResourceSystem();
444 wxModule::RegisterModules();
445 if (!wxModule::InitializeModules()) return FALSE
;
450 void wxApp::CleanUp()
453 // continuing to use user defined log target is unsafe from now on because
454 // some resources may be already unavailable, so replace it by something
456 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
461 delete gs_rootWindow
;
463 wxModule::CleanUpModules();
465 #if wxUSE_WX_RESOURCES
466 wxCleanUpResourceSystem();
469 if (wxTheColourDatabase
)
470 delete wxTheColourDatabase
;
472 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
474 wxDeleteStockObjects();
475 wxDeleteStockLists();
478 wxTheApp
= (wxApp
*) NULL
;
481 // GL: I'm annoyed ... I don't know where to put this and I don't want to
482 // create a module for that as it's part of the core.
484 delete wxPendingEvents
;
485 delete wxPendingEventsLocker
;
488 wxClassInfo::CleanUpClasses();
490 // Can't do this in wxModule, because fonts are needed by stock lists
491 // (do it after deleting wxTheApp and cleaning modules up, since somebody
492 // may be deleting fonts that lately)
493 delete wxTheFontsManager
;
494 wxTheFontsManager
= (wxFontsManager
*) NULL
;
496 // check for memory leaks
497 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
498 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
500 wxLogDebug(wxT("There were memory leaks.\n"));
501 wxDebugContext::Dump();
502 wxDebugContext::PrintStatistics();
507 // do this as the very last thing because everything else can log messages
508 wxLog::DontCreateOnDemand();
510 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
520 int wxEntryStart(int argc
, char *argv
[])
522 return wxApp::Initialize() ? 0 : -1;
528 return wxTheApp
->OnInitGui() ? 0 : -1;
532 void wxEntryCleanup()
539 int wxEntry(int argc
, char *argv
[])
542 // VS: disable long filenames under DJGPP as the very first thing,
543 // since SciTech MGL doesn't like them much...
544 wxSetEnv(wxT("LFN"), wxT("N"));
547 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
548 // This seems to be necessary since there are 'rogue'
549 // objects present at this point (perhaps global objects?)
550 // Setting a checkpoint will ignore them as far as the
551 // memory checking facility is concerned.
552 // Of course you may argue that memory allocated in globals should be
553 // checked, but this is a reasonable compromise.
554 wxDebugContext::SetCheckpoint();
556 int err
= wxEntryStart(argc
, argv
);
562 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
563 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
565 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
567 wxObject
*test_app
= app_ini();
569 wxTheApp
= (wxApp
*) test_app
;
572 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
574 wxTheApp
->argc
= argc
;
576 wxTheApp
->argv
= new wxChar
*[argc
+1];
578 while (mb_argc
< argc
)
580 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
583 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
585 wxTheApp
->argv
= argv
;
588 wxString
name(wxFileNameFromPath(argv
[0]));
589 wxStripExtension(name
);
590 wxTheApp
->SetAppName(name
);
593 retValue
= wxEntryInitGui();
595 // Here frames insert themselves automatically into wxTopLevelWindows by
596 // getting created in OnInit().
599 if ( !wxTheApp
->OnInit() )
605 /* delete pending toplevel windows (typically a single
606 dialog) so that, if there isn't any left, we don't
608 wxTheApp
->DeletePendingObjects();
610 if ( wxTheApp
->Initialized() )
614 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
617 /* Forcibly delete the window. */
618 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
619 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
621 topWindow
->Close(TRUE
);
622 wxTheApp
->DeletePendingObjects();
627 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
632 // flush the logged messages if any
633 wxLog
*log
= wxLog::GetActiveTarget();
634 if (log
!= NULL
&& log
->HasPendingMessages())
637 retValue
= wxTheApp
->OnExit();