]> git.saurik.com Git - wxWidgets.git/blame - src/common/init.cpp
check the week day for consistency in ParseFormat() (replaces patch 788052)
[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"
94826170 31 #include "wx/thread.h"
e87271f3 32#endif
e90c1d2a 33
abca8ebf
VZ
34#include "wx/init.h"
35
94826170 36#include "wx/ptr_scpd.h"
51abe921
SC
37#include "wx/module.h"
38
94826170
VZ
39#if defined(__WXMSW__) && defined(__WXDEBUG__)
40 #include "wx/msw/msvcrt.h"
41
42 static struct EnableMemLeakChecking
43 {
44 EnableMemLeakChecking()
45 {
46 // do check for memory leaks on program exit (another useful flag
47 // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated
48 // memory which may be used to simulate low-memory condition)
49 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
50 }
51 } gs_enableLeakChecks;
52#endif // __WXMSW__ && __WXDEBUG__
53
e90c1d2a
VZ
54// ----------------------------------------------------------------------------
55// private classes
56// ----------------------------------------------------------------------------
57
e2478fde 58// we need a dummy app object if the user doesn't want to create a real one
94826170 59class wxDummyConsoleApp : public wxAppConsole
e90c1d2a
VZ
60{
61public:
fc7a2a60
VZ
62 wxDummyConsoleApp() { }
63
e2478fde 64 virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; }
fc7a2a60
VZ
65
66 DECLARE_NO_COPY_CLASS(wxDummyConsoleApp)
e90c1d2a
VZ
67};
68
94826170 69// we need a special kind of auto pointer to wxApp which not only deletes the
7cafd224 70// pointer it holds in its dtor but also resets the global application pointer
77c6966e
VS
71wxDECLARE_SCOPED_PTR(wxAppConsole, wxAppPtrBase);
72wxDEFINE_SCOPED_PTR(wxAppConsole, wxAppPtrBase);
bc385ba9 73
94826170
VZ
74class wxAppPtr : public wxAppPtrBase
75{
76public:
77c6966e 77 wxEXPLICIT wxAppPtr(wxAppConsole *ptr = NULL) : wxAppPtrBase(ptr) { }
94826170
VZ
78 ~wxAppPtr()
79 {
80 if ( get() )
81 {
82 // the pointer is going to be deleted in the base class dtor, don't
83 // leave the dangling pointer!
a80e5f9e 84 wxApp::SetInstance(NULL);
94826170
VZ
85 }
86 }
87
77c6966e 88 void Set(wxAppConsole *ptr)
94826170
VZ
89 {
90 reset(ptr);
91
a80e5f9e 92 wxApp::SetInstance(ptr);
94826170 93 }
fc7a2a60
VZ
94
95 DECLARE_NO_COPY_CLASS(wxAppPtr)
94826170
VZ
96};
97
05e2b077
VZ
98// class to ensure that wxAppBase::CleanUp() is called if our Initialize()
99// fails
100class wxCallAppCleanup
101{
102public:
77c6966e 103 wxCallAppCleanup(wxAppConsole *app) : m_app(app) { }
05e2b077
VZ
104 ~wxCallAppCleanup() { if ( m_app ) m_app->CleanUp(); }
105
106 void Dismiss() { m_app = NULL; }
107
108private:
77c6966e 109 wxAppConsole *m_app;
05e2b077
VZ
110};
111
94826170
VZ
112// another tiny class which simply exists to ensure that wxEntryCleanup is
113// always called
114class wxCleanupOnExit
115{
116public:
117 ~wxCleanupOnExit() { wxEntryCleanup(); }
118};
bc385ba9 119
005b5298
VZ
120// ----------------------------------------------------------------------------
121// private functions
122// ----------------------------------------------------------------------------
123
124// suppress warnings about unused variables
125static inline void Use(void *) { }
126
127#define WX_SUPPRESS_UNUSED_WARN(x) Use(&x)
128
e90c1d2a 129// ----------------------------------------------------------------------------
94826170 130// initialization data
e90c1d2a
VZ
131// ----------------------------------------------------------------------------
132
94826170
VZ
133static struct InitData
134{
135 InitData()
136 {
137 nInitCount = 0;
138
139#if wxUSE_UNICODE
140 argc = 0;
141 // argv = NULL; -- not even really needed
142#endif // wxUSE_UNICODE
143 }
144
145 // critical section protecting this struct
146 wxCRIT_SECT_DECLARE_MEMBER(csInit);
147
148 // number of times wxInitialize() was called minus the number of times
149 // wxUninitialize() was
150 size_t nInitCount;
151
152#if wxUSE_UNICODE
153 int argc;
154
155 // if we receive the command line arguments as ASCII and have to convert
156 // them to Unicode ourselves (this is the case under Unix but not Windows,
157 // for example), we remember the converted argv here because we'll have to
158 // free it when doing cleanup to avoid memory leaks
150cb195 159 wchar_t **argv;
94826170 160#endif // wxUSE_UNICODE
fc7a2a60
VZ
161
162 DECLARE_NO_COPY_CLASS(InitData)
94826170 163} gs_initData;
e90c1d2a
VZ
164
165// ============================================================================
166// implementation
167// ============================================================================
168
252a752e 169// ----------------------------------------------------------------------------
94826170 170// command line arguments ANSI -> Unicode conversion
252a752e
VZ
171// ----------------------------------------------------------------------------
172
94826170
VZ
173#if wxUSE_UNICODE
174
175static void ConvertArgsToUnicode(int argc, char **argv)
e90c1d2a 176{
94826170
VZ
177 gs_initData.argv = new wchar_t *[argc + 1];
178 for ( int i = 0; i < argc; i++ )
e90c1d2a 179 {
94826170 180 gs_initData.argv[i] = wxStrdup(wxConvLocal.cMB2WX(argv[i]));
e90c1d2a
VZ
181 }
182
94826170
VZ
183 gs_initData.argv[argc] = NULL;
184}
e90c1d2a 185
94826170
VZ
186static void FreeConvertedArgs()
187{
150cb195 188 if ( gs_initData.argv )
bbfa0322 189 {
150cb195
VZ
190 for ( int i = 0; i < gs_initData.argc; i++ )
191 {
192 free(gs_initData.argv[i]);
193 }
194
195 delete [] gs_initData.argv;
196 gs_initData.argv = NULL;
bbfa0322 197 }
94826170 198}
bbfa0322 199
94826170 200#endif // wxUSE_UNICODE
e90c1d2a 201
94826170
VZ
202// ----------------------------------------------------------------------------
203// start up
204// ----------------------------------------------------------------------------
bbfa0322 205
94826170
VZ
206// initialization which is always done (not customizable) before wxApp creation
207static bool DoCommonPreInit()
208{
94826170 209 return true;
e90c1d2a
VZ
210}
211
94826170
VZ
212// non customizable initialization done after wxApp creation and initialization
213static bool DoCommonPostInit()
e90c1d2a 214{
94826170
VZ
215 wxModule::RegisterModules();
216
217 return wxModule::InitializeModules();
bc385ba9
VZ
218}
219
05e2b077 220bool wxEntryStart(int& argc, wxChar **argv)
bc385ba9 221{
94826170
VZ
222 // do minimal, always necessary, initialization
223 // --------------------------------------------
224
225 // initialize wxRTTI
226 if ( !DoCommonPreInit() )
bc385ba9 227 {
94826170 228 return false;
bc385ba9
VZ
229 }
230
bc385ba9 231
94826170
VZ
232 // first of all, we need an application object
233 // -------------------------------------------
234
235 // the user might have already created it himself somehow
236 wxAppPtr app(wxTheApp);
237 if ( !app.get() )
238 {
239 // if not, he might have used IMPLEMENT_APP() to give us a function to
240 // create it
bc385ba9
VZ
241 wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
242
94826170
VZ
243 if ( fnCreate )
244 {
245 // he did, try to create the custom wxApp object
7cafd224 246 app.Set((*fnCreate)());
94826170
VZ
247 }
248 }
249
250 if ( !app.get() )
251 {
252 // either IMPLEMENT_APP() was not used at all or it failed -- in any
253 // case we still need something
7cafd224 254 app.Set(new wxDummyConsoleApp);
bc385ba9
VZ
255 }
256
bc385ba9 257
94826170
VZ
258 // wxApp initialization: this can be customized
259 // --------------------------------------------
bc385ba9 260
7cafd224 261 if ( !app->Initialize(argc, argv) )
bc385ba9 262 {
94826170 263 return false;
bc385ba9 264 }
bc385ba9 265
7cafd224 266 wxCallAppCleanup callAppCleanup(app.get());
05e2b077
VZ
267
268 // for compatibility call the old initialization function too
7cafd224 269 if ( !app->OnInitGui() )
05e2b077
VZ
270 return false;
271
bc385ba9 272
94826170
VZ
273 // common initialization after wxTheApp creation
274 // ---------------------------------------------
bc385ba9 275
94826170 276 if ( !DoCommonPostInit() )
94826170 277 return false;
bc385ba9 278
bc385ba9 279
94826170
VZ
280 // prevent the smart pointer from destroying its contents
281 app.release();
282
05e2b077
VZ
283 // and the cleanup object from doing cleanup
284 callAppCleanup.Dismiss();
285
94826170 286 return true;
bc385ba9
VZ
287}
288
94826170 289#if wxUSE_UNICODE
bbfa0322 290
94826170 291// we provide a wxEntryStart() wrapper taking "char *" pointer too
05e2b077 292bool wxEntryStart(int& argc, char **argv)
bc385ba9 293{
94826170 294 ConvertArgsToUnicode(argc, argv);
bc385ba9 295
94826170 296 if ( !wxEntryStart(argc, gs_initData.argv) )
bc385ba9 297 {
94826170
VZ
298 FreeConvertedArgs();
299
300 return false;
e90c1d2a 301 }
bc385ba9 302
94826170 303 return true;
bc385ba9
VZ
304}
305
94826170
VZ
306#endif // wxUSE_UNICODE
307
308// ----------------------------------------------------------------------------
309// clean up
310// ----------------------------------------------------------------------------
311
312// cleanup done before destroying wxTheApp
313static void DoCommonPreCleanup()
bc385ba9
VZ
314{
315#if wxUSE_LOG
94826170
VZ
316 // flush the logged messages if any and install a 'safer' log target: the
317 // default one (wxLogGui) can't be used after the resources are freed just
318 // below and the user supplied one might be even more unsafe (using any
319 // wxWindows GUI function is unsafe starting from now)
8a9c2246 320 wxLog::DontCreateOnDemand();
94826170
VZ
321
322 // this will flush the old messages if any
8a9c2246 323 delete wxLog::SetActiveTarget(new wxLogStderr);
bc385ba9
VZ
324#endif // wxUSE_LOG
325
326 wxModule::CleanUpModules();
94826170 327}
bc385ba9 328
94826170
VZ
329// cleanup done after destroying wxTheApp
330static void DoCommonPostCleanup()
331{
cafc76a4 332 wxClassInfo::CleanUp();
bc385ba9 333
94826170
VZ
334 // we can't do this in wxApp itself because it doesn't know if argv had
335 // been allocated
90aaa865 336#if wxUSE_UNICODE
94826170 337 FreeConvertedArgs();
90aaa865
VZ
338#endif // wxUSE_UNICODE
339
ab944555
VS
340 // check for memory leaks
341#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
342 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
343 {
344 wxLogDebug(wxT("There were memory leaks.\n"));
345 wxDebugContext::Dump();
346 wxDebugContext::PrintStatistics();
347 }
348#endif // Debug
349
8a9c2246
VZ
350#if wxUSE_LOG
351 // and now delete the last logger as well
352 delete wxLog::SetActiveTarget(NULL);
353#endif // wxUSE_LOG
e90c1d2a 354}
e2450fa9 355
94826170
VZ
356void wxEntryCleanup()
357{
358 DoCommonPreCleanup();
359
360
361 // delete the application object
362 if ( wxTheApp )
363 {
364 wxTheApp->CleanUp();
365
366 delete wxTheApp;
a80e5f9e 367 wxApp::SetInstance(NULL);
94826170
VZ
368 }
369
370
371 DoCommonPostCleanup();
94826170
VZ
372}
373
374// ----------------------------------------------------------------------------
375// wxEntry
376// ----------------------------------------------------------------------------
377
378#if !defined(__WXMSW__) || !wxUSE_ON_FATAL_EXCEPTION
379 #define wxEntryReal wxEntry
380#endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION)
381
05e2b077 382int wxEntryReal(int& argc, wxChar **argv)
94826170
VZ
383{
384 // library initialization
385 if ( !wxEntryStart(argc, argv) )
386 {
387 return -1;
388 }
389
390 // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code
391 // below returns or throws
392 wxCleanupOnExit cleanupOnExit;
393
005b5298
VZ
394 WX_SUPPRESS_UNUSED_WARN(cleanupOnExit);
395
94826170 396 // app initialization
7fbc89bb 397 if ( !wxTheApp->CallOnInit() )
94826170
VZ
398 {
399 // don't call OnExit() if OnInit() failed
400 return -1;
401 }
402
403 // app execution
404 int retValue = wxTheApp->OnRun();
405
94826170
VZ
406 // app clean up
407 wxTheApp->OnExit();
408
409 return retValue;
410}
411
412// wrap real wxEntry in a try-except block to be able to call
413// OnFatalException() if necessary
414#if defined(__WXMSW__) && wxUSE_ON_FATAL_EXCEPTION
415
1c193821
JS
416#ifdef __WXWINCE__
417// For ExitThread
418#include "wx/msw/private.h"
419#endif
420
c2892ef8 421extern unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS *pExcPtrs);
94826170 422
abca8ebf 423int wxEntry(int& argc, wxChar **argv)
94826170
VZ
424{
425 __try
426 {
427 return wxEntryReal(argc, argv);
428 }
c2892ef8 429 __except ( wxGlobalSEHandler(GetExceptionInformation()) )
94826170 430 {
1c193821
JS
431#ifdef __WXWINCE__
432 ::ExitThread(3); // the same exit code as abort()
433#else
94826170 434 ::ExitProcess(3); // the same exit code as abort()
1c193821 435#endif
94826170 436
cda66071 437#if !defined(_MSC_VER) || _MSC_VER < 1300
94826170 438 // this code is unreachable but put it here to suppress warnings
cda66071 439 // from some compilers
94826170 440 return -1;
cda66071 441#endif
94826170
VZ
442 }
443}
444
445#endif // __WXMSW__ && wxUSE_ON_FATAL_EXCEPTION
446
447#if wxUSE_UNICODE
448
449// as with wxEntryStart, we provide an ANSI wrapper
abca8ebf 450int wxEntry(int& argc, char **argv)
94826170
VZ
451{
452 ConvertArgsToUnicode(argc, argv);
453
454 return wxEntry(argc, gs_initData.argv);
455}
456
457#endif // wxUSE_UNICODE
458
459// ----------------------------------------------------------------------------
460// wxInitialize/wxUninitialize
461// ----------------------------------------------------------------------------
462
463bool wxInitialize(int argc, wxChar **argv)
464{
465 wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit);
466
467 if ( gs_initData.nInitCount++ )
468 {
469 // already initialized
470 return true;
471 }
472
473 return wxEntryStart(argc, argv);
474}
475
476void wxUninitialize()
477{
478 wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit);
479
480 if ( !--gs_initData.nInitCount )
481 {
482 wxEntryCleanup();
483 }
484}
485