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/filename.h"
41 #endif // wxUSE_FILENAME
43 #include "wx/fontmap.h"
44 #endif // wxUSE_FONTMAP
45 #include "wx/msgout.h"
46 #include "wx/tokenzr.h"
48 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
49 #include <signal.h> // for SIGTRAP used by wxTrap()
52 #if defined(__WXMSW__)
53 #include "wx/msw/private.h" // includes windows.h for MessageBox()
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 wxApp
*wxTheApp
= NULL
;
94 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
96 // ============================================================================
97 // wxAppConsole implementation
98 // ============================================================================
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 wxAppConsole::wxAppConsole()
108 wxTheApp
= (wxApp
*)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
134 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
135 #else // !wxUSE_FILENAME
137 #endif // wxUSE_FILENAME/!wxUSE_FILENAME
143 void wxAppConsole::CleanUp()
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 bool wxAppConsole::OnInit()
153 #if wxUSE_CMDLINE_PARSER
154 wxCmdLineParser
parser(argc
, argv
);
156 OnInitCmdLine(parser
);
159 switch ( parser
.Parse(FALSE
/* don't show usage */) )
162 cont
= OnCmdLineHelp(parser
);
166 cont
= OnCmdLineParsed(parser
);
170 cont
= OnCmdLineError(parser
);
176 #endif // wxUSE_CMDLINE_PARSER
181 int wxAppConsole::OnExit()
184 // delete the config object if any (don't use Get() here, but Set()
185 // because Get() could create a new config object)
186 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
187 #endif // wxUSE_CONFIG
189 #ifdef __WXUNIVERSAL__
190 delete wxTheme::Set(NULL
);
191 #endif // __WXUNIVERSAL__
193 // use Set(NULL) and not Get() to avoid creating a message output object on
194 // demand when we just want to delete it
195 delete wxMessageOutput::Set(NULL
);
200 void wxAppConsole::Exit()
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 wxAppTraits
*wxAppConsole::CreateTraits()
211 return new wxConsoleAppTraits
;
214 wxAppTraits
*wxAppConsole::GetTraits()
216 // FIXME-MT: protect this with a CS?
219 m_traits
= CreateTraits();
221 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
227 // we must implement CreateXXX() in wxApp itself for backwards compatibility
228 #if WXWIN_COMPATIBILITY_2_4
232 wxLog
*wxAppConsole::CreateLogTarget()
234 wxAppTraits
*traits
= GetTraits();
235 return traits
? traits
->CreateLogTarget() : NULL
;
240 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
242 wxAppTraits
*traits
= GetTraits();
243 return traits
? traits
->CreateMessageOutput() : NULL
;
246 #endif // WXWIN_COMPATIBILITY_2_4
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 void wxAppConsole::ProcessPendingEvents()
254 // ensure that we're the only thread to modify the pending events list
255 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
257 if ( !wxPendingEvents
)
259 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
263 // iterate until the list becomes empty
264 wxNode
*node
= wxPendingEvents
->GetFirst();
267 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
270 // In ProcessPendingEvents(), new handlers might be add
271 // and we can safely leave the critical section here.
272 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
273 handler
->ProcessPendingEvents();
274 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
276 node
= wxPendingEvents
->GetFirst();
279 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
282 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
284 // process the events normally by default
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 #if wxUSE_CMDLINE_PARSER
294 #define OPTION_VERBOSE _T("verbose")
295 #define OPTION_THEME _T("theme")
296 #define OPTION_MODE _T("mode")
298 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
300 // the standard command line options
301 static const wxCmdLineEntryDesc cmdLineDesc
[] =
307 gettext_noop("show this help message"),
309 wxCMD_LINE_OPTION_HELP
317 gettext_noop("generate verbose log messages"),
323 #ifdef __WXUNIVERSAL__
328 gettext_noop("specify the theme to use"),
329 wxCMD_LINE_VAL_STRING
,
332 #endif // __WXUNIVERSAL__
334 #if defined(__WXMGL__)
335 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
336 // should provide this option. That's why it is in common/appcmn.cpp
337 // and not mgl/app.cpp
342 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
343 wxCMD_LINE_VAL_STRING
,
359 parser
.SetDesc(cmdLineDesc
);
362 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
365 if ( parser
.Found(OPTION_VERBOSE
) )
367 wxLog::SetVerbose(TRUE
);
371 #ifdef __WXUNIVERSAL__
373 if ( parser
.Found(OPTION_THEME
, &themeName
) )
375 wxTheme
*theme
= wxTheme::Create(themeName
);
378 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
382 // Delete the defaultly created theme and set the new theme.
383 delete wxTheme::Get();
386 #endif // __WXUNIVERSAL__
388 #if defined(__WXMGL__)
390 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
393 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
395 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
399 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
407 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
414 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
421 #endif // wxUSE_CMDLINE_PARSER
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
428 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
430 #define wxCMP(what) (what == opts.m_ ## what)
439 int verMaj
= wxMAJOR_VERSION
,
440 verMin
= wxMINOR_VERSION
;
442 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
445 wxString libDebug
, progDebug
;
448 libDebug
= wxT("debug");
450 libDebug
= wxT("no debug");
453 progDebug
= wxT("debug");
455 progDebug
= wxT("no debug");
457 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)."),
458 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
460 wxLogFatalError(msg
);
462 // normally wxLogFatalError doesn't return
472 void wxAppConsole::OnAssert(const wxChar
*file
,
477 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
480 #endif // __WXDEBUG__
482 // ============================================================================
483 // other classes implementations
484 // ============================================================================
486 // ----------------------------------------------------------------------------
487 // wxConsoleAppTraitsBase
488 // ----------------------------------------------------------------------------
492 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
494 return new wxLogStderr
;
499 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
501 return new wxMessageOutputStderr
;
506 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
508 return (wxFontMapper
*)new wxFontMapperBase
;
511 #endif // wxUSE_FONTMAP
514 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
516 return wxAppTraitsBase::ShowAssertDialog(msg
);
520 bool wxConsoleAppTraitsBase::HasStderr()
522 // console applications always have stderr, even under Mac/Windows
526 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
531 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
542 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
544 return DoShowAssertDialog(msg
);
547 #endif // __WXDEBUG__
549 // ============================================================================
550 // global functions implementation
551 // ============================================================================
561 // what else can we do?
570 wxTheApp
->WakeUpIdle();
572 //else: do nothing, what can we do?
578 bool wxAssertIsEqual(int x
, int y
)
583 // break into the debugger
586 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
588 #elif defined(__WXMAC__) && !defined(__DARWIN__)
594 #elif defined(__UNIX__)
601 void wxAssert(int cond
,
602 const wxChar
*szFile
,
604 const wxChar
*szCond
,
608 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
611 // this function is called when an assert fails
612 void wxOnAssert(const wxChar
*szFile
,
614 const wxChar
*szCond
,
618 static bool s_bInAssert
= FALSE
;
622 // He-e-e-e-elp!! we're trapped in endless loop
634 // by default, show the assert dialog box -- we can't customize this
636 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
640 // let the app process it as it wants
641 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
647 #endif // __WXDEBUG__
649 // ============================================================================
650 // private functions implementation
651 // ============================================================================
655 static void LINKAGEMODE
SetTraceMasks()
659 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
661 wxStringTokenizer
tkn(mask
, wxT(",;:"));
662 while ( tkn
.HasMoreTokens() )
663 wxLog::AddTraceMask(tkn
.GetNextToken());
668 bool DoShowAssertDialog(const wxString
& msg
)
670 // under MSW we can show the dialog even in the console mode
671 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
672 wxString
msgDlg(msg
);
674 // this message is intentionally not translated -- it is for
676 msgDlg
+= wxT("\nDo you want to stop the program?\n")
677 wxT("You can also choose [Cancel] to suppress ")
678 wxT("further warnings.");
680 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
681 MB_YESNOCANCEL
| MB_ICONSTOP
) )
691 //case IDNO: nothing to do
694 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
697 // TODO: ask the user to enter "Y" or "N" on the console?
699 #endif // __WXMSW__/!__WXMSW__
701 // continue with the asserts
705 // show the assert modal dialog
707 void ShowAssertDialog(const wxChar
*szFile
,
709 const wxChar
*szCond
,
713 // this variable can be set to true to suppress "assert failure" messages
714 static bool s_bNoAsserts
= FALSE
;
719 // make life easier for people using VC++ IDE by using this format: like
720 // this, clicking on the message will take us immediately to the place of
722 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
726 msg
<< _T(": ") << szMsg
;
728 else // no message given
734 // if we are not in the main thread, output the assert directly and trap
735 // since dialogs cannot be displayed
736 if ( !wxThread::IsMain() )
738 msg
+= wxT(" [in child thread]");
740 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
742 OutputDebugString(msg
);
745 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
748 // He-e-e-e-elp!! we're asserting in a child thread
751 #endif // wxUSE_THREADS
755 // send it to the normal log destination
756 wxLogDebug(_T("%s"), msg
);
760 // delegate showing assert dialog (if possible) to that class
761 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
763 else // no traits object
765 // fall back to the function of last resort
766 s_bNoAsserts
= DoShowAssertDialog(msg
);
771 #endif // __WXDEBUG__