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
57 #include "wx/gsocket.h"
58 #endif // wxUSE_SOCKETS
60 #if defined(__WXMAC__)
61 // VZ: MacTypes.h is enough under Mac OS X (where I could test it) but
62 // I don't know which headers are needed under earlier systems so
63 // include everything when in doubt
67 #include "wx/mac/private.h" // includes mac headers
71 // ----------------------------------------------------------------------------
72 // private functions prototypes
73 // ----------------------------------------------------------------------------
76 // really just show the assert dialog
77 static bool DoShowAssertDialog(const wxString
& msg
);
79 // prepare for showing the assert dialog, use the given traits or
80 // DoShowAssertDialog() as last fallback to really show it
82 void ShowAssertDialog(const wxChar
*szFile
,
86 wxAppTraits
*traits
= NULL
);
88 // turn on the trace masks specified in the env variable WXTRACE
89 static void LINKAGEMODE
SetTraceMasks();
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 wxAppConsole
*wxAppConsole
::ms_appInstance
= NULL
;
98 wxAppInitializerFunction wxAppConsole
::ms_appInitFn
= NULL
;
100 // ============================================================================
101 // wxAppConsole implementation
102 // ============================================================================
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 wxAppConsole
::wxAppConsole()
112 ms_appInstance
= this;
119 wxAppConsole
::~wxAppConsole()
124 // ----------------------------------------------------------------------------
125 // initilization/cleanup
126 // ----------------------------------------------------------------------------
128 bool wxAppConsole
::Initialize(int& argc
, wxChar
**argv
)
130 // remember the command line arguments
134 if ( m_appName
.empty() && argv
)
136 // the application name is, by default, the name of its executable file
137 wxFileName
::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
143 void wxAppConsole
::CleanUp()
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 bool wxAppConsole
::OnInit()
153 #if wxUSE_CMDLINE_PARSER
154 wxCmdLineParser
parser(argc
, argv
);
156 OnInitCmdLine(parser
);
159 switch ( parser
.Parse(FALSE
/* don't show usage */) )
162 cont
= OnCmdLineHelp(parser
);
166 cont
= OnCmdLineParsed(parser
);
170 cont
= OnCmdLineError(parser
);
176 #endif // wxUSE_CMDLINE_PARSER
181 int wxAppConsole
::OnExit()
184 // delete the config object if any (don't use Get() here, but Set()
185 // because Get() could create a new config object)
186 delete wxConfigBase
::Set((wxConfigBase
*) NULL
);
187 #endif // wxUSE_CONFIG
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
287 wxAppConsole
::HandleEvent(wxEvtHandler
*handler
,
288 wxEventFunction func
,
289 wxEvent
& event
) const
291 // by default, simply call the handler
292 (handler
->*func
)(event
);
295 #endif // wxUSE_EXCEPTIONS
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
301 #if wxUSE_CMDLINE_PARSER
303 #define OPTION_VERBOSE _T("verbose")
305 void wxAppConsole
::OnInitCmdLine(wxCmdLineParser
& parser
)
307 // the standard command line options
308 static const wxCmdLineEntryDesc cmdLineDesc
[] =
314 gettext_noop("show this help message"),
316 wxCMD_LINE_OPTION_HELP
324 gettext_noop("generate verbose log messages"),
341 parser
.SetDesc(cmdLineDesc
);
344 bool wxAppConsole
::OnCmdLineParsed(wxCmdLineParser
& parser
)
347 if ( parser
.Found(OPTION_VERBOSE
) )
349 wxLog
::SetVerbose(TRUE
);
356 bool wxAppConsole
::OnCmdLineHelp(wxCmdLineParser
& parser
)
363 bool wxAppConsole
::OnCmdLineError(wxCmdLineParser
& parser
)
370 #endif // wxUSE_CMDLINE_PARSER
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
377 bool wxAppConsole
::CheckBuildOptions(const char *optionsSignature
,
378 const char *componentName
)
380 #if 0 // can't use wxLogTrace, not up and running yet
381 printf("checking build options object '%s' (ptr %p) in '%s'\n",
382 optionsSignature
, optionsSignature
, componentName
);
385 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
387 wxString lib
= wxString
::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
388 wxString prog
= wxString
::FromAscii(optionsSignature
);
389 wxString progName
= wxString
::FromAscii(componentName
);
392 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
393 lib
.c_str(), progName
.c_str(), prog
.c_str());
395 wxLogFatalError(msg
);
397 // normally wxLogFatalError doesn't return
407 void wxAppConsole
::OnAssert(const wxChar
*file
,
412 ShowAssertDialog(file
, line
, cond
, msg
, GetTraits());
415 #endif // __WXDEBUG__
417 // ============================================================================
418 // other classes implementations
419 // ============================================================================
421 // ----------------------------------------------------------------------------
422 // wxConsoleAppTraitsBase
423 // ----------------------------------------------------------------------------
427 wxLog
*wxConsoleAppTraitsBase
::CreateLogTarget()
429 return new wxLogStderr
;
434 wxMessageOutput
*wxConsoleAppTraitsBase
::CreateMessageOutput()
436 return new wxMessageOutputStderr
;
441 wxFontMapper
*wxConsoleAppTraitsBase
::CreateFontMapper()
443 return (wxFontMapper
*)new wxFontMapperBase
;
446 #endif // wxUSE_FONTMAP
448 wxRendererNative
*wxConsoleAppTraitsBase
::CreateRenderer()
450 // console applications don't use renderers
455 bool wxConsoleAppTraitsBase
::ShowAssertDialog(const wxString
& msg
)
457 return wxAppTraitsBase
::ShowAssertDialog(msg
);
461 bool wxConsoleAppTraitsBase
::HasStderr()
463 // console applications always have stderr, even under Mac/Windows
467 void wxConsoleAppTraitsBase
::ScheduleForDestroy(wxObject
*object
)
472 void wxConsoleAppTraitsBase
::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
478 GSocketGUIFunctionsTable
* wxConsoleAppTraitsBase
::GetSocketGUIFunctionsTable()
483 // TODO: Use a different class that only stubs out the event loop functions
484 GSocketBSD
* wxConsoleAppTraitsBase
::CreateGSocket()
486 #ifdef wxUSE_GSOCKET_CPLUSPLUS
487 return new GSocketBSDGUIShim();
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
500 bool wxAppTraitsBase
::ShowAssertDialog(const wxString
& msg
)
502 return DoShowAssertDialog(msg
);
505 #endif // __WXDEBUG__
507 // ============================================================================
508 // global functions implementation
509 // ============================================================================
519 // what else can we do?
528 wxTheApp
->WakeUpIdle();
530 //else: do nothing, what can we do?
536 bool wxAssertIsEqual(int x
, int y
)
541 // break into the debugger
544 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
546 #elif defined(__WXMAC__) && !defined(__DARWIN__)
552 #elif defined(__UNIX__)
559 void wxAssert(int cond
,
560 const wxChar
*szFile
,
562 const wxChar
*szCond
,
566 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
569 // this function is called when an assert fails
570 void wxOnAssert(const wxChar
*szFile
,
572 const wxChar
*szCond
,
576 static bool s_bInAssert
= FALSE
;
580 // He-e-e-e-elp!! we're trapped in endless loop
592 // by default, show the assert dialog box -- we can't customize this
594 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
598 // let the app process it as it wants
599 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
605 #endif // __WXDEBUG__
607 // ============================================================================
608 // private functions implementation
609 // ============================================================================
613 static void LINKAGEMODE
SetTraceMasks()
617 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
619 wxStringTokenizer
tkn(mask
, wxT(",;:"));
620 while ( tkn
.HasMoreTokens() )
621 wxLog
::AddTraceMask(tkn
.GetNextToken());
626 bool DoShowAssertDialog(const wxString
& msg
)
628 // under MSW we can show the dialog even in the console mode
629 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
630 wxString
msgDlg(msg
);
632 // this message is intentionally not translated -- it is for
634 msgDlg
+= wxT("\nDo you want to stop the program?\n")
635 wxT("You can also choose [Cancel] to suppress ")
636 wxT("further warnings.");
638 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
639 MB_YESNOCANCEL
| MB_ICONSTOP
) )
649 //case IDNO: nothing to do
652 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
655 // TODO: ask the user to enter "Y" or "N" on the console?
657 #endif // __WXMSW__/!__WXMSW__
659 // continue with the asserts
663 // show the assert modal dialog
665 void ShowAssertDialog(const wxChar
*szFile
,
667 const wxChar
*szCond
,
671 // this variable can be set to true to suppress "assert failure" messages
672 static bool s_bNoAsserts
= FALSE
;
677 // make life easier for people using VC++ IDE by using this format: like
678 // this, clicking on the message will take us immediately to the place of
680 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
684 msg
<< _T(": ") << szMsg
;
686 else // no message given
692 // if we are not in the main thread, output the assert directly and trap
693 // since dialogs cannot be displayed
694 if ( !wxThread
::IsMain() )
696 msg
+= wxT(" [in child thread]");
698 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
700 OutputDebugString(msg
);
703 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
706 // He-e-e-e-elp!! we're asserting in a child thread
709 #endif // wxUSE_THREADS
713 // send it to the normal log destination
714 wxLogDebug(_T("%s"), msg
.c_str());
718 // delegate showing assert dialog (if possible) to that class
719 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
721 else // no traits object
723 // fall back to the function of last resort
724 s_bNoAsserts
= DoShowAssertDialog(msg
);
729 #endif // __WXDEBUG__