]>
Commit | Line | Data |
---|---|---|
72cdf4c9 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/appcmn.cpp | |
3 | // Purpose: wxAppBase methods common to all platforms | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 18.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 | #ifdef __GNUG__ | |
21 | #pragma implementation "appbase.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #if defined(__BORLANDC__) | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/app.h" | |
bf188f1a | 33 | #include "wx/intl.h" |
e87271f3 | 34 | #include "wx/list.h" |
a5f1fd3e VZ |
35 | #if wxUSE_GUI |
36 | #include "wx/msgdlg.h" | |
37 | #endif // wxUSE_GUI | |
72cdf4c9 VZ |
38 | #endif |
39 | ||
bf188f1a | 40 | #include "wx/cmdline.h" |
72cdf4c9 | 41 | #include "wx/thread.h" |
7beba2fc | 42 | #include "wx/confbase.h" |
e1ee679c | 43 | |
a5f1fd3e VZ |
44 | #if !defined(__WXMSW__) || defined(__WXMICROWIN__) |
45 | #include <signal.h> // for SIGTRAP used by wxTrap() | |
46 | #endif //Win/Unix | |
47 | ||
48 | #if defined(__WXMSW__) | |
49 | #include "wx/msw/private.h" // includes windows.h for MessageBox() | |
50 | #endif | |
51 | ||
d54598dd VZ |
52 | // =========================================================================== |
53 | // implementation | |
54 | // =========================================================================== | |
55 | ||
bf188f1a VZ |
56 | // ---------------------------------------------------------------------------- |
57 | // initialization and termination | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
1e6feb95 VZ |
60 | wxAppBase::wxAppBase() |
61 | { | |
62 | wxTheApp = (wxApp *)this; | |
63 | ||
64 | // VZ: what's this? is it obsolete? | |
65 | m_wantDebugOutput = FALSE; | |
66 | ||
67 | #if wxUSE_GUI | |
68 | m_topWindow = (wxWindow *)NULL; | |
69 | m_useBestVisual = FALSE; | |
70 | m_exitOnFrameDelete = TRUE; | |
71 | m_isActive = TRUE; | |
72 | #endif // wxUSE_GUI | |
73 | } | |
74 | ||
1e6feb95 VZ |
75 | #if wxUSE_GUI |
76 | bool wxAppBase::OnInitGui() | |
77 | { | |
78 | #ifdef __WXUNIVERSAL__ | |
bf188f1a | 79 | if ( !wxTheme::Get() && !wxTheme::CreateDefault() ) |
1e6feb95 VZ |
80 | return FALSE; |
81 | #endif // __WXUNIVERSAL__ | |
82 | ||
83 | return TRUE; | |
84 | } | |
85 | #endif // wxUSE_GUI | |
86 | ||
87 | int wxAppBase::OnExit() | |
88 | { | |
89 | #if wxUSE_CONFIG | |
90 | // delete the config object if any (don't use Get() here, but Set() | |
91 | // because Get() could create a new config object) | |
92 | delete wxConfigBase::Set((wxConfigBase *) NULL); | |
93 | #endif // wxUSE_CONFIG | |
94 | ||
95 | #ifdef __WXUNIVERSAL__ | |
96 | delete wxTheme::Set(NULL); | |
97 | #endif // __WXUNIVERSAL__ | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
72cdf4c9 VZ |
102 | // --------------------------------------------------------------------------- |
103 | // wxAppBase | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | void wxAppBase::ProcessPendingEvents() | |
107 | { | |
108 | // ensure that we're the only thread to modify the pending events list | |
16c1f79c | 109 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 VZ |
110 | |
111 | if ( !wxPendingEvents ) | |
16c1f79c RR |
112 | { |
113 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
72cdf4c9 | 114 | return; |
16c1f79c | 115 | } |
72cdf4c9 VZ |
116 | |
117 | // iterate until the list becomes empty | |
118 | wxNode *node = wxPendingEvents->First(); | |
119 | while (node) | |
120 | { | |
121 | wxEvtHandler *handler = (wxEvtHandler *)node->Data(); | |
16c1f79c | 122 | delete node; |
72cdf4c9 | 123 | |
16c1f79c | 124 | // In ProcessPendingEvents(), new handlers might be add |
1d910ac1 | 125 | // and we can safely leave the critical section here. |
16c1f79c | 126 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 | 127 | handler->ProcessPendingEvents(); |
16c1f79c | 128 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 | 129 | |
72cdf4c9 VZ |
130 | node = wxPendingEvents->First(); |
131 | } | |
1d910ac1 | 132 | |
16c1f79c | 133 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 VZ |
134 | } |
135 | ||
1e6feb95 VZ |
136 | // ---------------------------------------------------------------------------- |
137 | // misc | |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
140 | #if wxUSE_GUI | |
141 | ||
6e169cf3 | 142 | void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus)) |
7beba2fc | 143 | { |
66dfed9b VZ |
144 | if ( active == m_isActive ) |
145 | return; | |
146 | ||
1e6feb95 | 147 | m_isActive = active; |
66dfed9b VZ |
148 | |
149 | wxActivateEvent event(wxEVT_ACTIVATE_APP, active); | |
150 | event.SetEventObject(this); | |
151 | ||
152 | (void)ProcessEvent(event); | |
7beba2fc | 153 | } |
1e6feb95 VZ |
154 | |
155 | #endif // wxUSE_GUI | |
bf188f1a VZ |
156 | |
157 | // ---------------------------------------------------------------------------- | |
158 | // cmd line parsing | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | bool wxAppBase::OnInit() | |
162 | { | |
163 | #if wxUSE_CMDLINE_PARSER | |
164 | wxCmdLineParser parser(argc, argv); | |
165 | ||
166 | OnInitCmdLine(parser); | |
167 | ||
168 | bool cont; | |
169 | switch ( parser.Parse() ) | |
170 | { | |
171 | case -1: | |
172 | cont = OnCmdLineHelp(parser); | |
173 | break; | |
174 | ||
175 | case 0: | |
176 | cont = OnCmdLineParsed(parser); | |
177 | break; | |
178 | ||
179 | default: | |
180 | cont = OnCmdLineError(parser); | |
181 | break; | |
182 | } | |
183 | ||
184 | if ( !cont ) | |
185 | return FALSE; | |
186 | #endif // wxUSE_CMDLINE_PARSER | |
187 | ||
188 | return TRUE; | |
189 | } | |
190 | ||
191 | #if wxUSE_CMDLINE_PARSER | |
192 | ||
193 | #define OPTION_VERBOSE _T("verbose") | |
194 | #define OPTION_THEME _T("theme") | |
195 | ||
196 | void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser) | |
197 | { | |
198 | // the standard command line options | |
199 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
200 | { | |
201 | { | |
202 | wxCMD_LINE_SWITCH, | |
203 | _T("h"), | |
204 | _T("help"), | |
205 | gettext_noop("show this help message"), | |
206 | wxCMD_LINE_VAL_NONE, | |
207 | wxCMD_LINE_OPTION_HELP | |
208 | }, | |
209 | ||
210 | #if wxUSE_LOG | |
211 | { | |
212 | wxCMD_LINE_SWITCH, | |
213 | _T(""), | |
214 | OPTION_VERBOSE, | |
215 | gettext_noop("generate verbose log messages") | |
216 | }, | |
0f02d3d0 | 217 | #endif // wxUSE_LOG |
bf188f1a VZ |
218 | |
219 | #ifdef __WXUNIVERSAL__ | |
220 | { | |
221 | wxCMD_LINE_OPTION, | |
222 | _T(""), | |
223 | OPTION_THEME, | |
224 | gettext_noop("specify the theme to use"), | |
225 | wxCMD_LINE_VAL_STRING | |
226 | }, | |
227 | #endif // __WXUNIVERSAL__ | |
228 | ||
229 | // terminator | |
230 | { wxCMD_LINE_NONE } | |
231 | }; | |
232 | ||
233 | parser.SetDesc(cmdLineDesc); | |
234 | } | |
235 | ||
236 | bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser) | |
237 | { | |
238 | #if wxUSE_LOG | |
239 | if ( parser.Found(OPTION_VERBOSE) ) | |
240 | { | |
241 | wxLog::SetVerbose(TRUE); | |
242 | } | |
243 | #endif // wxUSE_LOG | |
244 | ||
245 | #ifdef __WXUNIVERSAL__ | |
246 | wxString themeName; | |
247 | if ( parser.Found(OPTION_THEME, &themeName) ) | |
248 | { | |
249 | wxTheme *theme = wxTheme::Create(themeName); | |
250 | if ( !theme ) | |
251 | { | |
252 | wxLogError(_("Unsupported theme '%s'."), themeName.c_str()); | |
253 | ||
254 | return FALSE; | |
255 | } | |
256 | ||
257 | wxTheme::Set(theme); | |
258 | } | |
259 | #endif // __WXUNIVERSAL__ | |
260 | ||
261 | return TRUE; | |
262 | } | |
263 | ||
264 | bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser) | |
265 | { | |
266 | parser.Usage(); | |
267 | ||
268 | return FALSE; | |
269 | } | |
270 | ||
271 | bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser) | |
272 | { | |
273 | parser.Usage(); | |
274 | ||
275 | return FALSE; | |
276 | } | |
277 | ||
278 | #endif // wxUSE_CMDLINE_PARSER | |
279 | ||
a5f1fd3e VZ |
280 | // ---------------------------------------------------------------------------- |
281 | // debugging support | |
282 | // ---------------------------------------------------------------------------- | |
283 | ||
284 | #ifdef __WXDEBUG__ | |
285 | ||
286 | // wxASSERT() helper | |
287 | bool wxAssertIsEqual(int x, int y) | |
288 | { | |
289 | return x == y; | |
290 | } | |
291 | ||
292 | // break into the debugger | |
293 | void wxTrap() | |
294 | { | |
295 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
296 | DebugBreak(); | |
297 | #elif defined(__WXMAC__) | |
298 | #if __powerc | |
299 | Debugger(); | |
300 | #else | |
301 | SysBreak(); | |
302 | #endif | |
303 | #elif defined(__UNIX__) | |
304 | raise(SIGTRAP); | |
305 | #else | |
306 | // TODO | |
307 | #endif // Win/Unix | |
308 | } | |
309 | ||
310 | // show the assert modal dialog | |
311 | static | |
312 | void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg) | |
313 | { | |
314 | // this variable can be set to true to suppress "assert failure" messages | |
315 | static bool s_bNoAsserts = FALSE; | |
316 | static bool s_bInAssert = FALSE; // FIXME MT-unsafe | |
317 | ||
318 | if ( s_bInAssert ) | |
319 | { | |
320 | // He-e-e-e-elp!! we're trapped in endless loop | |
321 | wxTrap(); | |
322 | ||
323 | s_bInAssert = FALSE; | |
324 | ||
325 | return; | |
326 | } | |
327 | ||
328 | s_bInAssert = TRUE; | |
329 | ||
330 | wxChar szBuf[4096]; | |
331 | ||
332 | // make life easier for people using VC++ IDE: clicking on the message | |
333 | // will take us immediately to the place of the failed assert | |
334 | wxSnprintf(szBuf, WXSIZEOF(szBuf), | |
335 | #ifdef __VISUALC__ | |
336 | wxT("%s(%d): assert failed"), | |
337 | #else // !VC++ | |
338 | // make the error message more clear for all the others | |
339 | wxT("Assert failed in file %s at line %d"), | |
340 | #endif // VC/!VC | |
341 | szFile, nLine); | |
342 | ||
343 | if ( szMsg != NULL ) | |
344 | { | |
345 | wxStrcat(szBuf, wxT(": ")); | |
346 | wxStrcat(szBuf, szMsg); | |
347 | } | |
348 | else // no message given | |
349 | { | |
350 | wxStrcat(szBuf, wxT(".")); | |
351 | } | |
352 | ||
353 | if ( !s_bNoAsserts ) | |
354 | { | |
355 | // send it to the normal log destination | |
356 | wxLogDebug(szBuf); | |
357 | ||
358 | #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__) | |
359 | // this message is intentionally not translated - it is for | |
360 | // developpers only | |
361 | wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings.")); | |
362 | ||
363 | // use the native message box if available: this is more robust than | |
364 | // using our own | |
365 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
366 | switch ( ::MessageBox(NULL, szBuf, _T("Debug"), | |
367 | MB_YESNOCANCEL | MB_ICONSTOP ) ) | |
368 | { | |
369 | case IDYES: | |
370 | wxTrap(); | |
371 | break; | |
372 | ||
373 | case IDCANCEL: | |
374 | s_bNoAsserts = TRUE; | |
375 | break; | |
376 | ||
377 | //case IDNO: nothing to do | |
378 | } | |
379 | #else // !MSW | |
380 | switch ( wxMessageBox(szBuf, wxT("Debug"), | |
381 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) | |
382 | { | |
383 | case wxYES: | |
384 | wxTrap(); | |
385 | break; | |
386 | ||
387 | case wxCANCEL: | |
388 | s_bNoAsserts = TRUE; | |
389 | break; | |
390 | ||
391 | //case wxNO: nothing to do | |
392 | } | |
393 | #endif // GUI or MSW | |
394 | ||
395 | #else // !GUI | |
396 | wxTrap(); | |
397 | #endif // GUI/!GUI | |
398 | } | |
399 | ||
400 | s_bInAssert = FALSE; | |
401 | } | |
402 | ||
403 | // this function is called when an assert fails | |
404 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) | |
405 | { | |
406 | if ( !wxTheApp ) | |
407 | { | |
408 | // by default, show the assert dialog box - we can't customize this | |
409 | // behaviour | |
410 | ShowAssertDialog(szFile, nLine, szMsg); | |
411 | } | |
412 | else | |
413 | { | |
414 | // let the app process it as it wants | |
415 | wxTheApp->OnAssert(szFile, nLine, szMsg); | |
416 | } | |
417 | } | |
418 | ||
419 | void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg) | |
420 | { | |
421 | ShowAssertDialog(file, line, msg); | |
422 | } | |
423 | ||
424 | #endif //WXDEBUG | |
425 |