]> git.saurik.com Git - wxWidgets.git/blob - src/common/init.cpp
Implemented wxStatusBar::Push/PopStatusText.
[wxWidgets.git] / src / common / init.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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/debug.h"
29 #include "wx/filefn.h"
30 #endif
31
32 #include "wx/module.h"
33
34 // ----------------------------------------------------------------------------
35 // global vars
36 // ----------------------------------------------------------------------------
37
38 WXDLLEXPORT wxApp *wxTheApp = NULL;
39
40 wxAppInitializerFunction
41 wxAppBase::m_appInitFn = (wxAppInitializerFunction)NULL;
42
43 // ----------------------------------------------------------------------------
44 // private classes
45 // ----------------------------------------------------------------------------
46
47 class /* no WXDLLEXPORT */ wxConsoleApp : public wxApp
48 {
49 public:
50 virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
51 };
52
53 // ----------------------------------------------------------------------------
54 // private functions
55 // ----------------------------------------------------------------------------
56
57 static bool DoInit();
58 static void DoCleanUp();
59
60 // ----------------------------------------------------------------------------
61 // private vars
62 // ----------------------------------------------------------------------------
63
64 static size_t gs_nInitCount = 0;
65
66 // ============================================================================
67 // implementation
68 // ============================================================================
69
70 // ----------------------------------------------------------------------------
71 // stubs for some GUI functions
72 // ----------------------------------------------------------------------------
73
74 void WXDLLEXPORT wxExit()
75 {
76 abort();
77 }
78
79 // Yield to other apps/messages
80 void WXDLLEXPORT wxWakeUpIdle()
81 {
82 // do nothing
83 }
84
85 // ----------------------------------------------------------------------------
86 // wxBase-specific functions
87 // ----------------------------------------------------------------------------
88
89 bool WXDLLEXPORT wxInitialize()
90 {
91 if ( gs_nInitCount )
92 {
93 // already initialized
94 return TRUE;
95 }
96
97 wxASSERT_MSG( !wxTheApp,
98 wxT("either call wxInitialize or create app, not both!") );
99
100 if ( !DoInit() )
101 {
102 return FALSE;
103 }
104
105 wxTheApp = new wxConsoleApp;
106
107 if ( !wxTheApp )
108 {
109 return FALSE;
110 }
111
112 gs_nInitCount++;
113
114 return TRUE;
115 }
116
117 void WXDLLEXPORT wxUninitialize()
118 {
119 if ( !--gs_nInitCount )
120 {
121 DoCleanUp();
122 }
123 }
124
125 int wxEntry(int argc, char **argv)
126 {
127 // library initialization
128 if ( !DoInit() )
129 {
130 return -1;
131 }
132
133 // create the app
134 if ( !wxTheApp )
135 {
136 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
137 wxT("No application object: use IMPLEMENT_APP macro.") );
138
139 wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
140
141 wxTheApp = (wxApp *)fnCreate();
142 }
143
144 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
145
146 // app preinitialization
147 wxTheApp->argc = argc;
148
149 #if wxUSE_UNICODE
150 wxTheApp->argv = new wxChar*[argc+1];
151 int mb_argc = 0;
152 while (mb_argc < argc)
153 {
154 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
155 mb_argc++;
156 }
157 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
158 #else
159 wxTheApp->argv = argv;
160 #endif
161
162 wxString name = wxFileNameFromPath(argv[0]);
163 wxStripExtension(name);
164 wxTheApp->SetAppName(name);
165
166 int retValue = 0;
167
168 // app initialization
169 if ( !wxTheApp->OnInit() )
170 retValue = -1;
171
172 // app execution
173 if ( retValue == 0 )
174 {
175 retValue = wxTheApp->OnRun();
176
177 // app clean up
178 wxTheApp->OnExit();
179 }
180
181 // library clean up
182 DoCleanUp();
183
184 return retValue;
185 }
186
187 // ----------------------------------------------------------------------------
188 // private functions
189 // ----------------------------------------------------------------------------
190
191 static bool DoInit()
192 {
193 wxClassInfo::InitializeClasses();
194
195 wxModule::RegisterModules();
196 if ( !wxModule::InitializeModules() )
197 {
198 return FALSE;
199 }
200
201 return TRUE;
202 }
203
204 static void DoCleanUp()
205 {
206 #if wxUSE_LOG
207 // flush the logged messages if any
208 wxLog *log = wxLog::GetActiveTarget();
209 if (log != NULL && log->HasPendingMessages())
210 log->Flush();
211
212 // continuing to use user defined log target is unsafe from now on because
213 // some resources may be already unavailable, so replace it by something
214 // more safe
215 wxLog::DontCreateOnDemand();
216 delete wxLog::SetActiveTarget(new wxLogStderr);
217 #endif // wxUSE_LOG
218
219 wxModule::CleanUpModules();
220
221 wxClassInfo::CleanUpClasses();
222
223 // delete the application object
224 delete wxTheApp;
225 wxTheApp = (wxApp *)NULL;
226
227 #if wxUSE_LOG
228 // and now delete the last logger as well
229 delete wxLog::SetActiveTarget(NULL);
230 #endif // wxUSE_LOG
231 }