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 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
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
510 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
512 return wxAppTraitsBase::ShowAssertDialog(msg
);
516 bool wxConsoleAppTraitsBase::HasStderr()
518 // console applications always have stderr, even under Mac/Windows
522 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
527 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
532 // ----------------------------------------------------------------------------
534 // ----------------------------------------------------------------------------
538 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
540 return DoShowAssertDialog(msg
);
543 #endif // __WXDEBUG__
545 // ============================================================================
546 // global functions implementation
547 // ============================================================================
557 // what else can we do?
566 wxTheApp
->WakeUpIdle();
568 //else: do nothing, what can we do?
574 bool wxAssertIsEqual(int x
, int y
)
579 // break into the debugger
582 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
584 #elif defined(__WXMAC__) && !defined(__DARWIN__)
590 #elif defined(__UNIX__)
597 void wxAssert(int cond
,
598 const wxChar
*szFile
,
600 const wxChar
*szCond
,
604 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
607 // this function is called when an assert fails
608 void wxOnAssert(const wxChar
*szFile
,
610 const wxChar
*szCond
,
614 static bool s_bInAssert
= FALSE
;
618 // He-e-e-e-elp!! we're trapped in endless loop
630 // by default, show the assert dialog box -- we can't customize this
632 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
636 // let the app process it as it wants
637 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
643 #endif // __WXDEBUG__
645 // ============================================================================
646 // private functions implementation
647 // ============================================================================
651 static void LINKAGEMODE
SetTraceMasks()
655 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
657 wxStringTokenizer
tkn(mask
, wxT(",;:"));
658 while ( tkn
.HasMoreTokens() )
659 wxLog::AddTraceMask(tkn
.GetNextToken());
664 bool DoShowAssertDialog(const wxString
& msg
)
666 // under MSW we can show the dialog even in the console mode
667 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
668 wxString
msgDlg(msg
);
670 // this message is intentionally not translated -- it is for
672 msgDlg
+= wxT("\nDo you want to stop the program?\n")
673 wxT("You can also choose [Cancel] to suppress ")
674 wxT("further warnings.");
676 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
677 MB_YESNOCANCEL
| MB_ICONSTOP
) )
687 //case IDNO: nothing to do
690 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
693 // TODO: ask the user to enter "Y" or "N" on the console?
695 #endif // __WXMSW__/!__WXMSW__
697 // continue with the asserts
701 // show the assert modal dialog
703 void ShowAssertDialog(const wxChar
*szFile
,
705 const wxChar
*szCond
,
709 // this variable can be set to true to suppress "assert failure" messages
710 static bool s_bNoAsserts
= FALSE
;
715 // make life easier for people using VC++ IDE by using this format: like
716 // this, clicking on the message will take us immediately to the place of
718 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
722 msg
<< _T(": ") << szMsg
;
724 else // no message given
730 // if we are not in the main thread, output the assert directly and trap
731 // since dialogs cannot be displayed
732 if ( !wxThread::IsMain() )
734 msg
+= wxT(" [in child thread]");
736 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
738 OutputDebugString(msg
);
741 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
744 // He-e-e-e-elp!! we're asserting in a child thread
747 #endif // wxUSE_THREADS
751 // send it to the normal log destination
752 wxLogDebug(_T("%s"), msg
.c_str());
756 // delegate showing assert dialog (if possible) to that class
757 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
759 else // no traits object
761 // fall back to the function of last resort
762 s_bNoAsserts
= DoShowAssertDialog(msg
);
767 #endif // __WXDEBUG__