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
287 // ----------------------------------------------------------------------------
288 // exception handling
289 // ----------------------------------------------------------------------------
294 wxAppConsole::HandleEvent(wxEvtHandler
*handler
,
295 wxEventFunction func
,
296 wxEvent
& event
) const
298 // by default, simply call the handler
299 (handler
->*func
)(event
);
303 wxAppConsole::OnExceptionInMainLoop()
307 // some compilers are too stupid to know that we never return after throw
308 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
313 #endif // wxUSE_EXCEPTIONS
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 #if wxUSE_CMDLINE_PARSER
321 #define OPTION_VERBOSE _T("verbose")
323 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
325 // the standard command line options
326 static const wxCmdLineEntryDesc cmdLineDesc
[] =
332 gettext_noop("show this help message"),
334 wxCMD_LINE_OPTION_HELP
342 gettext_noop("generate verbose log messages"),
359 parser
.SetDesc(cmdLineDesc
);
362 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
365 if ( parser
.Found(OPTION_VERBOSE
) )
367 wxLog::SetVerbose(TRUE
);
374 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
381 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
388 #endif // wxUSE_CMDLINE_PARSER
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
395 bool wxAppConsole::CheckBuildOptions(const char *optionsSignature
,
396 const char *componentName
)
398 #if 0 // can't use wxLogTrace, not up and running yet
399 printf("checking build options object '%s' (ptr %p) in '%s'\n",
400 optionsSignature
, optionsSignature
, componentName
);
403 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
405 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
406 wxString prog
= wxString::FromAscii(optionsSignature
);
407 wxString progName
= wxString::FromAscii(componentName
);
410 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
411 lib
.c_str(), progName
.c_str(), prog
.c_str());
413 wxLogFatalError(msg
);
415 // normally wxLogFatalError doesn't return
425 void wxAppConsole::OnAssert(const wxChar
*file
,
430 ShowAssertDialog(file
, line
, cond
, msg
, GetTraits());
433 #endif // __WXDEBUG__
435 // ============================================================================
436 // other classes implementations
437 // ============================================================================
439 // ----------------------------------------------------------------------------
440 // wxConsoleAppTraitsBase
441 // ----------------------------------------------------------------------------
445 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
447 return new wxLogStderr
;
452 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
454 return new wxMessageOutputStderr
;
459 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
461 return (wxFontMapper
*)new wxFontMapperBase
;
464 #endif // wxUSE_FONTMAP
466 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
468 // console applications don't use renderers
473 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
475 return wxAppTraitsBase::ShowAssertDialog(msg
);
479 bool wxConsoleAppTraitsBase::HasStderr()
481 // console applications always have stderr, even under Mac/Windows
485 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
490 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
496 GSocketGUIFunctionsTable
* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable()
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
508 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
510 return DoShowAssertDialog(msg
);
513 #endif // __WXDEBUG__
515 // ============================================================================
516 // global functions implementation
517 // ============================================================================
527 // what else can we do?
536 wxTheApp
->WakeUpIdle();
538 //else: do nothing, what can we do?
544 bool wxAssertIsEqual(int x
, int y
)
549 // break into the debugger
552 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
554 #elif defined(__WXMAC__) && !defined(__DARWIN__)
560 #elif defined(__UNIX__)
567 void wxAssert(int cond
,
568 const wxChar
*szFile
,
570 const wxChar
*szCond
,
574 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
577 // this function is called when an assert fails
578 void wxOnAssert(const wxChar
*szFile
,
580 const wxChar
*szCond
,
584 static bool s_bInAssert
= FALSE
;
588 // He-e-e-e-elp!! we're trapped in endless loop
600 // by default, show the assert dialog box -- we can't customize this
602 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
606 // let the app process it as it wants
607 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
613 #endif // __WXDEBUG__
615 // ============================================================================
616 // private functions implementation
617 // ============================================================================
621 static void LINKAGEMODE
SetTraceMasks()
625 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
627 wxStringTokenizer
tkn(mask
, wxT(",;:"));
628 while ( tkn
.HasMoreTokens() )
629 wxLog::AddTraceMask(tkn
.GetNextToken());
634 bool DoShowAssertDialog(const wxString
& msg
)
636 // under MSW we can show the dialog even in the console mode
637 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
638 wxString
msgDlg(msg
);
640 // this message is intentionally not translated -- it is for
642 msgDlg
+= wxT("\nDo you want to stop the program?\n")
643 wxT("You can also choose [Cancel] to suppress ")
644 wxT("further warnings.");
646 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
647 MB_YESNOCANCEL
| MB_ICONSTOP
) )
657 //case IDNO: nothing to do
660 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
663 // TODO: ask the user to enter "Y" or "N" on the console?
665 #endif // __WXMSW__/!__WXMSW__
667 // continue with the asserts
671 // show the assert modal dialog
673 void ShowAssertDialog(const wxChar
*szFile
,
675 const wxChar
*szCond
,
679 // this variable can be set to true to suppress "assert failure" messages
680 static bool s_bNoAsserts
= FALSE
;
685 // make life easier for people using VC++ IDE by using this format: like
686 // this, clicking on the message will take us immediately to the place of
688 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
692 msg
<< _T(": ") << szMsg
;
694 else // no message given
700 // if we are not in the main thread, output the assert directly and trap
701 // since dialogs cannot be displayed
702 if ( !wxThread::IsMain() )
704 msg
+= wxT(" [in child thread]");
706 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
708 OutputDebugString(msg
);
711 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
714 // He-e-e-e-elp!! we're asserting in a child thread
717 #endif // wxUSE_THREADS
721 // send it to the normal log destination
722 wxLogDebug(_T("%s"), msg
.c_str());
726 // delegate showing assert dialog (if possible) to that class
727 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
729 else // no traits object
731 // fall back to the function of last resort
732 s_bNoAsserts
= DoShowAssertDialog(msg
);
737 #endif // __WXDEBUG__