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