]>
Commit | Line | Data |
---|---|---|
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 | wxTheApp->DoInit(); | |
113 | gs_nInitCount++; | |
114 | ||
115 | return TRUE; | |
116 | } | |
117 | ||
118 | void WXDLLEXPORT wxUninitialize() | |
119 | { | |
120 | if ( !--gs_nInitCount ) | |
121 | { | |
122 | DoCleanUp(); | |
123 | } | |
124 | } | |
125 | ||
126 | int wxEntry(int argc, char **argv) | |
127 | { | |
128 | // library initialization | |
129 | if ( !DoInit() ) | |
130 | { | |
131 | return -1; | |
132 | } | |
133 | ||
134 | // create the app | |
135 | if ( !wxTheApp ) | |
136 | { | |
137 | wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, | |
138 | wxT("No application object: use IMPLEMENT_APP macro.") ); | |
139 | ||
140 | wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction(); | |
141 | ||
142 | wxTheApp = (wxApp *)fnCreate(); | |
143 | } | |
144 | ||
145 | wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); | |
146 | ||
147 | // app preinitialization | |
148 | wxTheApp->argc = argc; | |
149 | ||
150 | #if wxUSE_UNICODE | |
151 | wxTheApp->argv = new wxChar*[argc+1]; | |
152 | int mb_argc = 0; | |
153 | while (mb_argc < argc) | |
154 | { | |
155 | wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); | |
156 | mb_argc++; | |
157 | } | |
158 | wxTheApp->argv[mb_argc] = (wxChar *)NULL; | |
159 | #else | |
160 | wxTheApp->argv = argv; | |
161 | #endif | |
162 | ||
163 | wxString name = wxFileNameFromPath(argv[0]); | |
164 | wxStripExtension(name); | |
165 | wxTheApp->SetAppName(name); | |
166 | ||
167 | int retValue = 0; | |
168 | ||
169 | // app initialization | |
170 | if ( !wxTheApp->OnInit() ) | |
171 | retValue = -1; | |
172 | ||
173 | // app execution | |
174 | if ( retValue == 0 ) | |
175 | { | |
176 | retValue = wxTheApp->OnRun(); | |
177 | ||
178 | // app clean up | |
179 | wxTheApp->OnExit(); | |
180 | } | |
181 | ||
182 | // library clean up | |
183 | DoCleanUp(); | |
184 | ||
185 | return retValue; | |
186 | } | |
187 | ||
188 | // ---------------------------------------------------------------------------- | |
189 | // private functions | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | static bool DoInit() | |
193 | { | |
194 | wxClassInfo::InitializeClasses(); | |
195 | ||
196 | wxModule::RegisterModules(); | |
197 | if ( !wxModule::InitializeModules() ) | |
198 | { | |
199 | return FALSE; | |
200 | } | |
201 | ||
202 | return TRUE; | |
203 | } | |
204 | ||
205 | static void DoCleanUp() | |
206 | { | |
207 | #if wxUSE_LOG | |
208 | // flush the logged messages if any | |
209 | wxLog *log = wxLog::GetActiveTarget(); | |
210 | if (log != NULL && log->HasPendingMessages()) | |
211 | log->Flush(); | |
212 | ||
213 | // continuing to use user defined log target is unsafe from now on because | |
214 | // some resources may be already unavailable, so replace it by something | |
215 | // more safe | |
216 | wxLog::DontCreateOnDemand(); | |
217 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
218 | #endif // wxUSE_LOG | |
219 | ||
220 | wxModule::CleanUpModules(); | |
221 | ||
222 | wxClassInfo::CleanUpClasses(); | |
223 | ||
224 | // delete the application object | |
225 | delete wxTheApp; | |
226 | wxTheApp = (wxApp *)NULL; | |
227 | ||
228 | #if wxUSE_LOG | |
229 | // and now delete the last logger as well | |
230 | delete wxLog::SetActiveTarget(NULL); | |
231 | #endif // wxUSE_LOG | |
232 | } |