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
;
114 wxTheApp
->CreateMessageOutput();
120 void WXDLLEXPORT
wxUninitialize()
122 if ( !--gs_nInitCount
)
128 int wxEntry(int argc
, char **argv
)
130 // library initialization
139 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
140 wxT("No application object: use IMPLEMENT_APP macro.") );
142 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
144 wxTheApp
= (wxApp
*)fnCreate();
147 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
149 // app preinitialization
150 wxTheApp
->argc
= argc
;
153 wxTheApp
->argv
= new wxChar
*[argc
+1];
155 while (mb_argc
< argc
)
157 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
160 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
162 wxTheApp
->argv
= argv
;
165 wxString name
= wxFileNameFromPath(argv
[0]);
166 wxStripExtension(name
);
167 wxTheApp
->SetAppName(name
);
171 // app initialization
172 if ( !wxTheApp
->OnInit() )
178 retValue
= wxTheApp
->OnRun();
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
196 wxClassInfo::InitializeClasses();
198 wxModule::RegisterModules();
199 if ( !wxModule::InitializeModules() )
207 static void DoCleanUp()
210 // flush the logged messages if any
211 wxLog
*log
= wxLog::GetActiveTarget();
212 if (log
!= NULL
&& log
->HasPendingMessages())
215 // continuing to use user defined log target is unsafe from now on because
216 // some resources may be already unavailable, so replace it by something
218 wxLog::DontCreateOnDemand();
219 delete wxLog::SetActiveTarget(new wxLogStderr
);
222 wxModule::CleanUpModules();
224 wxClassInfo::CleanUpClasses();
226 // delete the application object
228 wxTheApp
= (wxApp
*)NULL
;
231 // and now delete the last logger as well
232 delete wxLog::SetActiveTarget(NULL
);