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/wrapwin.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;
113 // In unicode mode the SetTraceMasks call can cause an apptraits to be
114 // created, but since we are still in the constructor the wrong kind will
115 // be created for GUI apps. Destroy it so it can be created again later.
122 wxAppConsole::~wxAppConsole()
127 // ----------------------------------------------------------------------------
128 // initilization/cleanup
129 // ----------------------------------------------------------------------------
131 bool wxAppConsole::Initialize(int& argc
, wxChar
**argv
)
133 // remember the command line arguments
137 if ( m_appName
.empty() && argv
)
139 // the application name is, by default, the name of its executable file
140 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
146 void wxAppConsole::CleanUp()
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 bool wxAppConsole::OnInit()
156 #if wxUSE_CMDLINE_PARSER
157 wxCmdLineParser
parser(argc
, argv
);
159 OnInitCmdLine(parser
);
162 switch ( parser
.Parse(FALSE
/* don't show usage */) )
165 cont
= OnCmdLineHelp(parser
);
169 cont
= OnCmdLineParsed(parser
);
173 cont
= OnCmdLineError(parser
);
179 #endif // wxUSE_CMDLINE_PARSER
184 int wxAppConsole::OnExit()
187 // delete the config object if any (don't use Get() here, but Set()
188 // because Get() could create a new config object)
189 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
190 #endif // wxUSE_CONFIG
192 // use Set(NULL) and not Get() to avoid creating a message output object on
193 // demand when we just want to delete it
194 delete wxMessageOutput::Set(NULL
);
199 void wxAppConsole::Exit()
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 wxAppTraits
*wxAppConsole::CreateTraits()
210 return new wxConsoleAppTraits
;
213 wxAppTraits
*wxAppConsole::GetTraits()
215 // FIXME-MT: protect this with a CS?
218 m_traits
= CreateTraits();
220 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
226 // we must implement CreateXXX() in wxApp itself for backwards compatibility
227 #if WXWIN_COMPATIBILITY_2_4
231 wxLog
*wxAppConsole::CreateLogTarget()
233 wxAppTraits
*traits
= GetTraits();
234 return traits
? traits
->CreateLogTarget() : NULL
;
239 wxMessageOutput
*wxAppConsole::CreateMessageOutput()
241 wxAppTraits
*traits
= GetTraits();
242 return traits
? traits
->CreateMessageOutput() : NULL
;
245 #endif // WXWIN_COMPATIBILITY_2_4
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 void wxAppConsole::ProcessPendingEvents()
253 // ensure that we're the only thread to modify the pending events list
254 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
256 if ( !wxPendingEvents
)
258 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
262 // iterate until the list becomes empty
263 wxList::compatibility_iterator node
= wxPendingEvents
->GetFirst();
266 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->GetData();
267 wxPendingEvents
->Erase(node
);
269 // In ProcessPendingEvents(), new handlers might be add
270 // and we can safely leave the critical section here.
271 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
272 handler
->ProcessPendingEvents();
273 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
275 node
= wxPendingEvents
->GetFirst();
278 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
281 int wxAppConsole::FilterEvent(wxEvent
& WXUNUSED(event
))
283 // process the events normally by default
290 wxAppConsole::HandleEvent(wxEvtHandler
*handler
,
291 wxEventFunction func
,
292 wxEvent
& event
) const
294 // by default, simply call the handler
295 (handler
->*func
)(event
);
298 #endif // wxUSE_EXCEPTIONS
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 #if wxUSE_CMDLINE_PARSER
306 #define OPTION_VERBOSE _T("verbose")
308 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
310 // the standard command line options
311 static const wxCmdLineEntryDesc cmdLineDesc
[] =
317 gettext_noop("show this help message"),
319 wxCMD_LINE_OPTION_HELP
327 gettext_noop("generate verbose log messages"),
344 parser
.SetDesc(cmdLineDesc
);
347 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
350 if ( parser
.Found(OPTION_VERBOSE
) )
352 wxLog::SetVerbose(TRUE
);
359 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
366 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
373 #endif // wxUSE_CMDLINE_PARSER
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
380 bool wxAppConsole::CheckBuildOptions(const char *optionsSignature
,
381 const char *componentName
)
383 #if 0 // can't use wxLogTrace, not up and running yet
384 printf("checking build options object '%s' (ptr %p) in '%s'\n",
385 optionsSignature
, optionsSignature
, componentName
);
388 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
390 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
391 wxString prog
= wxString::FromAscii(optionsSignature
);
392 wxString progName
= wxString::FromAscii(componentName
);
395 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
396 lib
.c_str(), progName
.c_str(), prog
.c_str());
398 wxLogFatalError(msg
);
400 // normally wxLogFatalError doesn't return
410 void wxAppConsole::OnAssert(const wxChar
*file
,
415 ShowAssertDialog(file
, line
, cond
, msg
, GetTraits());
418 #endif // __WXDEBUG__
420 // ============================================================================
421 // other classes implementations
422 // ============================================================================
424 // ----------------------------------------------------------------------------
425 // wxConsoleAppTraitsBase
426 // ----------------------------------------------------------------------------
430 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
432 return new wxLogStderr
;
437 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
439 return new wxMessageOutputStderr
;
444 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
446 return (wxFontMapper
*)new wxFontMapperBase
;
449 #endif // wxUSE_FONTMAP
451 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
453 // console applications don't use renderers
458 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
460 return wxAppTraitsBase::ShowAssertDialog(msg
);
464 bool wxConsoleAppTraitsBase::HasStderr()
466 // console applications always have stderr, even under Mac/Windows
470 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
475 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
481 GSocketGUIFunctionsTable
* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable()
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
493 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
495 return DoShowAssertDialog(msg
);
498 #endif // __WXDEBUG__
500 // ============================================================================
501 // global functions implementation
502 // ============================================================================
512 // what else can we do?
521 wxTheApp
->WakeUpIdle();
523 //else: do nothing, what can we do?
529 bool wxAssertIsEqual(int x
, int y
)
534 // break into the debugger
537 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
539 #elif defined(__WXMAC__) && !defined(__DARWIN__)
545 #elif defined(__UNIX__)
552 void wxAssert(int cond
,
553 const wxChar
*szFile
,
555 const wxChar
*szCond
,
559 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
562 // this function is called when an assert fails
563 void wxOnAssert(const wxChar
*szFile
,
565 const wxChar
*szCond
,
569 static bool s_bInAssert
= FALSE
;
573 // He-e-e-e-elp!! we're trapped in endless loop
585 // by default, show the assert dialog box -- we can't customize this
587 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
591 // let the app process it as it wants
592 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
598 #endif // __WXDEBUG__
600 // ============================================================================
601 // private functions implementation
602 // ============================================================================
606 static void LINKAGEMODE
SetTraceMasks()
610 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
612 wxStringTokenizer
tkn(mask
, wxT(",;:"));
613 while ( tkn
.HasMoreTokens() )
614 wxLog::AddTraceMask(tkn
.GetNextToken());
619 bool DoShowAssertDialog(const wxString
& msg
)
621 // under MSW we can show the dialog even in the console mode
622 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
623 wxString
msgDlg(msg
);
625 // this message is intentionally not translated -- it is for
627 msgDlg
+= wxT("\nDo you want to stop the program?\n")
628 wxT("You can also choose [Cancel] to suppress ")
629 wxT("further warnings.");
631 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
632 MB_YESNOCANCEL
| MB_ICONSTOP
) )
642 //case IDNO: nothing to do
645 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
648 // TODO: ask the user to enter "Y" or "N" on the console?
650 #endif // __WXMSW__/!__WXMSW__
652 // continue with the asserts
656 // show the assert modal dialog
658 void ShowAssertDialog(const wxChar
*szFile
,
660 const wxChar
*szCond
,
664 // this variable can be set to true to suppress "assert failure" messages
665 static bool s_bNoAsserts
= FALSE
;
670 // make life easier for people using VC++ IDE by using this format: like
671 // this, clicking on the message will take us immediately to the place of
673 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
677 msg
<< _T(": ") << szMsg
;
679 else // no message given
685 // if we are not in the main thread, output the assert directly and trap
686 // since dialogs cannot be displayed
687 if ( !wxThread::IsMain() )
689 msg
+= wxT(" [in child thread]");
691 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
693 OutputDebugString(msg
);
696 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
699 // He-e-e-e-elp!! we're asserting in a child thread
702 #endif // wxUSE_THREADS
706 // send it to the normal log destination
707 wxLogDebug(_T("%s"), msg
.c_str());
711 // delegate showing assert dialog (if possible) to that class
712 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
714 else // no traits object
716 // fall back to the function of last resort
717 s_bNoAsserts
= DoShowAssertDialog(msg
);
722 #endif // __WXDEBUG__