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];
153 while (mb_argc
< argc
)
155 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.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
);