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"
36 #include "wx/apptrait.h"
37 #include "wx/cmdline.h"
38 #include "wx/confbase.h"
40 #include "wx/fontmap.h"
41 #endif // wxUSE_FONTMAP
42 #include "wx/msgout.h"
43 #include "wx/tokenzr.h"
45 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
46 #include <signal.h> // for SIGTRAP used by wxTrap()
49 #if defined(__WXMSW__)
50 #include "wx/msw/private.h" // includes windows.h for MessageBox()
53 #if defined(__WXMAC__)
54 #include "wx/mac/private.h" // includes mac headers
57 // ----------------------------------------------------------------------------
58 // private functions prototypes
59 // ----------------------------------------------------------------------------
62 // really just show the assert dialog
63 static bool DoShowAssertDialog(const wxString
& msg
);
65 // prepare for showing the assert dialog, use the given traits or
66 // DoShowAssertDialog() as last fallback to really show it
68 void ShowAssertDialog(const wxChar
*szFile
,
72 wxAppTraits
*traits
= NULL
);
74 // turn on the trace masks specified in the env variable WXTRACE
75 static void LINKAGEMODE
SetTraceMasks();
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 wxApp
*wxTheApp
= NULL
;
84 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
86 // ============================================================================
87 // wxAppConsole implementation
88 // ============================================================================
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 wxAppConsole::wxAppConsole()
98 wxTheApp
= (wxApp
*)this;
105 wxAppConsole::~wxAppConsole()
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 bool wxAppConsole::OnInit()
116 #if wxUSE_CMDLINE_PARSER
117 wxCmdLineParser
parser(argc
, argv
);
119 OnInitCmdLine(parser
);
122 switch ( parser
.Parse(FALSE
/* don't show usage */) )
125 cont
= OnCmdLineHelp(parser
);
129 cont
= OnCmdLineParsed(parser
);
133 cont
= OnCmdLineError(parser
);
139 #endif // wxUSE_CMDLINE_PARSER
144 int wxAppConsole::OnExit()
147 // delete the config object if any (don't use Get() here, but Set()
148 // because Get() could create a new config object)
149 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
150 #endif // wxUSE_CONFIG
152 #ifdef __WXUNIVERSAL__
153 delete wxTheme::Set(NULL
);
154 #endif // __WXUNIVERSAL__
156 // use Set(NULL) and not Get() to avoid creating a message output object on
157 // demand when we just want to delete it
158 delete wxMessageOutput::Set(NULL
);
163 void wxAppConsole::Exit()
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 wxAppTraits
*wxAppConsole::CreateTraits()
174 return wxAppTraits::CreateConsole();
177 wxAppTraits
*wxAppConsole::GetTraits()
179 // FIXME-MT: protect this with a CS?
182 m_traits
= CreateTraits();
184 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
190 // we must implement CreateXXX() in wxApp itself for backwards compatibility
191 #if WXWIN_COMPATIBILITY_2_4
195 wxLog
*wxAppConsole::CreateLogTarget()
197 wxAppTraits
*traits
= GetTraits();
198 return traits
? traits
->CreateLogTarget() : NULL
;
203 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
205 wxAppTraits
*traits
= GetTraits();
206 return traits
? traits
->CreateMessageOutput() : NULL
;
209 #endif // WXWIN_COMPATIBILITY_2_4
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
215 void wxAppConsole::ProcessPendingEvents()
217 // ensure that we're the only thread to modify the pending events list
218 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
220 if ( !wxPendingEvents
)
222 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
226 // iterate until the list becomes empty
227 wxNode
*node
= wxPendingEvents
->GetFirst();
230 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
233 // In ProcessPendingEvents(), new handlers might be add
234 // and we can safely leave the critical section here.
235 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
236 handler
->ProcessPendingEvents();
237 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
239 node
= wxPendingEvents
->GetFirst();
242 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
245 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
247 // process the events normally by default
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 #if wxUSE_CMDLINE_PARSER
257 #define OPTION_VERBOSE _T("verbose")
258 #define OPTION_THEME _T("theme")
259 #define OPTION_MODE _T("mode")
261 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
263 // the standard command line options
264 static const wxCmdLineEntryDesc cmdLineDesc
[] =
270 gettext_noop("show this help message"),
272 wxCMD_LINE_OPTION_HELP
280 gettext_noop("generate verbose log messages"),
286 #ifdef __WXUNIVERSAL__
291 gettext_noop("specify the theme to use"),
292 wxCMD_LINE_VAL_STRING
,
295 #endif // __WXUNIVERSAL__
297 #if defined(__WXMGL__)
298 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
299 // should provide this option. That's why it is in common/appcmn.cpp
300 // and not mgl/app.cpp
305 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
306 wxCMD_LINE_VAL_STRING
,
322 parser
.SetDesc(cmdLineDesc
);
325 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
328 if ( parser
.Found(OPTION_VERBOSE
) )
330 wxLog::SetVerbose(TRUE
);
334 #ifdef __WXUNIVERSAL__
336 if ( parser
.Found(OPTION_THEME
, &themeName
) )
338 wxTheme
*theme
= wxTheme::Create(themeName
);
341 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
345 // Delete the defaultly created theme and set the new theme.
346 delete wxTheme::Get();
349 #endif // __WXUNIVERSAL__
351 #if defined(__WXMGL__)
353 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
356 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
358 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
362 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
370 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
377 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
384 #endif // wxUSE_CMDLINE_PARSER
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
391 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
393 #define wxCMP(what) (what == opts.m_ ## what)
402 int verMaj
= wxMAJOR_VERSION
,
403 verMin
= wxMINOR_VERSION
;
405 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
408 wxString libDebug
, progDebug
;
411 libDebug
= wxT("debug");
413 libDebug
= wxT("no debug");
416 progDebug
= wxT("debug");
418 progDebug
= wxT("no debug");
420 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)."),
421 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
423 wxLogFatalError(msg
);
425 // normally wxLogFatalError doesn't return
435 void wxAppConsole::OnAssert(const wxChar
*file
,
440 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
443 #endif // __WXDEBUG__
445 // ============================================================================
446 // other classes implementations
447 // ============================================================================
449 // ----------------------------------------------------------------------------
450 // wxConsoleAppTraitsBase
451 // ----------------------------------------------------------------------------
455 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
457 return new wxLogStderr
;
462 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
464 return new wxMessageOutputStderr
;
469 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
471 return (wxFontMapper
*)new wxFontMapperBase
;
474 #endif // wxUSE_FONTMAP
477 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
479 return wxAppTraitsBase::ShowAssertDialog(msg
);
483 bool wxConsoleAppTraitsBase::HasStderr()
485 // console applications always have stderr, even under Mac/Windows
489 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
494 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
505 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
507 return DoShowAssertDialog(msg
);
510 #endif // __WXDEBUG__
512 wxAppTraits
*wxAppTraitsBase::CreateConsole()
514 return new wxConsoleAppTraits
;
517 // ============================================================================
518 // global functions implementation
519 // ============================================================================
529 // what else can we do?
538 wxTheApp
->WakeUpIdle();
540 //else: do nothing, what can we do?
546 bool wxAssertIsEqual(int x
, int y
)
551 // break into the debugger
554 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
556 #elif defined(__WXMAC__) && !defined(__DARWIN__)
562 #elif defined(__UNIX__)
569 void wxAssert(int cond
,
570 const wxChar
*szFile
,
572 const wxChar
*szCond
,
576 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
579 // this function is called when an assert fails
580 void wxOnAssert(const wxChar
*szFile
,
582 const wxChar
*szCond
,
586 static bool s_bInAssert
= FALSE
;
590 // He-e-e-e-elp!! we're trapped in endless loop
602 // by default, show the assert dialog box -- we can't customize this
604 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
608 // let the app process it as it wants
609 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
615 #endif // __WXDEBUG__
617 // ============================================================================
618 // private functions implementation
619 // ============================================================================
623 static void LINKAGEMODE
SetTraceMasks()
627 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
629 wxStringTokenizer
tkn(mask
, wxT(",;:"));
630 while ( tkn
.HasMoreTokens() )
631 wxLog::AddTraceMask(tkn
.GetNextToken());
636 bool DoShowAssertDialog(const wxString
& msg
)
638 // under MSW we can show the dialog even in the console mode
639 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
640 wxString
msgDlg(msg
);
642 // this message is intentionally not translated -- it is for
644 msgDlg
+= wxT("\nDo you want to stop the program?\n")
645 wxT("You can also choose [Cancel] to suppress ")
646 wxT("further warnings.");
648 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
649 MB_YESNOCANCEL
| MB_ICONSTOP
) )
659 //case IDNO: nothing to do
662 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
665 // TODO: ask the user to enter "Y" or "N" on the console?
667 #endif // __WXMSW__/!__WXMSW__
669 // continue with the asserts
673 // show the assert modal dialog
675 void ShowAssertDialog(const wxChar
*szFile
,
677 const wxChar
*szCond
,
681 // this variable can be set to true to suppress "assert failure" messages
682 static bool s_bNoAsserts
= FALSE
;
687 // make life easier for people using VC++ IDE by using this format: like
688 // this, clicking on the message will take us immediately to the place of
690 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
694 msg
<< _T(": ") << szMsg
;
696 else // no message given
702 // if we are not in the main thread, output the assert directly and trap
703 // since dialogs cannot be displayed
704 if ( !wxThread::IsMain() )
706 msg
+= wxT(" [in child thread]");
708 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
710 OutputDebugString(msg
);
713 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
716 // He-e-e-e-elp!! we're asserting in a child thread
719 #endif // wxUSE_THREADS
723 // send it to the normal log destination
724 wxLogDebug(_T("%s"), msg
);
728 // delegate showing assert dialog (if possible) to that class
729 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
731 else // no traits object
733 // fall back to the function of last resort
734 s_bNoAsserts
= DoShowAssertDialog(msg
);
739 #endif // __WXDEBUG__