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"
33 #include "wx/fontutil.h"
34 #include "wx/univ/theme.h"
35 #include "wx/univ/renderer.h"
36 #include "wx/mgl/private.h"
40 #if defined(MGL_DEBUG) && !defined(__WXDEBUG__)
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 wxApp
*wxTheApp
= NULL
;
49 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 static bool gs_inYield
= FALSE
;
71 if ( !wxThread::IsMain() )
73 // can't process events from other threads, MGL is thread-unsafe
76 #endif // wxUSE_THREADS
82 if ( wxEventLoop::GetActive() )
84 while (wxEventLoop::GetActive()->Pending())
85 wxEventLoop::GetActive()->Dispatch();
88 /* it's necessary to call ProcessIdle() to update the frames sizes which
89 might have been changed (it also will update other things set from
90 OnUpdateUI() which is a nice (and desired) side effect) */
91 while (wxTheApp
->ProcessIdle()) { }
100 bool wxYieldIfNeeded()
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
116 if (!wxThread::IsMain())
120 while (wxTheApp
->ProcessIdle()) {}
123 if (!wxThread::IsMain())
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
134 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
135 EVT_IDLE(wxApp::OnIdle
)
139 wxApp::wxApp() : m_mainLoop(NULL
)
147 bool wxApp::OnInitGui()
149 if ( !wxCreateMGL_WM() )
152 // This has to be done *after* wxCreateMGL_WM() because it initializes
154 if ( !wxAppBase::OnInitGui() )
158 // That damn MGL redirects stdin and stdout to physical console
159 FILE *file
= fopen("stderr", "wt");
160 wxLog::SetActiveTarget(new wxLogStderr(file
));
166 bool wxApp::ProcessIdle()
169 event
.SetEventObject(this);
172 return event
.MoreRequested();
175 void wxApp::OnIdle(wxIdleEvent
&event
)
177 static bool s_inOnIdle
= FALSE
;
179 /* Avoid recursion (via ProcessEvent default case) */
185 /* Resend in the main thread events which have been prepared in other
187 ProcessPendingEvents();
189 // 'Garbage' collection of windows deleted with Close().
190 DeletePendingObjects();
192 // Send OnIdle events to all windows
193 if ( SendIdleEvents() )
194 event
.RequestMore(TRUE
);
199 bool wxApp::SendIdleEvents()
201 bool needMore
= FALSE
;
203 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
206 wxWindow
* win
= node
->GetData();
207 if ( SendIdleEvents(win
) )
209 node
= node
->GetNext();
215 bool wxApp::SendIdleEvents(wxWindow
* win
)
217 bool needMore
= FALSE
;
220 event
.SetEventObject(win
);
222 win
->GetEventHandler()->ProcessEvent(event
);
224 if ( event
.MoreRequested() )
227 wxNode
* node
= win
->GetChildren().First();
230 wxWindow
* win
= (wxWindow
*) node
->Data();
231 if ( SendIdleEvents(win
) )
239 int wxApp::MainLoop()
242 m_mainLoop
= new wxEventLoop
;
244 rt
= m_mainLoop
->Run();
251 void wxApp::ExitMainLoop()
257 bool wxApp::Initialized()
259 return (wxTopLevelWindows
.GetCount() != 0);
262 bool wxApp::Pending()
264 return wxEventLoop::GetActive()->Pending();
267 void wxApp::Dispatch()
269 wxEventLoop::GetActive()->Dispatch();
272 void wxApp::DeletePendingObjects()
274 wxNode
*node
= wxPendingDelete
.First();
277 wxObject
*obj
= (wxObject
*)node
->Data();
281 if ( wxPendingDelete
.Find(obj
) )
284 node
= wxPendingDelete
.First();
288 bool wxApp::Initialize()
290 if ( MGL_init(".", NULL
) == 0 )
293 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
295 wxClassInfo::InitializeClasses();
297 wxSystemSettings::Init();
300 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
303 // GL: I'm annoyed ... I don't know where to put this and I don't want to
304 // create a module for that as it's part of the core.
306 wxPendingEvents
= new wxList
;
307 wxPendingEventsLocker
= new wxCriticalSection
;
310 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
311 wxTheColourDatabase
->Initialize();
313 // Can't do this in wxModule, because fonts are needed by stock lists
314 wxTheFontsManager
= new wxFontsManager
;
316 wxInitializeStockLists();
317 wxInitializeStockObjects();
319 #if wxUSE_WX_RESOURCES
320 wxInitializeResourceSystem();
323 wxModule::RegisterModules();
324 if (!wxModule::InitializeModules()) return FALSE
;
329 wxIcon
wxApp::GetStdIcon(int which
) const
331 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
334 void wxApp::CleanUp()
337 // flush the logged messages if any
338 wxLog
*log
= wxLog::GetActiveTarget();
339 if (log
!= NULL
&& log
->HasPendingMessages())
342 // continuing to use user defined log target is unsafe from now on because
343 // some resources may be already unavailable, so replace it by something
345 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
350 wxModule::CleanUpModules();
352 #if wxUSE_WX_RESOURCES
353 wxCleanUpResourceSystem();
356 if (wxTheColourDatabase
)
357 delete wxTheColourDatabase
;
359 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
361 wxDeleteStockObjects();
362 wxDeleteStockLists();
364 // Can't do this in wxModule, because fonts are needed by stock lists
365 delete wxTheFontsManager
;
366 wxTheFontsManager
= (wxFontsManager
*) NULL
;
369 wxTheApp
= (wxApp
*) NULL
;
371 // GL: I'm annoyed ... I don't know where to put this and I don't want to
372 // create a module for that as it's part of the core.
374 delete wxPendingEvents
;
375 delete wxPendingEventsLocker
;
378 wxSystemSettings::Done();
382 wxClassInfo::CleanUpClasses();
384 // check for memory leaks
385 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
386 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
388 wxLogDebug(wxT("There were memory leaks.\n"));
389 wxDebugContext::Dump();
390 wxDebugContext::PrintStatistics();
395 // do this as the very last thing because everything else can log messages
396 wxLog::DontCreateOnDemand();
398 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
408 int wxEntryStart(int argc
, char *argv
[])
410 return wxApp::Initialize() ? 0 : -1;
416 return wxTheApp
->OnInitGui() ? 0 : -1;
420 void wxEntryCleanup()
427 int wxEntry(int argc
, char *argv
[])
429 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
430 // This seems to be necessary since there are 'rogue'
431 // objects present at this point (perhaps global objects?)
432 // Setting a checkpoint will ignore them as far as the
433 // memory checking facility is concerned.
434 // Of course you may argue that memory allocated in globals should be
435 // checked, but this is a reasonable compromise.
436 wxDebugContext::SetCheckpoint();
438 int err
= wxEntryStart(argc
, argv
);
444 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
445 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
447 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
449 wxObject
*test_app
= app_ini();
451 wxTheApp
= (wxApp
*) test_app
;
454 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
456 wxTheApp
->argc
= argc
;
458 wxTheApp
->argv
= new wxChar
*[argc
+1];
460 while (mb_argc
< argc
)
462 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
465 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
467 wxTheApp
->argv
= argv
;
470 wxString
name(wxFileNameFromPath(argv
[0]));
471 wxStripExtension(name
);
472 wxTheApp
->SetAppName(name
);
475 retValue
= wxEntryInitGui();
477 // Here frames insert themselves automatically into wxTopLevelWindows by
478 // getting created in OnInit().
481 if ( !wxTheApp
->OnInit() )
487 /* delete pending toplevel windows (typically a single
488 dialog) so that, if there isn't any left, we don't
490 wxTheApp
->DeletePendingObjects();
492 if ( wxTheApp
->Initialized() )
496 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
499 /* Forcibly delete the window. */
500 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
501 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
503 topWindow
->Close(TRUE
);
504 wxTheApp
->DeletePendingObjects();
509 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
513 retValue
= wxTheApp
->OnExit();