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
;
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];
153 while (mb_argc
< argc
)
155 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
158 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
160 wxTheApp
->argv
= argv
;
163 wxString name
= wxFileNameFromPath(argv
[0]);
164 wxStripExtension(name
);
165 wxTheApp
->SetAppName(name
);
169 // app initialization
170 if ( !wxTheApp
->OnInit() )
176 retValue
= wxTheApp
->OnRun();
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
194 wxClassInfo::InitializeClasses();
196 wxModule::RegisterModules();
197 if ( !wxModule::InitializeModules() )
205 static void DoCleanUp()
208 // flush the logged messages if any
209 wxLog
*log
= wxLog::GetActiveTarget();
210 if (log
!= NULL
&& log
->HasPendingMessages())
213 // continuing to use user defined log target is unsafe from now on because
214 // some resources may be already unavailable, so replace it by something
216 wxLog::DontCreateOnDemand();
217 delete wxLog::SetActiveTarget(new wxLogStderr
);
220 wxModule::CleanUpModules();
222 wxClassInfo::CleanUpClasses();
224 // delete the application object
226 wxTheApp
= (wxApp
*)NULL
;
229 // and now delete the last logger as well
230 delete wxLog::SetActiveTarget(NULL
);