]> git.saurik.com Git - wxWidgets.git/blame - src/common/init.cpp
Unicode compilation fixes
[wxWidgets.git] / src / common / init.cpp
CommitLineData
e90c1d2a
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/init.cpp
3// Purpose: initialisation for the library
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.10.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
f4c890fd
JS
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif //__BORLANDC__
25
e87271f3
VZ
26#ifndef WX_PRECOMP
27 #include "wx/app.h"
28 #include "wx/debug.h"
bc385ba9 29 #include "wx/filefn.h"
e87271f3 30#endif
e90c1d2a 31
51abe921
SC
32#include "wx/module.h"
33
e90c1d2a
VZ
34// ----------------------------------------------------------------------------
35// global vars
36// ----------------------------------------------------------------------------
37
f6bcfd97 38WXDLLEXPORT wxApp *wxTheApp = NULL;
e90c1d2a
VZ
39
40wxAppInitializerFunction
41 wxAppBase::m_appInitFn = (wxAppInitializerFunction)NULL;
42
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47class /* no WXDLLEXPORT */ wxConsoleApp : public wxApp
48{
49public:
223d09f6 50 virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
e90c1d2a
VZ
51};
52
bc385ba9
VZ
53// ----------------------------------------------------------------------------
54// private functions
55// ----------------------------------------------------------------------------
56
57static bool DoInit();
58static void DoCleanUp();
59
e90c1d2a
VZ
60// ----------------------------------------------------------------------------
61// private vars
62// ----------------------------------------------------------------------------
63
64static size_t gs_nInitCount = 0;
65
66// ============================================================================
67// implementation
68// ============================================================================
69
252a752e
VZ
70// ----------------------------------------------------------------------------
71// stubs for some GUI functions
72// ----------------------------------------------------------------------------
73
74void WXDLLEXPORT wxExit()
75{
76 abort();
77}
78
252a752e
VZ
79// Yield to other apps/messages
80void WXDLLEXPORT wxWakeUpIdle()
81{
82 // do nothing
83}
84
85// ----------------------------------------------------------------------------
86// wxBase-specific functions
87// ----------------------------------------------------------------------------
88
e90c1d2a
VZ
89bool WXDLLEXPORT wxInitialize()
90{
bbfa0322 91 if ( gs_nInitCount )
e90c1d2a
VZ
92 {
93 // already initialized
94 return TRUE;
95 }
96
97 wxASSERT_MSG( !wxTheApp,
223d09f6 98 wxT("either call wxInitialize or create app, not both!") );
e90c1d2a 99
bc385ba9 100 if ( !DoInit() )
bbfa0322
VZ
101 {
102 return FALSE;
103 }
104
e90c1d2a
VZ
105 wxTheApp = new wxConsoleApp;
106
bbfa0322
VZ
107 if ( !wxTheApp )
108 {
109 return FALSE;
110 }
111
112 gs_nInitCount++;
113
114 return TRUE;
e90c1d2a
VZ
115}
116
117void WXDLLEXPORT wxUninitialize()
118{
119 if ( !--gs_nInitCount )
120 {
bc385ba9
VZ
121 DoCleanUp();
122 }
123}
124
125int wxEntry(int argc, char **argv)
126{
127 // library initialization
128 if ( !DoInit() )
129 {
130 return -1;
131 }
132
133 // create the app
134 if ( !wxTheApp )
135 {
136 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
137 wxT("No application object: use IMPLEMENT_APP macro.") );
138
139 wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
140
141 wxTheApp = (wxApp *)fnCreate();
142 }
143
144 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
145
146 // app preinitialization
147 wxTheApp->argc = argc;
148
149#if wxUSE_UNICODE
150 wxTheApp->argv = new wxChar*[argc+1];
151 int mb_argc = 0;
152 while (mb_argc < argc)
153 {
154 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
155 mb_argc++;
156 }
157 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
158#else
159 wxTheApp->argv = argv;
160#endif
161
162 wxString name = wxFileNameFromPath(argv[0]);
163 wxStripExtension(name);
164 wxTheApp->SetAppName(name);
165
166 int retValue = 0;
167
168 // app initialization
169 if ( !wxTheApp->OnInit() )
170 retValue = -1;
171
172 // app execution
173 if ( retValue == 0 )
174 {
175 retValue = wxTheApp->OnRun();
bbfa0322 176
bc385ba9
VZ
177 // app clean up
178 wxTheApp->OnExit();
179 }
180
181 // library clean up
182 DoCleanUp();
183
184 return retValue;
185}
186
187// ----------------------------------------------------------------------------
188// private functions
189// ----------------------------------------------------------------------------
bbfa0322 190
bc385ba9
VZ
191static bool DoInit()
192{
193 wxClassInfo::InitializeClasses();
194
195 wxModule::RegisterModules();
196 if ( !wxModule::InitializeModules() )
197 {
198 return FALSE;
e90c1d2a 199 }
bc385ba9
VZ
200
201 return TRUE;
202}
203
204static void DoCleanUp()
205{
206#if wxUSE_LOG
207 // flush the logged messages if any
208 wxLog *log = wxLog::GetActiveTarget();
209 if (log != NULL && log->HasPendingMessages())
210 log->Flush();
211
212 // continuing to use user defined log target is unsafe from now on because
213 // some resources may be already unavailable, so replace it by something
214 // more safe
8a9c2246
VZ
215 wxLog::DontCreateOnDemand();
216 delete wxLog::SetActiveTarget(new wxLogStderr);
bc385ba9
VZ
217#endif // wxUSE_LOG
218
219 wxModule::CleanUpModules();
220
221 wxClassInfo::CleanUpClasses();
222
223 // delete the application object
224 delete wxTheApp;
225 wxTheApp = (wxApp *)NULL;
8a9c2246
VZ
226
227#if wxUSE_LOG
228 // and now delete the last logger as well
229 delete wxLog::SetActiveTarget(NULL);
230#endif // wxUSE_LOG
e90c1d2a 231}