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"
35 #include "wx/apptrait.h"
36 #include "wx/cmdline.h"
37 #include "wx/confbase.h"
39 #include "wx/fontmap.h"
40 #endif // wxUSE_FONTMAP
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()
52 #if defined(__WXMAC__)
53 #include "wx/mac/private.h" // includes mac headers
56 // ----------------------------------------------------------------------------
57 // private functions prototypes
58 // ----------------------------------------------------------------------------
61 // really just show the assert dialog
62 static bool DoShowAssertDialog(const wxString
& msg
);
64 // prepare for showing the assert dialog, use the given traits or
65 // DoShowAssertDialog() as last fallback to really show it
67 void ShowAssertDialog(const wxChar
*szFile
,
71 wxAppTraits
*traits
= NULL
);
73 // turn on the trace masks specified in the env variable WXTRACE
74 static void LINKAGEMODE
SetTraceMasks();
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 wxApp
*wxTheApp
= NULL
;
83 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
85 // ============================================================================
86 // wxAppConsole implementation
87 // ============================================================================
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 wxAppConsole::wxAppConsole()
97 wxTheApp
= (wxApp
*)this;
104 wxAppConsole::~wxAppConsole()
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 bool wxAppConsole::OnInit()
115 #if wxUSE_CMDLINE_PARSER
116 wxCmdLineParser
parser(argc
, argv
);
118 OnInitCmdLine(parser
);
121 switch ( parser
.Parse(FALSE
/* don't show usage */) )
124 cont
= OnCmdLineHelp(parser
);
128 cont
= OnCmdLineParsed(parser
);
132 cont
= OnCmdLineError(parser
);
138 #endif // wxUSE_CMDLINE_PARSER
143 int wxAppConsole::OnExit()
146 // delete the config object if any (don't use Get() here, but Set()
147 // because Get() could create a new config object)
148 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
149 #endif // wxUSE_CONFIG
151 #ifdef __WXUNIVERSAL__
152 delete wxTheme::Set(NULL
);
153 #endif // __WXUNIVERSAL__
155 // use Set(NULL) and not Get() to avoid creating a message output object on
156 // demand when we just want to delete it
157 delete wxMessageOutput::Set(NULL
);
162 void wxAppConsole::Exit()
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 wxAppTraits
*wxAppConsole::CreateTraits()
173 return wxAppTraits::CreateConsole();
176 wxAppTraits
*wxAppConsole::GetTraits()
178 // FIXME-MT: protect this with a CS?
181 m_traits
= CreateTraits();
183 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
189 // we must implement CreateXXX() in wxApp itself for backwards compatibility
190 #if WXWIN_COMPATIBILITY_2_4
194 wxLog
*wxAppConsole::CreateLogTarget()
196 wxAppTraits
*traits
= GetTraits();
197 return traits
? traits
->CreateLogTarget() : NULL
;
202 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
204 wxAppTraits
*traits
= GetTraits();
205 return traits
? traits
->CreateMessageOutput() : NULL
;
208 #endif // WXWIN_COMPATIBILITY_2_4
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 void wxAppConsole::ProcessPendingEvents()
216 // ensure that we're the only thread to modify the pending events list
217 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
219 if ( !wxPendingEvents
)
221 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
225 // iterate until the list becomes empty
226 wxNode
*node
= wxPendingEvents
->GetFirst();
229 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
232 // In ProcessPendingEvents(), new handlers might be add
233 // and we can safely leave the critical section here.
234 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
235 handler
->ProcessPendingEvents();
236 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
238 node
= wxPendingEvents
->GetFirst();
241 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
244 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
246 // process the events normally by default
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 #if wxUSE_CMDLINE_PARSER
256 #define OPTION_VERBOSE _T("verbose")
257 #define OPTION_THEME _T("theme")
258 #define OPTION_MODE _T("mode")
260 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
262 // the standard command line options
263 static const wxCmdLineEntryDesc cmdLineDesc
[] =
269 gettext_noop("show this help message"),
271 wxCMD_LINE_OPTION_HELP
279 gettext_noop("generate verbose log messages"),
285 #ifdef __WXUNIVERSAL__
290 gettext_noop("specify the theme to use"),
291 wxCMD_LINE_VAL_STRING
,
294 #endif // __WXUNIVERSAL__
296 #if defined(__WXMGL__)
297 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
298 // should provide this option. That's why it is in common/appcmn.cpp
299 // and not mgl/app.cpp
304 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
305 wxCMD_LINE_VAL_STRING
,
321 parser
.SetDesc(cmdLineDesc
);
324 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
327 if ( parser
.Found(OPTION_VERBOSE
) )
329 wxLog::SetVerbose(TRUE
);
333 #ifdef __WXUNIVERSAL__
335 if ( parser
.Found(OPTION_THEME
, &themeName
) )
337 wxTheme
*theme
= wxTheme::Create(themeName
);
340 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
344 // Delete the defaultly created theme and set the new theme.
345 delete wxTheme::Get();
348 #endif // __WXUNIVERSAL__
350 #if defined(__WXMGL__)
352 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
355 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
357 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
361 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
369 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
376 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
383 #endif // wxUSE_CMDLINE_PARSER
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
390 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
392 #define wxCMP(what) (what == opts.m_ ## what)
401 int verMaj
= wxMAJOR_VERSION
,
402 verMin
= wxMINOR_VERSION
;
404 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
407 wxString libDebug
, progDebug
;
410 libDebug
= wxT("debug");
412 libDebug
= wxT("no debug");
415 progDebug
= wxT("debug");
417 progDebug
= wxT("no debug");
419 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)."),
420 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
422 wxLogFatalError(msg
);
424 // normally wxLogFatalError doesn't return
434 void wxAppConsole::OnAssert(const wxChar
*file
,
439 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
442 #endif // __WXDEBUG__
444 // ============================================================================
445 // other classes implementations
446 // ============================================================================
448 // ----------------------------------------------------------------------------
449 // wxConsoleAppTraitsBase
450 // ----------------------------------------------------------------------------
454 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
456 return new wxLogStderr
;
461 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
463 return new wxMessageOutputStderr
;
468 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
470 return (wxFontMapper
*)new wxFontMapperBase
;
473 #endif // wxUSE_FONTMAP
476 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
478 return wxAppTraitsBase::ShowAssertDialog(msg
);
482 bool wxConsoleAppTraitsBase::HasStderr()
484 // console applications always have stderr, even under Mac/Windows
488 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
493 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
504 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
506 return DoShowAssertDialog(msg
);
509 #endif // __WXDEBUG__
511 wxAppTraits
*wxAppTraitsBase::CreateConsole()
513 return new wxConsoleAppTraits
;
516 // ============================================================================
517 // global functions implementation
518 // ============================================================================
528 // what else can we do?
537 wxTheApp
->WakeUpIdle();
539 //else: do nothing, what can we do?
545 bool wxAssertIsEqual(int x
, int y
)
550 // break into the debugger
553 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
555 #elif defined(__WXMAC__) && !defined(__DARWIN__)
561 #elif defined(__UNIX__)
568 void wxAssert(int cond
,
569 const wxChar
*szFile
,
571 const wxChar
*szCond
,
575 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
578 // this function is called when an assert fails
579 void wxOnAssert(const wxChar
*szFile
,
581 const wxChar
*szCond
,
585 static bool s_bInAssert
= FALSE
;
589 // He-e-e-e-elp!! we're trapped in endless loop
601 // by default, show the assert dialog box -- we can't customize this
603 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
607 // let the app process it as it wants
608 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
614 #endif // __WXDEBUG__
616 // ============================================================================
617 // private functions implementation
618 // ============================================================================
622 static void LINKAGEMODE
SetTraceMasks()
626 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
628 wxStringTokenizer
tkn(mask
, wxT(",;:"));
629 while ( tkn
.HasMoreTokens() )
630 wxLog::AddTraceMask(tkn
.GetNextToken());
635 bool DoShowAssertDialog(const wxString
& msg
)
637 // under MSW we can show the dialog even in the console mode
638 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
639 wxString
msgDlg(msg
);
641 // this message is intentionally not translated -- it is for
643 msgDlg
+= wxT("\nDo you want to stop the program?\n")
644 wxT("You can also choose [Cancel] to suppress ")
645 wxT("further warnings.");
647 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
648 MB_YESNOCANCEL
| MB_ICONSTOP
) )
658 //case IDNO: nothing to do
661 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
664 // TODO: ask the user to enter "Y" or "N" on the console?
666 #endif // __WXMSW__/!__WXMSW__
668 // continue with the asserts
672 // show the assert modal dialog
674 void ShowAssertDialog(const wxChar
*szFile
,
676 const wxChar
*szCond
,
680 // this variable can be set to true to suppress "assert failure" messages
681 static bool s_bNoAsserts
= FALSE
;
686 // make life easier for people using VC++ IDE by using this format: like
687 // this, clicking on the message will take us immediately to the place of
689 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
693 msg
<< _T(": ") << szMsg
;
695 else // no message given
701 // if we are not in the main thread, output the assert directly and trap
702 // since dialogs cannot be displayed
703 if ( !wxThread::IsMain() )
705 msg
+= wxT(" [in child thread]");
707 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
709 OutputDebugString(msg
);
712 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
715 // He-e-e-e-elp!! we're asserting in a child thread
718 #endif // wxUSE_THREADS
722 // send it to the normal log destination
723 wxLogDebug(_T("%s"), msg
);
727 // delegate showing assert dialog (if possible) to that class
728 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
730 else // no traits object
732 // fall back to the function of last resort
733 s_bNoAsserts
= DoShowAssertDialog(msg
);
738 #endif // __WXDEBUG__