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"
41 #include "wx/filename.h"
42 #endif // wxUSE_FILENAME
44 #include "wx/fontmap.h"
45 #endif // wxUSE_FONTMAP
46 #include "wx/msgout.h"
47 #include "wx/tokenzr.h"
49 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
50 #include <signal.h> // for SIGTRAP used by wxTrap()
53 #if defined(__WXMSW__)
54 #include "wx/msw/private.h" // includes windows.h for MessageBox()
57 #if defined(__WXMAC__)
58 // VZ: MacTypes.h is enough under Mac OS X (where I could test it) but
59 // I don't know which headers are needed under earlier systems so
60 // include everything when in doubt
64 #include "wx/mac/private.h" // includes mac headers
68 // ----------------------------------------------------------------------------
69 // private functions prototypes
70 // ----------------------------------------------------------------------------
73 // really just show the assert dialog
74 static bool DoShowAssertDialog(const wxString
& msg
);
76 // prepare for showing the assert dialog, use the given traits or
77 // DoShowAssertDialog() as last fallback to really show it
79 void ShowAssertDialog(const wxChar
*szFile
,
83 wxAppTraits
*traits
= NULL
);
85 // turn on the trace masks specified in the env variable WXTRACE
86 static void LINKAGEMODE
SetTraceMasks();
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 wxApp
*wxTheApp
= NULL
;
95 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
97 // ============================================================================
98 // wxAppConsole implementation
99 // ============================================================================
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 wxAppConsole::wxAppConsole()
109 wxTheApp
= (wxApp
*)this;
116 wxAppConsole::~wxAppConsole()
121 // ----------------------------------------------------------------------------
122 // initilization/cleanup
123 // ----------------------------------------------------------------------------
125 bool wxAppConsole::Initialize(int& argc
, wxChar
**argv
)
127 // remember the command line arguments
131 if ( m_appName
.empty() && argv
)
133 // the application name is, by default, the name of its executable file
135 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
136 #else // !wxUSE_FILENAME
138 #endif // wxUSE_FILENAME/!wxUSE_FILENAME
144 void wxAppConsole::CleanUp()
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 bool wxAppConsole::OnInit()
154 #if wxUSE_CMDLINE_PARSER
155 wxCmdLineParser
parser(argc
, argv
);
157 OnInitCmdLine(parser
);
160 switch ( parser
.Parse(FALSE
/* don't show usage */) )
163 cont
= OnCmdLineHelp(parser
);
167 cont
= OnCmdLineParsed(parser
);
171 cont
= OnCmdLineError(parser
);
177 #endif // wxUSE_CMDLINE_PARSER
182 int wxAppConsole::OnExit()
185 // delete the config object if any (don't use Get() here, but Set()
186 // because Get() could create a new config object)
187 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
188 #endif // wxUSE_CONFIG
190 #ifdef __WXUNIVERSAL__
191 delete wxTheme::Set(NULL
);
192 #endif // __WXUNIVERSAL__
194 // use Set(NULL) and not Get() to avoid creating a message output object on
195 // demand when we just want to delete it
196 delete wxMessageOutput::Set(NULL
);
201 void wxAppConsole::Exit()
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 wxAppTraits
*wxAppConsole::CreateTraits()
212 return new wxConsoleAppTraits
;
215 wxAppTraits
*wxAppConsole::GetTraits()
217 // FIXME-MT: protect this with a CS?
220 m_traits
= CreateTraits();
222 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
228 // we must implement CreateXXX() in wxApp itself for backwards compatibility
229 #if WXWIN_COMPATIBILITY_2_4
233 wxLog
*wxAppConsole::CreateLogTarget()
235 wxAppTraits
*traits
= GetTraits();
236 return traits
? traits
->CreateLogTarget() : NULL
;
241 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
243 wxAppTraits
*traits
= GetTraits();
244 return traits
? traits
->CreateMessageOutput() : NULL
;
247 #endif // WXWIN_COMPATIBILITY_2_4
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 void wxAppConsole::ProcessPendingEvents()
255 // ensure that we're the only thread to modify the pending events list
256 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
258 if ( !wxPendingEvents
)
260 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
264 // iterate until the list becomes empty
265 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
268 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
269 wxPendingEvents
->Erase(node
);
271 // In ProcessPendingEvents(), new handlers might be add
272 // and we can safely leave the critical section here.
273 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
274 handler
->ProcessPendingEvents();
275 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
277 node
= wxPendingEvents
->GetFirst();
280 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
283 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
285 // process the events normally by default
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 #if wxUSE_CMDLINE_PARSER
295 #define OPTION_VERBOSE _T("verbose")
296 #define OPTION_THEME _T("theme")
297 #define OPTION_MODE _T("mode")
299 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
301 // the standard command line options
302 static const wxCmdLineEntryDesc cmdLineDesc
[] =
308 gettext_noop("show this help message"),
310 wxCMD_LINE_OPTION_HELP
318 gettext_noop("generate verbose log messages"),
324 #ifdef __WXUNIVERSAL__
329 gettext_noop("specify the theme to use"),
330 wxCMD_LINE_VAL_STRING
,
333 #endif // __WXUNIVERSAL__
335 #if defined(__WXMGL__)
336 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
337 // should provide this option. That's why it is in common/appcmn.cpp
338 // and not mgl/app.cpp
343 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
344 wxCMD_LINE_VAL_STRING
,
360 parser
.SetDesc(cmdLineDesc
);
363 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
366 if ( parser
.Found(OPTION_VERBOSE
) )
368 wxLog::SetVerbose(TRUE
);
372 #ifdef __WXUNIVERSAL__
374 if ( parser
.Found(OPTION_THEME
, &themeName
) )
376 wxTheme
*theme
= wxTheme::Create(themeName
);
379 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
383 // Delete the defaultly created theme and set the new theme.
384 delete wxTheme::Get();
387 #endif // __WXUNIVERSAL__
389 #if defined(__WXMGL__)
391 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
394 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
396 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
400 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
408 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
415 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
422 #endif // wxUSE_CMDLINE_PARSER
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
429 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
431 #define wxCMP(what) (what == opts.m_ ## what)
440 int verMaj
= wxMAJOR_VERSION
,
441 verMin
= wxMINOR_VERSION
;
443 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
446 wxString libDebug
, progDebug
;
449 libDebug
= wxT("debug");
451 libDebug
= wxT("no debug");
454 progDebug
= wxT("debug");
456 progDebug
= wxT("no debug");
458 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)."),
459 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
461 wxLogFatalError(msg
);
463 // normally wxLogFatalError doesn't return
473 void wxAppConsole::OnAssert(const wxChar
*file
,
478 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
481 #endif // __WXDEBUG__
483 // ============================================================================
484 // other classes implementations
485 // ============================================================================
487 // ----------------------------------------------------------------------------
488 // wxConsoleAppTraitsBase
489 // ----------------------------------------------------------------------------
493 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
495 return new wxLogStderr
;
500 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
502 return new wxMessageOutputStderr
;
507 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
509 return (wxFontMapper
*)new wxFontMapperBase
;
512 #endif // wxUSE_FONTMAP
515 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
517 return wxAppTraitsBase::ShowAssertDialog(msg
);
521 bool wxConsoleAppTraitsBase::HasStderr()
523 // console applications always have stderr, even under Mac/Windows
527 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
532 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
543 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
545 return DoShowAssertDialog(msg
);
548 #endif // __WXDEBUG__
550 // ============================================================================
551 // global functions implementation
552 // ============================================================================
562 // what else can we do?
571 wxTheApp
->WakeUpIdle();
573 //else: do nothing, what can we do?
579 bool wxAssertIsEqual(int x
, int y
)
584 // break into the debugger
587 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
589 #elif defined(__WXMAC__) && !defined(__DARWIN__)
595 #elif defined(__UNIX__)
602 void wxAssert(int cond
,
603 const wxChar
*szFile
,
605 const wxChar
*szCond
,
609 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
612 // this function is called when an assert fails
613 void wxOnAssert(const wxChar
*szFile
,
615 const wxChar
*szCond
,
619 static bool s_bInAssert
= FALSE
;
623 // He-e-e-e-elp!! we're trapped in endless loop
635 // by default, show the assert dialog box -- we can't customize this
637 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
641 // let the app process it as it wants
642 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
648 #endif // __WXDEBUG__
650 // ============================================================================
651 // private functions implementation
652 // ============================================================================
656 static void LINKAGEMODE
SetTraceMasks()
660 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
662 wxStringTokenizer
tkn(mask
, wxT(",;:"));
663 while ( tkn
.HasMoreTokens() )
664 wxLog::AddTraceMask(tkn
.GetNextToken());
669 bool DoShowAssertDialog(const wxString
& msg
)
671 // under MSW we can show the dialog even in the console mode
672 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
673 wxString
msgDlg(msg
);
675 // this message is intentionally not translated -- it is for
677 msgDlg
+= wxT("\nDo you want to stop the program?\n")
678 wxT("You can also choose [Cancel] to suppress ")
679 wxT("further warnings.");
681 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
682 MB_YESNOCANCEL
| MB_ICONSTOP
) )
692 //case IDNO: nothing to do
695 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
698 // TODO: ask the user to enter "Y" or "N" on the console?
700 #endif // __WXMSW__/!__WXMSW__
702 // continue with the asserts
706 // show the assert modal dialog
708 void ShowAssertDialog(const wxChar
*szFile
,
710 const wxChar
*szCond
,
714 // this variable can be set to true to suppress "assert failure" messages
715 static bool s_bNoAsserts
= FALSE
;
720 // make life easier for people using VC++ IDE by using this format: like
721 // this, clicking on the message will take us immediately to the place of
723 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
727 msg
<< _T(": ") << szMsg
;
729 else // no message given
735 // if we are not in the main thread, output the assert directly and trap
736 // since dialogs cannot be displayed
737 if ( !wxThread::IsMain() )
739 msg
+= wxT(" [in child thread]");
741 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
743 OutputDebugString(msg
);
746 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
749 // He-e-e-e-elp!! we're asserting in a child thread
752 #endif // wxUSE_THREADS
756 // send it to the normal log destination
757 wxLogDebug(_T("%s"), msg
.c_str());
761 // delegate showing assert dialog (if possible) to that class
762 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
764 else // no traits object
766 // fall back to the function of last resort
767 s_bNoAsserts
= DoShowAssertDialog(msg
);
772 #endif // __WXDEBUG__