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"
33 #include "wx/module.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // we need a dummy app object if the user doesn't want to create a real one
40 class wxDummyConsoleApp
: public wxApp
43 virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; }
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
51 static void DoCleanUp();
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 static size_t gs_nInitCount
= 0;
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxBase-specific functions
65 // ----------------------------------------------------------------------------
67 bool WXDLLEXPORT
wxInitialize()
71 // already initialized
75 wxASSERT_MSG( !wxTheApp
,
76 wxT("either call wxInitialize or create app, not both!") );
83 wxTheApp
= new wxDummyConsoleApp
;
95 void WXDLLEXPORT
wxUninitialize()
97 if ( !--gs_nInitCount
)
103 int wxEntry(int argc
, char **argv
)
105 // library initialization
114 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
115 wxT("No application object: use IMPLEMENT_APP macro.") );
117 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
119 wxTheApp
= (wxApp
*)fnCreate();
122 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
124 // app preinitialization
125 wxTheApp
->argc
= argc
;
128 wxTheApp
->argv
= new wxChar
*[argc
+1];
129 for ( int mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
131 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
133 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
135 wxTheApp
->argv
= argv
;
138 wxString name
= wxFileNameFromPath(wxTheApp
->argv
[0]);
139 wxStripExtension(name
);
140 wxTheApp
->SetAppName(name
);
144 // app initialization
145 if ( !wxTheApp
->OnInit() )
151 retValue
= wxTheApp
->OnRun();
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
169 wxClassInfo::InitializeClasses();
171 wxModule::RegisterModules();
172 if ( !wxModule::InitializeModules() )
180 static void DoCleanUp()
183 // flush the logged messages if any
184 wxLog
*log
= wxLog::GetActiveTarget();
185 if (log
!= NULL
&& log
->HasPendingMessages())
188 // continuing to use user defined log target is unsafe from now on because
189 // some resources may be already unavailable, so replace it by something
191 wxLog::DontCreateOnDemand();
192 delete wxLog::SetActiveTarget(new wxLogStderr
);
195 wxModule::CleanUpModules();
197 wxClassInfo::CleanUpClasses();
199 // TODO: this should really be done in ~wxApp
201 for ( int mb_argc
= 0; mb_argc
< wxTheApp
->argc
; mb_argc
++ )
203 free(wxTheApp
->argv
[mb_argc
]);
205 #endif // wxUSE_UNICODE
207 // delete the application object
209 wxTheApp
= (wxApp
*)NULL
;
212 // and now delete the last logger as well
213 delete wxLog::SetActiveTarget(NULL
);