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