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"
37 #include "wx/apptrait.h"
38 #include "wx/cmdline.h"
39 #include "wx/confbase.h"
40 #include "wx/filename.h"
41 #include "wx/msgout.h"
42 #include "wx/tokenzr.h"
44 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
45 #include <signal.h> // for SIGTRAP used by wxTrap()
48 #if defined(__WXMSW__)
49 #include "wx/msw/private.h" // includes windows.h for MessageBox()
53 #include "wx/fontmap.h"
54 #endif // wxUSE_FONTMAP
56 #if defined(__WXMAC__)
57 // VZ: MacTypes.h is enough under Mac OS X (where I could test it) but
58 // I don't know which headers are needed under earlier systems so
59 // include everything when in doubt
63 #include "wx/mac/private.h" // includes mac headers
67 // ----------------------------------------------------------------------------
68 // private functions prototypes
69 // ----------------------------------------------------------------------------
72 // really just show the assert dialog
73 static bool DoShowAssertDialog(const wxString
& msg
);
75 // prepare for showing the assert dialog, use the given traits or
76 // DoShowAssertDialog() as last fallback to really show it
78 void ShowAssertDialog(const wxChar
*szFile
,
82 wxAppTraits
*traits
= NULL
);
84 // turn on the trace masks specified in the env variable WXTRACE
85 static void LINKAGEMODE
SetTraceMasks();
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 wxAppConsole
*wxAppConsole::ms_appInstance
= NULL
;
94 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
96 // ============================================================================
97 // wxAppConsole implementation
98 // ============================================================================
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 wxAppConsole::wxAppConsole()
108 ms_appInstance
= this;
115 wxAppConsole::~wxAppConsole()
120 // ----------------------------------------------------------------------------
121 // initilization/cleanup
122 // ----------------------------------------------------------------------------
124 bool wxAppConsole::Initialize(int& argc
, wxChar
**argv
)
126 // remember the command line arguments
130 if ( m_appName
.empty() && argv
)
132 // the application name is, by default, the name of its executable file
133 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
139 void wxAppConsole::CleanUp()
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 bool wxAppConsole::OnInit()
149 #if wxUSE_CMDLINE_PARSER
150 wxCmdLineParser
parser(argc
, argv
);
152 OnInitCmdLine(parser
);
155 switch ( parser
.Parse(FALSE
/* don't show usage */) )
158 cont
= OnCmdLineHelp(parser
);
162 cont
= OnCmdLineParsed(parser
);
166 cont
= OnCmdLineError(parser
);
172 #endif // wxUSE_CMDLINE_PARSER
177 int wxAppConsole::OnExit()
180 // delete the config object if any (don't use Get() here, but Set()
181 // because Get() could create a new config object)
182 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
183 #endif // wxUSE_CONFIG
185 // use Set(NULL) and not Get() to avoid creating a message output object on
186 // demand when we just want to delete it
187 delete wxMessageOutput::Set(NULL
);
192 void wxAppConsole::Exit()
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 wxAppTraits
*wxAppConsole::CreateTraits()
203 return new wxConsoleAppTraits
;
206 wxAppTraits
*wxAppConsole::GetTraits()
208 // FIXME-MT: protect this with a CS?
211 m_traits
= CreateTraits();
213 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
219 // we must implement CreateXXX() in wxApp itself for backwards compatibility
220 #if WXWIN_COMPATIBILITY_2_4
224 wxLog
*wxAppConsole::CreateLogTarget()
226 wxAppTraits
*traits
= GetTraits();
227 return traits
? traits
->CreateLogTarget() : NULL
;
232 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
234 wxAppTraits
*traits
= GetTraits();
235 return traits
? traits
->CreateMessageOutput() : NULL
;
238 #endif // WXWIN_COMPATIBILITY_2_4
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 void wxAppConsole::ProcessPendingEvents()
246 // ensure that we're the only thread to modify the pending events list
247 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
249 if ( !wxPendingEvents
)
251 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
255 // iterate until the list becomes empty
256 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
259 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
260 wxPendingEvents
->Erase(node
);
262 // In ProcessPendingEvents(), new handlers might be add
263 // and we can safely leave the critical section here.
264 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
265 handler
->ProcessPendingEvents();
266 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
268 node
= wxPendingEvents
->GetFirst();
271 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
274 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
276 // process the events normally by default
283 wxAppConsole::HandleEvent(wxEvtHandler
*handler
,
284 wxEventFunction func
,
285 wxEvent
& event
) const
287 // by default, simply call the handler
288 (handler
->*func
)(event
);
291 #endif // wxUSE_EXCEPTIONS
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 #if wxUSE_CMDLINE_PARSER
299 #define OPTION_VERBOSE _T("verbose")
301 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
303 // the standard command line options
304 static const wxCmdLineEntryDesc cmdLineDesc
[] =
310 gettext_noop("show this help message"),
312 wxCMD_LINE_OPTION_HELP
320 gettext_noop("generate verbose log messages"),
337 parser
.SetDesc(cmdLineDesc
);
340 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
343 if ( parser
.Found(OPTION_VERBOSE
) )
345 wxLog::SetVerbose(TRUE
);
352 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
359 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
366 #endif // wxUSE_CMDLINE_PARSER
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
373 bool wxAppConsole::CheckBuildOptions(const char *optionsSignature
,
374 const char *componentName
)
376 #if 0 // can't use wxLogTrace, not up and running yet
377 printf("checking build options object '%s' (ptr %p) in '%s'\n",
378 optionsSignature
, optionsSignature
, componentName
);
381 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
383 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
384 wxString prog
= wxString::FromAscii(optionsSignature
);
385 wxString progName
= wxString::FromAscii(componentName
);
388 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
389 lib
.c_str(), progName
.c_str(), prog
.c_str());
391 wxLogFatalError(msg
);
393 // normally wxLogFatalError doesn't return
403 void wxAppConsole::OnAssert(const wxChar
*file
,
408 ShowAssertDialog(file
, line
, cond
, msg
, GetTraits());
411 #endif // __WXDEBUG__
413 // ============================================================================
414 // other classes implementations
415 // ============================================================================
417 // ----------------------------------------------------------------------------
418 // wxConsoleAppTraitsBase
419 // ----------------------------------------------------------------------------
423 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
425 return new wxLogStderr
;
430 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
432 return new wxMessageOutputStderr
;
437 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
439 return (wxFontMapper
*)new wxFontMapperBase
;
442 #endif // wxUSE_FONTMAP
444 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
446 // console applications don't use renderers
451 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
453 return wxAppTraitsBase::ShowAssertDialog(msg
);
457 bool wxConsoleAppTraitsBase::HasStderr()
459 // console applications always have stderr, even under Mac/Windows
463 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
468 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
474 GSocketGUIFunctionsTable
* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable()
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
486 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
488 return DoShowAssertDialog(msg
);
491 #endif // __WXDEBUG__
493 // ============================================================================
494 // global functions implementation
495 // ============================================================================
505 // what else can we do?
514 wxTheApp
->WakeUpIdle();
516 //else: do nothing, what can we do?
522 bool wxAssertIsEqual(int x
, int y
)
527 // break into the debugger
530 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
532 #elif defined(__WXMAC__) && !defined(__DARWIN__)
538 #elif defined(__UNIX__)
545 void wxAssert(int cond
,
546 const wxChar
*szFile
,
548 const wxChar
*szCond
,
552 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
555 // this function is called when an assert fails
556 void wxOnAssert(const wxChar
*szFile
,
558 const wxChar
*szCond
,
562 static bool s_bInAssert
= FALSE
;
566 // He-e-e-e-elp!! we're trapped in endless loop
578 // by default, show the assert dialog box -- we can't customize this
580 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
584 // let the app process it as it wants
585 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
591 #endif // __WXDEBUG__
593 // ============================================================================
594 // private functions implementation
595 // ============================================================================
599 static void LINKAGEMODE
SetTraceMasks()
603 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
605 wxStringTokenizer
tkn(mask
, wxT(",;:"));
606 while ( tkn
.HasMoreTokens() )
607 wxLog::AddTraceMask(tkn
.GetNextToken());
612 bool DoShowAssertDialog(const wxString
& msg
)
614 // under MSW we can show the dialog even in the console mode
615 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
616 wxString
msgDlg(msg
);
618 // this message is intentionally not translated -- it is for
620 msgDlg
+= wxT("\nDo you want to stop the program?\n")
621 wxT("You can also choose [Cancel] to suppress ")
622 wxT("further warnings.");
624 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
625 MB_YESNOCANCEL
| MB_ICONSTOP
) )
635 //case IDNO: nothing to do
638 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
641 // TODO: ask the user to enter "Y" or "N" on the console?
643 #endif // __WXMSW__/!__WXMSW__
645 // continue with the asserts
649 // show the assert modal dialog
651 void ShowAssertDialog(const wxChar
*szFile
,
653 const wxChar
*szCond
,
657 // this variable can be set to true to suppress "assert failure" messages
658 static bool s_bNoAsserts
= FALSE
;
663 // make life easier for people using VC++ IDE by using this format: like
664 // this, clicking on the message will take us immediately to the place of
666 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
670 msg
<< _T(": ") << szMsg
;
672 else // no message given
678 // if we are not in the main thread, output the assert directly and trap
679 // since dialogs cannot be displayed
680 if ( !wxThread::IsMain() )
682 msg
+= wxT(" [in child thread]");
684 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
686 OutputDebugString(msg
);
689 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
692 // He-e-e-e-elp!! we're asserting in a child thread
695 #endif // wxUSE_THREADS
699 // send it to the normal log destination
700 wxLogDebug(_T("%s"), msg
.c_str());
704 // delegate showing assert dialog (if possible) to that class
705 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
707 else // no traits object
709 // fall back to the function of last resort
710 s_bNoAsserts
= DoShowAssertDialog(msg
);
715 #endif // __WXDEBUG__