1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appbase.cpp
3 // Purpose: implements wxAppConsoleBase class
4 // Author: Vadim Zeitlin
6 // Created: 19.06.2003 (extracted from common/appcmn.cpp)
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox()
36 #include "wx/wxcrtvararg.h"
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"
49 #if wxUSE_EXCEPTIONS && wxUSE_STL
55 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
56 #include <signal.h> // for SIGTRAP used by wxTrap()
60 #endif // ! __WXPALMOS5__
63 #include "wx/fontmap.h"
64 #endif // wxUSE_FONTMAP
68 #include "wx/stackwalk.h"
70 #include "wx/msw/debughlp.h"
72 #endif // wxUSE_STACKWALKER
74 #include "wx/recguard.h"
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"
84 // ----------------------------------------------------------------------------
85 // private functions prototypes
86 // ----------------------------------------------------------------------------
89 // really just show the assert dialog
90 static bool DoShowAssertDialog(const wxString
& msg
);
92 // prepare for showing the assert dialog, use the given traits or
93 // DoShowAssertDialog() as last fallback to really show it
95 void ShowAssertDialog(const wxString
& szFile
,
97 const wxString
& szFunc
,
98 const wxString
& szCond
,
99 const wxString
& szMsg
,
100 wxAppTraits
*traits
= NULL
);
102 // turn on the trace masks specified in the env variable WXTRACE
103 static void LINKAGEMODE
SetTraceMasks();
104 #endif // __WXDEBUG__
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 wxAppConsole
*wxAppConsoleBase::ms_appInstance
= NULL
;
112 wxAppInitializerFunction
wxAppConsoleBase::ms_appInitFn
= NULL
;
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // this defines wxEventLoopPtr
119 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase
)
121 // ============================================================================
122 // wxAppConsoleBase implementation
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 wxAppConsoleBase::wxAppConsoleBase()
134 ms_appInstance
= static_cast<wxAppConsole
*>(this);
139 // In unicode mode the SetTraceMasks call can cause an apptraits to be
140 // created, but since we are still in the constructor the wrong kind will
141 // be created for GUI apps. Destroy it so it can be created again later.
148 wxAppConsoleBase::~wxAppConsoleBase()
153 // ----------------------------------------------------------------------------
154 // initilization/cleanup
155 // ----------------------------------------------------------------------------
157 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**argv
)
160 GetTraits()->SetLocale();
164 wxPendingEventsLocker
= new wxCriticalSection
;
168 if ( m_appName
.empty() && argv
&& argv
[0] )
170 // the application name is, by default, the name of its executable file
171 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
173 #endif // !__WXPALMOS__
178 wxEventLoopBase
*wxAppConsoleBase::CreateMainLoop()
180 return GetTraits()->CreateEventLoop();
183 void wxAppConsoleBase::CleanUp()
191 delete wxPendingEvents
;
192 wxPendingEvents
= NULL
;
195 delete wxPendingEventsLocker
;
196 wxPendingEventsLocker
= NULL
;
197 #endif // wxUSE_THREADS
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 bool wxAppConsoleBase::OnInit()
206 #if wxUSE_CMDLINE_PARSER
207 wxCmdLineParser
parser(argc
, argv
);
209 OnInitCmdLine(parser
);
212 switch ( parser
.Parse(false /* don't show usage */) )
215 cont
= OnCmdLineHelp(parser
);
219 cont
= OnCmdLineParsed(parser
);
223 cont
= OnCmdLineError(parser
);
229 #endif // wxUSE_CMDLINE_PARSER
234 int wxAppConsoleBase::OnRun()
239 int wxAppConsoleBase::OnExit()
242 // delete the config object if any (don't use Get() here, but Set()
243 // because Get() could create a new config object)
244 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
245 #endif // wxUSE_CONFIG
250 void wxAppConsoleBase::Exit()
252 if (m_mainLoop
!= NULL
)
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 wxAppTraits
*wxAppConsoleBase::CreateTraits()
264 return new wxConsoleAppTraits
;
267 wxAppTraits
*wxAppConsoleBase::GetTraits()
269 // FIXME-MT: protect this with a CS?
272 m_traits
= CreateTraits();
274 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
281 wxAppTraits
*wxAppConsoleBase::GetTraitsIfExists()
283 wxAppConsole
* const app
= GetInstance();
284 return app
? app
->GetTraits() : NULL
;
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 int wxAppConsoleBase::MainLoop()
293 wxEventLoopBaseTiedPtr
mainLoop(&m_mainLoop
, CreateMainLoop());
295 return m_mainLoop
? m_mainLoop
->Run() : -1;
298 void wxAppConsoleBase::ExitMainLoop()
300 // we should exit from the main event loop, not just any currently active
301 // (e.g. modal dialog) event loop
302 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
308 bool wxAppConsoleBase::Pending()
310 // use the currently active message loop here, not m_mainLoop, because if
311 // we're showing a modal dialog (with its own event loop) currently the
312 // main event loop is not running anyhow
313 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
315 return loop
&& loop
->Pending();
318 bool wxAppConsoleBase::Dispatch()
320 // see comment in Pending()
321 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
323 return loop
&& loop
->Dispatch();
326 bool wxAppConsoleBase::HasPendingEvents() const
328 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
330 bool has
= wxPendingEvents
&& !wxPendingEvents
->IsEmpty();
332 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
338 bool wxAppConsoleBase::IsMainLoopRunning()
340 const wxAppConsole
* const app
= GetInstance();
342 return app
&& app
->m_mainLoop
!= NULL
;
345 void wxAppConsoleBase::ProcessPendingEvents()
348 if ( !wxPendingEventsLocker
)
352 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
356 // iterate until the list becomes empty: the handlers remove themselves
357 // from it when they don't have any more pending events
358 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
361 // In ProcessPendingEvents(), new handlers might be add
362 // and we can safely leave the critical section here.
363 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
365 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
366 handler
->ProcessPendingEvents();
368 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
370 // restart as the iterators could have been invalidated
371 node
= wxPendingEvents
->GetFirst();
375 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
378 void wxAppConsoleBase::WakeUpIdle()
381 m_mainLoop
->WakeUp();
384 bool wxAppConsoleBase::ProcessIdle()
388 event
.SetEventObject(this);
390 return event
.MoreRequested();
393 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
395 // process the events normally by default
399 // ----------------------------------------------------------------------------
400 // exception handling
401 // ----------------------------------------------------------------------------
406 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
407 wxEventFunction func
,
408 wxEvent
& event
) const
410 // by default, simply call the handler
411 (handler
->*func
)(event
);
414 void wxAppConsoleBase::OnUnhandledException()
417 // we're called from an exception handler so we can re-throw the exception
418 // to recover its type
425 catch ( std::exception
& e
)
427 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
428 typeid(e
).name(), e
.what());
433 what
= "unknown exception";
436 wxMessageOutputBest().Printf(
437 "*** Caught unhandled %s; terminating\n", what
439 #endif // __WXDEBUG__
442 // ----------------------------------------------------------------------------
443 // exceptions support
444 // ----------------------------------------------------------------------------
446 bool wxAppConsoleBase::OnExceptionInMainLoop()
450 // some compilers are too stupid to know that we never return after throw
451 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
456 #endif // wxUSE_EXCEPTIONS
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 #if wxUSE_CMDLINE_PARSER
464 #define OPTION_VERBOSE "verbose"
466 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
468 // the standard command line options
469 static const wxCmdLineEntryDesc cmdLineDesc
[] =
475 gettext_noop("show this help message"),
477 wxCMD_LINE_OPTION_HELP
485 gettext_noop("generate verbose log messages"),
495 parser
.SetDesc(cmdLineDesc
);
498 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
501 if ( parser
.Found(OPTION_VERBOSE
) )
503 wxLog::SetVerbose(true);
512 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
519 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
526 #endif // wxUSE_CMDLINE_PARSER
528 // ----------------------------------------------------------------------------
530 // ----------------------------------------------------------------------------
533 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
534 const char *componentName
)
536 #if 0 // can't use wxLogTrace, not up and running yet
537 printf("checking build options object '%s' (ptr %p) in '%s'\n",
538 optionsSignature
, optionsSignature
, componentName
);
541 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
543 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
544 wxString prog
= wxString::FromAscii(optionsSignature
);
545 wxString progName
= wxString::FromAscii(componentName
);
548 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
549 lib
.c_str(), progName
.c_str(), prog
.c_str());
551 wxLogFatalError(msg
.c_str());
553 // normally wxLogFatalError doesn't return
563 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
569 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
572 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
577 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
580 #endif // __WXDEBUG__
582 // ============================================================================
583 // other classes implementations
584 // ============================================================================
586 // ----------------------------------------------------------------------------
587 // wxConsoleAppTraitsBase
588 // ----------------------------------------------------------------------------
592 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
594 return new wxLogStderr
;
599 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
601 return new wxMessageOutputStderr
;
606 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
608 return (wxFontMapper
*)new wxFontMapperBase
;
611 #endif // wxUSE_FONTMAP
613 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
615 // console applications don't use renderers
620 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
622 return wxAppTraitsBase::ShowAssertDialog(msg
);
626 bool wxConsoleAppTraitsBase::HasStderr()
628 // console applications always have stderr, even under Mac/Windows
632 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
637 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
642 // ----------------------------------------------------------------------------
644 // ----------------------------------------------------------------------------
647 void wxAppTraitsBase::SetLocale()
649 wxSetlocale(LC_ALL
, "");
650 wxUpdateLocaleIsUtf8();
655 void wxMutexGuiEnterImpl();
656 void wxMutexGuiLeaveImpl();
658 void wxAppTraitsBase::MutexGuiEnter()
660 wxMutexGuiEnterImpl();
663 void wxAppTraitsBase::MutexGuiLeave()
665 wxMutexGuiLeaveImpl();
668 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
670 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
672 traits
->MutexGuiEnter();
675 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
677 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
679 traits
->MutexGuiLeave();
681 #endif // wxUSE_THREADS
685 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
687 wxString msg
= msgOriginal
;
689 #if wxUSE_STACKWALKER
690 #if !defined(__WXMSW__)
691 // on Unix stack frame generation may take some time, depending on the
692 // size of the executable mainly... warn the user that we are working
693 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
697 const wxString stackTrace
= GetAssertStackTrace();
698 if ( !stackTrace
.empty() )
699 msg
<< _T("\n\nCall stack:\n") << stackTrace
;
700 #endif // wxUSE_STACKWALKER
702 return DoShowAssertDialog(msg
);
705 #if wxUSE_STACKWALKER
706 wxString
wxAppTraitsBase::GetAssertStackTrace()
710 class StackDump
: public wxStackWalker
715 const wxString
& GetStackTrace() const { return m_stackTrace
; }
718 virtual void OnStackFrame(const wxStackFrame
& frame
)
720 m_stackTrace
<< wxString::Format
723 wx_truncate_cast(int, frame
.GetLevel())
726 wxString name
= frame
.GetName();
729 m_stackTrace
<< wxString::Format(_T("%-40s"), name
.c_str());
733 m_stackTrace
<< wxString::Format(_T("%p"), frame
.GetAddress());
736 if ( frame
.HasSourceLocation() )
738 m_stackTrace
<< _T('\t')
739 << frame
.GetFileName()
744 m_stackTrace
<< _T('\n');
748 wxString m_stackTrace
;
751 // don't show more than maxLines or we could get a dialog too tall to be
752 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
753 // characters it is still only 300 pixels...
754 static const int maxLines
= 20;
757 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
758 stackTrace
= dump
.GetStackTrace();
760 const int count
= stackTrace
.Freq(wxT('\n'));
761 for ( int i
= 0; i
< count
- maxLines
; i
++ )
762 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
766 #endif // wxUSE_STACKWALKER
769 #endif // __WXDEBUG__
771 // ============================================================================
772 // global functions implementation
773 // ============================================================================
783 // what else can we do?
792 wxTheApp
->WakeUpIdle();
794 //else: do nothing, what can we do?
800 bool wxAssertIsEqual(int x
, int y
)
805 // break into the debugger
808 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
810 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
812 #elif defined(__UNIX__)
819 // this function is called when an assert fails
820 static void wxDoOnAssert(const wxString
& szFile
,
822 const wxString
& szFunc
,
823 const wxString
& szCond
,
824 const wxString
& szMsg
= wxEmptyString
)
827 static int s_bInAssert
= 0;
829 wxRecursionGuard
guard(s_bInAssert
);
830 if ( guard
.IsInside() )
832 // can't use assert here to avoid infinite loops, so just trap
840 // by default, show the assert dialog box -- we can't customize this
842 ShowAssertDialog(szFile
, nLine
, szFunc
, szCond
, szMsg
);
846 // let the app process it as it wants
847 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
848 wxTheApp
->OnAssertFailure(szFile
.c_str(), nLine
, szFunc
.c_str(),
849 szCond
.c_str(), szMsg
.c_str());
853 void wxOnAssert(const wxString
& szFile
,
855 const wxString
& szFunc
,
856 const wxString
& szCond
,
857 const wxString
& szMsg
)
859 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
862 void wxOnAssert(const wxString
& szFile
,
864 const wxString
& szFunc
,
865 const wxString
& szCond
)
867 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
);
870 void wxOnAssert(const wxChar
*szFile
,
873 const wxChar
*szCond
,
876 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
879 void wxOnAssert(const char *szFile
,
883 const wxString
& szMsg
)
885 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
888 void wxOnAssert(const char *szFile
,
892 const wxCStrData
& msg
)
894 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, msg
);
898 void wxOnAssert(const char *szFile
,
903 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
);
906 void wxOnAssert(const char *szFile
,
912 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
915 void wxOnAssert(const char *szFile
,
921 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
923 #endif // wxUSE_UNICODE
925 #endif // __WXDEBUG__
927 // ============================================================================
928 // private functions implementation
929 // ============================================================================
933 static void LINKAGEMODE
SetTraceMasks()
937 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
939 wxStringTokenizer
tkn(mask
, wxT(",;:"));
940 while ( tkn
.HasMoreTokens() )
941 wxLog::AddTraceMask(tkn
.GetNextToken());
947 bool DoShowAssertDialog(const wxString
& msg
)
949 // under MSW we can show the dialog even in the console mode
950 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
951 wxString
msgDlg(msg
);
953 // this message is intentionally not translated -- it is for
955 msgDlg
+= wxT("\nDo you want to stop the program?\n")
956 wxT("You can also choose [Cancel] to suppress ")
957 wxT("further warnings.");
959 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), _T("wxWidgets Debug Alert"),
960 MB_YESNOCANCEL
| MB_ICONSTOP
) )
970 //case IDNO: nothing to do
973 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
976 // TODO: ask the user to enter "Y" or "N" on the console?
978 #endif // __WXMSW__/!__WXMSW__
980 // continue with the asserts
984 // show the assert modal dialog
986 void ShowAssertDialog(const wxString
& szFile
,
988 const wxString
& szFunc
,
989 const wxString
& szCond
,
990 const wxString
& szMsg
,
993 // this variable can be set to true to suppress "assert failure" messages
994 static bool s_bNoAsserts
= false;
999 // make life easier for people using VC++ IDE by using this format: like
1000 // this, clicking on the message will take us immediately to the place of
1001 // the failed assert
1002 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
1004 // add the function name, if any
1005 if ( !szFunc
.empty() )
1006 msg
<< _T(" in ") << szFunc
<< _T("()");
1008 // and the message itself
1009 if ( !szMsg
.empty() )
1011 msg
<< _T(": ") << szMsg
;
1013 else // no message given
1019 // if we are not in the main thread, output the assert directly and trap
1020 // since dialogs cannot be displayed
1021 if ( !wxThread::IsMain() )
1023 msg
+= wxT(" [in child thread]");
1025 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1027 OutputDebugString(msg
.wx_str());
1030 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1033 // He-e-e-e-elp!! we're asserting in a child thread
1037 #endif // wxUSE_THREADS
1039 if ( !s_bNoAsserts
)
1041 // send it to the normal log destination
1042 wxLogDebug(_T("%s"), msg
.c_str());
1046 // delegate showing assert dialog (if possible) to that class
1047 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1049 else // no traits object
1051 // fall back to the function of last resort
1052 s_bNoAsserts
= DoShowAssertDialog(msg
);
1057 #endif // __WXDEBUG__