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"
22 #include "wx/settings.h"
23 #include "wx/module.h"
24 #include "wx/evtloop.h"
26 #include "wx/dialog.h"
31 #include "wx/fontutil.h"
32 #include "wx/mgl/private.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 wxApp
*wxTheApp
= NULL
;
39 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 static bool gs_inYield
= FALSE
;
61 if ( !wxThread::IsMain() )
63 // can't process events from other threads, MGL is thread-unsafe
66 #endif // wxUSE_THREADS
72 if ( wxEventLoop::GetActive() )
74 while (wxEventLoop::GetActive()->Pending())
75 wxEventLoop::GetActive()->Dispatch();
78 /* it's necessary to call ProcessIdle() to update the frames sizes which
79 might have been changed (it also will update other things set from
80 OnUpdateUI() which is a nice (and desired) side effect) */
81 while (wxTheApp
->ProcessIdle()) { }
90 bool wxYieldIfNeeded()
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
106 if (!wxThread::IsMain())
110 while (wxTheApp
->ProcessIdle()) {}
113 if (!wxThread::IsMain())
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
124 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
125 EVT_IDLE(wxApp::OnIdle
)
129 wxApp::wxApp() : m_mainLoop(NULL
)
137 bool wxApp::OnInitGui()
139 if ( MGL_init(".", NULL
) == 0 )
141 if ( !wxCreateMGL_WM() )
144 // This has to be done *after* wxCreateMGL_WM() because it initializes
146 if ( !wxAppBase::OnInitGui() )
152 bool wxApp::ProcessIdle()
155 event
.SetEventObject(this);
158 return event
.MoreRequested();
161 void wxApp::OnIdle(wxIdleEvent
&event
)
163 static bool s_inOnIdle
= FALSE
;
165 /* Avoid recursion (via ProcessEvent default case) */
171 /* Resend in the main thread events which have been prepared in other
173 ProcessPendingEvents();
175 // 'Garbage' collection of windows deleted with Close().
176 DeletePendingObjects();
178 // Send OnIdle events to all windows
179 if ( SendIdleEvents() )
180 event
.RequestMore(TRUE
);
185 bool wxApp::SendIdleEvents()
187 bool needMore
= FALSE
;
189 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
192 wxWindow
* win
= node
->GetData();
193 if ( SendIdleEvents(win
) )
195 node
= node
->GetNext();
201 bool wxApp::SendIdleEvents(wxWindow
* win
)
203 bool needMore
= FALSE
;
206 event
.SetEventObject(win
);
208 win
->GetEventHandler()->ProcessEvent(event
);
210 if ( event
.MoreRequested() )
213 wxNode
* node
= win
->GetChildren().First();
216 wxWindow
* win
= (wxWindow
*) node
->Data();
217 if ( SendIdleEvents(win
) )
225 int wxApp::MainLoop()
228 m_mainLoop
= new wxEventLoop
;
230 rt
= m_mainLoop
->Run();
237 void wxApp::ExitMainLoop()
243 bool wxApp::Initialized()
245 // FIXME_MGL -- only for now because we don't have wxFrame/wxDialog yet
247 //return (wxTopLevelWindows.GetCount() != 0);
250 bool wxApp::Pending()
252 return wxEventLoop::GetActive()->Pending();
255 void wxApp::Dispatch()
257 wxEventLoop::GetActive()->Dispatch();
260 void wxApp::DeletePendingObjects()
262 wxNode
*node
= wxPendingDelete
.First();
265 wxObject
*obj
= (wxObject
*)node
->Data();
269 if ( wxPendingDelete
.Find(obj
) )
272 node
= wxPendingDelete
.First();
276 bool wxApp::Initialize()
278 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
280 wxClassInfo::InitializeClasses();
282 wxSystemSettings::Init();
285 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
288 // GL: I'm annoyed ... I don't know where to put this and I don't want to
289 // create a module for that as it's part of the core.
291 wxPendingEvents
= new wxList
;
292 wxPendingEventsLocker
= new wxCriticalSection
;
295 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
296 wxTheColourDatabase
->Initialize();
298 // Can't do this in wxModule, because fonts are needed by stock lists
299 wxTheFontsManager
= new wxFontsManager
;
301 wxInitializeStockLists();
302 wxInitializeStockObjects();
304 #if wxUSE_WX_RESOURCES
305 wxInitializeResourceSystem();
308 wxModule::RegisterModules();
309 if (!wxModule::InitializeModules()) return FALSE
;
316 #include "question.xpm"
317 #include "warning.xpm"
319 wxIcon
wxApp::GetStdIcon(int which
) const
323 case wxICON_INFORMATION
:
324 return wxIcon(info_xpm
);
325 case wxICON_QUESTION
:
326 return wxIcon(question_xpm
);
327 case wxICON_EXCLAMATION
:
328 return wxIcon(warning_xpm
);
330 wxFAIL_MSG(wxT("requested non existent standard icon"));
331 // still fall through
333 return wxIcon(error_xpm
);
337 void wxApp::CleanUp()
340 // flush the logged messages if any
341 wxLog
*log
= wxLog::GetActiveTarget();
342 if (log
!= NULL
&& log
->HasPendingMessages())
345 // continuing to use user defined log target is unsafe from now on because
346 // some resources may be already unavailable, so replace it by something
348 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
353 wxModule::CleanUpModules();
355 #if wxUSE_WX_RESOURCES
356 wxCleanUpResourceSystem();
359 if (wxTheColourDatabase
)
360 delete wxTheColourDatabase
;
362 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
364 wxDeleteStockObjects();
365 wxDeleteStockLists();
367 // Can't do this in wxModule, because fonts are needed by stock lists
368 delete wxTheFontsManager
;
369 wxTheFontsManager
= (wxFontsManager
*) NULL
;
372 wxTheApp
= (wxApp
*) NULL
;
374 // GL: I'm annoyed ... I don't know where to put this and I don't want to
375 // create a module for that as it's part of the core.
377 delete wxPendingEvents
;
378 delete wxPendingEventsLocker
;
381 wxSystemSettings::Done();
385 wxClassInfo::CleanUpClasses();
387 // check for memory leaks
388 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
389 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
391 wxLogDebug(wxT("There were memory leaks.\n"));
392 wxDebugContext::Dump();
393 wxDebugContext::PrintStatistics();
398 // do this as the very last thing because everything else can log messages
399 wxLog::DontCreateOnDemand();
401 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
411 int wxEntryStart(int argc
, char *argv
[])
413 return wxApp::Initialize() ? 0 : -1;
419 return wxTheApp
->OnInitGui() ? 0 : -1;
423 void wxEntryCleanup()
430 int wxEntry(int argc
, char *argv
[])
432 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
433 // This seems to be necessary since there are 'rogue'
434 // objects present at this point (perhaps global objects?)
435 // Setting a checkpoint will ignore them as far as the
436 // memory checking facility is concerned.
437 // Of course you may argue that memory allocated in globals should be
438 // checked, but this is a reasonable compromise.
439 wxDebugContext::SetCheckpoint();
441 int err
= wxEntryStart(argc
, argv
);
447 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
448 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
450 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
452 wxObject
*test_app
= app_ini();
454 wxTheApp
= (wxApp
*) test_app
;
457 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
459 wxTheApp
->argc
= argc
;
461 wxTheApp
->argv
= new wxChar
*[argc
+1];
463 while (mb_argc
< argc
)
465 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
468 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
470 wxTheApp
->argv
= argv
;
473 wxString
name(wxFileNameFromPath(argv
[0]));
474 wxStripExtension(name
);
475 wxTheApp
->SetAppName(name
);
478 retValue
= wxEntryInitGui();
480 // Here frames insert themselves automatically into wxTopLevelWindows by
481 // getting created in OnInit().
484 if ( !wxTheApp
->OnInit() )
490 /* delete pending toplevel windows (typically a single
491 dialog) so that, if there isn't any left, we don't
493 wxTheApp
->DeletePendingObjects();
495 if ( wxTheApp
->Initialized() )
499 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
502 /* Forcibly delete the window. */
503 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
504 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
506 topWindow
->Close(TRUE
);
507 wxTheApp
->DeletePendingObjects();
512 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
516 retValue
= wxTheApp
->OnExit();