]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/appbase.cpp | |
3 | // Purpose: implements wxAppConsoleBase class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 19.06.2003 (extracted from common/appcmn.cpp) | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // License: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #ifdef __WXMSW__ | |
29 | #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox() | |
30 | #endif | |
31 | #include "wx/list.h" | |
32 | #include "wx/app.h" | |
33 | #include "wx/intl.h" | |
34 | #include "wx/log.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/wxcrtvararg.h" | |
37 | #endif //WX_PRECOMP | |
38 | ||
39 | #include "wx/apptrait.h" | |
40 | #include "wx/cmdline.h" | |
41 | #include "wx/confbase.h" | |
42 | #include "wx/evtloop.h" | |
43 | #include "wx/filename.h" | |
44 | #include "wx/msgout.h" | |
45 | #include "wx/ptr_scpd.h" | |
46 | #include "wx/tokenzr.h" | |
47 | #include "wx/thread.h" | |
48 | ||
49 | #if wxUSE_EXCEPTIONS && wxUSE_STL | |
50 | #include <exception> | |
51 | #include <typeinfo> | |
52 | #endif | |
53 | ||
54 | #ifndef __WXPALMOS5__ | |
55 | #if !defined(__WXMSW__) || defined(__WXMICROWIN__) | |
56 | #include <signal.h> // for SIGTRAP used by wxTrap() | |
57 | #endif //Win/Unix | |
58 | ||
59 | #include <locale.h> | |
60 | #endif // ! __WXPALMOS5__ | |
61 | ||
62 | #if wxUSE_FONTMAP | |
63 | #include "wx/fontmap.h" | |
64 | #endif // wxUSE_FONTMAP | |
65 | ||
66 | #ifdef __WXDEBUG__ | |
67 | #if wxUSE_STACKWALKER | |
68 | #include "wx/stackwalk.h" | |
69 | #ifdef __WXMSW__ | |
70 | #include "wx/msw/debughlp.h" | |
71 | #endif | |
72 | #endif // wxUSE_STACKWALKER | |
73 | ||
74 | #include "wx/recguard.h" | |
75 | #endif // __WXDEBUG__ | |
76 | ||
77 | // wxABI_VERSION can be defined when compiling applications but it should be | |
78 | // left undefined when compiling the library itself, it is then set to its | |
79 | // default value in version.h | |
80 | #if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99 | |
81 | #error "wxABI_VERSION should not be defined when compiling the library" | |
82 | #endif | |
83 | ||
84 | // ---------------------------------------------------------------------------- | |
85 | // private functions prototypes | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | #ifdef __WXDEBUG__ | |
89 | // really just show the assert dialog | |
90 | static bool DoShowAssertDialog(const wxString& msg); | |
91 | ||
92 | // prepare for showing the assert dialog, use the given traits or | |
93 | // DoShowAssertDialog() as last fallback to really show it | |
94 | static | |
95 | void ShowAssertDialog(const wxString& szFile, | |
96 | int nLine, | |
97 | const wxString& szFunc, | |
98 | const wxString& szCond, | |
99 | const wxString& szMsg, | |
100 | wxAppTraits *traits = NULL); | |
101 | ||
102 | // turn on the trace masks specified in the env variable WXTRACE | |
103 | static void LINKAGEMODE SetTraceMasks(); | |
104 | #endif // __WXDEBUG__ | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // global vars | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | wxAppConsole *wxAppConsoleBase::ms_appInstance = NULL; | |
111 | ||
112 | wxAppInitializerFunction wxAppConsoleBase::ms_appInitFn = NULL; | |
113 | ||
114 | wxSocketManager *wxAppTraitsBase::ms_manager = NULL; | |
115 | ||
116 | // ---------------------------------------------------------------------------- | |
117 | // wxEventLoopPtr | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
120 | // this defines wxEventLoopPtr | |
121 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase) | |
122 | ||
123 | // ============================================================================ | |
124 | // wxAppConsoleBase implementation | |
125 | // ============================================================================ | |
126 | ||
127 | // ---------------------------------------------------------------------------- | |
128 | // ctor/dtor | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
131 | wxAppConsoleBase::wxAppConsoleBase() | |
132 | { | |
133 | m_traits = NULL; | |
134 | m_mainLoop = NULL; | |
135 | ||
136 | ms_appInstance = static_cast<wxAppConsole *>(this); | |
137 | ||
138 | #ifdef __WXDEBUG__ | |
139 | SetTraceMasks(); | |
140 | #if wxUSE_UNICODE | |
141 | // In unicode mode the SetTraceMasks call can cause an apptraits to be | |
142 | // created, but since we are still in the constructor the wrong kind will | |
143 | // be created for GUI apps. Destroy it so it can be created again later. | |
144 | delete m_traits; | |
145 | m_traits = NULL; | |
146 | #endif | |
147 | #endif | |
148 | } | |
149 | ||
150 | wxAppConsoleBase::~wxAppConsoleBase() | |
151 | { | |
152 | delete m_traits; | |
153 | } | |
154 | ||
155 | // ---------------------------------------------------------------------------- | |
156 | // initilization/cleanup | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
159 | bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc), wxChar **argv) | |
160 | { | |
161 | #if wxUSE_INTL | |
162 | GetTraits()->SetLocale(); | |
163 | #endif // wxUSE_INTL | |
164 | ||
165 | #if wxUSE_THREADS | |
166 | wxPendingEventsLocker = new wxCriticalSection; | |
167 | #endif | |
168 | ||
169 | #ifndef __WXPALMOS__ | |
170 | if ( m_appName.empty() && argv && argv[0] ) | |
171 | { | |
172 | // the application name is, by default, the name of its executable file | |
173 | wxFileName::SplitPath(argv[0], NULL, &m_appName, NULL); | |
174 | } | |
175 | #endif // !__WXPALMOS__ | |
176 | ||
177 | return true; | |
178 | } | |
179 | ||
180 | wxEventLoopBase *wxAppConsoleBase::CreateMainLoop() | |
181 | { | |
182 | return GetTraits()->CreateEventLoop(); | |
183 | } | |
184 | ||
185 | void wxAppConsoleBase::CleanUp() | |
186 | { | |
187 | if ( m_mainLoop ) | |
188 | { | |
189 | delete m_mainLoop; | |
190 | m_mainLoop = NULL; | |
191 | } | |
192 | ||
193 | delete wxPendingEvents; | |
194 | wxPendingEvents = NULL; | |
195 | ||
196 | #if wxUSE_THREADS | |
197 | delete wxPendingEventsLocker; | |
198 | wxPendingEventsLocker = NULL; | |
199 | #endif // wxUSE_THREADS | |
200 | } | |
201 | ||
202 | // ---------------------------------------------------------------------------- | |
203 | // OnXXX() callbacks | |
204 | // ---------------------------------------------------------------------------- | |
205 | ||
206 | bool wxAppConsoleBase::OnInit() | |
207 | { | |
208 | #if wxUSE_CMDLINE_PARSER | |
209 | wxCmdLineParser parser(argc, argv); | |
210 | ||
211 | OnInitCmdLine(parser); | |
212 | ||
213 | bool cont; | |
214 | switch ( parser.Parse(false /* don't show usage */) ) | |
215 | { | |
216 | case -1: | |
217 | cont = OnCmdLineHelp(parser); | |
218 | break; | |
219 | ||
220 | case 0: | |
221 | cont = OnCmdLineParsed(parser); | |
222 | break; | |
223 | ||
224 | default: | |
225 | cont = OnCmdLineError(parser); | |
226 | break; | |
227 | } | |
228 | ||
229 | if ( !cont ) | |
230 | return false; | |
231 | #endif // wxUSE_CMDLINE_PARSER | |
232 | ||
233 | return true; | |
234 | } | |
235 | ||
236 | int wxAppConsoleBase::OnRun() | |
237 | { | |
238 | return MainLoop(); | |
239 | } | |
240 | ||
241 | int wxAppConsoleBase::OnExit() | |
242 | { | |
243 | #if wxUSE_CONFIG | |
244 | // delete the config object if any (don't use Get() here, but Set() | |
245 | // because Get() could create a new config object) | |
246 | delete wxConfigBase::Set((wxConfigBase *) NULL); | |
247 | #endif // wxUSE_CONFIG | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | void wxAppConsoleBase::Exit() | |
253 | { | |
254 | if (m_mainLoop != NULL) | |
255 | ExitMainLoop(); | |
256 | else | |
257 | exit(-1); | |
258 | } | |
259 | ||
260 | // ---------------------------------------------------------------------------- | |
261 | // traits stuff | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
264 | wxAppTraits *wxAppConsoleBase::CreateTraits() | |
265 | { | |
266 | return new wxConsoleAppTraits; | |
267 | } | |
268 | ||
269 | wxAppTraits *wxAppConsoleBase::GetTraits() | |
270 | { | |
271 | // FIXME-MT: protect this with a CS? | |
272 | if ( !m_traits ) | |
273 | { | |
274 | m_traits = CreateTraits(); | |
275 | ||
276 | wxASSERT_MSG( m_traits, _T("wxApp::CreateTraits() failed?") ); | |
277 | } | |
278 | ||
279 | return m_traits; | |
280 | } | |
281 | ||
282 | /* static */ | |
283 | wxAppTraits *wxAppConsoleBase::GetTraitsIfExists() | |
284 | { | |
285 | wxAppConsole * const app = GetInstance(); | |
286 | return app ? app->GetTraits() : NULL; | |
287 | } | |
288 | ||
289 | // ---------------------------------------------------------------------------- | |
290 | // event processing | |
291 | // ---------------------------------------------------------------------------- | |
292 | ||
293 | int wxAppConsoleBase::MainLoop() | |
294 | { | |
295 | wxEventLoopBaseTiedPtr mainLoop(&m_mainLoop, CreateMainLoop()); | |
296 | ||
297 | return m_mainLoop ? m_mainLoop->Run() : -1; | |
298 | } | |
299 | ||
300 | void wxAppConsoleBase::ExitMainLoop() | |
301 | { | |
302 | // we should exit from the main event loop, not just any currently active | |
303 | // (e.g. modal dialog) event loop | |
304 | if ( m_mainLoop && m_mainLoop->IsRunning() ) | |
305 | { | |
306 | m_mainLoop->Exit(0); | |
307 | } | |
308 | } | |
309 | ||
310 | bool wxAppConsoleBase::Pending() | |
311 | { | |
312 | // use the currently active message loop here, not m_mainLoop, because if | |
313 | // we're showing a modal dialog (with its own event loop) currently the | |
314 | // main event loop is not running anyhow | |
315 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
316 | ||
317 | return loop && loop->Pending(); | |
318 | } | |
319 | ||
320 | bool wxAppConsoleBase::Dispatch() | |
321 | { | |
322 | // see comment in Pending() | |
323 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
324 | ||
325 | return loop && loop->Dispatch(); | |
326 | } | |
327 | ||
328 | bool wxAppConsoleBase::HasPendingEvents() const | |
329 | { | |
330 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); | |
331 | ||
332 | bool has = wxPendingEvents && !wxPendingEvents->IsEmpty(); | |
333 | ||
334 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
335 | ||
336 | return has; | |
337 | } | |
338 | ||
339 | /* static */ | |
340 | bool wxAppConsoleBase::IsMainLoopRunning() | |
341 | { | |
342 | const wxAppConsole * const app = GetInstance(); | |
343 | ||
344 | return app && app->m_mainLoop != NULL; | |
345 | } | |
346 | ||
347 | void wxAppConsoleBase::ProcessPendingEvents() | |
348 | { | |
349 | #if wxUSE_THREADS | |
350 | if ( !wxPendingEventsLocker ) | |
351 | return; | |
352 | #endif | |
353 | ||
354 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); | |
355 | ||
356 | if (wxPendingEvents) | |
357 | { | |
358 | // iterate until the list becomes empty: the handlers remove themselves | |
359 | // from it when they don't have any more pending events | |
360 | wxList::compatibility_iterator node = wxPendingEvents->GetFirst(); | |
361 | while (node) | |
362 | { | |
363 | // In ProcessPendingEvents(), new handlers might be add | |
364 | // and we can safely leave the critical section here. | |
365 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
366 | ||
367 | wxEvtHandler *handler = (wxEvtHandler *)node->GetData(); | |
368 | handler->ProcessPendingEvents(); | |
369 | ||
370 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); | |
371 | ||
372 | // restart as the iterators could have been invalidated | |
373 | node = wxPendingEvents->GetFirst(); | |
374 | } | |
375 | } | |
376 | ||
377 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
378 | } | |
379 | ||
380 | void wxAppConsoleBase::WakeUpIdle() | |
381 | { | |
382 | if ( m_mainLoop ) | |
383 | m_mainLoop->WakeUp(); | |
384 | } | |
385 | ||
386 | bool wxAppConsoleBase::ProcessIdle() | |
387 | { | |
388 | wxIdleEvent event; | |
389 | ||
390 | event.SetEventObject(this); | |
391 | ProcessEvent(event); | |
392 | return event.MoreRequested(); | |
393 | } | |
394 | ||
395 | int wxAppConsoleBase::FilterEvent(wxEvent& WXUNUSED(event)) | |
396 | { | |
397 | // process the events normally by default | |
398 | return -1; | |
399 | } | |
400 | ||
401 | // ---------------------------------------------------------------------------- | |
402 | // exception handling | |
403 | // ---------------------------------------------------------------------------- | |
404 | ||
405 | #if wxUSE_EXCEPTIONS | |
406 | ||
407 | void | |
408 | wxAppConsoleBase::HandleEvent(wxEvtHandler *handler, | |
409 | wxEventFunction func, | |
410 | wxEvent& event) const | |
411 | { | |
412 | // by default, simply call the handler | |
413 | (handler->*func)(event); | |
414 | } | |
415 | ||
416 | void wxAppConsoleBase::OnUnhandledException() | |
417 | { | |
418 | #ifdef __WXDEBUG__ | |
419 | // we're called from an exception handler so we can re-throw the exception | |
420 | // to recover its type | |
421 | wxString what; | |
422 | try | |
423 | { | |
424 | throw; | |
425 | } | |
426 | #if wxUSE_STL | |
427 | catch ( std::exception& e ) | |
428 | { | |
429 | what.Printf("std::exception of type \"%s\", what() = \"%s\"", | |
430 | typeid(e).name(), e.what()); | |
431 | } | |
432 | #endif // wxUSE_STL | |
433 | catch ( ... ) | |
434 | { | |
435 | what = "unknown exception"; | |
436 | } | |
437 | ||
438 | wxMessageOutputBest().Printf( | |
439 | "*** Caught unhandled %s; terminating\n", what | |
440 | ); | |
441 | #endif // __WXDEBUG__ | |
442 | } | |
443 | ||
444 | // ---------------------------------------------------------------------------- | |
445 | // exceptions support | |
446 | // ---------------------------------------------------------------------------- | |
447 | ||
448 | bool wxAppConsoleBase::OnExceptionInMainLoop() | |
449 | { | |
450 | throw; | |
451 | ||
452 | // some compilers are too stupid to know that we never return after throw | |
453 | #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200) | |
454 | return false; | |
455 | #endif | |
456 | } | |
457 | ||
458 | #endif // wxUSE_EXCEPTIONS | |
459 | ||
460 | // ---------------------------------------------------------------------------- | |
461 | // cmd line parsing | |
462 | // ---------------------------------------------------------------------------- | |
463 | ||
464 | #if wxUSE_CMDLINE_PARSER | |
465 | ||
466 | #define OPTION_VERBOSE "verbose" | |
467 | ||
468 | void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser& parser) | |
469 | { | |
470 | // the standard command line options | |
471 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
472 | { | |
473 | { | |
474 | wxCMD_LINE_SWITCH, | |
475 | "h", | |
476 | "help", | |
477 | gettext_noop("show this help message"), | |
478 | wxCMD_LINE_VAL_NONE, | |
479 | wxCMD_LINE_OPTION_HELP | |
480 | }, | |
481 | ||
482 | #if wxUSE_LOG | |
483 | { | |
484 | wxCMD_LINE_SWITCH, | |
485 | NULL, | |
486 | OPTION_VERBOSE, | |
487 | gettext_noop("generate verbose log messages"), | |
488 | wxCMD_LINE_VAL_NONE, | |
489 | 0x0 | |
490 | }, | |
491 | #endif // wxUSE_LOG | |
492 | ||
493 | // terminator | |
494 | wxCMD_LINE_DESC_END | |
495 | }; | |
496 | ||
497 | parser.SetDesc(cmdLineDesc); | |
498 | } | |
499 | ||
500 | bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser& parser) | |
501 | { | |
502 | #if wxUSE_LOG | |
503 | if ( parser.Found(OPTION_VERBOSE) ) | |
504 | { | |
505 | wxLog::SetVerbose(true); | |
506 | } | |
507 | #else | |
508 | wxUnusedVar(parser); | |
509 | #endif // wxUSE_LOG | |
510 | ||
511 | return true; | |
512 | } | |
513 | ||
514 | bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser& parser) | |
515 | { | |
516 | parser.Usage(); | |
517 | ||
518 | return false; | |
519 | } | |
520 | ||
521 | bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser& parser) | |
522 | { | |
523 | parser.Usage(); | |
524 | ||
525 | return false; | |
526 | } | |
527 | ||
528 | #endif // wxUSE_CMDLINE_PARSER | |
529 | ||
530 | // ---------------------------------------------------------------------------- | |
531 | // debugging support | |
532 | // ---------------------------------------------------------------------------- | |
533 | ||
534 | /* static */ | |
535 | bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature, | |
536 | const char *componentName) | |
537 | { | |
538 | #if 0 // can't use wxLogTrace, not up and running yet | |
539 | printf("checking build options object '%s' (ptr %p) in '%s'\n", | |
540 | optionsSignature, optionsSignature, componentName); | |
541 | #endif | |
542 | ||
543 | if ( strcmp(optionsSignature, WX_BUILD_OPTIONS_SIGNATURE) != 0 ) | |
544 | { | |
545 | wxString lib = wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE); | |
546 | wxString prog = wxString::FromAscii(optionsSignature); | |
547 | wxString progName = wxString::FromAscii(componentName); | |
548 | wxString msg; | |
549 | ||
550 | msg.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."), | |
551 | lib.c_str(), progName.c_str(), prog.c_str()); | |
552 | ||
553 | wxLogFatalError(msg.c_str()); | |
554 | ||
555 | // normally wxLogFatalError doesn't return | |
556 | return false; | |
557 | } | |
558 | #undef wxCMP | |
559 | ||
560 | return true; | |
561 | } | |
562 | ||
563 | #ifdef __WXDEBUG__ | |
564 | ||
565 | void wxAppConsoleBase::OnAssertFailure(const wxChar *file, | |
566 | int line, | |
567 | const wxChar *func, | |
568 | const wxChar *cond, | |
569 | const wxChar *msg) | |
570 | { | |
571 | ShowAssertDialog(file, line, func, cond, msg, GetTraits()); | |
572 | } | |
573 | ||
574 | void wxAppConsoleBase::OnAssert(const wxChar *file, | |
575 | int line, | |
576 | const wxChar *cond, | |
577 | const wxChar *msg) | |
578 | { | |
579 | OnAssertFailure(file, line, NULL, cond, msg); | |
580 | } | |
581 | ||
582 | #endif // __WXDEBUG__ | |
583 | ||
584 | // ============================================================================ | |
585 | // other classes implementations | |
586 | // ============================================================================ | |
587 | ||
588 | // ---------------------------------------------------------------------------- | |
589 | // wxConsoleAppTraitsBase | |
590 | // ---------------------------------------------------------------------------- | |
591 | ||
592 | #if wxUSE_LOG | |
593 | ||
594 | wxLog *wxConsoleAppTraitsBase::CreateLogTarget() | |
595 | { | |
596 | return new wxLogStderr; | |
597 | } | |
598 | ||
599 | #endif // wxUSE_LOG | |
600 | ||
601 | wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput() | |
602 | { | |
603 | return new wxMessageOutputStderr; | |
604 | } | |
605 | ||
606 | #if wxUSE_FONTMAP | |
607 | ||
608 | wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper() | |
609 | { | |
610 | return (wxFontMapper *)new wxFontMapperBase; | |
611 | } | |
612 | ||
613 | #endif // wxUSE_FONTMAP | |
614 | ||
615 | wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() | |
616 | { | |
617 | // console applications don't use renderers | |
618 | return NULL; | |
619 | } | |
620 | ||
621 | #ifdef __WXDEBUG__ | |
622 | bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
623 | { | |
624 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
625 | } | |
626 | #endif | |
627 | ||
628 | bool wxConsoleAppTraitsBase::HasStderr() | |
629 | { | |
630 | // console applications always have stderr, even under Mac/Windows | |
631 | return true; | |
632 | } | |
633 | ||
634 | void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
635 | { | |
636 | delete object; | |
637 | } | |
638 | ||
639 | void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject * WXUNUSED(object)) | |
640 | { | |
641 | // nothing to do | |
642 | } | |
643 | ||
644 | // ---------------------------------------------------------------------------- | |
645 | // wxAppTraits | |
646 | // ---------------------------------------------------------------------------- | |
647 | ||
648 | #if wxUSE_INTL | |
649 | void wxAppTraitsBase::SetLocale() | |
650 | { | |
651 | wxSetlocale(LC_ALL, ""); | |
652 | wxUpdateLocaleIsUtf8(); | |
653 | } | |
654 | #endif | |
655 | ||
656 | #if wxUSE_THREADS | |
657 | void wxMutexGuiEnterImpl(); | |
658 | void wxMutexGuiLeaveImpl(); | |
659 | ||
660 | void wxAppTraitsBase::MutexGuiEnter() | |
661 | { | |
662 | wxMutexGuiEnterImpl(); | |
663 | } | |
664 | ||
665 | void wxAppTraitsBase::MutexGuiLeave() | |
666 | { | |
667 | wxMutexGuiLeaveImpl(); | |
668 | } | |
669 | ||
670 | void WXDLLIMPEXP_BASE wxMutexGuiEnter() | |
671 | { | |
672 | wxAppTraits * const traits = wxAppConsoleBase::GetTraitsIfExists(); | |
673 | if ( traits ) | |
674 | traits->MutexGuiEnter(); | |
675 | } | |
676 | ||
677 | void WXDLLIMPEXP_BASE wxMutexGuiLeave() | |
678 | { | |
679 | wxAppTraits * const traits = wxAppConsoleBase::GetTraitsIfExists(); | |
680 | if ( traits ) | |
681 | traits->MutexGuiLeave(); | |
682 | } | |
683 | #endif // wxUSE_THREADS | |
684 | ||
685 | #ifdef __WXDEBUG__ | |
686 | ||
687 | bool wxAppTraitsBase::ShowAssertDialog(const wxString& msgOriginal) | |
688 | { | |
689 | wxString msg = msgOriginal; | |
690 | ||
691 | #if wxUSE_STACKWALKER | |
692 | #if !defined(__WXMSW__) | |
693 | // on Unix stack frame generation may take some time, depending on the | |
694 | // size of the executable mainly... warn the user that we are working | |
695 | wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait")); | |
696 | fflush(stderr); | |
697 | #endif | |
698 | ||
699 | const wxString stackTrace = GetAssertStackTrace(); | |
700 | if ( !stackTrace.empty() ) | |
701 | msg << _T("\n\nCall stack:\n") << stackTrace; | |
702 | #endif // wxUSE_STACKWALKER | |
703 | ||
704 | return DoShowAssertDialog(msg); | |
705 | } | |
706 | ||
707 | #if wxUSE_STACKWALKER | |
708 | wxString wxAppTraitsBase::GetAssertStackTrace() | |
709 | { | |
710 | wxString stackTrace; | |
711 | ||
712 | class StackDump : public wxStackWalker | |
713 | { | |
714 | public: | |
715 | StackDump() { } | |
716 | ||
717 | const wxString& GetStackTrace() const { return m_stackTrace; } | |
718 | ||
719 | protected: | |
720 | virtual void OnStackFrame(const wxStackFrame& frame) | |
721 | { | |
722 | m_stackTrace << wxString::Format | |
723 | ( | |
724 | _T("[%02d] "), | |
725 | wx_truncate_cast(int, frame.GetLevel()) | |
726 | ); | |
727 | ||
728 | wxString name = frame.GetName(); | |
729 | if ( !name.empty() ) | |
730 | { | |
731 | m_stackTrace << wxString::Format(_T("%-40s"), name.c_str()); | |
732 | } | |
733 | else | |
734 | { | |
735 | m_stackTrace << wxString::Format(_T("%p"), frame.GetAddress()); | |
736 | } | |
737 | ||
738 | if ( frame.HasSourceLocation() ) | |
739 | { | |
740 | m_stackTrace << _T('\t') | |
741 | << frame.GetFileName() | |
742 | << _T(':') | |
743 | << frame.GetLine(); | |
744 | } | |
745 | ||
746 | m_stackTrace << _T('\n'); | |
747 | } | |
748 | ||
749 | private: | |
750 | wxString m_stackTrace; | |
751 | }; | |
752 | ||
753 | // don't show more than maxLines or we could get a dialog too tall to be | |
754 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
755 | // characters it is still only 300 pixels... | |
756 | static const int maxLines = 20; | |
757 | ||
758 | StackDump dump; | |
759 | dump.Walk(2, maxLines); // don't show OnAssert() call itself | |
760 | stackTrace = dump.GetStackTrace(); | |
761 | ||
762 | const int count = stackTrace.Freq(wxT('\n')); | |
763 | for ( int i = 0; i < count - maxLines; i++ ) | |
764 | stackTrace = stackTrace.BeforeLast(wxT('\n')); | |
765 | ||
766 | return stackTrace; | |
767 | } | |
768 | #endif // wxUSE_STACKWALKER | |
769 | ||
770 | ||
771 | #endif // __WXDEBUG__ | |
772 | ||
773 | // ============================================================================ | |
774 | // global functions implementation | |
775 | // ============================================================================ | |
776 | ||
777 | void wxExit() | |
778 | { | |
779 | if ( wxTheApp ) | |
780 | { | |
781 | wxTheApp->Exit(); | |
782 | } | |
783 | else | |
784 | { | |
785 | // what else can we do? | |
786 | exit(-1); | |
787 | } | |
788 | } | |
789 | ||
790 | void wxWakeUpIdle() | |
791 | { | |
792 | if ( wxTheApp ) | |
793 | { | |
794 | wxTheApp->WakeUpIdle(); | |
795 | } | |
796 | //else: do nothing, what can we do? | |
797 | } | |
798 | ||
799 | #ifdef __WXDEBUG__ | |
800 | ||
801 | // wxASSERT() helper | |
802 | bool wxAssertIsEqual(int x, int y) | |
803 | { | |
804 | return x == y; | |
805 | } | |
806 | ||
807 | // break into the debugger | |
808 | void wxTrap() | |
809 | { | |
810 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
811 | DebugBreak(); | |
812 | #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS | |
813 | Debugger(); | |
814 | #elif defined(__UNIX__) | |
815 | raise(SIGTRAP); | |
816 | #else | |
817 | // TODO | |
818 | #endif // Win/Unix | |
819 | } | |
820 | ||
821 | // this function is called when an assert fails | |
822 | static void wxDoOnAssert(const wxString& szFile, | |
823 | int nLine, | |
824 | const wxString& szFunc, | |
825 | const wxString& szCond, | |
826 | const wxString& szMsg = wxEmptyString) | |
827 | { | |
828 | // FIXME MT-unsafe | |
829 | static int s_bInAssert = 0; | |
830 | ||
831 | wxRecursionGuard guard(s_bInAssert); | |
832 | if ( guard.IsInside() ) | |
833 | { | |
834 | // can't use assert here to avoid infinite loops, so just trap | |
835 | wxTrap(); | |
836 | ||
837 | return; | |
838 | } | |
839 | ||
840 | if ( !wxTheApp ) | |
841 | { | |
842 | // by default, show the assert dialog box -- we can't customize this | |
843 | // behaviour | |
844 | ShowAssertDialog(szFile, nLine, szFunc, szCond, szMsg); | |
845 | } | |
846 | else | |
847 | { | |
848 | // let the app process it as it wants | |
849 | // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed | |
850 | wxTheApp->OnAssertFailure(szFile.c_str(), nLine, szFunc.c_str(), | |
851 | szCond.c_str(), szMsg.c_str()); | |
852 | } | |
853 | } | |
854 | ||
855 | void wxOnAssert(const wxString& szFile, | |
856 | int nLine, | |
857 | const wxString& szFunc, | |
858 | const wxString& szCond, | |
859 | const wxString& szMsg) | |
860 | { | |
861 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
862 | } | |
863 | ||
864 | void wxOnAssert(const wxString& szFile, | |
865 | int nLine, | |
866 | const wxString& szFunc, | |
867 | const wxString& szCond) | |
868 | { | |
869 | wxDoOnAssert(szFile, nLine, szFunc, szCond); | |
870 | } | |
871 | ||
872 | void wxOnAssert(const wxChar *szFile, | |
873 | int nLine, | |
874 | const char *szFunc, | |
875 | const wxChar *szCond, | |
876 | const wxChar *szMsg) | |
877 | { | |
878 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
879 | } | |
880 | ||
881 | void wxOnAssert(const char *szFile, | |
882 | int nLine, | |
883 | const char *szFunc, | |
884 | const char *szCond, | |
885 | const wxString& szMsg) | |
886 | { | |
887 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
888 | } | |
889 | ||
890 | void wxOnAssert(const char *szFile, | |
891 | int nLine, | |
892 | const char *szFunc, | |
893 | const char *szCond, | |
894 | const wxCStrData& msg) | |
895 | { | |
896 | wxDoOnAssert(szFile, nLine, szFunc, szCond, msg); | |
897 | } | |
898 | ||
899 | #if wxUSE_UNICODE | |
900 | void wxOnAssert(const char *szFile, | |
901 | int nLine, | |
902 | const char *szFunc, | |
903 | const char *szCond) | |
904 | { | |
905 | wxDoOnAssert(szFile, nLine, szFunc, szCond); | |
906 | } | |
907 | ||
908 | void wxOnAssert(const char *szFile, | |
909 | int nLine, | |
910 | const char *szFunc, | |
911 | const char *szCond, | |
912 | const char *szMsg) | |
913 | { | |
914 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
915 | } | |
916 | ||
917 | void wxOnAssert(const char *szFile, | |
918 | int nLine, | |
919 | const char *szFunc, | |
920 | const char *szCond, | |
921 | const wxChar *szMsg) | |
922 | { | |
923 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
924 | } | |
925 | #endif // wxUSE_UNICODE | |
926 | ||
927 | #endif // __WXDEBUG__ | |
928 | ||
929 | // ============================================================================ | |
930 | // private functions implementation | |
931 | // ============================================================================ | |
932 | ||
933 | #ifdef __WXDEBUG__ | |
934 | ||
935 | static void LINKAGEMODE SetTraceMasks() | |
936 | { | |
937 | #if wxUSE_LOG | |
938 | wxString mask; | |
939 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
940 | { | |
941 | wxStringTokenizer tkn(mask, wxT(",;:")); | |
942 | while ( tkn.HasMoreTokens() ) | |
943 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
944 | } | |
945 | #endif // wxUSE_LOG | |
946 | } | |
947 | ||
948 | static | |
949 | bool DoShowAssertDialog(const wxString& msg) | |
950 | { | |
951 | // under MSW we can show the dialog even in the console mode | |
952 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
953 | wxString msgDlg(msg); | |
954 | ||
955 | // this message is intentionally not translated -- it is for | |
956 | // developpers only | |
957 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
958 | wxT("You can also choose [Cancel] to suppress ") | |
959 | wxT("further warnings."); | |
960 | ||
961 | switch ( ::MessageBox(NULL, msgDlg.wx_str(), _T("wxWidgets Debug Alert"), | |
962 | MB_YESNOCANCEL | MB_ICONSTOP ) ) | |
963 | { | |
964 | case IDYES: | |
965 | wxTrap(); | |
966 | break; | |
967 | ||
968 | case IDCANCEL: | |
969 | // stop the asserts | |
970 | return true; | |
971 | ||
972 | //case IDNO: nothing to do | |
973 | } | |
974 | #else // !__WXMSW__ | |
975 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
976 | fflush(stderr); | |
977 | ||
978 | // TODO: ask the user to enter "Y" or "N" on the console? | |
979 | wxTrap(); | |
980 | #endif // __WXMSW__/!__WXMSW__ | |
981 | ||
982 | // continue with the asserts | |
983 | return false; | |
984 | } | |
985 | ||
986 | // show the assert modal dialog | |
987 | static | |
988 | void ShowAssertDialog(const wxString& szFile, | |
989 | int nLine, | |
990 | const wxString& szFunc, | |
991 | const wxString& szCond, | |
992 | const wxString& szMsg, | |
993 | wxAppTraits *traits) | |
994 | { | |
995 | // this variable can be set to true to suppress "assert failure" messages | |
996 | static bool s_bNoAsserts = false; | |
997 | ||
998 | wxString msg; | |
999 | msg.reserve(2048); | |
1000 | ||
1001 | // make life easier for people using VC++ IDE by using this format: like | |
1002 | // this, clicking on the message will take us immediately to the place of | |
1003 | // the failed assert | |
1004 | msg.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile, nLine, szCond); | |
1005 | ||
1006 | // add the function name, if any | |
1007 | if ( !szFunc.empty() ) | |
1008 | msg << _T(" in ") << szFunc << _T("()"); | |
1009 | ||
1010 | // and the message itself | |
1011 | if ( !szMsg.empty() ) | |
1012 | { | |
1013 | msg << _T(": ") << szMsg; | |
1014 | } | |
1015 | else // no message given | |
1016 | { | |
1017 | msg << _T('.'); | |
1018 | } | |
1019 | ||
1020 | #if wxUSE_THREADS | |
1021 | // if we are not in the main thread, output the assert directly and trap | |
1022 | // since dialogs cannot be displayed | |
1023 | if ( !wxThread::IsMain() ) | |
1024 | { | |
1025 | msg += wxT(" [in child thread]"); | |
1026 | ||
1027 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
1028 | msg << wxT("\r\n"); | |
1029 | OutputDebugString(msg.wx_str()); | |
1030 | #else | |
1031 | // send to stderr | |
1032 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
1033 | fflush(stderr); | |
1034 | #endif | |
1035 | // He-e-e-e-elp!! we're asserting in a child thread | |
1036 | wxTrap(); | |
1037 | } | |
1038 | else | |
1039 | #endif // wxUSE_THREADS | |
1040 | ||
1041 | if ( !s_bNoAsserts ) | |
1042 | { | |
1043 | // send it to the normal log destination | |
1044 | wxLogDebug(_T("%s"), msg.c_str()); | |
1045 | ||
1046 | if ( traits ) | |
1047 | { | |
1048 | // delegate showing assert dialog (if possible) to that class | |
1049 | s_bNoAsserts = traits->ShowAssertDialog(msg); | |
1050 | } | |
1051 | else // no traits object | |
1052 | { | |
1053 | // fall back to the function of last resort | |
1054 | s_bNoAsserts = DoShowAssertDialog(msg); | |
1055 | } | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | #endif // __WXDEBUG__ |