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
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 #if wxUSE_CMDLINE_PARSER
286 #define OPTION_VERBOSE _T("verbose")
288 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
290 // the standard command line options
291 static const wxCmdLineEntryDesc cmdLineDesc
[] =
297 gettext_noop("show this help message"),
299 wxCMD_LINE_OPTION_HELP
307 gettext_noop("generate verbose log messages"),
324 parser
.SetDesc(cmdLineDesc
);
327 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
330 if ( parser
.Found(OPTION_VERBOSE
) )
332 wxLog::SetVerbose(TRUE
);
339 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
346 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
353 #endif // wxUSE_CMDLINE_PARSER
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
360 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
362 #define wxCMP(what) (what == opts.m_ ## what)
371 int verMaj
= wxMAJOR_VERSION
,
372 verMin
= wxMINOR_VERSION
;
374 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
377 wxString libDebug
, progDebug
;
380 libDebug
= wxT("debug");
382 libDebug
= wxT("no debug");
385 progDebug
= wxT("debug");
387 progDebug
= wxT("no debug");
389 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %d.%d (%s), and your program used %d.%d (%s)."),
390 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
392 wxLogFatalError(msg
);
394 // normally wxLogFatalError doesn't return
404 void wxAppConsole::OnAssert(const wxChar
*file
,
409 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
412 #endif // __WXDEBUG__
414 // ============================================================================
415 // other classes implementations
416 // ============================================================================
418 // ----------------------------------------------------------------------------
419 // wxConsoleAppTraitsBase
420 // ----------------------------------------------------------------------------
424 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
426 return new wxLogStderr
;
431 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
433 return new wxMessageOutputStderr
;
438 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
440 return (wxFontMapper
*)new wxFontMapperBase
;
443 #endif // wxUSE_FONTMAP
445 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
447 // console applications don't use renderers
452 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
454 return wxAppTraitsBase::ShowAssertDialog(msg
);
458 bool wxConsoleAppTraitsBase::HasStderr()
460 // console applications always have stderr, even under Mac/Windows
464 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
469 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
474 // ----------------------------------------------------------------------------
476 // ----------------------------------------------------------------------------
480 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
482 return DoShowAssertDialog(msg
);
485 #endif // __WXDEBUG__
487 // ============================================================================
488 // global functions implementation
489 // ============================================================================
499 // what else can we do?
508 wxTheApp
->WakeUpIdle();
510 //else: do nothing, what can we do?
516 bool wxAssertIsEqual(int x
, int y
)
521 // break into the debugger
524 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
526 #elif defined(__WXMAC__) && !defined(__DARWIN__)
532 #elif defined(__UNIX__)
539 void wxAssert(int cond
,
540 const wxChar
*szFile
,
542 const wxChar
*szCond
,
546 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
549 // this function is called when an assert fails
550 void wxOnAssert(const wxChar
*szFile
,
552 const wxChar
*szCond
,
556 static bool s_bInAssert
= FALSE
;
560 // He-e-e-e-elp!! we're trapped in endless loop
572 // by default, show the assert dialog box -- we can't customize this
574 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
578 // let the app process it as it wants
579 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
585 #endif // __WXDEBUG__
587 // ============================================================================
588 // private functions implementation
589 // ============================================================================
593 static void LINKAGEMODE
SetTraceMasks()
597 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
599 wxStringTokenizer
tkn(mask
, wxT(",;:"));
600 while ( tkn
.HasMoreTokens() )
601 wxLog::AddTraceMask(tkn
.GetNextToken());
606 bool DoShowAssertDialog(const wxString
& msg
)
608 // under MSW we can show the dialog even in the console mode
609 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
610 wxString
msgDlg(msg
);
612 // this message is intentionally not translated -- it is for
614 msgDlg
+= wxT("\nDo you want to stop the program?\n")
615 wxT("You can also choose [Cancel] to suppress ")
616 wxT("further warnings.");
618 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
619 MB_YESNOCANCEL
| MB_ICONSTOP
) )
629 //case IDNO: nothing to do
632 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
635 // TODO: ask the user to enter "Y" or "N" on the console?
637 #endif // __WXMSW__/!__WXMSW__
639 // continue with the asserts
643 // show the assert modal dialog
645 void ShowAssertDialog(const wxChar
*szFile
,
647 const wxChar
*szCond
,
651 // this variable can be set to true to suppress "assert failure" messages
652 static bool s_bNoAsserts
= FALSE
;
657 // make life easier for people using VC++ IDE by using this format: like
658 // this, clicking on the message will take us immediately to the place of
660 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
664 msg
<< _T(": ") << szMsg
;
666 else // no message given
672 // if we are not in the main thread, output the assert directly and trap
673 // since dialogs cannot be displayed
674 if ( !wxThread::IsMain() )
676 msg
+= wxT(" [in child thread]");
678 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
680 OutputDebugString(msg
);
683 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
686 // He-e-e-e-elp!! we're asserting in a child thread
689 #endif // wxUSE_THREADS
693 // send it to the normal log destination
694 wxLogDebug(_T("%s"), msg
.c_str());
698 // delegate showing assert dialog (if possible) to that class
699 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
701 else // no traits object
703 // fall back to the function of last resort
704 s_bNoAsserts
= DoShowAssertDialog(msg
);
709 #endif // __WXDEBUG__