]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/init.cpp
corrected generation of all event members (client data) and implemented DoGetBestSize
[wxWidgets.git] / src / common / init.cpp
index 846026eb938e5503cdf9db2945f578b56a5c6b11..4f0aed6e49c55831e14f83e71621116449589d3e 100644 (file)
 #ifndef WX_PRECOMP
     #include "wx/app.h"
     #include "wx/debug.h"
+    #include "wx/filefn.h"
+    #include "wx/log.h"
 #endif
 
 #include "wx/module.h"
 
-
 // ----------------------------------------------------------------------------
 // global vars
 // ----------------------------------------------------------------------------
 
-wxApp * WXDLLEXPORT wxTheApp = NULL;
+WXDLLEXPORT wxApp *wxTheApp = NULL;
 
 wxAppInitializerFunction
     wxAppBase::m_appInitFn = (wxAppInitializerFunction)NULL;
@@ -50,6 +51,13 @@ public:
     virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
 };
 
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+static bool DoInit();
+static void DoCleanUp();
+
 // ----------------------------------------------------------------------------
 // private vars
 // ----------------------------------------------------------------------------
@@ -69,13 +77,6 @@ void WXDLLEXPORT wxExit()
     abort();
 }
 
-// Yield to other apps/messages
-bool WXDLLEXPORT wxYield()
-{
-    // do nothing
-    return TRUE;
-}
-
 // Yield to other apps/messages
 void WXDLLEXPORT wxWakeUpIdle()
 {
@@ -97,10 +98,7 @@ bool WXDLLEXPORT wxInitialize()
     wxASSERT_MSG( !wxTheApp,
                   wxT("either call wxInitialize or create app, not both!") );
 
-    wxClassInfo::InitializeClasses();
-
-    wxModule::RegisterModules();
-    if ( !wxModule::InitializeModules() )
+    if ( !DoInit() )
     {
         return FALSE;
     }
@@ -121,12 +119,121 @@ void WXDLLEXPORT wxUninitialize()
 {
     if ( !--gs_nInitCount )
     {
-        wxModule::CleanUpModules();
+        DoCleanUp();
+    }
+}
+
+int wxEntry(int argc, char **argv)
+{
+    // library initialization
+    if ( !DoInit() )
+    {
+        return -1;
+    }
+
+    // create the app
+    if ( !wxTheApp )
+    {
+        wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
+                wxT("No application object: use IMPLEMENT_APP macro.") );
+
+        wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
+
+        wxTheApp = (wxApp *)fnCreate();
+    }
+
+    wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
+
+    // app preinitialization
+    wxTheApp->argc = argc;
+
+#if wxUSE_UNICODE
+    wxTheApp->argv = new wxChar*[argc+1];
+    for ( int 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
+
+    wxString name = wxFileNameFromPath(wxTheApp->argv[0]);
+    wxStripExtension(name);
+    wxTheApp->SetAppName(name);
+
+    int retValue = 0;
 
-        wxClassInfo::CleanUpClasses();
+    // app initialization
+    if ( !wxTheApp->OnInit() )
+        retValue = -1;
 
-        // delete the application object
-        delete wxTheApp;
-        wxTheApp = (wxApp *)NULL;
+    // app execution
+    if ( retValue == 0 )
+    {
+        retValue = wxTheApp->OnRun();
+
+        // app clean up
+        wxTheApp->OnExit();
     }
+
+    // library clean up
+    DoCleanUp();
+
+    return retValue;
 }
+
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+static bool DoInit()
+{
+    wxClassInfo::InitializeClasses();
+
+    wxModule::RegisterModules();
+    if ( !wxModule::InitializeModules() )
+    {
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+static void DoCleanUp()
+{
+#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::DontCreateOnDemand();
+    delete wxLog::SetActiveTarget(new wxLogStderr);
+#endif // wxUSE_LOG
+
+    wxModule::CleanUpModules();
+
+    wxClassInfo::CleanUpClasses();
+
+    // TODO: this should really be done in ~wxApp
+#if wxUSE_UNICODE
+    for ( int mb_argc = 0; mb_argc < wxTheApp->argc; mb_argc++ )
+    {
+        free(wxTheApp->argv[mb_argc]);
+    }
+#endif // wxUSE_UNICODE
+
+    // delete the application object
+    delete wxTheApp;
+    wxTheApp = (wxApp *)NULL;
+
+#if wxUSE_LOG
+    // and now delete the last logger as well
+    delete wxLog::SetActiveTarget(NULL);
+#endif // wxUSE_LOG
+}
+