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 wxAppConsole
*wxAppConsole::ms_appInstance
= NULL
;
94 wxAppInitializerFunction
wxAppConsole::ms_appInitFn
= NULL
;
96 // ============================================================================
97 // wxAppConsole implementation
98 // ============================================================================
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 wxAppConsole::wxAppConsole()
108 ms_appInstance
= 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 // use Set(NULL) and not Get() to avoid creating a message output object on
186 // demand when we just want to delete it
187 delete wxMessageOutput::Set(NULL
);
192 void wxAppConsole::Exit()
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 wxAppTraits
*wxAppConsole::CreateTraits()
203 return new wxConsoleAppTraits
;
206 wxAppTraits
*wxAppConsole::GetTraits()
208 // FIXME-MT: protect this with a CS?
211 m_traits
= CreateTraits();
213 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
219 // we must implement CreateXXX() in wxApp itself for backwards compatibility
220 #if WXWIN_COMPATIBILITY_2_4
224 wxLog
*wxAppConsole::CreateLogTarget()
226 wxAppTraits
*traits
= GetTraits();
227 return traits
? traits
->CreateLogTarget() : NULL
;
232 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
234 wxAppTraits
*traits
= GetTraits();
235 return traits
? traits
->CreateMessageOutput() : NULL
;
238 #endif // WXWIN_COMPATIBILITY_2_4
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 void wxAppConsole::ProcessPendingEvents()
246 // ensure that we're the only thread to modify the pending events list
247 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
249 if ( !wxPendingEvents
)
251 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
255 // iterate until the list becomes empty
256 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
259 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
260 wxPendingEvents
->Erase(node
);
262 // In ProcessPendingEvents(), new handlers might be add
263 // and we can safely leave the critical section here.
264 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
265 handler
->ProcessPendingEvents();
266 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
268 node
= wxPendingEvents
->GetFirst();
271 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
274 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
276 // process the events normally by default
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 #if wxUSE_CMDLINE_PARSER
286 #define OPTION_VERBOSE _T("verbose")
288 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
290 // the standard command line options
291 static const wxCmdLineEntryDesc cmdLineDesc
[] =
297 gettext_noop("show this help message"),
299 wxCMD_LINE_OPTION_HELP
307 gettext_noop("generate verbose log messages"),
324 parser
.SetDesc(cmdLineDesc
);
327 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
330 if ( parser
.Found(OPTION_VERBOSE
) )
332 wxLog::SetVerbose(TRUE
);
339 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
346 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
353 #endif // wxUSE_CMDLINE_PARSER
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
360 bool wxAppConsole::CheckBuildOptions(const char *optionsSignature
,
361 const char *componentName
)
363 #if 0 // can't use wxLogTrace, not up and running yet
364 printf("checking build options object '%s' (ptr %p) in '%s'\n",
365 optionsSignature
, optionsSignature
, componentName
);
368 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
370 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
371 wxString prog
= wxString::FromAscii(optionsSignature
);
372 wxString progName
= wxString::FromAscii(componentName
);
375 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s, and %s used %s."),
376 lib
.c_str(), progName
.c_str(), prog
.c_str());
378 wxLogFatalError(msg
);
380 // normally wxLogFatalError doesn't return
390 void wxAppConsole::OnAssert(const wxChar
*file
,
395 ShowAssertDialog(file
, line
, cond
, msg
, m_traits
);
398 #endif // __WXDEBUG__
400 // ============================================================================
401 // other classes implementations
402 // ============================================================================
404 // ----------------------------------------------------------------------------
405 // wxConsoleAppTraitsBase
406 // ----------------------------------------------------------------------------
410 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
412 return new wxLogStderr
;
417 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
419 return new wxMessageOutputStderr
;
424 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
426 return (wxFontMapper
*)new wxFontMapperBase
;
429 #endif // wxUSE_FONTMAP
431 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
433 // console applications don't use renderers
438 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
440 return wxAppTraitsBase::ShowAssertDialog(msg
);
444 bool wxConsoleAppTraitsBase::HasStderr()
446 // console applications always have stderr, even under Mac/Windows
450 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
455 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
461 GSocketGUIFunctionsTable
* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable()
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
473 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
475 return DoShowAssertDialog(msg
);
478 #endif // __WXDEBUG__
480 // ============================================================================
481 // global functions implementation
482 // ============================================================================
492 // what else can we do?
501 wxTheApp
->WakeUpIdle();
503 //else: do nothing, what can we do?
509 bool wxAssertIsEqual(int x
, int y
)
514 // break into the debugger
517 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
519 #elif defined(__WXMAC__) && !defined(__DARWIN__)
525 #elif defined(__UNIX__)
532 void wxAssert(int cond
,
533 const wxChar
*szFile
,
535 const wxChar
*szCond
,
539 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
542 // this function is called when an assert fails
543 void wxOnAssert(const wxChar
*szFile
,
545 const wxChar
*szCond
,
549 static bool s_bInAssert
= FALSE
;
553 // He-e-e-e-elp!! we're trapped in endless loop
565 // by default, show the assert dialog box -- we can't customize this
567 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
571 // let the app process it as it wants
572 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
578 #endif // __WXDEBUG__
580 // ============================================================================
581 // private functions implementation
582 // ============================================================================
586 static void LINKAGEMODE
SetTraceMasks()
590 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
592 wxStringTokenizer
tkn(mask
, wxT(",;:"));
593 while ( tkn
.HasMoreTokens() )
594 wxLog::AddTraceMask(tkn
.GetNextToken());
599 bool DoShowAssertDialog(const wxString
& msg
)
601 // under MSW we can show the dialog even in the console mode
602 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
603 wxString
msgDlg(msg
);
605 // this message is intentionally not translated -- it is for
607 msgDlg
+= wxT("\nDo you want to stop the program?\n")
608 wxT("You can also choose [Cancel] to suppress ")
609 wxT("further warnings.");
611 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
612 MB_YESNOCANCEL
| MB_ICONSTOP
) )
622 //case IDNO: nothing to do
625 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
628 // TODO: ask the user to enter "Y" or "N" on the console?
630 #endif // __WXMSW__/!__WXMSW__
632 // continue with the asserts
636 // show the assert modal dialog
638 void ShowAssertDialog(const wxChar
*szFile
,
640 const wxChar
*szCond
,
644 // this variable can be set to true to suppress "assert failure" messages
645 static bool s_bNoAsserts
= FALSE
;
650 // make life easier for people using VC++ IDE by using this format: like
651 // this, clicking on the message will take us immediately to the place of
653 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
657 msg
<< _T(": ") << szMsg
;
659 else // no message given
665 // if we are not in the main thread, output the assert directly and trap
666 // since dialogs cannot be displayed
667 if ( !wxThread::IsMain() )
669 msg
+= wxT(" [in child thread]");
671 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
673 OutputDebugString(msg
);
676 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
679 // He-e-e-e-elp!! we're asserting in a child thread
682 #endif // wxUSE_THREADS
686 // send it to the normal log destination
687 wxLogDebug(_T("%s"), msg
.c_str());
691 // delegate showing assert dialog (if possible) to that class
692 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
694 else // no traits object
696 // fall back to the function of last resort
697 s_bNoAsserts
= DoShowAssertDialog(msg
);
702 #endif // __WXDEBUG__