1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/base/appbase.cpp
3 // Purpose: implements wxAppConsole 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"
35 #include "wx/apptrait.h"
36 #include "wx/cmdline.h"
37 #include "wx/confbase.h"
38 #include "wx/filename.h"
39 #include "wx/msgout.h"
40 #include "wx/tokenzr.h"
42 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
43 #include <signal.h> // for SIGTRAP used by wxTrap()
46 #if defined(__WXMSW__)
47 #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox()
51 #include "wx/fontmap.h"
52 #endif // wxUSE_FONTMAP
54 #if defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
55 // For MacTypes.h for Debugger function
56 #include <CoreFoundation/CFBase.h>
59 #if defined(__WXMAC__)
61 #include <CoreServices/CoreServices.h>
63 #include "wx/mac/private.h" // includes mac headers
69 #include "wx/stackwalk.h"
71 #include "wx/msw/debughlp.h"
73 #endif // wxUSE_STACKWALKER
76 // wxABI_VERSION can be defined when compiling applications but it should be
77 // left undefined when compiling the library itself, it is then set to its
78 // default value in version.h
79 #if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99
80 #error "wxABI_VERSION should not be defined when compiling the library"
83 // ----------------------------------------------------------------------------
84 // private functions prototypes
85 // ----------------------------------------------------------------------------
88 // really just show the assert dialog
89 static bool DoShowAssertDialog(const wxString
& msg
);
91 // prepare for showing the assert dialog, use the given traits or
92 // DoShowAssertDialog() as last fallback to really show it
94 void ShowAssertDialog(const wxChar
*szFile
,
98 wxAppTraits
*traits
= NULL
);
100 // turn on the trace masks specified in the env variable WXTRACE
101 static void LINKAGEMODE
SetTraceMasks();
102 #endif // __WXDEBUG__
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 wxAppConsole
*wxAppConsole::ms_appInstance
= NULL
;
110 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
112 // ============================================================================
113 // wxAppConsole implementation
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 wxAppConsole::wxAppConsole()
124 ms_appInstance
= this;
129 // In unicode mode the SetTraceMasks call can cause an apptraits to be
130 // created, but since we are still in the constructor the wrong kind will
131 // be created for GUI apps. Destroy it so it can be created again later.
138 wxAppConsole::~wxAppConsole()
143 // ----------------------------------------------------------------------------
144 // initilization/cleanup
145 // ----------------------------------------------------------------------------
147 bool wxAppConsole::Initialize(int& argc
, wxChar
**argv
)
149 // remember the command line arguments
154 if ( m_appName
.empty() && argv
)
156 // the application name is, by default, the name of its executable file
157 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
164 void wxAppConsole::CleanUp()
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 bool wxAppConsole::OnInit()
174 #if wxUSE_CMDLINE_PARSER
175 wxCmdLineParser
parser(argc
, argv
);
177 OnInitCmdLine(parser
);
180 switch ( parser
.Parse(false /* don't show usage */) )
183 cont
= OnCmdLineHelp(parser
);
187 cont
= OnCmdLineParsed(parser
);
191 cont
= OnCmdLineError(parser
);
197 #endif // wxUSE_CMDLINE_PARSER
202 int wxAppConsole::OnExit()
205 // delete the config object if any (don't use Get() here, but Set()
206 // because Get() could create a new config object)
207 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
208 #endif // wxUSE_CONFIG
210 // use Set(NULL) and not Get() to avoid creating a message output object on
211 // demand when we just want to delete it
212 delete wxMessageOutput::Set(NULL
);
217 void wxAppConsole::Exit()
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
226 wxAppTraits
*wxAppConsole::CreateTraits()
228 return new wxConsoleAppTraits
;
231 wxAppTraits
*wxAppConsole::GetTraits()
233 // FIXME-MT: protect this with a CS?
236 m_traits
= CreateTraits();
238 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
244 // we must implement CreateXXX() in wxApp itself for backwards compatibility
245 #if WXWIN_COMPATIBILITY_2_4
249 wxLog
*wxAppConsole::CreateLogTarget()
251 wxAppTraits
*traits
= GetTraits();
252 return traits
? traits
->CreateLogTarget() : NULL
;
257 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
259 wxAppTraits
*traits
= GetTraits();
260 return traits
? traits
->CreateMessageOutput() : NULL
;
263 #endif // WXWIN_COMPATIBILITY_2_4
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 void wxAppConsole::ProcessPendingEvents()
272 if ( !wxPendingEventsLocker
)
276 // ensure that we're the only thread to modify the pending events list
277 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
279 if ( !wxPendingEvents
)
281 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
285 // iterate until the list becomes empty
286 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
289 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
290 wxPendingEvents
->Erase(node
);
292 // In ProcessPendingEvents(), new handlers might be add
293 // and we can safely leave the critical section here.
294 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
296 handler
->ProcessPendingEvents();
298 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
300 node
= wxPendingEvents
->GetFirst();
303 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
306 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
308 // process the events normally by default
312 // ----------------------------------------------------------------------------
313 // exception handling
314 // ----------------------------------------------------------------------------
319 wxAppConsole::HandleEvent(wxEvtHandler
*handler
,
320 wxEventFunction func
,
321 wxEvent
& event
) const
323 // by default, simply call the handler
324 (handler
->*func
)(event
);
328 wxAppConsole::OnExceptionInMainLoop()
332 // some compilers are too stupid to know that we never return after throw
333 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
338 #endif // wxUSE_EXCEPTIONS
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 #if wxUSE_CMDLINE_PARSER
346 #define OPTION_VERBOSE _T("verbose")
348 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
350 // the standard command line options
351 static const wxCmdLineEntryDesc cmdLineDesc
[] =
357 gettext_noop("show this help message"),
359 wxCMD_LINE_OPTION_HELP
367 gettext_noop("generate verbose log messages"),
384 parser
.SetDesc(cmdLineDesc
);
387 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
390 if ( parser
.Found(OPTION_VERBOSE
) )
392 wxLog::SetVerbose(true);
401 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
408 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
415 #endif // wxUSE_CMDLINE_PARSER
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
422 bool wxAppConsole::CheckBuildOptions(const char *optionsSignature
,
423 const char *componentName
)
425 #if 0 // can't use wxLogTrace, not up and running yet
426 printf("checking build options object '%s' (ptr %p) in '%s'\n",
427 optionsSignature
, optionsSignature
, componentName
);
430 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
432 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
433 wxString prog
= wxString::FromAscii(optionsSignature
);
434 wxString progName
= wxString::FromAscii(componentName
);
437 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
438 lib
.c_str(), progName
.c_str(), prog
.c_str());
440 wxLogFatalError(msg
.c_str());
442 // normally wxLogFatalError doesn't return
452 void wxAppConsole::OnAssert(const wxChar
*file
,
457 ShowAssertDialog(file
, line
, cond
, msg
, GetTraits());
460 #endif // __WXDEBUG__
462 #if WXWIN_COMPATIBILITY_2_4
464 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& buildOptions
)
466 return CheckBuildOptions(buildOptions
.m_signature
, "your program");
471 // ============================================================================
472 // other classes implementations
473 // ============================================================================
475 // ----------------------------------------------------------------------------
476 // wxConsoleAppTraitsBase
477 // ----------------------------------------------------------------------------
481 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
483 return new wxLogStderr
;
488 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
490 return new wxMessageOutputStderr
;
495 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
497 return (wxFontMapper
*)new wxFontMapperBase
;
500 #endif // wxUSE_FONTMAP
502 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
504 // console applications don't use renderers
509 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
511 return wxAppTraitsBase::ShowAssertDialog(msg
);
515 bool wxConsoleAppTraitsBase::HasStderr()
517 // console applications always have stderr, even under Mac/Windows
521 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
526 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
532 GSocketGUIFunctionsTable
* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable()
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
544 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
546 return DoShowAssertDialog(msg
);
549 #endif // __WXDEBUG__
551 // ============================================================================
552 // global functions implementation
553 // ============================================================================
563 // what else can we do?
572 wxTheApp
->WakeUpIdle();
574 //else: do nothing, what can we do?
580 bool wxAssertIsEqual(int x
, int y
)
585 // break into the debugger
588 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
590 #elif defined(__WXMAC__) && !defined(__DARWIN__)
596 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
598 #elif defined(__UNIX__)
605 void wxAssert(int cond
,
606 const wxChar
*szFile
,
608 const wxChar
*szCond
,
612 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
615 // this function is called when an assert fails
616 void wxOnAssert(const wxChar
*szFile
,
618 const wxChar
*szCond
,
622 static bool s_bInAssert
= false;
626 // He-e-e-e-elp!! we're trapped in endless loop
638 // by default, show the assert dialog box -- we can't customize this
640 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
644 // let the app process it as it wants
645 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
651 #endif // __WXDEBUG__
653 // ============================================================================
654 // private functions implementation
655 // ============================================================================
659 static void LINKAGEMODE
SetTraceMasks()
663 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
665 wxStringTokenizer
tkn(mask
, wxT(",;:"));
666 while ( tkn
.HasMoreTokens() )
667 wxLog::AddTraceMask(tkn
.GetNextToken());
672 bool DoShowAssertDialog(const wxString
& msg
)
674 // under MSW we can show the dialog even in the console mode
675 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
676 wxString
msgDlg(msg
);
678 // this message is intentionally not translated -- it is for
680 msgDlg
+= wxT("\nDo you want to stop the program?\n")
681 wxT("You can also choose [Cancel] to suppress ")
682 wxT("further warnings.");
684 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWidgets Debug Alert"),
685 MB_YESNOCANCEL
| MB_ICONSTOP
) )
695 //case IDNO: nothing to do
698 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
701 // TODO: ask the user to enter "Y" or "N" on the console?
703 #endif // __WXMSW__/!__WXMSW__
705 // continue with the asserts
709 #if wxUSE_STACKWALKER
710 static wxString
GetAssertStackTrace()
714 class StackDump
: public wxStackWalker
719 const wxString
& GetStackTrace() const { return m_stackTrace
; }
722 virtual void OnStackFrame(const wxStackFrame
& frame
)
724 m_stackTrace
<< wxString::Format(_T("[%02d] "), frame
.GetLevel());
726 wxString name
= frame
.GetName();
729 m_stackTrace
<< wxString::Format(_T("%-40s"), name
.c_str());
733 m_stackTrace
<< wxString::Format
736 (unsigned long)frame
.GetAddress()
740 if ( frame
.HasSourceLocation() )
742 m_stackTrace
<< _T('\t')
743 << frame
.GetFileName()
748 m_stackTrace
<< _T('\n');
752 wxString m_stackTrace
;
756 dump
.Walk(5); // don't show OnAssert() call itself
757 stackTrace
= dump
.GetStackTrace();
759 // don't show more than maxLines or we could get a dialog too tall to be
760 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
761 // characters it is still only 300 pixels...
762 static const int maxLines
= 20;
763 const int count
= stackTrace
.Freq(wxT('\n'));
764 for ( int i
= 0; i
< count
- maxLines
; i
++ )
765 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
769 #endif // wxUSE_STACKWALKER
771 // show the assert modal dialog
773 void ShowAssertDialog(const wxChar
*szFile
,
775 const wxChar
*szCond
,
779 // this variable can be set to true to suppress "assert failure" messages
780 static bool s_bNoAsserts
= false;
785 // make life easier for people using VC++ IDE by using this format: like
786 // this, clicking on the message will take us immediately to the place of
788 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
792 msg
<< _T(": ") << szMsg
;
794 else // no message given
799 #if wxUSE_STACKWALKER
800 const wxString stackTrace
= GetAssertStackTrace();
801 if ( !stackTrace
.empty() )
803 msg
<< _T("\n\nCall stack:\n") << stackTrace
;
805 #endif // wxUSE_STACKWALKER
808 // if we are not in the main thread, output the assert directly and trap
809 // since dialogs cannot be displayed
810 if ( !wxThread::IsMain() )
812 msg
+= wxT(" [in child thread]");
814 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
816 OutputDebugString(msg
);
819 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
822 // He-e-e-e-elp!! we're asserting in a child thread
826 #endif // wxUSE_THREADS
830 // send it to the normal log destination
831 wxLogDebug(_T("%s"), msg
.c_str());
835 // delegate showing assert dialog (if possible) to that class
836 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
838 else // no traits object
840 // fall back to the function of last resort
841 s_bNoAsserts
= DoShowAssertDialog(msg
);
846 #endif // __WXDEBUG__