]> git.saurik.com Git - wxWidgets.git/blame - src/common/init.cpp
Introducing wxBase for borland based on Michael Fieldings patch 598106
[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; }
6788de4d 52 virtual bool ProcessIdle() { return TRUE; }
e90c1d2a
VZ
53};
54
bc385ba9
VZ
55// ----------------------------------------------------------------------------
56// private functions
57// ----------------------------------------------------------------------------
58
59static bool DoInit();
60static void DoCleanUp();
61
e90c1d2a
VZ
62// ----------------------------------------------------------------------------
63// private vars
64// ----------------------------------------------------------------------------
65
66static size_t gs_nInitCount = 0;
67
68// ============================================================================
69// implementation
70// ============================================================================
71
252a752e
VZ
72// ----------------------------------------------------------------------------
73// stubs for some GUI functions
74// ----------------------------------------------------------------------------
75
76void WXDLLEXPORT wxExit()
77{
78 abort();
79}
80
252a752e
VZ
81// Yield to other apps/messages
82void WXDLLEXPORT wxWakeUpIdle()
83{
84 // do nothing
85}
86
87// ----------------------------------------------------------------------------
88// wxBase-specific functions
89// ----------------------------------------------------------------------------
90
e90c1d2a
VZ
91bool WXDLLEXPORT wxInitialize()
92{
bbfa0322 93 if ( gs_nInitCount )
e90c1d2a
VZ
94 {
95 // already initialized
96 return TRUE;
97 }
98
99 wxASSERT_MSG( !wxTheApp,
223d09f6 100 wxT("either call wxInitialize or create app, not both!") );
e90c1d2a 101
bc385ba9 102 if ( !DoInit() )
bbfa0322
VZ
103 {
104 return FALSE;
105 }
106
e90c1d2a
VZ
107 wxTheApp = new wxConsoleApp;
108
bbfa0322
VZ
109 if ( !wxTheApp )
110 {
111 return FALSE;
112 }
113
114 gs_nInitCount++;
115
116 return TRUE;
e90c1d2a
VZ
117}
118
119void WXDLLEXPORT wxUninitialize()
120{
121 if ( !--gs_nInitCount )
122 {
bc385ba9
VZ
123 DoCleanUp();
124 }
125}
126
127int wxEntry(int argc, char **argv)
128{
129 // library initialization
130 if ( !DoInit() )
131 {
132 return -1;
133 }
134
135 // create the app
136 if ( !wxTheApp )
137 {
138 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
139 wxT("No application object: use IMPLEMENT_APP macro.") );
140
141 wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
142
143 wxTheApp = (wxApp *)fnCreate();
144 }
145
146 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
147
148 // app preinitialization
149 wxTheApp->argc = argc;
150
151#if wxUSE_UNICODE
152 wxTheApp->argv = new wxChar*[argc+1];
153 int mb_argc = 0;
154 while (mb_argc < argc)
155 {
b1ac3b56 156 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLocal.cMB2WX(argv[mb_argc]));
bc385ba9
VZ
157 mb_argc++;
158 }
159 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
160#else
161 wxTheApp->argv = argv;
162#endif
163
164 wxString name = wxFileNameFromPath(argv[0]);
165 wxStripExtension(name);
166 wxTheApp->SetAppName(name);
167
168 int retValue = 0;
169
170 // app initialization
171 if ( !wxTheApp->OnInit() )
172 retValue = -1;
173
174 // app execution
175 if ( retValue == 0 )
176 {
177 retValue = wxTheApp->OnRun();
bbfa0322 178
bc385ba9
VZ
179 // app clean up
180 wxTheApp->OnExit();
181 }
182
183 // library clean up
184 DoCleanUp();
185
186 return retValue;
187}
188
189// ----------------------------------------------------------------------------
190// private functions
191// ----------------------------------------------------------------------------
bbfa0322 192
bc385ba9
VZ
193static bool DoInit()
194{
195 wxClassInfo::InitializeClasses();
196
197 wxModule::RegisterModules();
198 if ( !wxModule::InitializeModules() )
199 {
200 return FALSE;
e90c1d2a 201 }
bc385ba9
VZ
202
203 return TRUE;
204}
205
206static void DoCleanUp()
207{
208#if wxUSE_LOG
209 // flush the logged messages if any
210 wxLog *log = wxLog::GetActiveTarget();
211 if (log != NULL && log->HasPendingMessages())
212 log->Flush();
213
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
216 // more safe
8a9c2246
VZ
217 wxLog::DontCreateOnDemand();
218 delete wxLog::SetActiveTarget(new wxLogStderr);
bc385ba9
VZ
219#endif // wxUSE_LOG
220
221 wxModule::CleanUpModules();
222
223 wxClassInfo::CleanUpClasses();
224
225 // delete the application object
226 delete wxTheApp;
227 wxTheApp = (wxApp *)NULL;
8a9c2246
VZ
228
229#if wxUSE_LOG
230 // and now delete the last logger as well
231 delete wxLog::SetActiveTarget(NULL);
232#endif // wxUSE_LOG
e90c1d2a 233}
e2450fa9
RL
234
235// vi:sts=4:sw=4:et