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; }
52 virtual bool ProcessIdle() { return TRUE
; }
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
60 static void DoCleanUp();
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 static size_t gs_nInitCount
= 0;
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // stubs for some GUI functions
74 // ----------------------------------------------------------------------------
76 void WXDLLEXPORT
wxExit()
81 // Yield to other apps/messages
82 void WXDLLEXPORT
wxWakeUpIdle()
87 // ----------------------------------------------------------------------------
88 // wxBase-specific functions
89 // ----------------------------------------------------------------------------
91 bool WXDLLEXPORT
wxInitialize()
95 // already initialized
99 wxASSERT_MSG( !wxTheApp
,
100 wxT("either call wxInitialize or create app, not both!") );
107 wxTheApp
= new wxConsoleApp
;
119 void WXDLLEXPORT
wxUninitialize()
121 if ( !--gs_nInitCount
)
127 int wxEntry(int argc
, char **argv
)
129 // library initialization
138 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
139 wxT("No application object: use IMPLEMENT_APP macro.") );
141 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
143 wxTheApp
= (wxApp
*)fnCreate();
146 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
148 // app preinitialization
149 wxTheApp
->argc
= argc
;
152 wxTheApp
->argv
= new wxChar
*[argc
+1];
154 while (mb_argc
< argc
)
156 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
159 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
161 wxTheApp
->argv
= argv
;
164 wxString name
= wxFileNameFromPath(argv
[0]);
165 wxStripExtension(name
);
166 wxTheApp
->SetAppName(name
);
170 // app initialization
171 if ( !wxTheApp
->OnInit() )
177 retValue
= wxTheApp
->OnRun();
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
195 wxClassInfo::InitializeClasses();
197 wxModule::RegisterModules();
198 if ( !wxModule::InitializeModules() )
206 static void DoCleanUp()
209 // flush the logged messages if any
210 wxLog
*log
= wxLog::GetActiveTarget();
211 if (log
!= NULL
&& log
->HasPendingMessages())
214 // continuing to use user defined log target is unsafe from now on because
215 // some resources may be already unavailable, so replace it by something
217 wxLog::DontCreateOnDemand();
218 delete wxLog::SetActiveTarget(new wxLogStderr
);
221 wxModule::CleanUpModules();
223 wxClassInfo::CleanUpClasses();
225 // delete the application object
227 wxTheApp
= (wxApp
*)NULL
;
230 // and now delete the last logger as well
231 delete wxLog::SetActiveTarget(NULL
);