]> git.saurik.com Git - wxWidgets.git/blob - src/common/init.cpp
applied (slightly modified) wxGLApp patch for MSW
[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 bool WXDLLEXPORT wxYield()
81 {
82 // do nothing
83 return TRUE;
84 }
85
86 // Yield to other apps/messages
87 void WXDLLEXPORT wxWakeUpIdle()
88 {
89 // do nothing
90 }
91
92 // ----------------------------------------------------------------------------
93 // wxBase-specific functions
94 // ----------------------------------------------------------------------------
95
96 bool WXDLLEXPORT wxInitialize()
97 {
98 if ( gs_nInitCount )
99 {
100 // already initialized
101 return TRUE;
102 }
103
104 wxASSERT_MSG( !wxTheApp,
105 wxT("either call wxInitialize or create app, not both!") );
106
107 if ( !DoInit() )
108 {
109 return FALSE;
110 }
111
112 wxTheApp = new wxConsoleApp;
113
114 if ( !wxTheApp )
115 {
116 return FALSE;
117 }
118
119 gs_nInitCount++;
120
121 return TRUE;
122 }
123
124 void WXDLLEXPORT wxUninitialize()
125 {
126 if ( !--gs_nInitCount )
127 {
128 DoCleanUp();
129 }
130 }
131
132 int wxEntry(int argc, char **argv)
133 {
134 // library initialization
135 if ( !DoInit() )
136 {
137 return -1;
138 }
139
140 // create the app
141 if ( !wxTheApp )
142 {
143 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
144 wxT("No application object: use IMPLEMENT_APP macro.") );
145
146 wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
147
148 wxTheApp = (wxApp *)fnCreate();
149 }
150
151 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
152
153 // app preinitialization
154 wxTheApp->argc = argc;
155
156 #if wxUSE_UNICODE
157 wxTheApp->argv = new wxChar*[argc+1];
158 int mb_argc = 0;
159 while (mb_argc < argc)
160 {
161 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
162 mb_argc++;
163 }
164 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
165 #else
166 wxTheApp->argv = argv;
167 #endif
168
169 wxString name = wxFileNameFromPath(argv[0]);
170 wxStripExtension(name);
171 wxTheApp->SetAppName(name);
172
173 int retValue = 0;
174
175 // app initialization
176 if ( !wxTheApp->OnInit() )
177 retValue = -1;
178
179 // app execution
180 if ( retValue == 0 )
181 {
182 retValue = wxTheApp->OnRun();
183
184 // app clean up
185 wxTheApp->OnExit();
186 }
187
188 // library clean up
189 DoCleanUp();
190
191 return retValue;
192 }
193
194 // ----------------------------------------------------------------------------
195 // private functions
196 // ----------------------------------------------------------------------------
197
198 static bool DoInit()
199 {
200 wxClassInfo::InitializeClasses();
201
202 wxModule::RegisterModules();
203 if ( !wxModule::InitializeModules() )
204 {
205 return FALSE;
206 }
207
208 return TRUE;
209 }
210
211 static void DoCleanUp()
212 {
213 #if wxUSE_LOG
214 // flush the logged messages if any
215 wxLog *log = wxLog::GetActiveTarget();
216 if (log != NULL && log->HasPendingMessages())
217 log->Flush();
218
219 // continuing to use user defined log target is unsafe from now on because
220 // some resources may be already unavailable, so replace it by something
221 // more safe
222 wxLog::DontCreateOnDemand();
223 delete wxLog::SetActiveTarget(new wxLogStderr);
224 #endif // wxUSE_LOG
225
226 wxModule::CleanUpModules();
227
228 wxClassInfo::CleanUpClasses();
229
230 // delete the application object
231 delete wxTheApp;
232 wxTheApp = (wxApp *)NULL;
233
234 #if wxUSE_LOG
235 // and now delete the last logger as well
236 delete wxLog::SetActiveTarget(NULL);
237 #endif // wxUSE_LOG
238 }