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
43 #include "wx/msgout.h"
44 #include "wx/tokenzr.h"
46 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
47 #include <signal.h> // for SIGTRAP used by wxTrap()
50 #if defined(__WXMSW__)
51 #include "wx/msw/private.h" // includes windows.h for MessageBox()
55 #include "wx/fontmap.h"
56 #endif // wxUSE_FONTMAP
58 #if defined(__WXMAC__)
59 // VZ: MacTypes.h is enough under Mac OS X (where I could test it) but
60 // I don't know which headers are needed under earlier systems so
61 // include everything when in doubt
65 #include "wx/mac/private.h" // includes mac headers
69 // ----------------------------------------------------------------------------
70 // private functions prototypes
71 // ----------------------------------------------------------------------------
74 // really just show the assert dialog
75 static bool DoShowAssertDialog(const wxString
& msg
);
77 // prepare for showing the assert dialog, use the given traits or
78 // DoShowAssertDialog() as last fallback to really show it
80 void ShowAssertDialog(const wxChar
*szFile
,
84 wxAppTraits
*traits
= NULL
);
86 // turn on the trace masks specified in the env variable WXTRACE
87 static void LINKAGEMODE
SetTraceMasks();
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 wxApp
*wxTheApp
= NULL
;
96 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
98 // ============================================================================
99 // wxAppConsole implementation
100 // ============================================================================
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 wxAppConsole::wxAppConsole()
110 wxTheApp
= (wxApp
*)this;
117 wxAppConsole::~wxAppConsole()
122 // ----------------------------------------------------------------------------
123 // initilization/cleanup
124 // ----------------------------------------------------------------------------
126 bool wxAppConsole::Initialize(int& argc
, wxChar
**argv
)
128 // remember the command line arguments
132 if ( m_appName
.empty() && argv
)
134 // the application name is, by default, the name of its executable file
136 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
137 #else // !wxUSE_FILENAME
139 #endif // wxUSE_FILENAME/!wxUSE_FILENAME
145 void wxAppConsole::CleanUp()
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 bool wxAppConsole::OnInit()
155 #if wxUSE_CMDLINE_PARSER
156 wxCmdLineParser
parser(argc
, argv
);
158 OnInitCmdLine(parser
);
161 switch ( parser
.Parse(FALSE
/* don't show usage */) )
164 cont
= OnCmdLineHelp(parser
);
168 cont
= OnCmdLineParsed(parser
);
172 cont
= OnCmdLineError(parser
);
178 #endif // wxUSE_CMDLINE_PARSER
183 int wxAppConsole::OnExit()
186 // delete the config object if any (don't use Get() here, but Set()
187 // because Get() could create a new config object)
188 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
189 #endif // wxUSE_CONFIG
191 #ifdef __WXUNIVERSAL__
192 delete wxTheme::Set(NULL
);
193 #endif // __WXUNIVERSAL__
195 // use Set(NULL) and not Get() to avoid creating a message output object on
196 // demand when we just want to delete it
197 delete wxMessageOutput::Set(NULL
);
202 void wxAppConsole::Exit()
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 wxAppTraits
*wxAppConsole::CreateTraits()
213 return new wxConsoleAppTraits
;
216 wxAppTraits
*wxAppConsole::GetTraits()
218 // FIXME-MT: protect this with a CS?
221 m_traits
= CreateTraits();
223 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
229 // we must implement CreateXXX() in wxApp itself for backwards compatibility
230 #if WXWIN_COMPATIBILITY_2_4
234 wxLog
*wxAppConsole::CreateLogTarget()
236 wxAppTraits
*traits
= GetTraits();
237 return traits
? traits
->CreateLogTarget() : NULL
;
242 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
244 wxAppTraits
*traits
= GetTraits();
245 return traits
? traits
->CreateMessageOutput() : NULL
;
248 #endif // WXWIN_COMPATIBILITY_2_4
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 void wxAppConsole::ProcessPendingEvents()
256 // ensure that we're the only thread to modify the pending events list
257 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
259 if ( !wxPendingEvents
)
261 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
265 // iterate until the list becomes empty
266 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
269 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
270 wxPendingEvents
->Erase(node
);
272 // In ProcessPendingEvents(), new handlers might be add
273 // and we can safely leave the critical section here.
274 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
275 handler
->ProcessPendingEvents();
276 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
278 node
= wxPendingEvents
->GetFirst();
281 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
284 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
286 // process the events normally by default
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 #if wxUSE_CMDLINE_PARSER
296 #define OPTION_VERBOSE _T("verbose")
297 #define OPTION_THEME _T("theme")
298 #define OPTION_MODE _T("mode")
300 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
302 // the standard command line options
303 static const wxCmdLineEntryDesc cmdLineDesc
[] =
309 gettext_noop("show this help message"),
311 wxCMD_LINE_OPTION_HELP
319 gettext_noop("generate verbose log messages"),
325 #ifdef __WXUNIVERSAL__
330 gettext_noop("specify the theme to use"),
331 wxCMD_LINE_VAL_STRING
,
334 #endif // __WXUNIVERSAL__
336 #if defined(__WXMGL__)
337 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
338 // should provide this option. That's why it is in common/appcmn.cpp
339 // and not mgl/app.cpp
344 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
345 wxCMD_LINE_VAL_STRING
,
361 parser
.SetDesc(cmdLineDesc
);
364 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
367 if ( parser
.Found(OPTION_VERBOSE
) )
369 wxLog::SetVerbose(TRUE
);
373 #ifdef __WXUNIVERSAL__
375 if ( parser
.Found(OPTION_THEME
, &themeName
) )
377 wxTheme
*theme
= wxTheme::Create(themeName
);
380 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
384 // Delete the defaultly created theme and set the new theme.
385 delete wxTheme::Get();
388 #endif // __WXUNIVERSAL__
390 #if defined(__WXMGL__)
392 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
395 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
397 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
401 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
409 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
416 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
423 #endif // wxUSE_CMDLINE_PARSER
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
430 bool wxAppConsole::CheckBuildOptions(const wxBuildOptions
& opts
)
432 #define wxCMP(what) (what == opts.m_ ## what)
441 int verMaj
= wxMAJOR_VERSION
,
442 verMin
= wxMINOR_VERSION
;
444 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
447 wxString libDebug
, progDebug
;
450 libDebug
= wxT("debug");
452 libDebug
= wxT("no debug");
455 progDebug
= wxT("debug");
457 progDebug
= wxT("no debug");
459 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)."),
460 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
462 wxLogFatalError(msg
);
464 // normally wxLogFatalError doesn't return
474 void wxAppConsole::OnAssert(const wxChar
*file
,
479 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
482 #endif // __WXDEBUG__
484 // ============================================================================
485 // other classes implementations
486 // ============================================================================
488 // ----------------------------------------------------------------------------
489 // wxConsoleAppTraitsBase
490 // ----------------------------------------------------------------------------
494 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
496 return new wxLogStderr
;
501 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
503 return new wxMessageOutputStderr
;
508 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
510 return (wxFontMapper
*)new wxFontMapperBase
;
513 #endif // wxUSE_FONTMAP
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__