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
;
68 bool wxApp::Yield(bool onlyIfNeeded
)
74 wxFAIL_MSG( wxT("wxYield called recursively" ) );
81 if ( !wxThread::IsMain() )
83 // can't process events from other threads, MGL is thread-unsafe
86 #endif // wxUSE_THREADS
92 if ( wxEventLoop::GetActive() )
94 while (wxEventLoop::GetActive()->Pending())
95 wxEventLoop::GetActive()->Dispatch();
98 /* it's necessary to call ProcessIdle() to update the frames sizes which
99 might have been changed (it also will update other things set from
100 OnUpdateUI() which is a nice (and desired) side effect) */
101 while (wxTheApp
->ProcessIdle()) { }
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
118 if (!wxThread::IsMain())
122 while (wxTheApp
->ProcessIdle()) {}
125 if (!wxThread::IsMain())
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
134 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
136 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
137 EVT_IDLE(wxApp::OnIdle
)
141 wxApp::wxApp() : m_mainLoop(NULL
)
149 bool wxApp::OnInitGui()
151 if ( !wxCreateMGL_WM() )
154 // This has to be done *after* wxCreateMGL_WM() because it initializes
156 if ( !wxAppBase::OnInitGui() )
160 // That damn MGL redirects stdin and stdout to physical console
161 FILE *file
= fopen("stderr", "wt");
162 wxLog::SetActiveTarget(new wxLogStderr(file
));
168 bool wxApp::ProcessIdle()
171 event
.SetEventObject(this);
174 return event
.MoreRequested();
177 void wxApp::OnIdle(wxIdleEvent
&event
)
179 static bool s_inOnIdle
= FALSE
;
181 /* Avoid recursion (via ProcessEvent default case) */
187 /* Resend in the main thread events which have been prepared in other
189 ProcessPendingEvents();
191 // 'Garbage' collection of windows deleted with Close().
192 DeletePendingObjects();
194 // Send OnIdle events to all windows
195 if ( SendIdleEvents() )
196 event
.RequestMore(TRUE
);
201 bool wxApp::SendIdleEvents()
203 bool needMore
= FALSE
;
205 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
208 wxWindow
* win
= node
->GetData();
209 if ( SendIdleEvents(win
) )
211 node
= node
->GetNext();
217 bool wxApp::SendIdleEvents(wxWindow
* win
)
219 bool needMore
= FALSE
;
222 event
.SetEventObject(win
);
224 win
->GetEventHandler()->ProcessEvent(event
);
226 if ( event
.MoreRequested() )
229 wxNode
* node
= win
->GetChildren().First();
232 wxWindow
* win
= (wxWindow
*) node
->Data();
233 if ( SendIdleEvents(win
) )
241 int wxApp::MainLoop()
244 m_mainLoop
= new wxEventLoop
;
246 rt
= m_mainLoop
->Run();
253 void wxApp::ExitMainLoop()
259 bool wxApp::Initialized()
261 return (wxTopLevelWindows
.GetCount() != 0);
264 bool wxApp::Pending()
266 return wxEventLoop::GetActive()->Pending();
269 void wxApp::Dispatch()
271 wxEventLoop::GetActive()->Dispatch();
274 void wxApp::DeletePendingObjects()
276 wxNode
*node
= wxPendingDelete
.First();
279 wxObject
*obj
= (wxObject
*)node
->Data();
283 if ( wxPendingDelete
.Find(obj
) )
286 node
= wxPendingDelete
.First();
290 bool wxApp::Initialize()
292 if ( MGL_init(".", NULL
) == 0 )
295 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
297 wxClassInfo::InitializeClasses();
299 wxSystemSettings::Init();
302 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
305 // GL: I'm annoyed ... I don't know where to put this and I don't want to
306 // create a module for that as it's part of the core.
308 wxPendingEvents
= new wxList
;
309 wxPendingEventsLocker
= new wxCriticalSection
;
312 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
313 wxTheColourDatabase
->Initialize();
315 // Can't do this in wxModule, because fonts are needed by stock lists
316 wxTheFontsManager
= new wxFontsManager
;
318 wxInitializeStockLists();
319 wxInitializeStockObjects();
321 #if wxUSE_WX_RESOURCES
322 wxInitializeResourceSystem();
325 wxModule::RegisterModules();
326 if (!wxModule::InitializeModules()) return FALSE
;
331 wxIcon
wxApp::GetStdIcon(int which
) const
333 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
336 void wxApp::CleanUp()
339 // flush the logged messages if any
340 wxLog
*log
= wxLog::GetActiveTarget();
341 if (log
!= NULL
&& log
->HasPendingMessages())
344 // continuing to use user defined log target is unsafe from now on because
345 // some resources may be already unavailable, so replace it by something
347 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
352 wxModule::CleanUpModules();
354 #if wxUSE_WX_RESOURCES
355 wxCleanUpResourceSystem();
358 if (wxTheColourDatabase
)
359 delete wxTheColourDatabase
;
361 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
363 wxDeleteStockObjects();
364 wxDeleteStockLists();
366 // Can't do this in wxModule, because fonts are needed by stock lists
367 delete wxTheFontsManager
;
368 wxTheFontsManager
= (wxFontsManager
*) NULL
;
371 wxTheApp
= (wxApp
*) NULL
;
373 // GL: I'm annoyed ... I don't know where to put this and I don't want to
374 // create a module for that as it's part of the core.
376 delete wxPendingEvents
;
377 delete wxPendingEventsLocker
;
380 wxSystemSettings::Done();
384 wxClassInfo::CleanUpClasses();
386 // check for memory leaks
387 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
388 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
390 wxLogDebug(wxT("There were memory leaks.\n"));
391 wxDebugContext::Dump();
392 wxDebugContext::PrintStatistics();
397 // do this as the very last thing because everything else can log messages
398 wxLog::DontCreateOnDemand();
400 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
410 int wxEntryStart(int argc
, char *argv
[])
412 return wxApp::Initialize() ? 0 : -1;
418 return wxTheApp
->OnInitGui() ? 0 : -1;
422 void wxEntryCleanup()
429 int wxEntry(int argc
, char *argv
[])
431 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
432 // This seems to be necessary since there are 'rogue'
433 // objects present at this point (perhaps global objects?)
434 // Setting a checkpoint will ignore them as far as the
435 // memory checking facility is concerned.
436 // Of course you may argue that memory allocated in globals should be
437 // checked, but this is a reasonable compromise.
438 wxDebugContext::SetCheckpoint();
440 int err
= wxEntryStart(argc
, argv
);
446 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
447 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
449 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
451 wxObject
*test_app
= app_ini();
453 wxTheApp
= (wxApp
*) test_app
;
456 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
458 wxTheApp
->argc
= argc
;
460 wxTheApp
->argv
= new wxChar
*[argc
+1];
462 while (mb_argc
< argc
)
464 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
467 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
469 wxTheApp
->argv
= argv
;
472 wxString
name(wxFileNameFromPath(argv
[0]));
473 wxStripExtension(name
);
474 wxTheApp
->SetAppName(name
);
477 retValue
= wxEntryInitGui();
479 // Here frames insert themselves automatically into wxTopLevelWindows by
480 // getting created in OnInit().
483 if ( !wxTheApp
->OnInit() )
489 /* delete pending toplevel windows (typically a single
490 dialog) so that, if there isn't any left, we don't
492 wxTheApp
->DeletePendingObjects();
494 if ( wxTheApp
->Initialized() )
498 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
501 /* Forcibly delete the window. */
502 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
503 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
505 topWindow
->Close(TRUE
);
506 wxTheApp
->DeletePendingObjects();
511 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
515 retValue
= wxTheApp
->OnExit();