]> git.saurik.com Git - wxWidgets.git/blame - src/common/init.cpp
Somehow, setting a tint color makes gauge work :/.
[wxWidgets.git] / src / common / init.cpp
CommitLineData
e90c1d2a 1/////////////////////////////////////////////////////////////////////////////
9a6384ca 2// Name: src/common/init.cpp
e90c1d2a
VZ
3// Purpose: initialisation for the library
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 04.10.99
e90c1d2a 7// Copyright: (c) Vadim Zeitlin
65571936 8// Licence: wxWindows licence
e90c1d2a
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
f4c890fd
JS
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
9a6384ca 22 #pragma hdrstop
f4c890fd
JS
23#endif //__BORLANDC__
24
e87271f3
VZ
25#ifndef WX_PRECOMP
26 #include "wx/app.h"
bc385ba9 27 #include "wx/filefn.h"
e2450fa9 28 #include "wx/log.h"
a20599da 29 #include "wx/intl.h"
02761f6c 30 #include "wx/module.h"
e87271f3 31#endif
e90c1d2a 32
abca8ebf 33#include "wx/init.h"
50a743c9 34#include "wx/thread.h"
abca8ebf 35
664e1314 36#include "wx/scopedptr.h"
fb3e83b6 37#include "wx/except.h"
51abe921 38
d98a58c5 39#if defined(__WINDOWS__)
98a0eff6 40 #include "wx/msw/private.h"
94826170
VZ
41 #include "wx/msw/msvcrt.h"
42
4b6a582b
VZ
43 #ifdef wxCrtSetDbgFlag
44 static struct EnableMemLeakChecking
94826170 45 {
4b6a582b
VZ
46 EnableMemLeakChecking()
47 {
48 // check for memory leaks on program exit (another useful flag
49 // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated
50 // memory which may be used to simulate low-memory condition)
51 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
52 }
53 } gs_enableLeakChecks;
54 #endif // wxCrtSetDbgFlag
d98a58c5 55#endif // __WINDOWS__
94826170 56
f5462b02
VS
57#if wxUSE_UNICODE && defined(__WXOSX__)
58 #include <locale.h>
59#endif
60
e90c1d2a
VZ
61// ----------------------------------------------------------------------------
62// private classes
63// ----------------------------------------------------------------------------
64
e2478fde 65// we need a dummy app object if the user doesn't want to create a real one
94826170 66class wxDummyConsoleApp : public wxAppConsole
e90c1d2a
VZ
67{
68public:
fc7a2a60
VZ
69 wxDummyConsoleApp() { }
70
9a83f860 71 virtual int OnRun() { wxFAIL_MSG( wxT("unreachable code") ); return 0; }
d48b06bd 72 virtual bool DoYield(bool, long) { return true; }
fc7a2a60 73
c0c133e1 74 wxDECLARE_NO_COPY_CLASS(wxDummyConsoleApp);
e90c1d2a
VZ
75};
76
94826170 77// we need a special kind of auto pointer to wxApp which not only deletes the
7cafd224 78// pointer it holds in its dtor but also resets the global application pointer
d0ee33f5
WS
79wxDECLARE_SCOPED_PTR(wxAppConsole, wxAppPtrBase)
80wxDEFINE_SCOPED_PTR(wxAppConsole, wxAppPtrBase)
bc385ba9 81
94826170
VZ
82class wxAppPtr : public wxAppPtrBase
83{
84public:
77c6966e 85 wxEXPLICIT wxAppPtr(wxAppConsole *ptr = NULL) : wxAppPtrBase(ptr) { }
94826170
VZ
86 ~wxAppPtr()
87 {
88 if ( get() )
89 {
90 // the pointer is going to be deleted in the base class dtor, don't
91 // leave the dangling pointer!
a80e5f9e 92 wxApp::SetInstance(NULL);
94826170
VZ
93 }
94 }
95
77c6966e 96 void Set(wxAppConsole *ptr)
94826170
VZ
97 {
98 reset(ptr);
99
a80e5f9e 100 wxApp::SetInstance(ptr);
94826170 101 }
fc7a2a60 102
c0c133e1 103 wxDECLARE_NO_COPY_CLASS(wxAppPtr);
94826170
VZ
104};
105
05e2b077
VZ
106// class to ensure that wxAppBase::CleanUp() is called if our Initialize()
107// fails
108class wxCallAppCleanup
109{
110public:
77c6966e 111 wxCallAppCleanup(wxAppConsole *app) : m_app(app) { }
05e2b077
VZ
112 ~wxCallAppCleanup() { if ( m_app ) m_app->CleanUp(); }
113
114 void Dismiss() { m_app = NULL; }
115
116private:
77c6966e 117 wxAppConsole *m_app;
05e2b077
VZ
118};
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 161
c0c133e1 162 wxDECLARE_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 177 gs_initData.argv = new wchar_t *[argc + 1];
265db88d 178 int wargc = 0;
94826170 179 for ( int i = 0; i < argc; i++ )
e90c1d2a 180 {
a47f55c5
SC
181#ifdef __DARWIN__
182 wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[i]));
183#else
faa112c4 184 wxWCharBuffer buf(wxConvLocal.cMB2WX(argv[i]));
a47f55c5 185#endif
265db88d
VZ
186 if ( !buf )
187 {
188 wxLogWarning(_("Command line argument %d couldn't be converted to Unicode and will be ignored."),
189 i);
190 }
191 else // converted ok
192 {
193 gs_initData.argv[wargc++] = wxStrdup(buf);
194 }
e90c1d2a
VZ
195 }
196
265db88d
VZ
197 gs_initData.argc = wargc;
198 gs_initData.argv[wargc] = NULL;
94826170 199}
e90c1d2a 200
94826170
VZ
201static void FreeConvertedArgs()
202{
150cb195 203 if ( gs_initData.argv )
bbfa0322 204 {
150cb195
VZ
205 for ( int i = 0; i < gs_initData.argc; i++ )
206 {
207 free(gs_initData.argv[i]);
208 }
209
5276b0a5 210 wxDELETEA(gs_initData.argv);
08989e30 211 gs_initData.argc = 0;
bbfa0322 212 }
94826170 213}
bbfa0322 214
94826170 215#endif // wxUSE_UNICODE
e90c1d2a 216
94826170
VZ
217// ----------------------------------------------------------------------------
218// start up
219// ----------------------------------------------------------------------------
bbfa0322 220
94826170
VZ
221// initialization which is always done (not customizable) before wxApp creation
222static bool DoCommonPreInit()
223{
270bae20
VS
224#if wxUSE_UNICODE && defined(__WXOSX__)
225 // In OS X and iOS, wchar_t CRT functions convert to char* and fail under
226 // some locales. The safest fix is to set LC_CTYPE to UTF-8 to ensure that
227 // they can handle any input.
228 //
229 // Note that this must be done for any app, Cocoa or console, whether or
230 // not it uses wxLocale.
231 //
232 // See http://stackoverflow.com/questions/11713745/why-does-the-printf-family-of-functions-care-about-locale
233 setlocale(LC_CTYPE, "UTF-8");
234#endif // wxUSE_UNICODE && defined(__WXOSX__)
235
1c7b2f01 236#if wxUSE_LOG
e94cd97d
DE
237 // Reset logging in case we were cleaned up and are being reinitialized.
238 wxLog::DoCreateOnDemand();
a2084452
VZ
239
240 // force wxLog to create a log target now: we do it because wxTheApp
241 // doesn't exist yet so wxLog will create a special log target which is
242 // safe to use even when the GUI is not available while without this call
243 // we could create wxApp in wxEntryStart() below, then log an error about
244 // e.g. failure to establish connection to the X server and wxLog would
245 // send it to wxLogGui (because wxTheApp does exist already) which, of
246 // course, can't be used in this case
247 //
248 // notice also that this does nothing if the user had set up a custom log
249 // target before -- which is fine as we want to give him this possibility
250 // (as it's impossible to override logging by overriding wxAppTraits::
251 // CreateLogTarget() before wxApp is created) and we just assume he knows
252 // what he is doing
253 wxLog::GetActiveTarget();
1c7b2f01
VZ
254#endif // wxUSE_LOG
255
d98a58c5 256#ifdef __WINDOWS__
98a0eff6
VZ
257 // GUI applications obtain HINSTANCE in their WinMain() but we also need to
258 // initialize the global wxhInstance variable for the console programs as
259 // they may need it too, so set it here if it wasn't done yet
260 if ( !wxGetInstance() )
261 {
262 wxSetInstance(::GetModuleHandle(NULL));
263 }
d98a58c5 264#endif // __WINDOWS__
98a0eff6 265
94826170 266 return true;
e90c1d2a
VZ
267}
268
94826170
VZ
269// non customizable initialization done after wxApp creation and initialization
270static bool DoCommonPostInit()
e90c1d2a 271{
94826170
VZ
272 wxModule::RegisterModules();
273
1c7b2f01
VZ
274 if ( !wxModule::InitializeModules() )
275 {
276 wxLogError(_("Initialization failed in post init, aborting."));
277 return false;
278 }
279
280 return true;
bc385ba9
VZ
281}
282
05e2b077 283bool wxEntryStart(int& argc, wxChar **argv)
bc385ba9 284{
94826170
VZ
285 // do minimal, always necessary, initialization
286 // --------------------------------------------
287
288 // initialize wxRTTI
289 if ( !DoCommonPreInit() )
94826170 290 return false;
bc385ba9 291
bc385ba9 292
94826170
VZ
293 // first of all, we need an application object
294 // -------------------------------------------
295
296 // the user might have already created it himself somehow
297 wxAppPtr app(wxTheApp);
298 if ( !app.get() )
299 {
300 // if not, he might have used IMPLEMENT_APP() to give us a function to
301 // create it
bc385ba9
VZ
302 wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
303
94826170
VZ
304 if ( fnCreate )
305 {
306 // he did, try to create the custom wxApp object
7cafd224 307 app.Set((*fnCreate)());
94826170
VZ
308 }
309 }
310
311 if ( !app.get() )
312 {
313 // either IMPLEMENT_APP() was not used at all or it failed -- in any
314 // case we still need something
7cafd224 315 app.Set(new wxDummyConsoleApp);
bc385ba9
VZ
316 }
317
bc385ba9 318
94826170
VZ
319 // wxApp initialization: this can be customized
320 // --------------------------------------------
bc385ba9 321
7cafd224 322 if ( !app->Initialize(argc, argv) )
94826170 323 return false;
bc385ba9 324
4f6b94a3
VZ
325 // remember, possibly modified (e.g. due to removal of toolkit-specific
326 // parameters), command line arguments in member variables
327 app->argc = argc;
328 app->argv = argv;
329
7cafd224 330 wxCallAppCleanup callAppCleanup(app.get());
05e2b077 331
bc385ba9 332
94826170
VZ
333 // common initialization after wxTheApp creation
334 // ---------------------------------------------
bc385ba9 335
94826170 336 if ( !DoCommonPostInit() )
94826170 337 return false;
bc385ba9 338
bc385ba9 339
94826170
VZ
340 // prevent the smart pointer from destroying its contents
341 app.release();
342
05e2b077
VZ
343 // and the cleanup object from doing cleanup
344 callAppCleanup.Dismiss();
345
1c7b2f01
VZ
346#if wxUSE_LOG
347 // now that we have a valid wxApp (wxLogGui would have crashed if we used
348 // it before now), we can delete the temporary sink we had created for the
349 // initialization messages -- the next time logging function is called, the
350 // sink will be recreated but this time wxAppTraits will be used
351 delete wxLog::SetActiveTarget(NULL);
352#endif // wxUSE_LOG
353
94826170 354 return true;
bc385ba9
VZ
355}
356
94826170 357#if wxUSE_UNICODE
bbfa0322 358
94826170 359// we provide a wxEntryStart() wrapper taking "char *" pointer too
05e2b077 360bool wxEntryStart(int& argc, char **argv)
bc385ba9 361{
94826170 362 ConvertArgsToUnicode(argc, argv);
bc385ba9 363
265db88d 364 if ( !wxEntryStart(gs_initData.argc, gs_initData.argv) )
bc385ba9 365 {
94826170
VZ
366 FreeConvertedArgs();
367
368 return false;
e90c1d2a 369 }
bc385ba9 370
94826170 371 return true;
bc385ba9
VZ
372}
373
94826170
VZ
374#endif // wxUSE_UNICODE
375
376// ----------------------------------------------------------------------------
377// clean up
378// ----------------------------------------------------------------------------
379
7beb59f3 380// cleanup done before destroying wxTheApp
94826170 381static void DoCommonPreCleanup()
bc385ba9
VZ
382{
383#if wxUSE_LOG
55b24eb8
VZ
384 // flush the logged messages if any and don't use the current probably
385 // unsafe log target any more: the default one (wxLogGui) can't be used
386 // after the resources are freed which happens when we return and the user
387 // supplied one might be even more unsafe (using any wxWidgets GUI function
388 // is unsafe starting from now)
389 //
390 // notice that wxLog will still recreate a default log target if any
391 // messages are logged but that one will be safe to use until the very end
392 delete wxLog::SetActiveTarget(NULL);
bc385ba9 393#endif // wxUSE_LOG
94826170 394}
bc385ba9 395
94826170
VZ
396// cleanup done after destroying wxTheApp
397static void DoCommonPostCleanup()
398{
a4de7e8c
VZ
399 wxModule::CleanUpModules();
400
94826170
VZ
401 // we can't do this in wxApp itself because it doesn't know if argv had
402 // been allocated
90aaa865 403#if wxUSE_UNICODE
94826170 404 FreeConvertedArgs();
90aaa865
VZ
405#endif // wxUSE_UNICODE
406
2ee96a25
VZ
407 // use Set(NULL) and not Get() to avoid creating a message output object on
408 // demand when we just want to delete it
409 delete wxMessageOutput::Set(NULL);
410
8a9c2246 411#if wxUSE_LOG
bca27719
VZ
412 // call this first as it has a side effect: in addition to flushing all
413 // logs for this thread, it also flushes everything logged from other
414 // threads
415 wxLog::FlushActive();
416
8a9c2246 417 // and now delete the last logger as well
55b24eb8
VZ
418 //
419 // we still don't disable log target auto-vivification even if any log
420 // objects created now will result in memory leaks because it seems better
421 // to leak memory which doesn't matter much considering the application is
422 // exiting anyhow than to not show messages which could still be logged
423 // from the user code (e.g. static dtors and such)
8a9c2246
VZ
424 delete wxLog::SetActiveTarget(NULL);
425#endif // wxUSE_LOG
e90c1d2a 426}
e2450fa9 427
94826170
VZ
428void wxEntryCleanup()
429{
430 DoCommonPreCleanup();
431
432
433 // delete the application object
434 if ( wxTheApp )
435 {
436 wxTheApp->CleanUp();
437
b7b00ae1
VZ
438 // reset the global pointer to it to NULL before destroying it as in
439 // some circumstances this can result in executing the code using
440 // wxTheApp and using half-destroyed object is no good
441 wxAppConsole * const app = wxApp::GetInstance();
a80e5f9e 442 wxApp::SetInstance(NULL);
b7b00ae1 443 delete app;
94826170
VZ
444 }
445
446
447 DoCommonPostCleanup();
94826170
VZ
448}
449
450// ----------------------------------------------------------------------------
451// wxEntry
452// ----------------------------------------------------------------------------
453
226c11c0 454// for MSW the real wxEntry is defined in msw/main.cpp
d98a58c5 455#ifndef __WINDOWS__
94826170 456 #define wxEntryReal wxEntry
d98a58c5 457#endif // !__WINDOWS__
94826170 458
05e2b077 459int wxEntryReal(int& argc, wxChar **argv)
94826170
VZ
460{
461 // library initialization
6ecb1fdd
VS
462 wxInitializer initializer(argc, argv);
463
464 if ( !initializer.IsOk() )
94826170 465 {
688e04b1 466#if wxUSE_LOG
1c7b2f01
VZ
467 // flush any log messages explaining why we failed
468 delete wxLog::SetActiveTarget(NULL);
688e04b1 469#endif
94826170
VZ
470 return -1;
471 }
472
fb3e83b6 473 wxTRY
94826170 474 {
31fda0c2 475#if 0 // defined(__WXOSX__) && wxOSX_USE_COCOA_OR_IPHONE
b0ae6049
SC
476 // everything done in OnRun using native callbacks
477#else
fb3e83b6
VZ
478 // app initialization
479 if ( !wxTheApp->CallOnInit() )
480 {
481 // don't call OnExit() if OnInit() failed
482 return -1;
483 }
94826170 484
fe277be0
VZ
485 // ensure that OnExit() is called if OnInit() had succeeded
486 class CallOnExit
487 {
488 public:
489 ~CallOnExit() { wxTheApp->OnExit(); }
490 } callOnExit;
94826170 491
9e9eea73 492 WX_SUPPRESS_UNUSED_WARN(callOnExit);
b0ae6049 493#endif
fe277be0
VZ
494 // app execution
495 return wxTheApp->OnRun();
fb3e83b6
VZ
496 }
497 wxCATCH_ALL( wxTheApp->OnUnhandledException(); return -1; )
94826170
VZ
498}
499
94826170
VZ
500#if wxUSE_UNICODE
501
502// as with wxEntryStart, we provide an ANSI wrapper
abca8ebf 503int wxEntry(int& argc, char **argv)
94826170
VZ
504{
505 ConvertArgsToUnicode(argc, argv);
506
265db88d 507 return wxEntry(gs_initData.argc, gs_initData.argv);
94826170
VZ
508}
509
510#endif // wxUSE_UNICODE
511
512// ----------------------------------------------------------------------------
513// wxInitialize/wxUninitialize
514// ----------------------------------------------------------------------------
515
d3688603
VS
516bool wxInitialize()
517{
518 return wxInitialize(0, (wxChar**)NULL);
519}
520
94826170
VZ
521bool wxInitialize(int argc, wxChar **argv)
522{
523 wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit);
524
525 if ( gs_initData.nInitCount++ )
526 {
527 // already initialized
528 return true;
529 }
530
531 return wxEntryStart(argc, argv);
532}
533
f380e251
VS
534#if wxUSE_UNICODE
535bool wxInitialize(int argc, char **argv)
536{
537 wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit);
538
539 if ( gs_initData.nInitCount++ )
540 {
541 // already initialized
542 return true;
543 }
544
545 return wxEntryStart(argc, argv);
546}
547#endif // wxUSE_UNICODE
548
94826170
VZ
549void wxUninitialize()
550{
551 wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit);
552
8d7eaf91 553 if ( --gs_initData.nInitCount == 0 )
94826170
VZ
554 {
555 wxEntryCleanup();
556 }
557}