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 #ifdef __WXUNIVERSAL__
186 delete wxTheme::Set(NULL
);
187 #endif // __WXUNIVERSAL__
189 // use Set(NULL) and not Get() to avoid creating a message output object on
190 // demand when we just want to delete it
191 delete wxMessageOutput::Set(NULL
);
196 void wxAppConsole::Exit()
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 wxAppTraits
*wxAppConsole::CreateTraits()
207 return new wxConsoleAppTraits
;
210 wxAppTraits
*wxAppConsole::GetTraits()
212 // FIXME-MT: protect this with a CS?
215 m_traits
= CreateTraits();
217 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
223 // we must implement CreateXXX() in wxApp itself for backwards compatibility
224 #if WXWIN_COMPATIBILITY_2_4
228 wxLog
*wxAppConsole::CreateLogTarget()
230 wxAppTraits
*traits
= GetTraits();
231 return traits
? traits
->CreateLogTarget() : NULL
;
236 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
238 wxAppTraits
*traits
= GetTraits();
239 return traits
? traits
->CreateMessageOutput() : NULL
;
242 #endif // WXWIN_COMPATIBILITY_2_4
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 void wxAppConsole::ProcessPendingEvents()
250 // ensure that we're the only thread to modify the pending events list
251 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
253 if ( !wxPendingEvents
)
255 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
259 // iterate until the list becomes empty
260 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
263 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
264 wxPendingEvents
->Erase(node
);
266 // In ProcessPendingEvents(), new handlers might be add
267 // and we can safely leave the critical section here.
268 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
269 handler
->ProcessPendingEvents();
270 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
272 node
= wxPendingEvents
->GetFirst();
275 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
278 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
280 // process the events normally by default
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 #if wxUSE_CMDLINE_PARSER
290 #define OPTION_VERBOSE _T("verbose")
291 #define OPTION_THEME _T("theme")
292 #define OPTION_MODE _T("mode")
294 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
296 // the standard command line options
297 static const wxCmdLineEntryDesc cmdLineDesc
[] =
303 gettext_noop("show this help message"),
305 wxCMD_LINE_OPTION_HELP
313 gettext_noop("generate verbose log messages"),
319 #ifdef __WXUNIVERSAL__
324 gettext_noop("specify the theme to use"),
325 wxCMD_LINE_VAL_STRING
,
328 #endif // __WXUNIVERSAL__
330 #if defined(__WXMGL__)
331 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
332 // should provide this option. That's why it is in common/appcmn.cpp
333 // and not mgl/app.cpp
338 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
339 wxCMD_LINE_VAL_STRING
,
355 parser
.SetDesc(cmdLineDesc
);
358 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
361 if ( parser
.Found(OPTION_VERBOSE
) )
363 wxLog::SetVerbose(TRUE
);
367 #ifdef __WXUNIVERSAL__
369 if ( parser
.Found(OPTION_THEME
, &themeName
) )
371 wxTheme
*theme
= wxTheme::Create(themeName
);
374 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
378 // Delete the defaultly created theme and set the new theme.
379 delete wxTheme::Get();
382 #endif // __WXUNIVERSAL__
384 #if defined(__WXMGL__)
386 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
389 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
391 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
395 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
403 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
410 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
417 #endif // wxUSE_CMDLINE_PARSER
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
424 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
426 #define wxCMP(what) (what == opts.m_ ## what)
435 int verMaj
= wxMAJOR_VERSION
,
436 verMin
= wxMINOR_VERSION
;
438 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
441 wxString libDebug
, progDebug
;
444 libDebug
= wxT("debug");
446 libDebug
= wxT("no debug");
449 progDebug
= wxT("debug");
451 progDebug
= wxT("no debug");
453 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)."),
454 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
456 wxLogFatalError(msg
);
458 // normally wxLogFatalError doesn't return
468 void wxAppConsole::OnAssert(const wxChar
*file
,
473 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
476 #endif // __WXDEBUG__
478 // ============================================================================
479 // other classes implementations
480 // ============================================================================
482 // ----------------------------------------------------------------------------
483 // wxConsoleAppTraitsBase
484 // ----------------------------------------------------------------------------
488 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
490 return new wxLogStderr
;
495 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
497 return new wxMessageOutputStderr
;
502 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
504 return (wxFontMapper
*)new wxFontMapperBase
;
507 #endif // wxUSE_FONTMAP
509 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
511 // console applications don't use renderers
516 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
518 return wxAppTraitsBase::ShowAssertDialog(msg
);
522 bool wxConsoleAppTraitsBase::HasStderr()
524 // console applications always have stderr, even under Mac/Windows
528 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
533 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
544 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
546 return DoShowAssertDialog(msg
);
549 #endif // __WXDEBUG__
551 // ============================================================================
552 // global functions implementation
553 // ============================================================================
563 // what else can we do?
572 wxTheApp
->WakeUpIdle();
574 //else: do nothing, what can we do?
580 bool wxAssertIsEqual(int x
, int y
)
585 // break into the debugger
588 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
590 #elif defined(__WXMAC__) && !defined(__DARWIN__)
596 #elif defined(__UNIX__)
603 void wxAssert(int cond
,
604 const wxChar
*szFile
,
606 const wxChar
*szCond
,
610 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
613 // this function is called when an assert fails
614 void wxOnAssert(const wxChar
*szFile
,
616 const wxChar
*szCond
,
620 static bool s_bInAssert
= FALSE
;
624 // He-e-e-e-elp!! we're trapped in endless loop
636 // by default, show the assert dialog box -- we can't customize this
638 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
642 // let the app process it as it wants
643 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
649 #endif // __WXDEBUG__
651 // ============================================================================
652 // private functions implementation
653 // ============================================================================
657 static void LINKAGEMODE
SetTraceMasks()
661 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
663 wxStringTokenizer
tkn(mask
, wxT(",;:"));
664 while ( tkn
.HasMoreTokens() )
665 wxLog::AddTraceMask(tkn
.GetNextToken());
670 bool DoShowAssertDialog(const wxString
& msg
)
672 // under MSW we can show the dialog even in the console mode
673 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
674 wxString
msgDlg(msg
);
676 // this message is intentionally not translated -- it is for
678 msgDlg
+= wxT("\nDo you want to stop the program?\n")
679 wxT("You can also choose [Cancel] to suppress ")
680 wxT("further warnings.");
682 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
683 MB_YESNOCANCEL
| MB_ICONSTOP
) )
693 //case IDNO: nothing to do
696 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
699 // TODO: ask the user to enter "Y" or "N" on the console?
701 #endif // __WXMSW__/!__WXMSW__
703 // continue with the asserts
707 // show the assert modal dialog
709 void ShowAssertDialog(const wxChar
*szFile
,
711 const wxChar
*szCond
,
715 // this variable can be set to true to suppress "assert failure" messages
716 static bool s_bNoAsserts
= FALSE
;
721 // make life easier for people using VC++ IDE by using this format: like
722 // this, clicking on the message will take us immediately to the place of
724 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
728 msg
<< _T(": ") << szMsg
;
730 else // no message given
736 // if we are not in the main thread, output the assert directly and trap
737 // since dialogs cannot be displayed
738 if ( !wxThread::IsMain() )
740 msg
+= wxT(" [in child thread]");
742 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
744 OutputDebugString(msg
);
747 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
750 // He-e-e-e-elp!! we're asserting in a child thread
753 #endif // wxUSE_THREADS
757 // send it to the normal log destination
758 wxLogDebug(_T("%s"), msg
.c_str());
762 // delegate showing assert dialog (if possible) to that class
763 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
765 else // no traits object
767 // fall back to the function of last resort
768 s_bNoAsserts
= DoShowAssertDialog(msg
);
773 #endif // __WXDEBUG__