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/mgl/private.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 wxApp
*wxTheApp
= NULL
;
38 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
40 static wxEventLoop
*gs_mainEventLoop
= NULL
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 static bool gs_inYield
= FALSE
;
62 if ( !wxThread::IsMain() )
64 // can't process events from other threads, MGL is thread-unsafe
67 #endif // wxUSE_THREADS
73 while (gs_mainEventLoop
->Pending())
74 gs_mainEventLoop
->Dispatch();
76 /* it's necessary to call ProcessIdle() to update the frames sizes which
77 might have been changed (it also will update other things set from
78 OnUpdateUI() which is a nice (and desired) side effect) */
79 while (wxTheApp
->ProcessIdle()) { }
88 bool wxYieldIfNeeded()
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
104 if (!wxThread::IsMain())
108 while (wxTheApp
->ProcessIdle()) {}
111 if (!wxThread::IsMain())
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
122 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
123 EVT_IDLE(wxApp::OnIdle
)
135 bool wxApp::OnInitGui()
137 if ( MGL_init(".", NULL
) == 0 )
139 if ( !wxCreateMGL_WM() )
142 // This has to be done *after* wxCreateMGL_WM() because it initializes
144 if ( !wxAppBase::OnInitGui() )
150 bool wxApp::ProcessIdle()
153 event
.SetEventObject(this);
156 return event
.MoreRequested();
159 void wxApp::OnIdle(wxIdleEvent
&event
)
161 static bool s_inOnIdle
= FALSE
;
163 /* Avoid recursion (via ProcessEvent default case) */
169 /* Resend in the main thread events which have been prepared in other
171 ProcessPendingEvents();
173 // 'Garbage' collection of windows deleted with Close().
174 DeletePendingObjects();
176 // Send OnIdle events to all windows
177 if ( SendIdleEvents() )
178 event
.RequestMore(TRUE
);
183 bool wxApp::SendIdleEvents()
185 bool needMore
= FALSE
;
187 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
190 wxWindow
* win
= node
->GetData();
191 if ( SendIdleEvents(win
) )
193 node
= node
->GetNext();
199 bool wxApp::SendIdleEvents(wxWindow
* win
)
201 bool needMore
= FALSE
;
204 event
.SetEventObject(win
);
206 win
->GetEventHandler()->ProcessEvent(event
);
208 #if 0 // FIXME_MGL - what the hell it is?
209 win
->OnInternalIdle();
211 if ( event
.MoreRequested() )
215 wxNode
* node
= win
->GetChildren().First();
218 wxWindow
* win
= (wxWindow
*) node
->Data();
219 if ( SendIdleEvents(win
) )
227 int wxApp::MainLoop()
229 gs_mainEventLoop
= new wxEventLoop
;
230 return gs_mainEventLoop
->Run();
231 delete gs_mainEventLoop
;
232 gs_mainEventLoop
= NULL
;
235 void wxApp::ExitMainLoop()
237 gs_mainEventLoop
->Exit(0);
240 bool wxApp::Initialized()
242 return (GetTopWindow() != NULL
);
245 bool wxApp::Pending()
247 return gs_mainEventLoop
->Pending();
250 void wxApp::Dispatch()
252 gs_mainEventLoop
->Dispatch();
255 void wxApp::DeletePendingObjects()
257 wxNode
*node
= wxPendingDelete
.First();
260 wxObject
*obj
= (wxObject
*)node
->Data();
264 if ( wxPendingDelete
.Find(obj
) )
267 node
= wxPendingDelete
.First();
271 bool wxApp::Initialize()
273 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
275 wxClassInfo::InitializeClasses();
277 wxSystemSettings::Init();
280 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
283 // GL: I'm annoyed ... I don't know where to put this and I don't want to
284 // create a module for that as it's part of the core.
286 wxPendingEvents
= new wxList
;
287 wxPendingEventsLocker
= new wxCriticalSection
;
290 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
291 wxTheColourDatabase
->Initialize();
293 wxInitializeStockLists();
294 wxInitializeStockObjects();
296 #if wxUSE_WX_RESOURCES
297 wxInitializeResourceSystem();
300 wxModule::RegisterModules();
301 if (!wxModule::InitializeModules()) return FALSE
;
308 #include "question.xpm"
309 #include "warning.xpm"
311 wxIcon
wxApp::GetStdIcon(int which
) const
315 case wxICON_INFORMATION
:
316 return wxIcon(info_xpm
);
317 case wxICON_QUESTION
:
318 return wxIcon(question_xpm
);
319 case wxICON_EXCLAMATION
:
320 return wxIcon(warning_xpm
);
322 wxFAIL_MSG(wxT("requested non existent standard icon"));
323 // still fall through
325 return wxIcon(error_xpm
);
329 void wxApp::CleanUp()
332 // flush the logged messages if any
333 wxLog
*log
= wxLog::GetActiveTarget();
334 if (log
!= NULL
&& log
->HasPendingMessages())
337 // continuing to use user defined log target is unsafe from now on because
338 // some resources may be already unavailable, so replace it by something
340 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
345 wxModule::CleanUpModules();
347 #if wxUSE_WX_RESOURCES
348 wxCleanUpResourceSystem();
351 if (wxTheColourDatabase
)
352 delete wxTheColourDatabase
;
354 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
356 wxDeleteStockObjects();
358 wxDeleteStockLists();
361 wxTheApp
= (wxApp
*) NULL
;
363 // GL: I'm annoyed ... I don't know where to put this and I don't want to
364 // create a module for that as it's part of the core.
366 delete wxPendingEvents
;
367 delete wxPendingEventsLocker
;
370 wxSystemSettings::Done();
374 wxClassInfo::CleanUpClasses();
376 // check for memory leaks
377 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
378 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
380 wxLogDebug(wxT("There were memory leaks.\n"));
381 wxDebugContext::Dump();
382 wxDebugContext::PrintStatistics();
387 // do this as the very last thing because everything else can log messages
388 wxLog::DontCreateOnDemand();
390 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
400 int wxEntryStart(int argc
, char *argv
[])
402 return wxApp::Initialize() ? 0 : -1;
408 return wxTheApp
->OnInitGui() ? 0 : -1;
412 void wxEntryCleanup()
419 int wxEntry(int argc
, char *argv
[])
421 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
422 // This seems to be necessary since there are 'rogue'
423 // objects present at this point (perhaps global objects?)
424 // Setting a checkpoint will ignore them as far as the
425 // memory checking facility is concerned.
426 // Of course you may argue that memory allocated in globals should be
427 // checked, but this is a reasonable compromise.
428 wxDebugContext::SetCheckpoint();
430 int err
= wxEntryStart(argc
, argv
);
436 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
437 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
439 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
441 wxObject
*test_app
= app_ini();
443 wxTheApp
= (wxApp
*) test_app
;
446 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
448 wxTheApp
->argc
= argc
;
450 wxTheApp
->argv
= new wxChar
*[argc
+1];
452 while (mb_argc
< argc
)
454 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
457 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
459 wxTheApp
->argv
= argv
;
462 wxString
name(wxFileNameFromPath(argv
[0]));
463 wxStripExtension(name
);
464 wxTheApp
->SetAppName(name
);
467 retValue
= wxEntryInitGui();
469 // Here frames insert themselves automatically into wxTopLevelWindows by
470 // getting created in OnInit().
473 if ( !wxTheApp
->OnInit() )
479 /* delete pending toplevel windows (typically a single
480 dialog) so that, if there isn't any left, we don't
482 wxTheApp
->DeletePendingObjects();
484 if ( wxTheApp
->Initialized() &&
485 wxTopLevelWindows
.GetCount() != 0 )
489 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
492 /* Forcibly delete the window. */
493 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
494 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
496 topWindow
->Close(TRUE
);
497 wxTheApp
->DeletePendingObjects();
502 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
506 retValue
= wxTheApp
->OnExit();