1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/init.cpp
3 // Purpose: initialisation for the library
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/filefn.h"
32 #include "wx/module.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 WXDLLEXPORT wxApp *wxTheApp = NULL;
40 wxAppInitializerFunction
41 wxAppBase::m_appInitFn = (wxAppInitializerFunction)NULL;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class /* no WXDLLEXPORT */ wxConsoleApp : public wxApp
50 virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
58 static void DoCleanUp();
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 static size_t gs_nInitCount = 0;
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
71 // stubs for some GUI functions
72 // ----------------------------------------------------------------------------
74 void WXDLLEXPORT wxExit()
79 // Yield to other apps/messages
80 void WXDLLEXPORT wxWakeUpIdle()
85 // ----------------------------------------------------------------------------
86 // wxBase-specific functions
87 // ----------------------------------------------------------------------------
89 bool WXDLLEXPORT wxInitialize()
93 // already initialized
97 wxASSERT_MSG( !wxTheApp,
98 wxT("either call wxInitialize or create app, not both!") );
105 wxTheApp = new wxConsoleApp;
117 void WXDLLEXPORT wxUninitialize()
119 if ( !--gs_nInitCount )
125 int wxEntry(int argc, char **argv)
127 // library initialization
136 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
137 wxT("No application object: use IMPLEMENT_APP macro.") );
139 wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
141 wxTheApp = (wxApp *)fnCreate();
144 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
146 // app preinitialization
147 wxTheApp->argc = argc;
150 wxTheApp->argv = new wxChar*[argc+1];
152 while (mb_argc < argc)
154 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
157 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
159 wxTheApp->argv = argv;
162 wxString name = wxFileNameFromPath(argv[0]);
163 wxStripExtension(name);
164 wxTheApp->SetAppName(name);
168 // app initialization
169 if ( !wxTheApp->OnInit() )
175 retValue = wxTheApp->OnRun();
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
193 wxClassInfo::InitializeClasses();
195 wxModule::RegisterModules();
196 if ( !wxModule::InitializeModules() )
204 static void DoCleanUp()
207 // flush the logged messages if any
208 wxLog *log = wxLog::GetActiveTarget();
209 if (log != NULL && log->HasPendingMessages())
212 // continuing to use user defined log target is unsafe from now on because
213 // some resources may be already unavailable, so replace it by something
215 wxLog::DontCreateOnDemand();
216 delete wxLog::SetActiveTarget(new wxLogStderr);
219 wxModule::CleanUpModules();
221 wxClassInfo::CleanUpClasses();
223 // delete the application object
225 wxTheApp = (wxApp *)NULL;
228 // and now delete the last logger as well
229 delete wxLog::SetActiveTarget(NULL);