1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/init.cpp
3 // Purpose: initialisation for the library
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/filefn.h"
32 #include "wx/module.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 WXDLLEXPORT wxApp
*wxTheApp
= NULL
;
40 wxAppInitializerFunction
41 wxAppBase::m_appInitFn
= (wxAppInitializerFunction
)NULL
;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class /* no WXDLLEXPORT */ wxConsoleApp
: public wxApp
50 virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
51 virtual bool ProcessIdle() { return TRUE
; }
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
59 static void DoCleanUp();
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 static size_t gs_nInitCount
= 0;
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
72 // stubs for some GUI functions
73 // ----------------------------------------------------------------------------
75 void WXDLLEXPORT
wxExit()
80 // Yield to other apps/messages
81 void WXDLLEXPORT
wxWakeUpIdle()
86 // ----------------------------------------------------------------------------
87 // wxBase-specific functions
88 // ----------------------------------------------------------------------------
90 bool WXDLLEXPORT
wxInitialize()
94 // already initialized
98 wxASSERT_MSG( !wxTheApp
,
99 wxT("either call wxInitialize or create app, not both!") );
106 wxTheApp
= new wxConsoleApp
;
119 void WXDLLEXPORT
wxUninitialize()
121 if ( !--gs_nInitCount
)
127 int wxEntry(int argc
, char **argv
)
129 // library initialization
138 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
139 wxT("No application object: use IMPLEMENT_APP macro.") );
141 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
143 wxTheApp
= (wxApp
*)fnCreate();
146 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
148 // app preinitialization
149 wxTheApp
->argc
= argc
;
152 wxTheApp
->argv
= new wxChar
*[argc
+1];
154 while (mb_argc
< argc
)
156 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[mb_argc
]));
159 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
161 wxTheApp
->argv
= argv
;
164 wxString name
= wxFileNameFromPath(argv
[0]);
165 wxStripExtension(name
);
166 wxTheApp
->SetAppName(name
);
170 // app initialization
171 if ( !wxTheApp
->OnInit() )
177 retValue
= wxTheApp
->OnRun();
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
195 wxClassInfo::InitializeClasses();
197 wxModule::RegisterModules();
198 if ( !wxModule::InitializeModules() )
206 static void DoCleanUp()
209 // flush the logged messages if any
210 wxLog
*log
= wxLog::GetActiveTarget();
211 if (log
!= NULL
&& log
->HasPendingMessages())
214 // continuing to use user defined log target is unsafe from now on because
215 // some resources may be already unavailable, so replace it by something
217 wxLog::DontCreateOnDemand();
218 delete wxLog::SetActiveTarget(new wxLogStderr
);
221 wxModule::CleanUpModules();
223 wxClassInfo::CleanUpClasses();
225 // delete the application object
227 wxTheApp
= (wxApp
*)NULL
;
230 // and now delete the last logger as well
231 delete wxLog::SetActiveTarget(NULL
);