]>
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" |
697c5f51 | 43 | #include "wx/tokenzr.h" |
bebc39e3 | 44 | #include "wx/utils.h" |
8d38cdb7 VZ |
45 | |
46 | #if wxUSE_GUI | |
47 | #include "wx/artprov.h" | |
48 | #endif // wxUSE_GUI | |
e1ee679c | 49 | |
a5f1fd3e VZ |
50 | #if !defined(__WXMSW__) || defined(__WXMICROWIN__) |
51 | #include <signal.h> // for SIGTRAP used by wxTrap() | |
52 | #endif //Win/Unix | |
53 | ||
54 | #if defined(__WXMSW__) | |
55 | #include "wx/msw/private.h" // includes windows.h for MessageBox() | |
56 | #endif | |
57 | ||
5128e3be SC |
58 | #if defined(__WXMAC__) |
59 | #include "wx/mac/private.h" // includes mac headers | |
60 | #endif | |
61 | ||
090a6d7a VZ |
62 | // private functions prototypes |
63 | #ifdef __WXDEBUG__ | |
64 | static void LINKAGEMODE SetTraceMasks(); | |
65 | #endif // __WXDEBUG__ | |
66 | ||
d54598dd VZ |
67 | // =========================================================================== |
68 | // implementation | |
69 | // =========================================================================== | |
70 | ||
bf188f1a VZ |
71 | // ---------------------------------------------------------------------------- |
72 | // initialization and termination | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
090a6d7a | 75 | wxAppBase::wxAppBase() |
697c5f51 | 76 | { |
090a6d7a VZ |
77 | // this function is defined by IMPLEMENT_APP() macro in the user code |
78 | extern const wxBuildOptions& wxGetBuildOptions(); | |
79 | ||
80 | if ( !CheckBuildOptions(wxGetBuildOptions()) ) | |
697c5f51 | 81 | { |
090a6d7a VZ |
82 | wxLogFatalError(_T("Mismatch between the program and library build ") |
83 | _T("versions detected.")); | |
697c5f51 | 84 | } |
697c5f51 | 85 | |
1e6feb95 VZ |
86 | wxTheApp = (wxApp *)this; |
87 | ||
73deed44 | 88 | #if WXWIN_COMPATIBILITY_2_2 |
1e6feb95 | 89 | m_wantDebugOutput = FALSE; |
73deed44 | 90 | #endif // WXWIN_COMPATIBILITY_2_2 |
1e6feb95 VZ |
91 | |
92 | #if wxUSE_GUI | |
93 | m_topWindow = (wxWindow *)NULL; | |
94 | m_useBestVisual = FALSE; | |
95 | m_exitOnFrameDelete = TRUE; | |
96 | m_isActive = TRUE; | |
97 | #endif // wxUSE_GUI | |
697c5f51 VS |
98 | |
99 | #ifdef __WXDEBUG__ | |
100 | SetTraceMasks(); | |
101 | #endif | |
1e6feb95 VZ |
102 | } |
103 | ||
799ea011 GD |
104 | wxAppBase::~wxAppBase() |
105 | { | |
106 | // this destructor is required for Darwin | |
107 | } | |
108 | ||
1e6feb95 VZ |
109 | #if wxUSE_GUI |
110 | bool wxAppBase::OnInitGui() | |
111 | { | |
112 | #ifdef __WXUNIVERSAL__ | |
bf188f1a | 113 | if ( !wxTheme::Get() && !wxTheme::CreateDefault() ) |
1e6feb95 | 114 | return FALSE; |
536b70ac VS |
115 | wxArtProvider *art = wxTheme::Get()->GetArtProvider(); |
116 | if ( art ) | |
117 | wxArtProvider::PushProvider(art); | |
1e6feb95 VZ |
118 | #endif // __WXUNIVERSAL__ |
119 | ||
120 | return TRUE; | |
121 | } | |
122 | #endif // wxUSE_GUI | |
123 | ||
124 | int wxAppBase::OnExit() | |
125 | { | |
126 | #if wxUSE_CONFIG | |
127 | // delete the config object if any (don't use Get() here, but Set() | |
128 | // because Get() could create a new config object) | |
129 | delete wxConfigBase::Set((wxConfigBase *) NULL); | |
130 | #endif // wxUSE_CONFIG | |
131 | ||
132 | #ifdef __WXUNIVERSAL__ | |
133 | delete wxTheme::Set(NULL); | |
134 | #endif // __WXUNIVERSAL__ | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
72cdf4c9 VZ |
139 | // --------------------------------------------------------------------------- |
140 | // wxAppBase | |
141 | // ---------------------------------------------------------------------------- | |
142 | ||
143 | void wxAppBase::ProcessPendingEvents() | |
144 | { | |
145 | // ensure that we're the only thread to modify the pending events list | |
16c1f79c | 146 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 VZ |
147 | |
148 | if ( !wxPendingEvents ) | |
16c1f79c RR |
149 | { |
150 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
72cdf4c9 | 151 | return; |
16c1f79c | 152 | } |
72cdf4c9 VZ |
153 | |
154 | // iterate until the list becomes empty | |
155 | wxNode *node = wxPendingEvents->First(); | |
156 | while (node) | |
157 | { | |
158 | wxEvtHandler *handler = (wxEvtHandler *)node->Data(); | |
16c1f79c | 159 | delete node; |
72cdf4c9 | 160 | |
16c1f79c | 161 | // In ProcessPendingEvents(), new handlers might be add |
1d910ac1 | 162 | // and we can safely leave the critical section here. |
16c1f79c | 163 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 | 164 | handler->ProcessPendingEvents(); |
16c1f79c | 165 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 | 166 | |
72cdf4c9 VZ |
167 | node = wxPendingEvents->First(); |
168 | } | |
1d910ac1 | 169 | |
16c1f79c | 170 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); |
72cdf4c9 VZ |
171 | } |
172 | ||
1e6feb95 VZ |
173 | // ---------------------------------------------------------------------------- |
174 | // misc | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | #if wxUSE_GUI | |
178 | ||
6e169cf3 | 179 | void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus)) |
7beba2fc | 180 | { |
66dfed9b VZ |
181 | if ( active == m_isActive ) |
182 | return; | |
183 | ||
1e6feb95 | 184 | m_isActive = active; |
66dfed9b VZ |
185 | |
186 | wxActivateEvent event(wxEVT_ACTIVATE_APP, active); | |
187 | event.SetEventObject(this); | |
188 | ||
189 | (void)ProcessEvent(event); | |
7beba2fc | 190 | } |
1e6feb95 VZ |
191 | |
192 | #endif // wxUSE_GUI | |
bf188f1a VZ |
193 | |
194 | // ---------------------------------------------------------------------------- | |
195 | // cmd line parsing | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | bool wxAppBase::OnInit() | |
199 | { | |
200 | #if wxUSE_CMDLINE_PARSER | |
201 | wxCmdLineParser parser(argc, argv); | |
202 | ||
203 | OnInitCmdLine(parser); | |
204 | ||
205 | bool cont; | |
be03c0ec | 206 | switch ( parser.Parse(FALSE /* don't show usage */) ) |
bf188f1a VZ |
207 | { |
208 | case -1: | |
209 | cont = OnCmdLineHelp(parser); | |
210 | break; | |
211 | ||
212 | case 0: | |
213 | cont = OnCmdLineParsed(parser); | |
214 | break; | |
215 | ||
216 | default: | |
217 | cont = OnCmdLineError(parser); | |
218 | break; | |
219 | } | |
220 | ||
221 | if ( !cont ) | |
222 | return FALSE; | |
223 | #endif // wxUSE_CMDLINE_PARSER | |
224 | ||
225 | return TRUE; | |
226 | } | |
227 | ||
228 | #if wxUSE_CMDLINE_PARSER | |
229 | ||
230 | #define OPTION_VERBOSE _T("verbose") | |
231 | #define OPTION_THEME _T("theme") | |
c358c660 | 232 | #define OPTION_MODE _T("mode") |
bf188f1a VZ |
233 | |
234 | void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser) | |
235 | { | |
236 | // the standard command line options | |
237 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
238 | { | |
239 | { | |
240 | wxCMD_LINE_SWITCH, | |
241 | _T("h"), | |
242 | _T("help"), | |
243 | gettext_noop("show this help message"), | |
244 | wxCMD_LINE_VAL_NONE, | |
245 | wxCMD_LINE_OPTION_HELP | |
246 | }, | |
247 | ||
248 | #if wxUSE_LOG | |
249 | { | |
250 | wxCMD_LINE_SWITCH, | |
251 | _T(""), | |
252 | OPTION_VERBOSE, | |
ba161d7e GD |
253 | gettext_noop("generate verbose log messages"), |
254 | wxCMD_LINE_VAL_NONE, | |
255 | 0x0 | |
bf188f1a | 256 | }, |
0f02d3d0 | 257 | #endif // wxUSE_LOG |
bf188f1a VZ |
258 | |
259 | #ifdef __WXUNIVERSAL__ | |
260 | { | |
261 | wxCMD_LINE_OPTION, | |
262 | _T(""), | |
263 | OPTION_THEME, | |
264 | gettext_noop("specify the theme to use"), | |
ba161d7e GD |
265 | wxCMD_LINE_VAL_STRING, |
266 | 0x0 | |
bf188f1a VZ |
267 | }, |
268 | #endif // __WXUNIVERSAL__ | |
269 | ||
c358c660 VS |
270 | #if defined(__WXMGL__) |
271 | // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports | |
272 | // should provide this option. That's why it is in common/appcmn.cpp | |
273 | // and not mgl/app.cpp | |
274 | { | |
275 | wxCMD_LINE_OPTION, | |
276 | _T(""), | |
277 | OPTION_MODE, | |
278 | gettext_noop("specify display mode to use (e.g. 640x480-16)"), | |
ba161d7e GD |
279 | wxCMD_LINE_VAL_STRING, |
280 | 0x0 | |
c358c660 VS |
281 | }, |
282 | #endif // __WXMGL__ | |
283 | ||
bf188f1a | 284 | // terminator |
ba161d7e GD |
285 | { |
286 | wxCMD_LINE_NONE, | |
287 | _T(""), | |
288 | _T(""), | |
289 | _T(""), | |
290 | wxCMD_LINE_VAL_NONE, | |
291 | 0x0 | |
292 | } | |
bf188f1a VZ |
293 | }; |
294 | ||
295 | parser.SetDesc(cmdLineDesc); | |
296 | } | |
297 | ||
298 | bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser) | |
299 | { | |
300 | #if wxUSE_LOG | |
301 | if ( parser.Found(OPTION_VERBOSE) ) | |
302 | { | |
303 | wxLog::SetVerbose(TRUE); | |
304 | } | |
305 | #endif // wxUSE_LOG | |
306 | ||
307 | #ifdef __WXUNIVERSAL__ | |
308 | wxString themeName; | |
309 | if ( parser.Found(OPTION_THEME, &themeName) ) | |
310 | { | |
311 | wxTheme *theme = wxTheme::Create(themeName); | |
312 | if ( !theme ) | |
313 | { | |
314 | wxLogError(_("Unsupported theme '%s'."), themeName.c_str()); | |
315 | ||
316 | return FALSE; | |
317 | } | |
318 | ||
319 | wxTheme::Set(theme); | |
320 | } | |
321 | #endif // __WXUNIVERSAL__ | |
322 | ||
c358c660 VS |
323 | #if defined(__WXMGL__) |
324 | wxString modeDesc; | |
325 | if ( parser.Found(OPTION_MODE, &modeDesc) ) | |
326 | { | |
327 | unsigned w, h, bpp; | |
328 | if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 ) | |
329 | { | |
49e885f8 | 330 | wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str()); |
c358c660 VS |
331 | |
332 | return FALSE; | |
333 | } | |
334 | ||
07082b28 | 335 | if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) ) |
49e885f8 | 336 | return FALSE; |
c358c660 | 337 | } |
be03c0ec | 338 | #endif // __WXMGL__ |
c358c660 | 339 | |
bf188f1a VZ |
340 | return TRUE; |
341 | } | |
342 | ||
343 | bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser) | |
344 | { | |
345 | parser.Usage(); | |
346 | ||
347 | return FALSE; | |
348 | } | |
349 | ||
350 | bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser) | |
351 | { | |
352 | parser.Usage(); | |
353 | ||
354 | return FALSE; | |
355 | } | |
356 | ||
357 | #endif // wxUSE_CMDLINE_PARSER | |
358 | ||
a5f1fd3e VZ |
359 | // ---------------------------------------------------------------------------- |
360 | // debugging support | |
361 | // ---------------------------------------------------------------------------- | |
362 | ||
090a6d7a VZ |
363 | /* static */ |
364 | bool wxAppBase::CheckBuildOptions(const wxBuildOptions& opts) | |
365 | { | |
366 | #define wxCMP(what) (what == opts.m_ ## what) | |
367 | ||
368 | bool | |
369 | #ifdef __WXDEBUG__ | |
370 | isDebug = TRUE; | |
371 | #else | |
372 | isDebug = FALSE; | |
373 | #endif | |
374 | ||
375 | int verMaj = wxMAJOR_VERSION, | |
376 | verMin = wxMINOR_VERSION; | |
377 | ||
378 | return wxCMP(isDebug) && wxCMP(verMaj) && wxCMP(verMin); | |
379 | ||
380 | #undef wxCMP | |
381 | } | |
382 | ||
a5f1fd3e VZ |
383 | #ifdef __WXDEBUG__ |
384 | ||
090a6d7a VZ |
385 | static void LINKAGEMODE SetTraceMasks() |
386 | { | |
387 | wxString mask; | |
388 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
389 | { | |
390 | wxStringTokenizer tkn(mask, wxT(",")); | |
391 | while ( tkn.HasMoreTokens() ) | |
392 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
393 | } | |
394 | } | |
395 | ||
a5f1fd3e VZ |
396 | // wxASSERT() helper |
397 | bool wxAssertIsEqual(int x, int y) | |
398 | { | |
399 | return x == y; | |
400 | } | |
401 | ||
402 | // break into the debugger | |
403 | void wxTrap() | |
404 | { | |
405 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
406 | DebugBreak(); | |
c31ad41d | 407 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
a5f1fd3e VZ |
408 | #if __powerc |
409 | Debugger(); | |
410 | #else | |
411 | SysBreak(); | |
412 | #endif | |
413 | #elif defined(__UNIX__) | |
414 | raise(SIGTRAP); | |
415 | #else | |
416 | // TODO | |
417 | #endif // Win/Unix | |
418 | } | |
419 | ||
420 | // show the assert modal dialog | |
421 | static | |
422 | void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg) | |
423 | { | |
424 | // this variable can be set to true to suppress "assert failure" messages | |
425 | static bool s_bNoAsserts = FALSE; | |
a5f1fd3e VZ |
426 | |
427 | wxChar szBuf[4096]; | |
428 | ||
429 | // make life easier for people using VC++ IDE: clicking on the message | |
430 | // will take us immediately to the place of the failed assert | |
431 | wxSnprintf(szBuf, WXSIZEOF(szBuf), | |
432 | #ifdef __VISUALC__ | |
433 | wxT("%s(%d): assert failed"), | |
434 | #else // !VC++ | |
435 | // make the error message more clear for all the others | |
436 | wxT("Assert failed in file %s at line %d"), | |
437 | #endif // VC/!VC | |
438 | szFile, nLine); | |
439 | ||
440 | if ( szMsg != NULL ) | |
441 | { | |
442 | wxStrcat(szBuf, wxT(": ")); | |
443 | wxStrcat(szBuf, szMsg); | |
444 | } | |
445 | else // no message given | |
446 | { | |
447 | wxStrcat(szBuf, wxT(".")); | |
448 | } | |
449 | ||
450 | if ( !s_bNoAsserts ) | |
451 | { | |
452 | // send it to the normal log destination | |
453 | wxLogDebug(szBuf); | |
454 | ||
455 | #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__) | |
456 | // this message is intentionally not translated - it is for | |
457 | // developpers only | |
458 | wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings.")); | |
459 | ||
460 | // use the native message box if available: this is more robust than | |
461 | // using our own | |
462 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
463 | switch ( ::MessageBox(NULL, szBuf, _T("Debug"), | |
464 | MB_YESNOCANCEL | MB_ICONSTOP ) ) | |
465 | { | |
466 | case IDYES: | |
467 | wxTrap(); | |
468 | break; | |
469 | ||
470 | case IDCANCEL: | |
471 | s_bNoAsserts = TRUE; | |
472 | break; | |
473 | ||
474 | //case IDNO: nothing to do | |
475 | } | |
476 | #else // !MSW | |
477 | switch ( wxMessageBox(szBuf, wxT("Debug"), | |
478 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) | |
479 | { | |
480 | case wxYES: | |
481 | wxTrap(); | |
482 | break; | |
483 | ||
484 | case wxCANCEL: | |
485 | s_bNoAsserts = TRUE; | |
486 | break; | |
487 | ||
488 | //case wxNO: nothing to do | |
489 | } | |
490 | #endif // GUI or MSW | |
491 | ||
492 | #else // !GUI | |
493 | wxTrap(); | |
494 | #endif // GUI/!GUI | |
495 | } | |
a5f1fd3e VZ |
496 | } |
497 | ||
498 | // this function is called when an assert fails | |
499 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) | |
500 | { | |
76456676 VZ |
501 | // FIXME MT-unsafe |
502 | static bool s_bInAssert = FALSE; | |
503 | ||
504 | if ( s_bInAssert ) | |
505 | { | |
506 | // He-e-e-e-elp!! we're trapped in endless loop | |
507 | wxTrap(); | |
508 | ||
509 | s_bInAssert = FALSE; | |
510 | ||
511 | return; | |
512 | } | |
513 | ||
514 | s_bInAssert = TRUE; | |
515 | ||
a5f1fd3e VZ |
516 | if ( !wxTheApp ) |
517 | { | |
518 | // by default, show the assert dialog box - we can't customize this | |
519 | // behaviour | |
520 | ShowAssertDialog(szFile, nLine, szMsg); | |
521 | } | |
522 | else | |
523 | { | |
524 | // let the app process it as it wants | |
525 | wxTheApp->OnAssert(szFile, nLine, szMsg); | |
526 | } | |
76456676 VZ |
527 | |
528 | s_bInAssert = FALSE; | |
a5f1fd3e VZ |
529 | } |
530 | ||
531 | void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg) | |
532 | { | |
533 | ShowAssertDialog(file, line, msg); | |
534 | } | |
535 | ||
536 | #endif //WXDEBUG | |
537 |