| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/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/filefn.h" |
| 29 | #include "wx/log.h" |
| 30 | #include "wx/intl.h" |
| 31 | #include "wx/module.h" |
| 32 | #endif |
| 33 | |
| 34 | #include "wx/init.h" |
| 35 | #include "wx/thread.h" |
| 36 | |
| 37 | #include "wx/scopedptr.h" |
| 38 | #include "wx/except.h" |
| 39 | |
| 40 | #if defined(__WXMSW__) && defined(__WXDEBUG__) |
| 41 | #include "wx/msw/msvcrt.h" |
| 42 | |
| 43 | static struct EnableMemLeakChecking |
| 44 | { |
| 45 | EnableMemLeakChecking() |
| 46 | { |
| 47 | // do check for memory leaks on program exit (another useful flag |
| 48 | // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated |
| 49 | // memory which may be used to simulate low-memory condition) |
| 50 | wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); |
| 51 | } |
| 52 | } gs_enableLeakChecks; |
| 53 | #endif // __WXMSW__ && __WXDEBUG__ |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | // private classes |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | // we need a dummy app object if the user doesn't want to create a real one |
| 60 | class wxDummyConsoleApp : public wxAppConsole |
| 61 | { |
| 62 | public: |
| 63 | wxDummyConsoleApp() { } |
| 64 | |
| 65 | virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; } |
| 66 | virtual bool DoYield(bool, long) { return true; } |
| 67 | |
| 68 | wxDECLARE_NO_COPY_CLASS(wxDummyConsoleApp); |
| 69 | }; |
| 70 | |
| 71 | // we need a special kind of auto pointer to wxApp which not only deletes the |
| 72 | // pointer it holds in its dtor but also resets the global application pointer |
| 73 | wxDECLARE_SCOPED_PTR(wxAppConsole, wxAppPtrBase) |
| 74 | wxDEFINE_SCOPED_PTR(wxAppConsole, wxAppPtrBase) |
| 75 | |
| 76 | class wxAppPtr : public wxAppPtrBase |
| 77 | { |
| 78 | public: |
| 79 | wxEXPLICIT wxAppPtr(wxAppConsole *ptr = NULL) : wxAppPtrBase(ptr) { } |
| 80 | ~wxAppPtr() |
| 81 | { |
| 82 | if ( get() ) |
| 83 | { |
| 84 | // the pointer is going to be deleted in the base class dtor, don't |
| 85 | // leave the dangling pointer! |
| 86 | wxApp::SetInstance(NULL); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void Set(wxAppConsole *ptr) |
| 91 | { |
| 92 | reset(ptr); |
| 93 | |
| 94 | wxApp::SetInstance(ptr); |
| 95 | } |
| 96 | |
| 97 | wxDECLARE_NO_COPY_CLASS(wxAppPtr); |
| 98 | }; |
| 99 | |
| 100 | // class to ensure that wxAppBase::CleanUp() is called if our Initialize() |
| 101 | // fails |
| 102 | class wxCallAppCleanup |
| 103 | { |
| 104 | public: |
| 105 | wxCallAppCleanup(wxAppConsole *app) : m_app(app) { } |
| 106 | ~wxCallAppCleanup() { if ( m_app ) m_app->CleanUp(); } |
| 107 | |
| 108 | void Dismiss() { m_app = NULL; } |
| 109 | |
| 110 | private: |
| 111 | wxAppConsole *m_app; |
| 112 | }; |
| 113 | |
| 114 | // another tiny class which simply exists to ensure that wxEntryCleanup is |
| 115 | // always called |
| 116 | class wxCleanupOnExit |
| 117 | { |
| 118 | public: |
| 119 | ~wxCleanupOnExit() { wxEntryCleanup(); } |
| 120 | }; |
| 121 | |
| 122 | // ---------------------------------------------------------------------------- |
| 123 | // private functions |
| 124 | // ---------------------------------------------------------------------------- |
| 125 | |
| 126 | // suppress warnings about unused variables |
| 127 | static inline void Use(void *) { } |
| 128 | |
| 129 | #define WX_SUPPRESS_UNUSED_WARN(x) Use(&x) |
| 130 | |
| 131 | // ---------------------------------------------------------------------------- |
| 132 | // initialization data |
| 133 | // ---------------------------------------------------------------------------- |
| 134 | |
| 135 | static struct InitData |
| 136 | { |
| 137 | InitData() |
| 138 | { |
| 139 | nInitCount = 0; |
| 140 | |
| 141 | #if wxUSE_UNICODE |
| 142 | argc = 0; |
| 143 | // argv = NULL; -- not even really needed |
| 144 | #endif // wxUSE_UNICODE |
| 145 | } |
| 146 | |
| 147 | // critical section protecting this struct |
| 148 | wxCRIT_SECT_DECLARE_MEMBER(csInit); |
| 149 | |
| 150 | // number of times wxInitialize() was called minus the number of times |
| 151 | // wxUninitialize() was |
| 152 | size_t nInitCount; |
| 153 | |
| 154 | #if wxUSE_UNICODE |
| 155 | int argc; |
| 156 | |
| 157 | // if we receive the command line arguments as ASCII and have to convert |
| 158 | // them to Unicode ourselves (this is the case under Unix but not Windows, |
| 159 | // for example), we remember the converted argv here because we'll have to |
| 160 | // free it when doing cleanup to avoid memory leaks |
| 161 | wchar_t **argv; |
| 162 | #endif // wxUSE_UNICODE |
| 163 | |
| 164 | wxDECLARE_NO_COPY_CLASS(InitData); |
| 165 | } gs_initData; |
| 166 | |
| 167 | // ============================================================================ |
| 168 | // implementation |
| 169 | // ============================================================================ |
| 170 | |
| 171 | // ---------------------------------------------------------------------------- |
| 172 | // command line arguments ANSI -> Unicode conversion |
| 173 | // ---------------------------------------------------------------------------- |
| 174 | |
| 175 | #if wxUSE_UNICODE |
| 176 | |
| 177 | static void ConvertArgsToUnicode(int argc, char **argv) |
| 178 | { |
| 179 | gs_initData.argv = new wchar_t *[argc + 1]; |
| 180 | int wargc = 0; |
| 181 | for ( int i = 0; i < argc; i++ ) |
| 182 | { |
| 183 | #ifdef __DARWIN__ |
| 184 | wxWCharBuffer buf(wxConvFileName->cMB2WX(argv[i])); |
| 185 | #else |
| 186 | wxWCharBuffer buf(wxConvLocal.cMB2WX(argv[i])); |
| 187 | #endif |
| 188 | if ( !buf ) |
| 189 | { |
| 190 | wxLogWarning(_("Command line argument %d couldn't be converted to Unicode and will be ignored."), |
| 191 | i); |
| 192 | } |
| 193 | else // converted ok |
| 194 | { |
| 195 | gs_initData.argv[wargc++] = wxStrdup(buf); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | gs_initData.argc = wargc; |
| 200 | gs_initData.argv[wargc] = NULL; |
| 201 | } |
| 202 | |
| 203 | static void FreeConvertedArgs() |
| 204 | { |
| 205 | if ( gs_initData.argv ) |
| 206 | { |
| 207 | for ( int i = 0; i < gs_initData.argc; i++ ) |
| 208 | { |
| 209 | free(gs_initData.argv[i]); |
| 210 | } |
| 211 | |
| 212 | delete [] gs_initData.argv; |
| 213 | gs_initData.argv = NULL; |
| 214 | gs_initData.argc = 0; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #endif // wxUSE_UNICODE |
| 219 | |
| 220 | // ---------------------------------------------------------------------------- |
| 221 | // start up |
| 222 | // ---------------------------------------------------------------------------- |
| 223 | |
| 224 | // initialization which is always done (not customizable) before wxApp creation |
| 225 | static bool DoCommonPreInit() |
| 226 | { |
| 227 | #if wxUSE_LOG |
| 228 | // Reset logging in case we were cleaned up and are being reinitialized. |
| 229 | wxLog::DoCreateOnDemand(); |
| 230 | |
| 231 | // install temporary log sink: we can't use wxLogGui before wxApp is |
| 232 | // constructed and if we use wxLogStderr, all messages during |
| 233 | // initialization simply disappear under Windows |
| 234 | // |
| 235 | // note that we will delete this log target below |
| 236 | delete wxLog::SetActiveTarget(new wxLogBuffer); |
| 237 | #endif // wxUSE_LOG |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | // non customizable initialization done after wxApp creation and initialization |
| 243 | static bool DoCommonPostInit() |
| 244 | { |
| 245 | wxModule::RegisterModules(); |
| 246 | |
| 247 | if ( !wxModule::InitializeModules() ) |
| 248 | { |
| 249 | wxLogError(_("Initialization failed in post init, aborting.")); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | #if defined(__WXDEBUG__) |
| 254 | // check if event classes implement Clone() correctly |
| 255 | // NOTE: the check is done against _all_ event classes which are linked to |
| 256 | // the executable currently running, which are not necessarily all |
| 257 | // wxWidgets event classes. |
| 258 | const wxClassInfo *ci = wxClassInfo::GetFirst(); |
| 259 | for (; ci; ci = ci->GetNext()) |
| 260 | { |
| 261 | // is this class derived from wxEvent? |
| 262 | if (!ci->IsKindOf(CLASSINFO(wxEvent)) || wxString(ci->GetClassName()) == "wxEvent") |
| 263 | continue; |
| 264 | |
| 265 | if (!ci->IsDynamic()) |
| 266 | { |
| 267 | wxLogWarning("The event class '%s' should have a DECLARE_DYNAMIC_CLASS macro!", |
| 268 | ci->GetClassName()); |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | // yes; test if it implements Clone() correctly |
| 273 | wxEvent* test = wxDynamicCast(ci->CreateObject(),wxEvent); |
| 274 | if (test == NULL) |
| 275 | { |
| 276 | wxLogWarning("The event class '%s' should have a DECLARE_DYNAMIC_CLASS macro!", |
| 277 | ci->GetClassName()); |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | wxEvent* cloned = test->Clone(); |
| 282 | if (!cloned || cloned->GetClassInfo() != ci) |
| 283 | wxLogWarning("The event class '%s' does not correctly implement Clone()!", |
| 284 | ci->GetClassName()); |
| 285 | |
| 286 | delete cloned; |
| 287 | delete test; |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | bool wxEntryStart(int& argc, wxChar **argv) |
| 295 | { |
| 296 | // do minimal, always necessary, initialization |
| 297 | // -------------------------------------------- |
| 298 | |
| 299 | // initialize wxRTTI |
| 300 | if ( !DoCommonPreInit() ) |
| 301 | return false; |
| 302 | |
| 303 | |
| 304 | // first of all, we need an application object |
| 305 | // ------------------------------------------- |
| 306 | |
| 307 | // the user might have already created it himself somehow |
| 308 | wxAppPtr app(wxTheApp); |
| 309 | if ( !app.get() ) |
| 310 | { |
| 311 | // if not, he might have used IMPLEMENT_APP() to give us a function to |
| 312 | // create it |
| 313 | wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction(); |
| 314 | |
| 315 | if ( fnCreate ) |
| 316 | { |
| 317 | // he did, try to create the custom wxApp object |
| 318 | app.Set((*fnCreate)()); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if ( !app.get() ) |
| 323 | { |
| 324 | // either IMPLEMENT_APP() was not used at all or it failed -- in any |
| 325 | // case we still need something |
| 326 | app.Set(new wxDummyConsoleApp); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | // wxApp initialization: this can be customized |
| 331 | // -------------------------------------------- |
| 332 | |
| 333 | if ( !app->Initialize(argc, argv) ) |
| 334 | return false; |
| 335 | |
| 336 | // remember, possibly modified (e.g. due to removal of toolkit-specific |
| 337 | // parameters), command line arguments in member variables |
| 338 | app->argc = argc; |
| 339 | app->argv = argv; |
| 340 | |
| 341 | wxCallAppCleanup callAppCleanup(app.get()); |
| 342 | |
| 343 | |
| 344 | // common initialization after wxTheApp creation |
| 345 | // --------------------------------------------- |
| 346 | |
| 347 | if ( !DoCommonPostInit() ) |
| 348 | return false; |
| 349 | |
| 350 | |
| 351 | // prevent the smart pointer from destroying its contents |
| 352 | app.release(); |
| 353 | |
| 354 | // and the cleanup object from doing cleanup |
| 355 | callAppCleanup.Dismiss(); |
| 356 | |
| 357 | #if wxUSE_LOG |
| 358 | // now that we have a valid wxApp (wxLogGui would have crashed if we used |
| 359 | // it before now), we can delete the temporary sink we had created for the |
| 360 | // initialization messages -- the next time logging function is called, the |
| 361 | // sink will be recreated but this time wxAppTraits will be used |
| 362 | delete wxLog::SetActiveTarget(NULL); |
| 363 | #endif // wxUSE_LOG |
| 364 | |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | #if wxUSE_UNICODE |
| 369 | |
| 370 | // we provide a wxEntryStart() wrapper taking "char *" pointer too |
| 371 | bool wxEntryStart(int& argc, char **argv) |
| 372 | { |
| 373 | ConvertArgsToUnicode(argc, argv); |
| 374 | |
| 375 | if ( !wxEntryStart(gs_initData.argc, gs_initData.argv) ) |
| 376 | { |
| 377 | FreeConvertedArgs(); |
| 378 | |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | #endif // wxUSE_UNICODE |
| 386 | |
| 387 | // ---------------------------------------------------------------------------- |
| 388 | // clean up |
| 389 | // ---------------------------------------------------------------------------- |
| 390 | |
| 391 | // cleanup done before destroying wxTheApp |
| 392 | static void DoCommonPreCleanup() |
| 393 | { |
| 394 | #if wxUSE_LOG |
| 395 | // flush the logged messages if any and install a 'safer' log target: the |
| 396 | // default one (wxLogGui) can't be used after the resources are freed just |
| 397 | // below and the user supplied one might be even more unsafe (using any |
| 398 | // wxWidgets GUI function is unsafe starting from now) |
| 399 | wxLog::DontCreateOnDemand(); |
| 400 | |
| 401 | // this will flush the old messages if any |
| 402 | delete wxLog::SetActiveTarget(new wxLogStderr); |
| 403 | #endif // wxUSE_LOG |
| 404 | } |
| 405 | |
| 406 | // cleanup done after destroying wxTheApp |
| 407 | static void DoCommonPostCleanup() |
| 408 | { |
| 409 | wxModule::CleanUpModules(); |
| 410 | |
| 411 | // we can't do this in wxApp itself because it doesn't know if argv had |
| 412 | // been allocated |
| 413 | #if wxUSE_UNICODE |
| 414 | FreeConvertedArgs(); |
| 415 | #endif // wxUSE_UNICODE |
| 416 | |
| 417 | // use Set(NULL) and not Get() to avoid creating a message output object on |
| 418 | // demand when we just want to delete it |
| 419 | delete wxMessageOutput::Set(NULL); |
| 420 | |
| 421 | #if wxUSE_LOG |
| 422 | // and now delete the last logger as well |
| 423 | delete wxLog::SetActiveTarget(NULL); |
| 424 | #endif // wxUSE_LOG |
| 425 | } |
| 426 | |
| 427 | void wxEntryCleanup() |
| 428 | { |
| 429 | DoCommonPreCleanup(); |
| 430 | |
| 431 | |
| 432 | // delete the application object |
| 433 | if ( wxTheApp ) |
| 434 | { |
| 435 | wxTheApp->CleanUp(); |
| 436 | |
| 437 | // reset the global pointer to it to NULL before destroying it as in |
| 438 | // some circumstances this can result in executing the code using |
| 439 | // wxTheApp and using half-destroyed object is no good |
| 440 | wxAppConsole * const app = wxApp::GetInstance(); |
| 441 | wxApp::SetInstance(NULL); |
| 442 | delete app; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | DoCommonPostCleanup(); |
| 447 | } |
| 448 | |
| 449 | // ---------------------------------------------------------------------------- |
| 450 | // wxEntry |
| 451 | // ---------------------------------------------------------------------------- |
| 452 | |
| 453 | // for MSW the real wxEntry is defined in msw/main.cpp |
| 454 | #ifndef __WXMSW__ |
| 455 | #define wxEntryReal wxEntry |
| 456 | #endif // !__WXMSW__ |
| 457 | |
| 458 | int wxEntryReal(int& argc, wxChar **argv) |
| 459 | { |
| 460 | // library initialization |
| 461 | if ( !wxEntryStart(argc, argv) ) |
| 462 | { |
| 463 | #if wxUSE_LOG |
| 464 | // flush any log messages explaining why we failed |
| 465 | delete wxLog::SetActiveTarget(NULL); |
| 466 | #endif |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code |
| 471 | // below returns or throws |
| 472 | wxCleanupOnExit cleanupOnExit; |
| 473 | |
| 474 | WX_SUPPRESS_UNUSED_WARN(cleanupOnExit); |
| 475 | |
| 476 | wxTRY |
| 477 | { |
| 478 | // app initialization |
| 479 | if ( !wxTheApp->CallOnInit() ) |
| 480 | { |
| 481 | // don't call OnExit() if OnInit() failed |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | // ensure that OnExit() is called if OnInit() had succeeded |
| 486 | class CallOnExit |
| 487 | { |
| 488 | public: |
| 489 | ~CallOnExit() { wxTheApp->OnExit(); } |
| 490 | } callOnExit; |
| 491 | |
| 492 | WX_SUPPRESS_UNUSED_WARN(callOnExit); |
| 493 | |
| 494 | // app execution |
| 495 | return wxTheApp->OnRun(); |
| 496 | } |
| 497 | wxCATCH_ALL( wxTheApp->OnUnhandledException(); return -1; ) |
| 498 | } |
| 499 | |
| 500 | #if wxUSE_UNICODE |
| 501 | |
| 502 | // as with wxEntryStart, we provide an ANSI wrapper |
| 503 | int wxEntry(int& argc, char **argv) |
| 504 | { |
| 505 | ConvertArgsToUnicode(argc, argv); |
| 506 | |
| 507 | return wxEntry(gs_initData.argc, gs_initData.argv); |
| 508 | } |
| 509 | |
| 510 | #endif // wxUSE_UNICODE |
| 511 | |
| 512 | // ---------------------------------------------------------------------------- |
| 513 | // wxInitialize/wxUninitialize |
| 514 | // ---------------------------------------------------------------------------- |
| 515 | |
| 516 | bool wxInitialize(int argc, wxChar **argv) |
| 517 | { |
| 518 | wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit); |
| 519 | |
| 520 | if ( gs_initData.nInitCount++ ) |
| 521 | { |
| 522 | // already initialized |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | return wxEntryStart(argc, argv); |
| 527 | } |
| 528 | |
| 529 | void wxUninitialize() |
| 530 | { |
| 531 | wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit); |
| 532 | |
| 533 | if ( --gs_initData.nInitCount == 0 ) |
| 534 | { |
| 535 | wxEntryCleanup(); |
| 536 | } |
| 537 | } |