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 )
415 wxClassInfo::InitializeClasses();
418 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
421 // GL: I'm annoyed ... I don't know where to put this and I don't want to
422 // create a module for that as it's part of the core.
424 wxPendingEvents
= new wxList
;
425 wxPendingEventsLocker
= new wxCriticalSection
;
428 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
429 wxTheColourDatabase
->Initialize();
431 // Can't do this in wxModule, because fonts are needed by stock lists
432 wxTheFontsManager
= new wxFontsManager
;
434 wxInitializeStockLists();
435 wxInitializeStockObjects();
437 #if wxUSE_WX_RESOURCES
438 wxInitializeResourceSystem();
441 wxModule::RegisterModules();
442 if (!wxModule::InitializeModules()) return FALSE
;
447 wxIcon
wxApp::GetStdIcon(int which
) const
449 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
452 void wxApp::CleanUp()
455 // continuing to use user defined log target is unsafe from now on because
456 // some resources may be already unavailable, so replace it by something
458 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
463 delete gs_rootWindow
;
465 wxModule::CleanUpModules();
467 #if wxUSE_WX_RESOURCES
468 wxCleanUpResourceSystem();
471 if (wxTheColourDatabase
)
472 delete wxTheColourDatabase
;
474 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
476 wxDeleteStockObjects();
477 wxDeleteStockLists();
480 wxTheApp
= (wxApp
*) NULL
;
483 // GL: I'm annoyed ... I don't know where to put this and I don't want to
484 // create a module for that as it's part of the core.
486 delete wxPendingEvents
;
487 delete wxPendingEventsLocker
;
490 wxClassInfo::CleanUpClasses();
492 // Can't do this in wxModule, because fonts are needed by stock lists
493 // (do it after deleting wxTheApp and cleaning modules up, since somebody
494 // may be deleting fonts that lately)
495 delete wxTheFontsManager
;
496 wxTheFontsManager
= (wxFontsManager
*) NULL
;
498 // check for memory leaks
499 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
500 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
502 wxLogDebug(wxT("There were memory leaks.\n"));
503 wxDebugContext::Dump();
504 wxDebugContext::PrintStatistics();
509 // do this as the very last thing because everything else can log messages
510 wxLog::DontCreateOnDemand();
512 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
522 int wxEntryStart(int argc
, char *argv
[])
524 return wxApp::Initialize() ? 0 : -1;
530 return wxTheApp
->OnInitGui() ? 0 : -1;
534 void wxEntryCleanup()
541 int wxEntry(int argc
, char *argv
[])
544 // VS: disable long filenames under DJGPP as the very first thing,
545 // since SciTech MGL doesn't like them much...
546 wxSetEnv(wxT("LFN"), wxT("N"));
549 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
550 // This seems to be necessary since there are 'rogue'
551 // objects present at this point (perhaps global objects?)
552 // Setting a checkpoint will ignore them as far as the
553 // memory checking facility is concerned.
554 // Of course you may argue that memory allocated in globals should be
555 // checked, but this is a reasonable compromise.
556 wxDebugContext::SetCheckpoint();
558 int err
= wxEntryStart(argc
, argv
);
564 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
565 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
567 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
569 wxObject
*test_app
= app_ini();
571 wxTheApp
= (wxApp
*) test_app
;
574 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
576 wxTheApp
->argc
= argc
;
578 wxTheApp
->argv
= new wxChar
*[argc
+1];
580 while (mb_argc
< argc
)
582 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
585 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
587 wxTheApp
->argv
= argv
;
590 wxString
name(wxFileNameFromPath(argv
[0]));
591 wxStripExtension(name
);
592 wxTheApp
->SetAppName(name
);
595 retValue
= wxEntryInitGui();
597 // Here frames insert themselves automatically into wxTopLevelWindows by
598 // getting created in OnInit().
601 if ( !wxTheApp
->OnInit() )
607 /* delete pending toplevel windows (typically a single
608 dialog) so that, if there isn't any left, we don't
610 wxTheApp
->DeletePendingObjects();
612 if ( wxTheApp
->Initialized() )
616 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
619 /* Forcibly delete the window. */
620 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
621 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
623 topWindow
->Close(TRUE
);
624 wxTheApp
->DeletePendingObjects();
629 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
634 // flush the logged messages if any
635 wxLog
*log
= wxLog::GetActiveTarget();
636 if (log
!= NULL
&& log
->HasPendingMessages())
639 retValue
= wxTheApp
->OnExit();