1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
4 // based on GTK and MSW implementations
6 // Copyright: (c) 2001 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 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
417 wxClassInfo::InitializeClasses();
420 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
423 // GL: I'm annoyed ... I don't know where to put this and I don't want to
424 // create a module for that as it's part of the core.
426 wxPendingEvents
= new wxList
;
427 wxPendingEventsLocker
= new wxCriticalSection
;
430 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
431 wxTheColourDatabase
->Initialize();
433 // Can't do this in wxModule, because fonts are needed by stock lists
434 wxTheFontsManager
= new wxFontsManager
;
436 wxInitializeStockLists();
437 wxInitializeStockObjects();
439 #if wxUSE_WX_RESOURCES
440 wxInitializeResourceSystem();
443 wxModule::RegisterModules();
444 if (!wxModule::InitializeModules()) return FALSE
;
449 wxIcon
wxApp::GetStdIcon(int which
) const
451 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
454 void wxApp::CleanUp()
457 // continuing to use user defined log target is unsafe from now on because
458 // some resources may be already unavailable, so replace it by something
460 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
465 delete gs_rootWindow
;
467 wxModule::CleanUpModules();
469 #if wxUSE_WX_RESOURCES
470 wxCleanUpResourceSystem();
473 if (wxTheColourDatabase
)
474 delete wxTheColourDatabase
;
476 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
478 wxDeleteStockObjects();
479 wxDeleteStockLists();
482 wxTheApp
= (wxApp
*) NULL
;
485 // GL: I'm annoyed ... I don't know where to put this and I don't want to
486 // create a module for that as it's part of the core.
488 delete wxPendingEvents
;
489 delete wxPendingEventsLocker
;
492 wxClassInfo::CleanUpClasses();
494 // Can't do this in wxModule, because fonts are needed by stock lists
495 // (do it after deleting wxTheApp and cleaning modules up, since somebody
496 // may be deleting fonts that lately)
497 delete wxTheFontsManager
;
498 wxTheFontsManager
= (wxFontsManager
*) NULL
;
502 // check for memory leaks
503 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
504 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
506 wxLogDebug(wxT("There were memory leaks.\n"));
507 wxDebugContext::Dump();
508 wxDebugContext::PrintStatistics();
513 // do this as the very last thing because everything else can log messages
514 wxLog::DontCreateOnDemand();
516 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
526 int wxEntryStart(int argc
, char *argv
[])
528 return wxApp::Initialize() ? 0 : -1;
534 return wxTheApp
->OnInitGui() ? 0 : -1;
538 void wxEntryCleanup()
545 int wxEntry(int argc
, char *argv
[])
548 // VS: disable long filenames under DJGPP as the very first thing,
549 // since SciTech MGL doesn't like them much...
550 wxSetEnv(wxT("LFN"), wxT("N"));
553 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
554 // This seems to be necessary since there are 'rogue'
555 // objects present at this point (perhaps global objects?)
556 // Setting a checkpoint will ignore them as far as the
557 // memory checking facility is concerned.
558 // Of course you may argue that memory allocated in globals should be
559 // checked, but this is a reasonable compromise.
560 wxDebugContext::SetCheckpoint();
562 int err
= wxEntryStart(argc
, argv
);
568 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
569 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
571 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
573 wxObject
*test_app
= app_ini();
575 wxTheApp
= (wxApp
*) test_app
;
578 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
580 wxTheApp
->argc
= argc
;
582 wxTheApp
->argv
= new wxChar
*[argc
+1];
584 while (mb_argc
< argc
)
586 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
589 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
591 wxTheApp
->argv
= argv
;
594 wxString
name(wxFileNameFromPath(argv
[0]));
595 wxStripExtension(name
);
596 wxTheApp
->SetAppName(name
);
599 retValue
= wxEntryInitGui();
601 // Here frames insert themselves automatically into wxTopLevelWindows by
602 // getting created in OnInit().
605 if ( !wxTheApp
->OnInit() )
611 /* delete pending toplevel windows (typically a single
612 dialog) so that, if there isn't any left, we don't
614 wxTheApp
->DeletePendingObjects();
616 if ( wxTheApp
->Initialized() )
620 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
623 /* Forcibly delete the window. */
624 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
625 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
627 topWindow
->Close(TRUE
);
628 wxTheApp
->DeletePendingObjects();
633 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
638 // flush the logged messages if any
639 wxLog
*log
= wxLog::GetActiveTarget();
640 if (log
!= NULL
&& log
->HasPendingMessages())
643 retValue
= wxTheApp
->OnExit();