]>
Commit | Line | Data |
---|---|---|
e90c1d2a VZ |
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 | ||
f4c890fd JS |
20 | #include "wx/wxprec.h" |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif //__BORLANDC__ | |
25 | ||
e87271f3 VZ |
26 | #ifndef WX_PRECOMP |
27 | #include "wx/app.h" | |
28 | #include "wx/debug.h" | |
bc385ba9 | 29 | #include "wx/filefn.h" |
e87271f3 | 30 | #endif |
e90c1d2a | 31 | |
51abe921 SC |
32 | #include "wx/module.h" |
33 | ||
e90c1d2a VZ |
34 | // ---------------------------------------------------------------------------- |
35 | // global vars | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
f6bcfd97 | 38 | WXDLLEXPORT wxApp *wxTheApp = NULL; |
e90c1d2a VZ |
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: | |
223d09f6 | 50 | virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; } |
6788de4d | 51 | virtual bool ProcessIdle() { return TRUE; } |
e90c1d2a VZ |
52 | }; |
53 | ||
bc385ba9 VZ |
54 | // ---------------------------------------------------------------------------- |
55 | // private functions | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | static bool DoInit(); | |
59 | static void DoCleanUp(); | |
60 | ||
e90c1d2a VZ |
61 | // ---------------------------------------------------------------------------- |
62 | // private vars | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | static size_t gs_nInitCount = 0; | |
66 | ||
67 | // ============================================================================ | |
68 | // implementation | |
69 | // ============================================================================ | |
70 | ||
252a752e VZ |
71 | // ---------------------------------------------------------------------------- |
72 | // stubs for some GUI functions | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | void WXDLLEXPORT wxExit() | |
76 | { | |
77 | abort(); | |
78 | } | |
79 | ||
252a752e VZ |
80 | // Yield to other apps/messages |
81 | void WXDLLEXPORT wxWakeUpIdle() | |
82 | { | |
83 | // do nothing | |
84 | } | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // wxBase-specific functions | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
e90c1d2a VZ |
90 | bool WXDLLEXPORT wxInitialize() |
91 | { | |
bbfa0322 | 92 | if ( gs_nInitCount ) |
e90c1d2a VZ |
93 | { |
94 | // already initialized | |
95 | return TRUE; | |
96 | } | |
97 | ||
98 | wxASSERT_MSG( !wxTheApp, | |
223d09f6 | 99 | wxT("either call wxInitialize or create app, not both!") ); |
e90c1d2a | 100 | |
bc385ba9 | 101 | if ( !DoInit() ) |
bbfa0322 VZ |
102 | { |
103 | return FALSE; | |
104 | } | |
105 | ||
e90c1d2a VZ |
106 | wxTheApp = new wxConsoleApp; |
107 | ||
bbfa0322 VZ |
108 | if ( !wxTheApp ) |
109 | { | |
110 | return FALSE; | |
111 | } | |
112 | ||
74698d3a | 113 | wxTheApp->DoInit(); |
bbfa0322 VZ |
114 | gs_nInitCount++; |
115 | ||
116 | return TRUE; | |
e90c1d2a VZ |
117 | } |
118 | ||
119 | void WXDLLEXPORT wxUninitialize() | |
120 | { | |
121 | if ( !--gs_nInitCount ) | |
122 | { | |
bc385ba9 VZ |
123 | DoCleanUp(); |
124 | } | |
125 | } | |
126 | ||
127 | int wxEntry(int argc, char **argv) | |
128 | { | |
129 | // library initialization | |
130 | if ( !DoInit() ) | |
131 | { | |
132 | return -1; | |
133 | } | |
134 | ||
135 | // create the app | |
136 | if ( !wxTheApp ) | |
137 | { | |
138 | wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, | |
139 | wxT("No application object: use IMPLEMENT_APP macro.") ); | |
140 | ||
141 | wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction(); | |
142 | ||
143 | wxTheApp = (wxApp *)fnCreate(); | |
144 | } | |
145 | ||
146 | wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); | |
147 | ||
148 | // app preinitialization | |
149 | wxTheApp->argc = argc; | |
150 | ||
151 | #if wxUSE_UNICODE | |
152 | wxTheApp->argv = new wxChar*[argc+1]; | |
153 | int mb_argc = 0; | |
154 | while (mb_argc < argc) | |
155 | { | |
156 | wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); | |
157 | mb_argc++; | |
158 | } | |
159 | wxTheApp->argv[mb_argc] = (wxChar *)NULL; | |
160 | #else | |
161 | wxTheApp->argv = argv; | |
162 | #endif | |
163 | ||
164 | wxString name = wxFileNameFromPath(argv[0]); | |
165 | wxStripExtension(name); | |
166 | wxTheApp->SetAppName(name); | |
167 | ||
168 | int retValue = 0; | |
169 | ||
170 | // app initialization | |
171 | if ( !wxTheApp->OnInit() ) | |
172 | retValue = -1; | |
173 | ||
174 | // app execution | |
175 | if ( retValue == 0 ) | |
176 | { | |
177 | retValue = wxTheApp->OnRun(); | |
bbfa0322 | 178 | |
bc385ba9 VZ |
179 | // app clean up |
180 | wxTheApp->OnExit(); | |
181 | } | |
182 | ||
183 | // library clean up | |
184 | DoCleanUp(); | |
185 | ||
186 | return retValue; | |
187 | } | |
188 | ||
189 | // ---------------------------------------------------------------------------- | |
190 | // private functions | |
191 | // ---------------------------------------------------------------------------- | |
bbfa0322 | 192 | |
bc385ba9 VZ |
193 | static bool DoInit() |
194 | { | |
195 | wxClassInfo::InitializeClasses(); | |
196 | ||
197 | wxModule::RegisterModules(); | |
198 | if ( !wxModule::InitializeModules() ) | |
199 | { | |
200 | return FALSE; | |
e90c1d2a | 201 | } |
bc385ba9 VZ |
202 | |
203 | return TRUE; | |
204 | } | |
205 | ||
206 | static void DoCleanUp() | |
207 | { | |
208 | #if wxUSE_LOG | |
209 | // flush the logged messages if any | |
210 | wxLog *log = wxLog::GetActiveTarget(); | |
211 | if (log != NULL && log->HasPendingMessages()) | |
212 | log->Flush(); | |
213 | ||
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 | |
216 | // more safe | |
8a9c2246 VZ |
217 | wxLog::DontCreateOnDemand(); |
218 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
bc385ba9 VZ |
219 | #endif // wxUSE_LOG |
220 | ||
221 | wxModule::CleanUpModules(); | |
222 | ||
223 | wxClassInfo::CleanUpClasses(); | |
224 | ||
225 | // delete the application object | |
226 | delete wxTheApp; | |
227 | wxTheApp = (wxApp *)NULL; | |
8a9c2246 VZ |
228 | |
229 | #if wxUSE_LOG | |
230 | // and now delete the last logger as well | |
231 | delete wxLog::SetActiveTarget(NULL); | |
232 | #endif // wxUSE_LOG | |
e90c1d2a | 233 | } |