]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/init.cpp
Committing in .
[wxWidgets.git] / src / common / init.cpp
index e1fef28c238169c9d20d09d5911ee1d7329d8f8d..41b3d29330f87cd820ffa60a49845aff2794fe44 100644 (file)
 // headers
 // ----------------------------------------------------------------------------
 
 // headers
 // ----------------------------------------------------------------------------
 
-#ifdef __GNUG__
-    #pragma implementation "appbase.h"
+#include "wx/wxprec.h"
+
+#ifdef    __BORLANDC__
+  #pragma hdrstop
+#endif  //__BORLANDC__
+
+#ifndef WX_PRECOMP
+    #include "wx/app.h"
+    #include "wx/debug.h"
+    #include "wx/filefn.h"
 #endif
 
 #endif
 
-#include "wx/app.h"
-#include "wx/debug.h"
+#include "wx/module.h"
 
 // ----------------------------------------------------------------------------
 // global vars
 // ----------------------------------------------------------------------------
 
 
 // ----------------------------------------------------------------------------
 // global vars
 // ----------------------------------------------------------------------------
 
-wxApp * WXDLLEXPORT wxTheApp = NULL;
+WXDLLEXPORT wxApp *wxTheApp = NULL;
 
 wxAppInitializerFunction
     wxAppBase::m_appInitFn = (wxAppInitializerFunction)NULL;
 
 
 wxAppInitializerFunction
     wxAppBase::m_appInitFn = (wxAppInitializerFunction)NULL;
 
-#if wxUSE_THREADS
-    // List of events pending processing
-    wxList *wxPendingEvents = NULL;
-    wxCriticalSection *wxPendingEventsLocker = NULL;
-#endif // wxUSE_THREADS
-
 // ----------------------------------------------------------------------------
 // private classes
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // private classes
 // ----------------------------------------------------------------------------
@@ -49,6 +50,13 @@ public:
     virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
 };
 
     virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
 };
 
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+static bool DoInit();
+static void DoCleanUp();
+
 // ----------------------------------------------------------------------------
 // private vars
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // private vars
 // ----------------------------------------------------------------------------
@@ -59,6 +67,25 @@ static size_t gs_nInitCount = 0;
 // implementation
 // ============================================================================
 
 // implementation
 // ============================================================================
 
+// ----------------------------------------------------------------------------
+// stubs for some GUI functions
+// ----------------------------------------------------------------------------
+
+void WXDLLEXPORT wxExit()
+{
+    abort();
+}
+
+// Yield to other apps/messages
+void WXDLLEXPORT wxWakeUpIdle()
+{
+    // do nothing
+}
+
+// ----------------------------------------------------------------------------
+// wxBase-specific functions
+// ----------------------------------------------------------------------------
+
 bool WXDLLEXPORT wxInitialize()
 {
     if ( gs_nInitCount )
 bool WXDLLEXPORT wxInitialize()
 {
     if ( gs_nInitCount )
@@ -70,10 +97,7 @@ bool WXDLLEXPORT wxInitialize()
     wxASSERT_MSG( !wxTheApp,
                   wxT("either call wxInitialize or create app, not both!") );
 
     wxASSERT_MSG( !wxTheApp,
                   wxT("either call wxInitialize or create app, not both!") );
 
-    wxClassInfo::InitializeClasses();
-
-    wxModule::RegisterModules();
-    if ( !wxModule::InitializeModules() )
+    if ( !DoInit() )
     {
         return FALSE;
     }
     {
         return FALSE;
     }
@@ -94,12 +118,114 @@ void WXDLLEXPORT wxUninitialize()
 {
     if ( !--gs_nInitCount )
     {
 {
     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.") );
 
 
-        wxClassInfo::CleanUpClasses();
+        wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
 
 
-        // delete the application object
-        delete wxTheApp;
-        wxTheApp = (wxApp *)NULL;
+        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];
+    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 = 0;
+
+    // app initialization
+    if ( !wxTheApp->OnInit() )
+        retValue = -1;
+
+    // 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();
+
+    // 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
 }
 }