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