don't call CreatesMessageOutput() explicitly, it will be created on demand as/if...
[wxWidgets.git] / src / common / init.cpp
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
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif //__BORLANDC__
25
26 #ifndef WX_PRECOMP
27 #include "wx/app.h"
28 #include "wx/debug.h"
29 #include "wx/filefn.h"
30 #include "wx/log.h"
31 #endif
32
33 #include "wx/module.h"
34
35 // ----------------------------------------------------------------------------
36 // global vars
37 // ----------------------------------------------------------------------------
38
39 WXDLLEXPORT wxApp *wxTheApp = NULL;
40
41 wxAppInitializerFunction
42 wxAppBase::m_appInitFn = (wxAppInitializerFunction)NULL;
43
44 // ----------------------------------------------------------------------------
45 // private classes
46 // ----------------------------------------------------------------------------
47
48 class /* no WXDLLEXPORT */ wxConsoleApp : public wxApp
49 {
50 public:
51 virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
52 virtual bool ProcessIdle() { return TRUE; }
53 };
54
55 // ----------------------------------------------------------------------------
56 // private functions
57 // ----------------------------------------------------------------------------
58
59 static bool DoInit();
60 static void DoCleanUp();
61
62 // ----------------------------------------------------------------------------
63 // private vars
64 // ----------------------------------------------------------------------------
65
66 static size_t gs_nInitCount = 0;
67
68 // ============================================================================
69 // implementation
70 // ============================================================================
71
72 // ----------------------------------------------------------------------------
73 // stubs for some GUI functions
74 // ----------------------------------------------------------------------------
75
76 void WXDLLEXPORT wxExit()
77 {
78 abort();
79 }
80
81 // Yield to other apps/messages
82 void WXDLLEXPORT wxWakeUpIdle()
83 {
84 // do nothing
85 }
86
87 // ----------------------------------------------------------------------------
88 // wxBase-specific functions
89 // ----------------------------------------------------------------------------
90
91 bool WXDLLEXPORT wxInitialize()
92 {
93 if ( gs_nInitCount )
94 {
95 // already initialized
96 return TRUE;
97 }
98
99 wxASSERT_MSG( !wxTheApp,
100 wxT("either call wxInitialize or create app, not both!") );
101
102 if ( !DoInit() )
103 {
104 return FALSE;
105 }
106
107 wxTheApp = new wxConsoleApp;
108
109 if ( !wxTheApp )
110 {
111 return FALSE;
112 }
113
114 gs_nInitCount++;
115
116 return TRUE;
117 }
118
119 void WXDLLEXPORT wxUninitialize()
120 {
121 if ( !--gs_nInitCount )
122 {
123 DoCleanUp();
124 }
125 }
126
127 int 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 {
156 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLocal.cMB2WX(argv[mb_argc]));
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();
178
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 // ----------------------------------------------------------------------------
192
193 static bool DoInit()
194 {
195 wxClassInfo::InitializeClasses();
196
197 wxModule::RegisterModules();
198 if ( !wxModule::InitializeModules() )
199 {
200 return FALSE;
201 }
202
203 return TRUE;
204 }
205
206 static 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
217 wxLog::DontCreateOnDemand();
218 delete wxLog::SetActiveTarget(new wxLogStderr);
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;
228
229 #if wxUSE_LOG
230 // and now delete the last logger as well
231 delete wxLog::SetActiveTarget(NULL);
232 #endif // wxUSE_LOG
233 }
234
235 // vi:sts=4:sw=4:et