From 05e2b077c6187ff4d894e4af58ccd99536c9c584 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 30 Jun 2003 20:02:05 +0000 Subject: [PATCH] more work on fixing wxEntry() and ANSI/Unicode cmd line args mess git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21520 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/app.h | 4 +- include/wx/cocoa/app.h | 2 +- include/wx/gtk/app.h | 2 +- include/wx/gtk1/app.h | 2 +- include/wx/init.h | 7 +- include/wx/mac/app.h | 2 +- include/wx/mgl/app.h | 2 +- include/wx/motif/app.h | 2 +- include/wx/msw/app.h | 2 +- include/wx/os2/app.h | 2 +- include/wx/x11/app.h | 2 +- src/cocoa/app.mm | 111 +++------------------ src/common/appbase.cpp | 2 +- src/common/appcmn.cpp | 10 +- src/common/init.cpp | 31 +++++- src/gtk/app.cpp | 204 +++++++++++--------------------------- src/gtk1/app.cpp | 204 +++++++++++--------------------------- src/mac/app.cpp | 138 +++----------------------- src/mac/carbon/app.cpp | 138 +++----------------------- src/mgl/app.cpp | 134 ++----------------------- src/motif/app.cpp | 104 +------------------- src/msw/app.cpp | 2 +- src/os2/app.cpp | 100 +------------------ src/x11/app.cpp | 215 ++++++++++++----------------------------- 24 files changed, 270 insertions(+), 1152 deletions(-) diff --git a/include/wx/app.h b/include/wx/app.h index 5ed82cebef..22afb2459b 100644 --- a/include/wx/app.h +++ b/include/wx/app.h @@ -95,7 +95,7 @@ public: // reason, you must override it (instead of just overriding OnInit(), as // usual, for app-specific initializations), do not forget to call the base // class version! - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); // Called before OnRun(), this is a good place to do initialization -- if // anything fails, return false from here to prevent the program from @@ -314,7 +314,7 @@ public: // very first initialization function // // Override: very rarely - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); // a platform-dependent version of OnInit(): the code here is likely to // depend on the toolkit. default version does nothing. diff --git a/include/wx/cocoa/app.h b/include/wx/cocoa/app.h index ffb6b3d82c..cb9c7d933e 100644 --- a/include/wx/cocoa/app.h +++ b/include/wx/cocoa/app.h @@ -66,7 +66,7 @@ public: // Returns TRUE if more idle time is requested. bool SendIdleEvents(wxWindowCocoa* win); - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); virtual bool OnInit(); diff --git a/include/wx/gtk/app.h b/include/wx/gtk/app.h index fea48cf032..1cda1b39ea 100644 --- a/include/wx/gtk/app.h +++ b/include/wx/gtk/app.h @@ -58,7 +58,7 @@ public: bool SendIdleEvents(); bool SendIdleEvents( wxWindow* win ); - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); static bool InitialzeVisual(); diff --git a/include/wx/gtk1/app.h b/include/wx/gtk1/app.h index fea48cf032..1cda1b39ea 100644 --- a/include/wx/gtk1/app.h +++ b/include/wx/gtk1/app.h @@ -58,7 +58,7 @@ public: bool SendIdleEvents(); bool SendIdleEvents( wxWindow* win ); - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); static bool InitialzeVisual(); diff --git a/include/wx/init.h b/include/wx/init.h index f973e9b0f3..5b6672c9a7 100644 --- a/include/wx/init.h +++ b/include/wx/init.h @@ -24,8 +24,9 @@ // this function also creates wxTheApp as a side effect, if IMPLEMENT_APP // hadn't been used a dummy default application object is created // -// note that the parameters may be modified -extern bool WXDLLEXPORT wxEntryStart(int argc, wxChar **argv); +// note that the parameters may be modified, this is why we pass them by +// reference! +extern bool WXDLLEXPORT wxEntryStart(int& argc, wxChar **argv); // free the resources allocated by the library in wxEntryStart() and shut it // down (wxEntryStart() may be called again afterwards if necessary) @@ -41,7 +42,7 @@ extern void WXDLLEXPORT wxEntryCleanup(); // but this one always exists under all platforms // // returns the program exit code -extern int WXDLLEXPORT wxEntry(int argc, wxChar **argv); +extern int WXDLLEXPORT wxEntry(int& argc, wxChar **argv); // ---------------------------------------------------------------------------- diff --git a/include/wx/mac/app.h b/include/wx/mac/app.h index 592e797f3f..99556638d1 100644 --- a/include/wx/mac/app.h +++ b/include/wx/mac/app.h @@ -91,7 +91,7 @@ protected: public: // Implementation - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); bool IsExiting() { return !m_keepGoing ; } diff --git a/include/wx/mgl/app.h b/include/wx/mgl/app.h index ea3a377ab6..1142fd2da6 100644 --- a/include/wx/mgl/app.h +++ b/include/wx/mgl/app.h @@ -54,7 +54,7 @@ public: bool SendIdleEvents(); bool SendIdleEvents(wxWindow* win); - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); virtual bool Yield(bool onlyIfNeeded = FALSE); diff --git a/include/wx/motif/app.h b/include/wx/motif/app.h index 8f8b632ba8..fef30fbcdc 100644 --- a/include/wx/motif/app.h +++ b/include/wx/motif/app.h @@ -82,7 +82,7 @@ protected: public: // Implementation - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); // Motif-specific diff --git a/include/wx/msw/app.h b/include/wx/msw/app.h index 756430f73c..768833038d 100644 --- a/include/wx/msw/app.h +++ b/include/wx/msw/app.h @@ -36,7 +36,7 @@ public: virtual ~wxApp(); // override base class (pure) virtuals - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); virtual int MainLoop(); diff --git a/include/wx/os2/app.h b/include/wx/os2/app.h index 915e1a1267..9a26ede018 100644 --- a/include/wx/os2/app.h +++ b/include/wx/os2/app.h @@ -126,7 +126,7 @@ private: public: // Implementation - virtual bool Initialize(int argc, wxChar **argv); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(void); static bool RegisterWindowClasses(HAB vHab); diff --git a/include/wx/x11/app.h b/include/wx/x11/app.h index d8d709a77a..edbe541820 100644 --- a/include/wx/x11/app.h +++ b/include/wx/x11/app.h @@ -89,7 +89,7 @@ protected: public: // Implementation - virtual bool Initialize(); + virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); void DeletePendingObjects(); diff --git a/src/cocoa/app.mm b/src/cocoa/app.mm index 8532a12f94..58addbd3d7 100644 --- a/src/cocoa/app.mm +++ b/src/cocoa/app.mm @@ -111,99 +111,6 @@ WX_IMPLEMENT_POSER(wxPoserNSApplication); // functions // ============================================================================ -//---------------------------------------------------------------------- -// wxEntry -//---------------------------------------------------------------------- - -int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] ) -{ - return wxApp::Initialize(); -} - -int WXDLLEXPORT wxEntryInitGui() -{ - return wxTheApp->OnInitGui(); -} - -void WXDLLEXPORT wxEntryCleanup() -{ - wxApp::CleanUp(); -} - -int wxEntry( int argc, char *argv[]) -{ - if (!wxEntryStart(argc, argv)) { - return 0; - } - wxLogDebug("Creating application"); - // create the application object or ensure that one already exists - if (!wxTheApp) - { - // The app may have declared a global application object, but we recommend - // the IMPLEMENT_APP macro is used instead, which sets an initializer - // function for delayed, dynamic app object construction. - wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, - wxT("No initializer - use IMPLEMENT_APP macro.") ); - - wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) (); - } - - wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); - - // Mac OS X passes a process serial number command line argument when - // the application is launched from the Finder. This argument must be - // removed from the command line arguments before being handled by the - // application (otherwise applications would need to handle it) - - if (argc > 1) { - char theArg[6] = ""; - strncpy(theArg, argv[1], 5); - - if (strcmp(theArg, "-psn_") == 0) { - // assume the argument is always the only one and remove it - --argc; - } - } - - wxTheApp->argc = argc; - wxTheApp->argv = argv; - - wxLogDebug("initializing gui"); - // GUI-specific initialization, such as creating an app context. - wxEntryInitGui(); - - // Here frames insert themselves automatically - // into wxTopLevelWindows by getting created - // in OnInit(). - - int retValue = 0; - - wxLogDebug("Time to run"); - retValue = wxTheApp->OnRun(); - - wxWindow *topWindow = wxTheApp->GetTopWindow(); - if ( topWindow ) - { - // Forcibly delete the window. - if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) || - topWindow->IsKindOf(CLASSINFO(wxDialog)) ) - { - topWindow->Close(TRUE); - } - else - { - delete topWindow; - wxTheApp->SetTopWindow(NULL); - } - } - - wxTheApp->OnExit(); - - wxEntryCleanup(); - - return retValue; -} - void wxApp::Exit() { wxApp::CleanUp(); @@ -229,11 +136,25 @@ END_EVENT_TABLE() #endif // ---------------------------------------------------------------------------- -// wxApp static functions +// wxApp initialization/cleanup // ---------------------------------------------------------------------------- -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { + // Mac OS X passes a process serial number command line argument when + // the application is launched from the Finder. This argument must be + // removed from the command line arguments before being handled by the + // application (otherwise applications would need to handle it) + if ( argc > 1 ) + { + static const wxChar *ARG_PSN = _T("-psn_"); + if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 ) + { + // remove this argument + memmove(argv, argv + 1, argc--); + } + } + // VZ: apparently this needs to be done a.s.a.p., right? it is done after // wxClassInfo::InitializeClasses() now but usd to be done before, I // hope it's not a problem -- if it is, please let me know, David (if diff --git a/src/common/appbase.cpp b/src/common/appbase.cpp index 99708fd85e..f3a5e7460b 100644 --- a/src/common/appbase.cpp +++ b/src/common/appbase.cpp @@ -121,7 +121,7 @@ wxAppConsole::~wxAppConsole() // initilization/cleanup // ---------------------------------------------------------------------------- -bool wxAppConsole::Initialize(int argc, wxChar **argv) +bool wxAppConsole::Initialize(int& argc, wxChar **argv) { // remember the command line arguments this->argc = argc; diff --git a/src/common/appcmn.cpp b/src/common/appcmn.cpp index 21466c15ec..78ede412b9 100644 --- a/src/common/appcmn.cpp +++ b/src/common/appcmn.cpp @@ -73,19 +73,11 @@ wxAppBase::wxAppBase() m_exitOnFrameDelete = Later; } -bool wxAppBase::Initialize(int argc, wxChar **argv) +bool wxAppBase::Initialize(int& argc, wxChar **argv) { if ( !wxAppConsole::Initialize(argc, argv) ) return false; - // for compatibility call the old initialization function too - if ( !OnInitGui() ) - { - wxAppConsole::CleanUp(); - - return false; - } - #if wxUSE_THREADS wxPendingEventsLocker = new wxCriticalSection; #endif diff --git a/src/common/init.cpp b/src/common/init.cpp index e696eacb12..ec81ea6ede 100644 --- a/src/common/init.cpp +++ b/src/common/init.cpp @@ -87,6 +87,20 @@ public: } }; +// class to ensure that wxAppBase::CleanUp() is called if our Initialize() +// fails +class wxCallAppCleanup +{ +public: + wxCallAppCleanup(wxApp *app) : m_app(app) { } + ~wxCallAppCleanup() { if ( m_app ) m_app->CleanUp(); } + + void Dismiss() { m_app = NULL; } + +private: + wxApp *m_app; +}; + // another tiny class which simply exists to ensure that wxEntryCleanup is // always called class wxCleanupOnExit @@ -180,7 +194,7 @@ static bool DoCommonPostInit() return wxModule::InitializeModules(); } -bool wxEntryStart(int argc, wxChar **argv) +bool wxEntryStart(int& argc, wxChar **argv) { // do minimal, always necessary, initialization // -------------------------------------------- @@ -229,26 +243,33 @@ bool wxEntryStart(int argc, wxChar **argv) return false; } + wxCallAppCleanup callAppCleanup(wxTheApp); + + // for compatibility call the old initialization function too + if ( !wxTheApp->OnInitGui() ) + return false; + // common initialization after wxTheApp creation // --------------------------------------------- if ( !DoCommonPostInit() ) - { return false; - } // prevent the smart pointer from destroying its contents app.release(); + // and the cleanup object from doing cleanup + callAppCleanup.Dismiss(); + return true; } #if wxUSE_UNICODE // we provide a wxEntryStart() wrapper taking "char *" pointer too -bool wxEntryStart(int argc, char **argv) +bool wxEntryStart(int& argc, char **argv) { ConvertArgsToUnicode(argc, argv); @@ -339,7 +360,7 @@ void wxEntryCleanup() #define wxEntryReal wxEntry #endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION) -int wxEntryReal(int argc, wxChar **argv) +int wxEntryReal(int& argc, wxChar **argv) { // library initialization if ( !wxEntryStart(argc, argv) ) diff --git a/src/gtk/app.cpp b/src/gtk/app.cpp index a062fd79bf..66f2b9a395 100644 --- a/src/gtk/app.cpp +++ b/src/gtk/app.cpp @@ -676,25 +676,6 @@ bool wxApp::Initialize(int argc, wxChar **argv) if ( !wxAppBase::Initialize(argc, argv) ) return false; -#if wxUSE_INTL - wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); -#endif - - return true; -} - -void wxApp::CleanUp() -{ - wxAppBase::CleanUp(); -} - -//----------------------------------------------------------------------------- -// wxEntry -//----------------------------------------------------------------------------- - -// NB: argc and argv may be changed here, pass by reference! -int wxEntryStart( int& argc, char *argv[] ) -{ #if wxUSE_THREADS // GTK 1.2 up to version 1.2.3 has broken threads if ((gtk_major_version == 1) && @@ -707,162 +688,85 @@ int wxEntryStart( int& argc, char *argv[] ) { g_thread_init(NULL); } -#endif +#endif // wxUSE_THREADS gtk_set_locale(); // We should have the wxUSE_WCHAR_T test on the _outside_ #if wxUSE_WCHAR_T -#if defined(__WXGTK20__) - // gtk+ 2.0 supports Unicode through UTF-8 strings - wxConvCurrent = &wxConvUTF8; -#else - if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; -#endif -#else - if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL; -#endif + #if defined(__WXGTK20__) + // gtk+ 2.0 supports Unicode through UTF-8 strings + wxConvCurrent = &wxConvUTF8; + #else // GTK 1.x + if (!wxOKlibc()) + wxConvCurrent = &wxConvLocal; + #endif +#else // !wxUSE_WCHAR_T + if (!wxOKlibc()) + wxConvCurrent = (wxMBConv*) NULL; +#endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T - gtk_init( &argc, &argv ); - - /* we can not enter threads before gtk_init is done */ - gdk_threads_enter(); - - wxSetDetectableAutoRepeat( TRUE ); - - if (!wxApp::Initialize()) +#if wxUSE_UNICODE + // gtk_init() wants UTF-8, not wchar_t, so convert + int i; + char *argvGTK = new char *[argc + 1]; + for ( i = 0; i < argc; i++ ) { - gdk_threads_leave(); - return -1; + argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i])); } - return 0; -} - - -int wxEntryInitGui() -{ - int retValue = 0; - - if ( !wxTheApp->OnInitGui() ) - retValue = -1; - - wxGetRootWindow(); - - return retValue; -} - - -void wxEntryCleanup() -{ -#if wxUSE_LOG - // flush the logged messages if any - wxLog *log = wxLog::GetActiveTarget(); - if (log != NULL && log->HasPendingMessages()) - log->Flush(); - - // continuing to use user defined log target is unsafe from now on because - // some resources may be already unavailable, so replace it by something - // more safe - wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr); - if ( oldlog ) - delete oldlog; -#endif // wxUSE_LOG + argvGTK[argc] = NULL; - wxApp::CleanUp(); + int argcGTK = argc; + gtk_init( &argcGTK, &argvGTK ); - gdk_threads_leave(); -} - - -int wxEntry( int argc, char *argv[] ) -{ -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - // This seems to be necessary since there are 'rogue' - // objects present at this point (perhaps global objects?) - // Setting a checkpoint will ignore them as far as the - // memory checking facility is concerned. - // Of course you may argue that memory allocated in globals should be - // checked, but this is a reasonable compromise. - wxDebugContext::SetCheckpoint(); -#endif - int err = wxEntryStart(argc, argv); - if (err) - return err; - - if (!wxTheApp) - { - wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, - wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); - - wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); - - wxObject *test_app = app_ini(); - - wxTheApp = (wxApp*) test_app; - } - - wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); - - wxTheApp->argc = argc; -#if wxUSE_UNICODE - wxTheApp->argv = new wxChar*[argc+1]; - int mb_argc = 0; - while (mb_argc < argc) + if ( argcGTK != argc ) { - wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); - mb_argc++; - } - wxTheApp->argv[mb_argc] = (wxChar *)NULL; -#else - wxTheApp->argv = argv; -#endif + // we have to drop the parameters which were consumed by GTK+ + for ( i = 0; i < argcGTK; i++ ) + { + while ( wxStrcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 ) + { + memmove(argv + i, argv + i + 1, argc - i); + } + } - if (wxTheApp->argc > 0) - { - wxFileName fname( wxTheApp->argv[0] ); - wxTheApp->SetAppName( fname.GetName() ); + argc = argcGTK; } + //else: gtk_init() didn't modify our parameters - int retValue; - retValue = wxEntryInitGui(); - - // Here frames insert themselves automatically into wxTopLevelWindows by - // getting created in OnInit(). - if ( retValue == 0 ) + // free our copy + for ( i = 0; i < argcGTK; i++ ) { - if ( !wxTheApp->OnInit() ) - retValue = -1; + free(argvGTK[i]); } - if ( retValue == 0 ) - { - // Delete pending toplevel windows - wxTheApp->DeletePendingObjects(); - - // When is the app not initialized ? - wxTheApp->m_initialized = TRUE; + delete [] argvGTK; +#else // !wxUSE_UNICODE + // gtk_init() shouldn't actually change argv itself (just its contents) so + // it's ok to pass pointer to it + gtk_init( &argc, &argv ); +#endif // wxUSE_UNICODE/!wxUSE_UNICODE - if (wxTheApp->Initialized()) - { - wxTheApp->OnRun(); + // we can not enter threads before gtk_init is done + gdk_threads_enter(); - wxWindow *topWindow = wxTheApp->GetTopWindow(); + wxSetDetectableAutoRepeat( TRUE ); - // Delete all pending windows if any - wxTheApp->DeletePendingObjects(); +#if wxUSE_INTL + wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); +#endif - // Reset top window - if (topWindow) - wxTheApp->SetTopWindow( (wxWindow*) NULL ); + wxGetRootWindow(); - retValue = wxTheApp->OnExit(); - } - } + return true; +} - wxEntryCleanup(); +void wxApp::CleanUp() +{ + wxAppBase::CleanUp(); - return retValue; + gdk_threads_leave(); } #ifdef __WXDEBUG__ diff --git a/src/gtk1/app.cpp b/src/gtk1/app.cpp index a062fd79bf..66f2b9a395 100644 --- a/src/gtk1/app.cpp +++ b/src/gtk1/app.cpp @@ -676,25 +676,6 @@ bool wxApp::Initialize(int argc, wxChar **argv) if ( !wxAppBase::Initialize(argc, argv) ) return false; -#if wxUSE_INTL - wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); -#endif - - return true; -} - -void wxApp::CleanUp() -{ - wxAppBase::CleanUp(); -} - -//----------------------------------------------------------------------------- -// wxEntry -//----------------------------------------------------------------------------- - -// NB: argc and argv may be changed here, pass by reference! -int wxEntryStart( int& argc, char *argv[] ) -{ #if wxUSE_THREADS // GTK 1.2 up to version 1.2.3 has broken threads if ((gtk_major_version == 1) && @@ -707,162 +688,85 @@ int wxEntryStart( int& argc, char *argv[] ) { g_thread_init(NULL); } -#endif +#endif // wxUSE_THREADS gtk_set_locale(); // We should have the wxUSE_WCHAR_T test on the _outside_ #if wxUSE_WCHAR_T -#if defined(__WXGTK20__) - // gtk+ 2.0 supports Unicode through UTF-8 strings - wxConvCurrent = &wxConvUTF8; -#else - if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; -#endif -#else - if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL; -#endif + #if defined(__WXGTK20__) + // gtk+ 2.0 supports Unicode through UTF-8 strings + wxConvCurrent = &wxConvUTF8; + #else // GTK 1.x + if (!wxOKlibc()) + wxConvCurrent = &wxConvLocal; + #endif +#else // !wxUSE_WCHAR_T + if (!wxOKlibc()) + wxConvCurrent = (wxMBConv*) NULL; +#endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T - gtk_init( &argc, &argv ); - - /* we can not enter threads before gtk_init is done */ - gdk_threads_enter(); - - wxSetDetectableAutoRepeat( TRUE ); - - if (!wxApp::Initialize()) +#if wxUSE_UNICODE + // gtk_init() wants UTF-8, not wchar_t, so convert + int i; + char *argvGTK = new char *[argc + 1]; + for ( i = 0; i < argc; i++ ) { - gdk_threads_leave(); - return -1; + argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i])); } - return 0; -} - - -int wxEntryInitGui() -{ - int retValue = 0; - - if ( !wxTheApp->OnInitGui() ) - retValue = -1; - - wxGetRootWindow(); - - return retValue; -} - - -void wxEntryCleanup() -{ -#if wxUSE_LOG - // flush the logged messages if any - wxLog *log = wxLog::GetActiveTarget(); - if (log != NULL && log->HasPendingMessages()) - log->Flush(); - - // continuing to use user defined log target is unsafe from now on because - // some resources may be already unavailable, so replace it by something - // more safe - wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr); - if ( oldlog ) - delete oldlog; -#endif // wxUSE_LOG + argvGTK[argc] = NULL; - wxApp::CleanUp(); + int argcGTK = argc; + gtk_init( &argcGTK, &argvGTK ); - gdk_threads_leave(); -} - - -int wxEntry( int argc, char *argv[] ) -{ -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - // This seems to be necessary since there are 'rogue' - // objects present at this point (perhaps global objects?) - // Setting a checkpoint will ignore them as far as the - // memory checking facility is concerned. - // Of course you may argue that memory allocated in globals should be - // checked, but this is a reasonable compromise. - wxDebugContext::SetCheckpoint(); -#endif - int err = wxEntryStart(argc, argv); - if (err) - return err; - - if (!wxTheApp) - { - wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, - wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); - - wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); - - wxObject *test_app = app_ini(); - - wxTheApp = (wxApp*) test_app; - } - - wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); - - wxTheApp->argc = argc; -#if wxUSE_UNICODE - wxTheApp->argv = new wxChar*[argc+1]; - int mb_argc = 0; - while (mb_argc < argc) + if ( argcGTK != argc ) { - wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); - mb_argc++; - } - wxTheApp->argv[mb_argc] = (wxChar *)NULL; -#else - wxTheApp->argv = argv; -#endif + // we have to drop the parameters which were consumed by GTK+ + for ( i = 0; i < argcGTK; i++ ) + { + while ( wxStrcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 ) + { + memmove(argv + i, argv + i + 1, argc - i); + } + } - if (wxTheApp->argc > 0) - { - wxFileName fname( wxTheApp->argv[0] ); - wxTheApp->SetAppName( fname.GetName() ); + argc = argcGTK; } + //else: gtk_init() didn't modify our parameters - int retValue; - retValue = wxEntryInitGui(); - - // Here frames insert themselves automatically into wxTopLevelWindows by - // getting created in OnInit(). - if ( retValue == 0 ) + // free our copy + for ( i = 0; i < argcGTK; i++ ) { - if ( !wxTheApp->OnInit() ) - retValue = -1; + free(argvGTK[i]); } - if ( retValue == 0 ) - { - // Delete pending toplevel windows - wxTheApp->DeletePendingObjects(); - - // When is the app not initialized ? - wxTheApp->m_initialized = TRUE; + delete [] argvGTK; +#else // !wxUSE_UNICODE + // gtk_init() shouldn't actually change argv itself (just its contents) so + // it's ok to pass pointer to it + gtk_init( &argc, &argv ); +#endif // wxUSE_UNICODE/!wxUSE_UNICODE - if (wxTheApp->Initialized()) - { - wxTheApp->OnRun(); + // we can not enter threads before gtk_init is done + gdk_threads_enter(); - wxWindow *topWindow = wxTheApp->GetTopWindow(); + wxSetDetectableAutoRepeat( TRUE ); - // Delete all pending windows if any - wxTheApp->DeletePendingObjects(); +#if wxUSE_INTL + wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); +#endif - // Reset top window - if (topWindow) - wxTheApp->SetTopWindow( (wxWindow*) NULL ); + wxGetRootWindow(); - retValue = wxTheApp->OnExit(); - } - } + return true; +} - wxEntryCleanup(); +void wxApp::CleanUp() +{ + wxAppBase::CleanUp(); - return retValue; + gdk_threads_leave(); } #ifdef __WXDEBUG__ diff --git a/src/mac/app.cpp b/src/mac/app.cpp index 9c3502089d..9fd758b3a0 100644 --- a/src/mac/app.cpp +++ b/src/mac/app.cpp @@ -447,7 +447,7 @@ DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler ) WXIMPORT char std::__throws_bad_alloc ; #endif -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { int error = 0 ; @@ -535,6 +535,20 @@ bool wxApp::Initialize(int argc, wxChar **argv) s_macCursorRgn = ::NewRgn() ; + // Mac OS X passes a process serial number command line argument when + // the application is launched from the Finder. This argument must be + // removed from the command line arguments before being handled by the + // application (otherwise applications would need to handle it) + if ( argc > 1 ) + { + static const wxChar *ARG_PSN = _T("-psn_"); + if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 ) + { + // remove this argument + memmove(argv, argv + 1, argc--); + } + } + if ( !wxAppBase::Initialize(argc, argv) ) return false; @@ -634,7 +648,7 @@ void wxApp::CleanUp() } //---------------------------------------------------------------------- -// wxEntry +// misc initialization stuff //---------------------------------------------------------------------- // extern variable for shared library resource id @@ -813,126 +827,6 @@ pascal void __wxterminate(void) #endif /* WXMAKINGDLL && !__DARWIN__ */ -int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] ) -{ - return wxApp::Initialize(); -} - -int WXDLLEXPORT wxEntryInitGui() -{ - return wxTheApp->OnInitGui(); -} - -void WXDLLEXPORT wxEntryCleanup() -{ - wxApp::CleanUp(); -} - -int wxEntry( int argc, char *argv[] , bool enterLoop ) -{ -#ifdef __MWERKS__ -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - // This seems to be necessary since there are 'rogue' - // objects present at this point (perhaps global objects?) - // Setting a checkpoint will ignore them as far as the - // memory checking facility is concerned. - // Of course you may argue that memory allocated in globals should be - // checked, but this is a reasonable compromise. - wxDebugContext::SetCheckpoint(); -#endif -#endif - if (!wxEntryStart(argc, argv)) { - return 0; - } - // create the application object or ensure that one already exists - if (!wxTheApp) - { - // The app may have declared a global application object, but we recommend - // the IMPLEMENT_APP macro is used instead, which sets an initializer - // function for delayed, dynamic app object construction. - wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, - wxT("No initializer - use IMPLEMENT_APP macro.") ); - - wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) (); - } - - wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); - -#ifdef __DARWIN__ - // Mac OS X passes a process serial number command line argument when - // the application is launched from the Finder. This argument must be - // removed from the command line arguments before being handled by the - // application (otherwise applications would need to handle it) - - if (argc > 1) { - if (strncmp(argv[1], "-psn_", 5) == 0) { - // assume the argument is always the only one and remove it - --argc; - } - } -#else - argc = 0 ; // currently we don't support files as parameters -#endif - // we could try to get the open apple events here to adjust argc and argv better - - wxTheApp->argc = argc; -#if wxUSE_UNICODE - wxTheApp->argv = new wxChar*[argc+1]; - int mb_argc ; - for ( mb_argc = 0; mb_argc < argc; mb_argc++ ) - { - wxTheApp->argv[mb_argc] = wxStrdup(wxConvLocal.cMB2WX(argv[mb_argc])); - } - wxTheApp->argv[mb_argc] = (wxChar *)NULL; -#else - wxTheApp->argv = argv; -#endif - - // GUI-specific initialization, such as creating an app context. - wxEntryInitGui(); - - // Here frames insert themselves automatically - // into wxTopLevelWindows by getting created - // in OnInit(). - - int retValue = 0; - - if ( wxTheApp->OnInit() ) - { - if ( enterLoop ) - { - retValue = wxTheApp->OnRun(); - } - else - // We want to initialize, but not run or exit immediately. - return 1; - } - //else: app initialization failed, so we skipped OnRun() - - wxWindow *topWindow = wxTheApp->GetTopWindow(); - if ( topWindow ) - { - // Forcibly delete the window. - if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) || - topWindow->IsKindOf(CLASSINFO(wxDialog)) ) - { - topWindow->Close(TRUE); - wxTheApp->DeletePendingObjects(); - } - else - { - delete topWindow; - wxTheApp->SetTopWindow(NULL); - } - } - - wxTheApp->OnExit(); - - wxEntryCleanup(); - - return retValue; -} - #if TARGET_CARBON bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec) diff --git a/src/mac/carbon/app.cpp b/src/mac/carbon/app.cpp index 9c3502089d..9fd758b3a0 100644 --- a/src/mac/carbon/app.cpp +++ b/src/mac/carbon/app.cpp @@ -447,7 +447,7 @@ DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler ) WXIMPORT char std::__throws_bad_alloc ; #endif -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { int error = 0 ; @@ -535,6 +535,20 @@ bool wxApp::Initialize(int argc, wxChar **argv) s_macCursorRgn = ::NewRgn() ; + // Mac OS X passes a process serial number command line argument when + // the application is launched from the Finder. This argument must be + // removed from the command line arguments before being handled by the + // application (otherwise applications would need to handle it) + if ( argc > 1 ) + { + static const wxChar *ARG_PSN = _T("-psn_"); + if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 ) + { + // remove this argument + memmove(argv, argv + 1, argc--); + } + } + if ( !wxAppBase::Initialize(argc, argv) ) return false; @@ -634,7 +648,7 @@ void wxApp::CleanUp() } //---------------------------------------------------------------------- -// wxEntry +// misc initialization stuff //---------------------------------------------------------------------- // extern variable for shared library resource id @@ -813,126 +827,6 @@ pascal void __wxterminate(void) #endif /* WXMAKINGDLL && !__DARWIN__ */ -int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] ) -{ - return wxApp::Initialize(); -} - -int WXDLLEXPORT wxEntryInitGui() -{ - return wxTheApp->OnInitGui(); -} - -void WXDLLEXPORT wxEntryCleanup() -{ - wxApp::CleanUp(); -} - -int wxEntry( int argc, char *argv[] , bool enterLoop ) -{ -#ifdef __MWERKS__ -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - // This seems to be necessary since there are 'rogue' - // objects present at this point (perhaps global objects?) - // Setting a checkpoint will ignore them as far as the - // memory checking facility is concerned. - // Of course you may argue that memory allocated in globals should be - // checked, but this is a reasonable compromise. - wxDebugContext::SetCheckpoint(); -#endif -#endif - if (!wxEntryStart(argc, argv)) { - return 0; - } - // create the application object or ensure that one already exists - if (!wxTheApp) - { - // The app may have declared a global application object, but we recommend - // the IMPLEMENT_APP macro is used instead, which sets an initializer - // function for delayed, dynamic app object construction. - wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, - wxT("No initializer - use IMPLEMENT_APP macro.") ); - - wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) (); - } - - wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); - -#ifdef __DARWIN__ - // Mac OS X passes a process serial number command line argument when - // the application is launched from the Finder. This argument must be - // removed from the command line arguments before being handled by the - // application (otherwise applications would need to handle it) - - if (argc > 1) { - if (strncmp(argv[1], "-psn_", 5) == 0) { - // assume the argument is always the only one and remove it - --argc; - } - } -#else - argc = 0 ; // currently we don't support files as parameters -#endif - // we could try to get the open apple events here to adjust argc and argv better - - wxTheApp->argc = argc; -#if wxUSE_UNICODE - wxTheApp->argv = new wxChar*[argc+1]; - int mb_argc ; - for ( mb_argc = 0; mb_argc < argc; mb_argc++ ) - { - wxTheApp->argv[mb_argc] = wxStrdup(wxConvLocal.cMB2WX(argv[mb_argc])); - } - wxTheApp->argv[mb_argc] = (wxChar *)NULL; -#else - wxTheApp->argv = argv; -#endif - - // GUI-specific initialization, such as creating an app context. - wxEntryInitGui(); - - // Here frames insert themselves automatically - // into wxTopLevelWindows by getting created - // in OnInit(). - - int retValue = 0; - - if ( wxTheApp->OnInit() ) - { - if ( enterLoop ) - { - retValue = wxTheApp->OnRun(); - } - else - // We want to initialize, but not run or exit immediately. - return 1; - } - //else: app initialization failed, so we skipped OnRun() - - wxWindow *topWindow = wxTheApp->GetTopWindow(); - if ( topWindow ) - { - // Forcibly delete the window. - if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) || - topWindow->IsKindOf(CLASSINFO(wxDialog)) ) - { - topWindow->Close(TRUE); - wxTheApp->DeletePendingObjects(); - } - else - { - delete topWindow; - wxTheApp->SetTopWindow(NULL); - } - } - - wxTheApp->OnExit(); - - wxEntryCleanup(); - - return retValue; -} - #if TARGET_CARBON bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec) diff --git a/src/mgl/app.cpp b/src/mgl/app.cpp index f21c6a862a..2e85732f90 100644 --- a/src/mgl/app.cpp +++ b/src/mgl/app.cpp @@ -383,8 +383,14 @@ void wxApp::Dispatch() wxEventLoop::GetActive()->Dispatch(); } -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { +#ifdef __DJGPP__ + // VS: disable long filenames under DJGPP as the very first thing, + // since SciTech MGL doesn't like them much... + wxSetEnv(wxT("LFN"), wxT("N")); +#endif + // must do it before calling wxAppBase::Initialize(), because fonts are // needed by stock lists which are created there wxTheFontsManager = new wxFontsManager; @@ -422,129 +428,3 @@ void wxApp::CleanUp() MGL_exit(); } - -int wxEntryStart(int argc, char *argv[]) -{ - return wxApp::Initialize() ? 0 : -1; -} - - -int wxEntryInitGui() -{ - return wxTheApp->OnInitGui() ? 0 : -1; -} - - -void wxEntryCleanup() -{ - wxApp::CleanUp(); -} - - - -int wxEntry(int argc, char *argv[]) -{ -#ifdef __DJGPP__ - // VS: disable long filenames under DJGPP as the very first thing, - // since SciTech MGL doesn't like them much... - wxSetEnv(wxT("LFN"), wxT("N")); -#endif - -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - // This seems to be necessary since there are 'rogue' - // objects present at this point (perhaps global objects?) - // Setting a checkpoint will ignore them as far as the - // memory checking facility is concerned. - // Of course you may argue that memory allocated in globals should be - // checked, but this is a reasonable compromise. - wxDebugContext::SetCheckpoint(); -#endif - int err = wxEntryStart(argc, argv); - if ( err ) - return err; - - if ( !wxTheApp ) - { - wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, - wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); - - wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); - - wxObject *test_app = app_ini(); - - wxTheApp = (wxApp*) test_app; - } - - wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); - - wxTheApp->argc = argc; -#if wxUSE_UNICODE - wxTheApp->argv = new wxChar*[argc+1]; - int mb_argc = 0; - while (mb_argc < argc) - { - wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); - mb_argc++; - } - wxTheApp->argv[mb_argc] = (wxChar *)NULL; -#else - wxTheApp->argv = argv; -#endif - - wxString name(wxFileNameFromPath(argv[0])); - wxStripExtension(name); - wxTheApp->SetAppName(name); - - int retValue; - retValue = wxEntryInitGui(); - - // Here frames insert themselves automatically into wxTopLevelWindows by - // getting created in OnInit(). - if ( retValue == 0 ) - { - if ( !wxTheApp->OnInit() ) - retValue = -1; - } - - if ( retValue == 0 ) - { - /* delete pending toplevel windows (typically a single - dialog) so that, if there isn't any left, we don't - call OnRun() */ - wxTheApp->DeletePendingObjects(); - - if ( wxTheApp->Initialized() ) - { - wxTheApp->OnRun(); - - wxWindow *topWindow = wxTheApp->GetTopWindow(); - if ( topWindow ) - { - /* Forcibly delete the window. */ - if (topWindow->IsKindOf(CLASSINFO(wxFrame)) || - topWindow->IsKindOf(CLASSINFO(wxDialog)) ) - { - topWindow->Close(TRUE); - wxTheApp->DeletePendingObjects(); - } - else - { - delete topWindow; - wxTheApp->SetTopWindow((wxWindow*) NULL); - } - } - -#if wxUSE_LOG - // flush the logged messages if any - wxLog *log = wxLog::GetActiveTarget(); - if (log != NULL && log->HasPendingMessages()) - log->Flush(); -#endif // wxUSE_LOG - retValue = wxTheApp->OnExit(); - } - } - - wxEntryCleanup(); - - return retValue; -} diff --git a/src/motif/app.cpp b/src/motif/app.cpp index a2e569037c..0185173142 100644 --- a/src/motif/app.cpp +++ b/src/motif/app.cpp @@ -86,7 +86,7 @@ END_EVENT_TABLE() } #endif // __WXDEBUG__ -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { if ( !wxAppBase::Initialize(argc, argv) ) return false; @@ -112,109 +112,9 @@ void wxApp::Exit() } // ============================================================================ -// wxEntry* +// wxApp // ============================================================================ -int wxEntryStart( int argc, char* argv[] ) -{ -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - // This seems to be necessary since there are 'rogue' - // objects present at this point (perhaps global objects?) - // Setting a checkpoint will ignore them as far as the - // memory checking facility is concerned. - // Of course you may argue that memory allocated in globals should be - // checked, but this is a reasonable compromise. - wxDebugContext::SetCheckpoint(); -#endif - - if (!wxApp::Initialize()) - return -1; - - return 0; -} - -int wxEntryInitGui() -{ - int retValue = 0; - - // GUI-specific initialization, such as creating an app context. - if (!wxTheApp->OnInitGui()) - retValue = -1; - - return retValue; -} - -void wxEntryCleanup() -{ - // So dialog boxes aren't used for further messages - delete wxLog::SetActiveTarget(new wxLogStderr); - - // flush the logged messages if any - wxLog *pLog = wxLog::GetActiveTarget(); - if ( pLog != NULL && pLog->HasPendingMessages() ) - pLog->Flush(); - - wxApp::CleanUp(); -} - -int wxEntry( int argc, char *argv[] ) -{ - int retValue = 0; - - retValue = wxEntryStart( argc, argv ); - if (retValue) return retValue; - - if (!wxTheApp) - { - if (!wxApp::GetInitializerFunction()) - { - printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); - return 0; - }; - - wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) (); - }; - - if (!wxTheApp) - { - printf( "wxWindows error: wxTheApp == NULL\n" ); - return 0; - }; - - wxTheApp->SetClassName(wxFileNameFromPath(argv[0])); - wxTheApp->SetAppName(wxFileNameFromPath(argv[0])); - - wxTheApp->argc = argc; - wxTheApp->argv = argv; - - // GUI-specific initialization, such as creating an app context. - retValue = wxEntryInitGui(); - if (retValue) return retValue; - - // Here frames insert themselves automatically into wxTopLevelWindows by - // getting created in OnInit(). - - if (wxTheApp->OnInit()) - { - if (wxTheApp->Initialized()) - wxTheApp->OnRun(); - } - - if (wxTheApp->GetTopWindow()) - { - delete wxTheApp->GetTopWindow(); - wxTheApp->SetTopWindow(NULL); - } - - wxTheApp->DeletePendingObjects(); - - retValue = wxTheApp->OnExit(); - - wxEntryCleanup(); - - return retValue; -} - wxApp::wxApp() { argc = 0; diff --git a/src/msw/app.cpp b/src/msw/app.cpp index d0adf0525b..904f0aaed7 100644 --- a/src/msw/app.cpp +++ b/src/msw/app.cpp @@ -258,7 +258,7 @@ private: }; //// Initialize -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { if ( !wxAppBase::Initialize(argc, argv) ) return false; diff --git a/src/os2/app.cpp b/src/os2/app.cpp index bde40f8e18..fc5d965009 100644 --- a/src/os2/app.cpp +++ b/src/os2/app.cpp @@ -226,7 +226,7 @@ void wxApp::HandleSockets() // // Initialize // -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { if ( !wxAppBase::Initialize(argc, argv) ) return false; @@ -470,104 +470,6 @@ void wxApp::CleanUp() wxAppBase::CleanUp(); } // end of wxApp::CleanUp -//---------------------------------------------------------------------- -// Main wxWindows entry point -//---------------------------------------------------------------------- -int wxEntry( - int argc -, char* argv[] -) -{ - HAB vHab = 0; - - if (!wxApp::Initialize(vHab)) - return 0; - - // - // create the application object or ensure that one already exists - // - if (!wxTheApp) - { - // The app may have declared a global application object, but we recommend - // the IMPLEMENT_APP macro is used instead, which sets an initializer - // function for delayed, dynamic app object construction. - wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, - wxT("No initializer - use IMPLEMENT_APP macro.") ); - wxTheApp = (*wxApp::GetInitializerFunction()) (); - } - wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); - wxTheApp->argc = argc; - -#if wxUSE_UNICODE - wxTheApp->argv = new wxChar*[argc+1]; - - int nArgc = 0; - - while (nArgc < argc) - { - wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc])); - nArgc++; - } - wxTheApp->argv[nArgc] = (wxChar *)NULL; -#else - wxTheApp->argv = argv; -#endif - - wxString sName(wxFileNameFromPath(argv[0])); - - wxStripExtension(sName); - wxTheApp->SetAppName(sName); - - int nRetValue = 0; - - if (!wxTheApp->OnInitGui()) - nRetValue = -1; - - if (nRetValue == 0) - { - if (wxTheApp->OnInit()) - { - wxTheApp->OnRun(); - } - // Normal exit - wxWindow* pTopWindow = wxTheApp->GetTopWindow(); - if (pTopWindow) - { - // Forcibly delete the window. - if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) || - pTopWindow->IsKindOf(CLASSINFO(wxDialog)) ) - { - pTopWindow->Close(TRUE); - wxTheApp->DeletePendingObjects(); - } - else - { - delete pTopWindow; - wxTheApp->SetTopWindow(NULL); - } - } - } - else // app initialization failed - { - wxLogLastError(" Gui initialization failed, exitting"); - } -#if wxUSE_CONSOLEDEBUG - printf("wxTheApp->OnExit "); - fflush(stdout); -#endif - nRetValue = wxTheApp->OnExit(); -#if wxUSE_CONSOLEDEBUG - printf("wxApp::CleanUp "); - fflush(stdout); -#endif - wxApp::CleanUp(); -#if wxUSE_CONSOLEDEBUG - printf("return %i ", nRetValue); - fflush(stdout); -#endif - return(nRetValue); -} // end of wxEntry - bool wxApp::OnInitGui() { ERRORID vError; diff --git a/src/x11/app.cpp b/src/x11/app.cpp index b24c11fbab..4a6aca507e 100644 --- a/src/x11/app.cpp +++ b/src/x11/app.cpp @@ -49,8 +49,6 @@ extern wxList wxPendingDelete; wxHashTable *wxWidgetHashTable = NULL; wxHashTable *wxClientWidgetHashTable = NULL; -// This is set within wxEntryStart -- too early on -// to put these in wxTheApp static bool g_showIconic = FALSE; static wxSize g_initialSize = wxDefaultSize; @@ -93,99 +91,99 @@ BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) EVT_IDLE(wxApp::OnIdle) END_EVENT_TABLE() -bool wxApp::Initialize(int argc, wxChar **argv) +bool wxApp::Initialize(int& argc, wxChar **argv) { - if ( !wxAppBase::Initialize(argc, argv) ) - return false; - -#if wxUSE_INTL - wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); -#endif - - wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); - wxClientWidgetHashTable = new wxHashTable(wxKEY_INTEGER); - - return true; -} - -void wxApp::CleanUp() -{ - delete wxWidgetHashTable; - wxWidgetHashTable = NULL; - delete wxClientWidgetHashTable; - wxClientWidgetHashTable = NULL; - - wxAppBase::CleanUp(); -} - -// NB: argc and argv may be changed here, pass by reference! -int wxEntryStart( int& argc, char *argv[] ) -{ -#ifdef __WXDEBUG__ -#if !wxUSE_NANOX +#if defined(__WXDEBUG__) && !wxUSE_NANOX // install the X error handler gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler ); -#endif #endif // __WXDEBUG__ char *displayName = NULL; bool syncDisplay = FALSE; - int i; - for (i = 0; i < argc; i++) + int argcOrig = argc; + for ( int i = 0; i < argcOrig; i++ ) { - if (strcmp( argv[i], "-display") == 0) + if (wxStrcmp( argv[i], _T("-display") ) == 0) { if (i < (argc - 1)) { - i ++; + argv[i++] = NULL; + displayName = argv[i]; - continue; + + argv[i] = NULL; + argc -= 2; } } - else if (strcmp( argv[i], "-geometry") == 0) + else if (wxStrcmp( argv[i], _T("-geometry") ) == 0) { if (i < (argc - 1)) { - i ++; + argv[i++] = NULL; + int w, h; - if (sscanf(argv[i], "%dx%d", &w, &h) != 2) + if (wxSscanf(argv[i], _T("%dx%d"), &w, &h) != 2) { - wxLogError( _("Invalid geometry specification '%s'"), wxString::FromAscii(argv[i]).c_str() ); + wxLogError( _("Invalid geometry specification '%s'"), + wxString::FromAscii(argv[i]).c_str() ); } else { g_initialSize = wxSize(w, h); } - continue; + + argv[i] = NULL; + argc -= 2; } } - else if (strcmp( argv[i], "-sync") == 0) + else if (wxStrcmp( argv[i], _T("-sync") ) == 0) { syncDisplay = TRUE; - continue; + + argv[i] = NULL; + argc--; } - else if (strcmp( argv[i], "-iconic") == 0) + else if (wxStrcmp( argv[i], _T("-iconic") ) == 0) { g_showIconic = TRUE; - continue; + argv[i] = NULL; + argc--; } + } + if ( argc != argcOrig ) + { + // remove the argumens we consumed + for ( int i = 0; i < argc; i++ ) + { + while ( !argv[i] ) + { + memmove(argv + i, argv + i + 1, argcOrig - i); + } + } } // X11 display stuff - Display* xdisplay = XOpenDisplay( displayName ); + Display *xdisplay = XOpenDisplay( displayName ); if (!xdisplay) { wxLogError( _("wxWindows could not open display. Exiting.") ); - return -1; + return false; + } + + if ( !wxAppBase::Initialize(argc, argv) ) + { + XCloseDisplay(xdisplay); + + return false; } if (syncDisplay) XSynchronize(xdisplay, True); - wxApp::ms_display = (WXDisplay*) xdisplay; + ms_display = (WXDisplay*) xdisplay; XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask); @@ -197,118 +195,25 @@ int wxEntryStart( int& argc, char *argv[] ) g_type_init(); #endif - if (!wxApp::Initialize()) - return -1; - - return 0; -} - -int wxEntryInitGui() -{ - int retValue = 0; +#if wxUSE_INTL + wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); +#endif - if ( !wxTheApp->OnInitGui() ) - retValue = -1; + wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); + wxClientWidgetHashTable = new wxHashTable(wxKEY_INTEGER); - return retValue; + return true; } - -int wxEntry( int argc, char *argv[] ) +void wxApp::CleanUp() { -#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT - // This seems to be necessary since there are 'rogue' - // objects present at this point (perhaps global objects?) - // Setting a checkpoint will ignore them as far as the - // memory checking facility is concerned. - // Of course you may argue that memory allocated in globals should be - // checked, but this is a reasonable compromise. - wxDebugContext::SetCheckpoint(); -#endif - int err = wxEntryStart(argc, argv); - if (err) - return err; - - if (!wxTheApp) - { - if (!wxApp::GetInitializerFunction()) - { - printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); - return 0; - } - - wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) (); - } - - if (!wxTheApp) - { - printf( "wxWindows error: wxTheApp == NULL\n" ); - return 0; - } - - // Command line argument stuff - wxTheApp->argc = argc; -#if wxUSE_UNICODE - wxTheApp->argv = new wxChar*[argc+1]; - int mb_argc = 0; - while (mb_argc < argc) - { - wxString tmp = wxString::FromAscii( argv[mb_argc] ); - wxTheApp->argv[mb_argc] = wxStrdup( tmp.c_str() ); - mb_argc++; - } - wxTheApp->argv[mb_argc] = (wxChar *)NULL; -#else - wxTheApp->argv = argv; -#endif - - if (wxTheApp->argc > 0) - { - wxFileName fname( wxTheApp->argv[0] ); - wxTheApp->SetAppName( fname.GetName() ); - } - - wxTheApp->m_showIconic = g_showIconic; - wxTheApp->m_initialSize = g_initialSize; - - int retValue; - retValue = wxEntryInitGui(); - - // Here frames insert themselves automatically into wxTopLevelWindows by - // getting created in OnInit(). - if ( retValue == 0 ) - { - if ( !wxTheApp->OnInit() ) - retValue = -1; - } - - if ( retValue == 0 ) - { - if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); - } - - // flush the logged messages if any - wxLog *pLog = wxLog::GetActiveTarget(); - if ( pLog != NULL && pLog->HasPendingMessages() ) - pLog->Flush(); - - delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used - // for further messages - - if (wxTheApp->GetTopWindow()) - { - delete wxTheApp->GetTopWindow(); - wxTheApp->SetTopWindow(NULL); - } - - wxTheApp->DeletePendingObjects(); - - wxTheApp->OnExit(); - - wxApp::CleanUp(); + delete wxWidgetHashTable; + wxWidgetHashTable = NULL; + delete wxClientWidgetHashTable; + wxClientWidgetHashTable = NULL; - return retValue; -}; + wxAppBase::CleanUp(); +} wxApp::wxApp() { -- 2.47.2