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/mgl/private.h"
38 #if defined(MGL_DEBUG) && !defined(__WXDEBUG__)
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 wxApp
*wxTheApp
= NULL
;
47 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 static bool gs_inYield
= FALSE
;
69 if ( !wxThread::IsMain() )
71 // can't process events from other threads, MGL is thread-unsafe
74 #endif // wxUSE_THREADS
80 if ( wxEventLoop::GetActive() )
82 while (wxEventLoop::GetActive()->Pending())
83 wxEventLoop::GetActive()->Dispatch();
86 /* it's necessary to call ProcessIdle() to update the frames sizes which
87 might have been changed (it also will update other things set from
88 OnUpdateUI() which is a nice (and desired) side effect) */
89 while (wxTheApp
->ProcessIdle()) { }
98 bool wxYieldIfNeeded()
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
114 if (!wxThread::IsMain())
118 while (wxTheApp
->ProcessIdle()) {}
121 if (!wxThread::IsMain())
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
132 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
133 EVT_IDLE(wxApp::OnIdle
)
137 wxApp::wxApp() : m_mainLoop(NULL
)
145 bool wxApp::OnInitGui()
147 if ( !wxCreateMGL_WM() )
150 // This has to be done *after* wxCreateMGL_WM() because it initializes
152 if ( !wxAppBase::OnInitGui() )
156 // That damn MGL redirects stdin and stdout to physical console
157 FILE *file
= fopen("stderr", "wt");
158 wxLog::SetActiveTarget(new wxLogStderr(file
));
164 bool wxApp::ProcessIdle()
167 event
.SetEventObject(this);
170 return event
.MoreRequested();
173 void wxApp::OnIdle(wxIdleEvent
&event
)
175 static bool s_inOnIdle
= FALSE
;
177 /* Avoid recursion (via ProcessEvent default case) */
183 /* Resend in the main thread events which have been prepared in other
185 ProcessPendingEvents();
187 // 'Garbage' collection of windows deleted with Close().
188 DeletePendingObjects();
190 // Send OnIdle events to all windows
191 if ( SendIdleEvents() )
192 event
.RequestMore(TRUE
);
197 bool wxApp::SendIdleEvents()
199 bool needMore
= FALSE
;
201 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
204 wxWindow
* win
= node
->GetData();
205 if ( SendIdleEvents(win
) )
207 node
= node
->GetNext();
213 bool wxApp::SendIdleEvents(wxWindow
* win
)
215 bool needMore
= FALSE
;
218 event
.SetEventObject(win
);
220 win
->GetEventHandler()->ProcessEvent(event
);
222 if ( event
.MoreRequested() )
225 wxNode
* node
= win
->GetChildren().First();
228 wxWindow
* win
= (wxWindow
*) node
->Data();
229 if ( SendIdleEvents(win
) )
237 int wxApp::MainLoop()
240 m_mainLoop
= new wxEventLoop
;
242 rt
= m_mainLoop
->Run();
249 void wxApp::ExitMainLoop()
255 bool wxApp::Initialized()
257 return (wxTopLevelWindows
.GetCount() != 0);
260 bool wxApp::Pending()
262 return wxEventLoop::GetActive()->Pending();
265 void wxApp::Dispatch()
267 wxEventLoop::GetActive()->Dispatch();
270 void wxApp::DeletePendingObjects()
272 wxNode
*node
= wxPendingDelete
.First();
275 wxObject
*obj
= (wxObject
*)node
->Data();
279 if ( wxPendingDelete
.Find(obj
) )
282 node
= wxPendingDelete
.First();
286 bool wxApp::Initialize()
288 if ( MGL_init(".", NULL
) == 0 )
291 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
293 wxClassInfo::InitializeClasses();
295 wxSystemSettings::Init();
298 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
301 // GL: I'm annoyed ... I don't know where to put this and I don't want to
302 // create a module for that as it's part of the core.
304 wxPendingEvents
= new wxList
;
305 wxPendingEventsLocker
= new wxCriticalSection
;
308 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
309 wxTheColourDatabase
->Initialize();
311 // Can't do this in wxModule, because fonts are needed by stock lists
312 wxTheFontsManager
= new wxFontsManager
;
314 wxInitializeStockLists();
315 wxInitializeStockObjects();
317 #if wxUSE_WX_RESOURCES
318 wxInitializeResourceSystem();
321 wxModule::RegisterModules();
322 if (!wxModule::InitializeModules()) return FALSE
;
329 #include "question.xpm"
330 #include "warning.xpm"
332 wxIcon
wxApp::GetStdIcon(int which
) const
336 case wxICON_INFORMATION
:
337 return wxIcon(info_xpm
);
338 case wxICON_QUESTION
:
339 return wxIcon(question_xpm
);
340 case wxICON_EXCLAMATION
:
341 return wxIcon(warning_xpm
);
343 wxFAIL_MSG(wxT("requested non existent standard icon"));
344 // still fall through
346 return wxIcon(error_xpm
);
350 void wxApp::CleanUp()
353 // flush the logged messages if any
354 wxLog
*log
= wxLog::GetActiveTarget();
355 if (log
!= NULL
&& log
->HasPendingMessages())
358 // continuing to use user defined log target is unsafe from now on because
359 // some resources may be already unavailable, so replace it by something
361 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
366 wxModule::CleanUpModules();
368 #if wxUSE_WX_RESOURCES
369 wxCleanUpResourceSystem();
372 if (wxTheColourDatabase
)
373 delete wxTheColourDatabase
;
375 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
377 wxDeleteStockObjects();
378 wxDeleteStockLists();
380 // Can't do this in wxModule, because fonts are needed by stock lists
381 delete wxTheFontsManager
;
382 wxTheFontsManager
= (wxFontsManager
*) NULL
;
385 wxTheApp
= (wxApp
*) NULL
;
387 // GL: I'm annoyed ... I don't know where to put this and I don't want to
388 // create a module for that as it's part of the core.
390 delete wxPendingEvents
;
391 delete wxPendingEventsLocker
;
394 wxSystemSettings::Done();
398 wxClassInfo::CleanUpClasses();
400 // check for memory leaks
401 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
402 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
404 wxLogDebug(wxT("There were memory leaks.\n"));
405 wxDebugContext::Dump();
406 wxDebugContext::PrintStatistics();
411 // do this as the very last thing because everything else can log messages
412 wxLog::DontCreateOnDemand();
414 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
424 int wxEntryStart(int argc
, char *argv
[])
426 return wxApp::Initialize() ? 0 : -1;
432 return wxTheApp
->OnInitGui() ? 0 : -1;
436 void wxEntryCleanup()
443 int wxEntry(int argc
, char *argv
[])
445 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
446 // This seems to be necessary since there are 'rogue'
447 // objects present at this point (perhaps global objects?)
448 // Setting a checkpoint will ignore them as far as the
449 // memory checking facility is concerned.
450 // Of course you may argue that memory allocated in globals should be
451 // checked, but this is a reasonable compromise.
452 wxDebugContext::SetCheckpoint();
454 int err
= wxEntryStart(argc
, argv
);
460 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
461 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
463 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
465 wxObject
*test_app
= app_ini();
467 wxTheApp
= (wxApp
*) test_app
;
470 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
472 wxTheApp
->argc
= argc
;
474 wxTheApp
->argv
= new wxChar
*[argc
+1];
476 while (mb_argc
< argc
)
478 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
481 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
483 wxTheApp
->argv
= argv
;
486 wxString
name(wxFileNameFromPath(argv
[0]));
487 wxStripExtension(name
);
488 wxTheApp
->SetAppName(name
);
491 retValue
= wxEntryInitGui();
493 // Here frames insert themselves automatically into wxTopLevelWindows by
494 // getting created in OnInit().
497 if ( !wxTheApp
->OnInit() )
503 /* delete pending toplevel windows (typically a single
504 dialog) so that, if there isn't any left, we don't
506 wxTheApp
->DeletePendingObjects();
508 if ( wxTheApp
->Initialized() )
512 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
515 /* Forcibly delete the window. */
516 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
517 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
519 topWindow
->Close(TRUE
);
520 wxTheApp
->DeletePendingObjects();
525 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
529 retValue
= wxTheApp
->OnExit();