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"
33 #include "wx/apptrait.h"
34 #include "wx/cmdline.h"
35 #include "wx/confbase.h"
37 #include "wx/fontmap.h"
38 #endif // wxUSE_FONTMAP
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/private.h" // includes windows.h for MessageBox()
50 #if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
54 // ----------------------------------------------------------------------------
55 // private functions prototypes
56 // ----------------------------------------------------------------------------
59 // really just show the assert dialog
60 static bool DoShowAssertDialog(const wxString
& msg
);
62 // prepare for showing the assert dialog, use the given traits or
63 // DoShowAssertDialog() as last fallback to really show it
65 void ShowAssertDialog(const wxChar
*szFile
,
69 wxAppTraits
*traits
= NULL
);
71 // turn on the trace masks specified in the env variable WXTRACE
72 static void LINKAGEMODE
SetTraceMasks();
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 wxApp
*wxTheApp
= NULL
;
81 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
83 // ============================================================================
84 // wxAppConsole implementation
85 // ============================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 wxAppConsole::wxAppConsole()
95 wxTheApp
= (wxApp
*)this;
102 wxAppConsole::~wxAppConsole()
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 bool wxAppConsole::OnInit()
113 #if wxUSE_CMDLINE_PARSER
114 wxCmdLineParser
parser(argc
, argv
);
116 OnInitCmdLine(parser
);
119 switch ( parser
.Parse(FALSE
/* don't show usage */) )
122 cont
= OnCmdLineHelp(parser
);
126 cont
= OnCmdLineParsed(parser
);
130 cont
= OnCmdLineError(parser
);
136 #endif // wxUSE_CMDLINE_PARSER
141 int wxAppConsole::OnExit()
144 // delete the config object if any (don't use Get() here, but Set()
145 // because Get() could create a new config object)
146 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
147 #endif // wxUSE_CONFIG
149 #ifdef __WXUNIVERSAL__
150 delete wxTheme::Set(NULL
);
151 #endif // __WXUNIVERSAL__
153 // use Set(NULL) and not Get() to avoid creating a message output object on
154 // demand when we just want to delete it
155 delete wxMessageOutput::Set(NULL
);
160 void wxAppConsole::Exit()
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 wxAppTraits
*wxAppConsole::CreateTraits()
171 return wxAppTraits::CreateConsole();
174 wxAppTraits
*wxAppConsole::GetTraits()
176 // FIXME-MT: protect this with a CS?
179 m_traits
= CreateTraits();
181 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
187 // we must implement CreateXXX() in wxApp itself for backwards compatibility
188 #if WXWIN_COMPATIBILITY_2_4
192 wxLog
*wxAppConsole::CreateLogTarget()
194 wxAppTraits
*traits
= GetTraits();
195 return traits
? traits
->CreateLogTarget() : NULL
;
200 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
202 wxAppTraits
*traits
= GetTraits();
203 return traits
? traits
->CreateMessageOutput() : NULL
;
206 #endif // WXWIN_COMPATIBILITY_2_4
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void wxAppConsole::ProcessPendingEvents()
214 // ensure that we're the only thread to modify the pending events list
215 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
217 if ( !wxPendingEvents
)
219 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
223 // iterate until the list becomes empty
224 wxNode
*node
= wxPendingEvents
->GetFirst();
227 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
230 // In ProcessPendingEvents(), new handlers might be add
231 // and we can safely leave the critical section here.
232 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
233 handler
->ProcessPendingEvents();
234 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
236 node
= wxPendingEvents
->GetFirst();
239 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
242 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
244 // process the events normally by default
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 #if wxUSE_CMDLINE_PARSER
254 #define OPTION_VERBOSE _T("verbose")
255 #define OPTION_THEME _T("theme")
256 #define OPTION_MODE _T("mode")
258 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
260 // the standard command line options
261 static const wxCmdLineEntryDesc cmdLineDesc
[] =
267 gettext_noop("show this help message"),
269 wxCMD_LINE_OPTION_HELP
277 gettext_noop("generate verbose log messages"),
283 #ifdef __WXUNIVERSAL__
288 gettext_noop("specify the theme to use"),
289 wxCMD_LINE_VAL_STRING
,
292 #endif // __WXUNIVERSAL__
294 #if defined(__WXMGL__)
295 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
296 // should provide this option. That's why it is in common/appcmn.cpp
297 // and not mgl/app.cpp
302 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
303 wxCMD_LINE_VAL_STRING
,
319 parser
.SetDesc(cmdLineDesc
);
322 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
325 if ( parser
.Found(OPTION_VERBOSE
) )
327 wxLog::SetVerbose(TRUE
);
331 #ifdef __WXUNIVERSAL__
333 if ( parser
.Found(OPTION_THEME
, &themeName
) )
335 wxTheme
*theme
= wxTheme::Create(themeName
);
338 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
342 // Delete the defaultly created theme and set the new theme.
343 delete wxTheme::Get();
346 #endif // __WXUNIVERSAL__
348 #if defined(__WXMGL__)
350 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
353 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
355 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
359 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
367 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
374 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
381 #endif // wxUSE_CMDLINE_PARSER
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
388 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
390 #define wxCMP(what) (what == opts.m_ ## what)
399 int verMaj
= wxMAJOR_VERSION
,
400 verMin
= wxMINOR_VERSION
;
402 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
405 wxString libDebug
, progDebug
;
408 libDebug
= wxT("debug");
410 libDebug
= wxT("no debug");
413 progDebug
= wxT("debug");
415 progDebug
= wxT("no debug");
417 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)."),
418 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
420 wxLogFatalError(msg
);
422 // normally wxLogFatalError doesn't return
432 void wxAppConsole::OnAssert(const wxChar
*file
,
437 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
440 #endif // __WXDEBUG__
442 // ============================================================================
443 // other classes implementations
444 // ============================================================================
446 // ----------------------------------------------------------------------------
447 // wxConsoleAppTraitsBase
448 // ----------------------------------------------------------------------------
452 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
454 return new wxLogStderr
;
459 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
461 return new wxMessageOutputStderr
;
466 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
468 return (wxFontMapper
*)new wxFontMapperBase
;
471 #endif // wxUSE_FONTMAP
473 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
475 return wxAppTraitsBase::ShowAssertDialog(msg
);
478 bool wxConsoleAppTraitsBase::HasStderr()
480 // console applications always have stderr, even under Mac/Windows
484 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
489 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
500 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
502 return DoShowAssertDialog(msg
);
505 #endif // __WXDEBUG__
507 wxAppTraits
*wxAppTraitsBase::CreateConsole()
509 return new wxConsoleAppTraits
;
512 // ============================================================================
513 // global functions implementation
514 // ============================================================================
524 // what else can we do?
533 wxTheApp
->WakeUpIdle();
535 //else: do nothing, what can we do?
541 bool wxAssertIsEqual(int x
, int y
)
546 // break into the debugger
549 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
551 #elif defined(__WXMAC__) && !defined(__DARWIN__)
557 #elif defined(__UNIX__)
564 void wxAssert(int cond
,
565 const wxChar
*szFile
,
567 const wxChar
*szCond
,
571 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
574 // this function is called when an assert fails
575 void wxOnAssert(const wxChar
*szFile
,
577 const wxChar
*szCond
,
581 static bool s_bInAssert
= FALSE
;
585 // He-e-e-e-elp!! we're trapped in endless loop
597 // by default, show the assert dialog box -- we can't customize this
599 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
603 // let the app process it as it wants
604 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
610 #endif // __WXDEBUG__
612 // ============================================================================
613 // private functions implementation
614 // ============================================================================
618 static void LINKAGEMODE
SetTraceMasks()
622 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
624 wxStringTokenizer
tkn(mask
, wxT(",;:"));
625 while ( tkn
.HasMoreTokens() )
626 wxLog::AddTraceMask(tkn
.GetNextToken());
631 bool DoShowAssertDialog(const wxString
& msg
)
633 // under MSW we can show the dialog even in the console mode
634 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
635 wxString
msgDlg(msg
);
637 // this message is intentionally not translated -- it is for
639 msgDlg
+= wxT("\nDo you want to stop the program?\n")
640 wxT("You can also choose [Cancel] to suppress ")
641 wxT("further warnings.");
643 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
644 MB_YESNOCANCEL
| MB_ICONSTOP
) )
654 //case IDNO: nothing to do
657 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
660 // TODO: ask the user to enter "Y" or "N" on the console?
662 #endif // __WXMSW__/!__WXMSW__
664 // continue with the asserts
668 // show the assert modal dialog
670 void ShowAssertDialog(const wxChar
*szFile
,
672 const wxChar
*szCond
,
676 // this variable can be set to true to suppress "assert failure" messages
677 static bool s_bNoAsserts
= FALSE
;
682 // make life easier for people using VC++ IDE by using this format: like
683 // this, clicking on the message will take us immediately to the place of
685 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
689 msg
<< _T(": ") << szMsg
;
691 else // no message given
697 // if we are not in the main thread, output the assert directly and trap
698 // since dialogs cannot be displayed
699 if ( !wxThread::IsMain() )
701 msg
+= wxT(" [in child thread]");
703 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
705 OutputDebugString(msg
);
708 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
711 // He-e-e-e-elp!! we're asserting in a child thread
714 #endif // wxUSE_THREADS
718 // send it to the normal log destination
719 wxLogDebug(_T("%s"), msg
);
723 // delegate showing assert dialog (if possible) to that class
724 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
726 else // no traits object
728 // fall back to the function of last resort
729 s_bNoAsserts
= DoShowAssertDialog(msg
);
734 #endif // __WXDEBUG__