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 WXDLLEXPORT wxApp
*wxTheApp
= NULL
;
41 wxAppInitializerFunction
42 wxAppBase::m_appInitFn
= (wxAppInitializerFunction
)NULL
;
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class /* no WXDLLEXPORT */ wxConsoleApp
: public wxApp
51 virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
59 static void DoCleanUp();
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 static size_t gs_nInitCount
= 0;
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
72 // stubs for some GUI functions
73 // ----------------------------------------------------------------------------
75 void WXDLLEXPORT
wxExit()
80 // Yield to other apps/messages
81 void WXDLLEXPORT
wxWakeUpIdle()
86 // ----------------------------------------------------------------------------
87 // wxBase-specific functions
88 // ----------------------------------------------------------------------------
90 bool WXDLLEXPORT
wxInitialize()
94 // already initialized
98 wxASSERT_MSG( !wxTheApp
,
99 wxT("either call wxInitialize or create app, not both!") );
106 wxTheApp
= new wxConsoleApp
;
118 void WXDLLEXPORT
wxUninitialize()
120 if ( !--gs_nInitCount
)
126 int wxEntry(int argc
, char **argv
)
128 // library initialization
137 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
138 wxT("No application object: use IMPLEMENT_APP macro.") );
140 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
142 wxTheApp
= (wxApp
*)fnCreate();
145 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
147 // app preinitialization
148 wxTheApp
->argc
= argc
;
151 wxTheApp
->argv
= new wxChar
*[argc
+1];
152 for ( int mb_argc
= 0; mb_argc
< argc
; mb_argc
++ )
154 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
156 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
158 wxTheApp
->argv
= argv
;
161 wxString name
= wxFileNameFromPath(wxTheApp
->argv
[0]);
162 wxStripExtension(name
);
163 wxTheApp
->SetAppName(name
);
167 // app initialization
168 if ( !wxTheApp
->OnInit() )
174 retValue
= wxTheApp
->OnRun();
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
192 wxClassInfo::InitializeClasses();
194 wxModule::RegisterModules();
195 if ( !wxModule::InitializeModules() )
203 static void DoCleanUp()
206 // flush the logged messages if any
207 wxLog
*log
= wxLog::GetActiveTarget();
208 if (log
!= NULL
&& log
->HasPendingMessages())
211 // continuing to use user defined log target is unsafe from now on because
212 // some resources may be already unavailable, so replace it by something
214 wxLog::DontCreateOnDemand();
215 delete wxLog::SetActiveTarget(new wxLogStderr
);
218 wxModule::CleanUpModules();
220 wxClassInfo::CleanUpClasses();
222 // TODO: this should really be done in ~wxApp
224 for ( int mb_argc
= 0; mb_argc
< wxTheApp
->argc
; mb_argc
++ )
226 free(wxTheApp
->argv
[mb_argc
]);
228 #endif // wxUSE_UNICODE
230 // delete the application object
232 wxTheApp
= (wxApp
*)NULL
;
235 // and now delete the last logger as well
236 delete wxLog::SetActiveTarget(NULL
);