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 // VZ: MacTypes.h is enough under Mac OS X (where I could test it) but
55 // I don't know which headers are needed under earlier systems so
56 // include everything when in doubt
60 #include "wx/mac/private.h" // includes mac headers
64 // ----------------------------------------------------------------------------
65 // private functions prototypes
66 // ----------------------------------------------------------------------------
69 // really just show the assert dialog
70 static bool DoShowAssertDialog(const wxString
& msg
);
72 // prepare for showing the assert dialog, use the given traits or
73 // DoShowAssertDialog() as last fallback to really show it
75 void ShowAssertDialog(const wxChar
*szFile
,
79 wxAppTraits
*traits
= NULL
);
81 // turn on the trace masks specified in the env variable WXTRACE
82 static void LINKAGEMODE
SetTraceMasks();
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 wxApp
*wxTheApp
= NULL
;
91 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
93 // ============================================================================
94 // wxAppConsole implementation
95 // ============================================================================
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 wxAppConsole::wxAppConsole()
105 wxTheApp
= (wxApp
*)this;
112 wxAppConsole::~wxAppConsole()
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 bool wxAppConsole::OnInit()
123 #if wxUSE_CMDLINE_PARSER
124 wxCmdLineParser
parser(argc
, argv
);
126 OnInitCmdLine(parser
);
129 switch ( parser
.Parse(FALSE
/* don't show usage */) )
132 cont
= OnCmdLineHelp(parser
);
136 cont
= OnCmdLineParsed(parser
);
140 cont
= OnCmdLineError(parser
);
146 #endif // wxUSE_CMDLINE_PARSER
151 int wxAppConsole::OnExit()
154 // delete the config object if any (don't use Get() here, but Set()
155 // because Get() could create a new config object)
156 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
157 #endif // wxUSE_CONFIG
159 #ifdef __WXUNIVERSAL__
160 delete wxTheme::Set(NULL
);
161 #endif // __WXUNIVERSAL__
163 // use Set(NULL) and not Get() to avoid creating a message output object on
164 // demand when we just want to delete it
165 delete wxMessageOutput::Set(NULL
);
170 void wxAppConsole::Exit()
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 wxAppTraits
*wxAppConsole::CreateTraits()
181 return wxAppTraits::CreateConsole();
184 wxAppTraits
*wxAppConsole::GetTraits()
186 // FIXME-MT: protect this with a CS?
189 m_traits
= CreateTraits();
191 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
197 // we must implement CreateXXX() in wxApp itself for backwards compatibility
198 #if WXWIN_COMPATIBILITY_2_4
202 wxLog
*wxAppConsole::CreateLogTarget()
204 wxAppTraits
*traits
= GetTraits();
205 return traits
? traits
->CreateLogTarget() : NULL
;
210 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
212 wxAppTraits
*traits
= GetTraits();
213 return traits
? traits
->CreateMessageOutput() : NULL
;
216 #endif // WXWIN_COMPATIBILITY_2_4
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 void wxAppConsole::ProcessPendingEvents()
224 // ensure that we're the only thread to modify the pending events list
225 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
227 if ( !wxPendingEvents
)
229 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
233 // iterate until the list becomes empty
234 wxNode
*node
= wxPendingEvents
->GetFirst();
237 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
240 // In ProcessPendingEvents(), new handlers might be add
241 // and we can safely leave the critical section here.
242 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
243 handler
->ProcessPendingEvents();
244 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
246 node
= wxPendingEvents
->GetFirst();
249 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
252 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
254 // process the events normally by default
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 #if wxUSE_CMDLINE_PARSER
264 #define OPTION_VERBOSE _T("verbose")
265 #define OPTION_THEME _T("theme")
266 #define OPTION_MODE _T("mode")
268 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
270 // the standard command line options
271 static const wxCmdLineEntryDesc cmdLineDesc
[] =
277 gettext_noop("show this help message"),
279 wxCMD_LINE_OPTION_HELP
287 gettext_noop("generate verbose log messages"),
293 #ifdef __WXUNIVERSAL__
298 gettext_noop("specify the theme to use"),
299 wxCMD_LINE_VAL_STRING
,
302 #endif // __WXUNIVERSAL__
304 #if defined(__WXMGL__)
305 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
306 // should provide this option. That's why it is in common/appcmn.cpp
307 // and not mgl/app.cpp
312 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
313 wxCMD_LINE_VAL_STRING
,
329 parser
.SetDesc(cmdLineDesc
);
332 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
335 if ( parser
.Found(OPTION_VERBOSE
) )
337 wxLog::SetVerbose(TRUE
);
341 #ifdef __WXUNIVERSAL__
343 if ( parser
.Found(OPTION_THEME
, &themeName
) )
345 wxTheme
*theme
= wxTheme::Create(themeName
);
348 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
352 // Delete the defaultly created theme and set the new theme.
353 delete wxTheme::Get();
356 #endif // __WXUNIVERSAL__
358 #if defined(__WXMGL__)
360 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
363 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
365 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
369 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
377 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
384 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
391 #endif // wxUSE_CMDLINE_PARSER
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
398 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
400 #define wxCMP(what) (what == opts.m_ ## what)
409 int verMaj
= wxMAJOR_VERSION
,
410 verMin
= wxMINOR_VERSION
;
412 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
415 wxString libDebug
, progDebug
;
418 libDebug
= wxT("debug");
420 libDebug
= wxT("no debug");
423 progDebug
= wxT("debug");
425 progDebug
= wxT("no debug");
427 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)."),
428 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
430 wxLogFatalError(msg
);
432 // normally wxLogFatalError doesn't return
442 void wxAppConsole::OnAssert(const wxChar
*file
,
447 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
450 #endif // __WXDEBUG__
452 // ============================================================================
453 // other classes implementations
454 // ============================================================================
456 // ----------------------------------------------------------------------------
457 // wxConsoleAppTraitsBase
458 // ----------------------------------------------------------------------------
462 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
464 return new wxLogStderr
;
469 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
471 return new wxMessageOutputStderr
;
476 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
478 return (wxFontMapper
*)new wxFontMapperBase
;
481 #endif // wxUSE_FONTMAP
484 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
486 return wxAppTraitsBase::ShowAssertDialog(msg
);
490 bool wxConsoleAppTraitsBase::HasStderr()
492 // console applications always have stderr, even under Mac/Windows
496 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
501 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
512 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
514 return DoShowAssertDialog(msg
);
517 #endif // __WXDEBUG__
519 wxAppTraits
*wxAppTraitsBase::CreateConsole()
521 return new wxConsoleAppTraits
;
524 // ============================================================================
525 // global functions implementation
526 // ============================================================================
536 // what else can we do?
545 wxTheApp
->WakeUpIdle();
547 //else: do nothing, what can we do?
553 bool wxAssertIsEqual(int x
, int y
)
558 // break into the debugger
561 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
563 #elif defined(__WXMAC__) && !defined(__DARWIN__)
569 #elif defined(__UNIX__)
576 void wxAssert(int cond
,
577 const wxChar
*szFile
,
579 const wxChar
*szCond
,
583 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
586 // this function is called when an assert fails
587 void wxOnAssert(const wxChar
*szFile
,
589 const wxChar
*szCond
,
593 static bool s_bInAssert
= FALSE
;
597 // He-e-e-e-elp!! we're trapped in endless loop
609 // by default, show the assert dialog box -- we can't customize this
611 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
615 // let the app process it as it wants
616 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
622 #endif // __WXDEBUG__
624 // ============================================================================
625 // private functions implementation
626 // ============================================================================
630 static void LINKAGEMODE
SetTraceMasks()
634 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
636 wxStringTokenizer
tkn(mask
, wxT(",;:"));
637 while ( tkn
.HasMoreTokens() )
638 wxLog::AddTraceMask(tkn
.GetNextToken());
643 bool DoShowAssertDialog(const wxString
& msg
)
645 // under MSW we can show the dialog even in the console mode
646 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
647 wxString
msgDlg(msg
);
649 // this message is intentionally not translated -- it is for
651 msgDlg
+= wxT("\nDo you want to stop the program?\n")
652 wxT("You can also choose [Cancel] to suppress ")
653 wxT("further warnings.");
655 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
656 MB_YESNOCANCEL
| MB_ICONSTOP
) )
666 //case IDNO: nothing to do
669 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
672 // TODO: ask the user to enter "Y" or "N" on the console?
674 #endif // __WXMSW__/!__WXMSW__
676 // continue with the asserts
680 // show the assert modal dialog
682 void ShowAssertDialog(const wxChar
*szFile
,
684 const wxChar
*szCond
,
688 // this variable can be set to true to suppress "assert failure" messages
689 static bool s_bNoAsserts
= FALSE
;
694 // make life easier for people using VC++ IDE by using this format: like
695 // this, clicking on the message will take us immediately to the place of
697 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
701 msg
<< _T(": ") << szMsg
;
703 else // no message given
709 // if we are not in the main thread, output the assert directly and trap
710 // since dialogs cannot be displayed
711 if ( !wxThread::IsMain() )
713 msg
+= wxT(" [in child thread]");
715 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
717 OutputDebugString(msg
);
720 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
723 // He-e-e-e-elp!! we're asserting in a child thread
726 #endif // wxUSE_THREADS
730 // send it to the normal log destination
731 wxLogDebug(_T("%s"), msg
);
735 // delegate showing assert dialog (if possible) to that class
736 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
738 else // no traits object
740 // fall back to the function of last resort
741 s_bNoAsserts
= DoShowAssertDialog(msg
);
746 #endif // __WXDEBUG__