]>
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 | virtual bool ProcessIdle() { return TRUE; } | |
52 | }; | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // private functions | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | static bool DoInit(); | |
59 | static void DoCleanUp(); | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // private vars | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | static size_t gs_nInitCount = 0; | |
66 | ||
67 | // ============================================================================ | |
68 | // implementation | |
69 | // ============================================================================ | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // stubs for some GUI functions | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | void WXDLLEXPORT wxExit() | |
76 | { | |
77 | abort(); | |
78 | } | |
79 | ||
80 | // Yield to other apps/messages | |
81 | void WXDLLEXPORT wxWakeUpIdle() | |
82 | { | |
83 | // do nothing | |
84 | } | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // wxBase-specific functions | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | bool WXDLLEXPORT wxInitialize() | |
91 | { | |
92 | if ( gs_nInitCount ) | |
93 | { | |
94 | // already initialized | |
95 | return TRUE; | |
96 | } | |
97 | ||
98 | wxASSERT_MSG( !wxTheApp, | |
99 | wxT("either call wxInitialize or create app, not both!") ); | |
100 | ||
101 | if ( !DoInit() ) | |
102 | { | |
103 | return FALSE; | |
104 | } | |
105 | ||
106 | wxTheApp = new wxConsoleApp; | |
107 | ||
108 | if ( !wxTheApp ) | |
109 | { | |
110 | return FALSE; | |
111 | } | |
112 | ||
113 | wxTheApp->DoInit(); | |
114 | gs_nInitCount++; | |
115 | ||
116 | return TRUE; | |
117 | } | |
118 | ||
119 | void WXDLLEXPORT wxUninitialize() | |
120 | { | |
121 | if ( !--gs_nInitCount ) | |
122 | { | |
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(wxConvLocal.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(); | |
178 | ||
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 | // ---------------------------------------------------------------------------- | |
192 | ||
193 | static bool DoInit() | |
194 | { | |
195 | wxClassInfo::InitializeClasses(); | |
196 | ||
197 | wxModule::RegisterModules(); | |
198 | if ( !wxModule::InitializeModules() ) | |
199 | { | |
200 | return FALSE; | |
201 | } | |
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 | |
217 | wxLog::DontCreateOnDemand(); | |
218 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
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; | |
228 | ||
229 | #if wxUSE_LOG | |
230 | // and now delete the last logger as well | |
231 | delete wxLog::SetActiveTarget(NULL); | |
232 | #endif // wxUSE_LOG | |
233 | } |