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
;
97 bool wxAppConsole::s_macDefaultEncodingIsPC
= true ;
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
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 #if wxUSE_CMDLINE_PARSER
290 #define OPTION_VERBOSE _T("verbose")
292 void wxAppConsole::OnInitCmdLine(wxCmdLineParser
& parser
)
294 // the standard command line options
295 static const wxCmdLineEntryDesc cmdLineDesc
[] =
301 gettext_noop("show this help message"),
303 wxCMD_LINE_OPTION_HELP
311 gettext_noop("generate verbose log messages"),
328 parser
.SetDesc(cmdLineDesc
);
331 bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser
& parser
)
334 if ( parser
.Found(OPTION_VERBOSE
) )
336 wxLog::SetVerbose(TRUE
);
343 bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser
& parser
)
350 bool wxAppConsole::OnCmdLineError(wxCmdLineParser
& parser
)
357 #endif // wxUSE_CMDLINE_PARSER
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
364 bool wxAppConsole::CheckBuildOptions(const char *optionsSignature
,
365 const char *componentName
)
367 #if 0 // can't use wxLogTrace, not up and running yet
368 printf("checking build options object '%s' (ptr %p) in '%s'\n",
369 optionsSignature
, optionsSignature
, componentName
);
372 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
374 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
375 wxString prog
= wxString::FromAscii(optionsSignature
);
376 wxString progName
= wxString::FromAscii(componentName
);
379 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
380 lib
.c_str(), progName
.c_str(), prog
.c_str());
382 wxLogFatalError(msg
);
384 // normally wxLogFatalError doesn't return
394 void wxAppConsole::OnAssert(const wxChar
*file
,
399 ShowAssertDialog(file
, line
, cond
, msg
, GetTraits());
402 #endif // __WXDEBUG__
404 // ============================================================================
405 // other classes implementations
406 // ============================================================================
408 // ----------------------------------------------------------------------------
409 // wxConsoleAppTraitsBase
410 // ----------------------------------------------------------------------------
414 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
416 return new wxLogStderr
;
421 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
423 return new wxMessageOutputStderr
;
428 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
430 return (wxFontMapper
*)new wxFontMapperBase
;
433 #endif // wxUSE_FONTMAP
435 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
437 // console applications don't use renderers
442 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
444 return wxAppTraitsBase::ShowAssertDialog(msg
);
448 bool wxConsoleAppTraitsBase::HasStderr()
450 // console applications always have stderr, even under Mac/Windows
454 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
459 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
465 GSocketGUIFunctionsTable
* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable()
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
477 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
479 return DoShowAssertDialog(msg
);
482 #endif // __WXDEBUG__
484 // ============================================================================
485 // global functions implementation
486 // ============================================================================
496 // what else can we do?
505 wxTheApp
->WakeUpIdle();
507 //else: do nothing, what can we do?
513 bool wxAssertIsEqual(int x
, int y
)
518 // break into the debugger
521 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
523 #elif defined(__WXMAC__) && !defined(__DARWIN__)
529 #elif defined(__UNIX__)
536 void wxAssert(int cond
,
537 const wxChar
*szFile
,
539 const wxChar
*szCond
,
543 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
546 // this function is called when an assert fails
547 void wxOnAssert(const wxChar
*szFile
,
549 const wxChar
*szCond
,
553 static bool s_bInAssert
= FALSE
;
557 // He-e-e-e-elp!! we're trapped in endless loop
569 // by default, show the assert dialog box -- we can't customize this
571 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
575 // let the app process it as it wants
576 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
582 #endif // __WXDEBUG__
584 // ============================================================================
585 // private functions implementation
586 // ============================================================================
590 static void LINKAGEMODE
SetTraceMasks()
594 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
596 wxStringTokenizer
tkn(mask
, wxT(",;:"));
597 while ( tkn
.HasMoreTokens() )
598 wxLog::AddTraceMask(tkn
.GetNextToken());
603 bool DoShowAssertDialog(const wxString
& msg
)
605 // under MSW we can show the dialog even in the console mode
606 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
607 wxString
msgDlg(msg
);
609 // this message is intentionally not translated -- it is for
611 msgDlg
+= wxT("\nDo you want to stop the program?\n")
612 wxT("You can also choose [Cancel] to suppress ")
613 wxT("further warnings.");
615 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
616 MB_YESNOCANCEL
| MB_ICONSTOP
) )
626 //case IDNO: nothing to do
629 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
632 // TODO: ask the user to enter "Y" or "N" on the console?
634 #endif // __WXMSW__/!__WXMSW__
636 // continue with the asserts
640 // show the assert modal dialog
642 void ShowAssertDialog(const wxChar
*szFile
,
644 const wxChar
*szCond
,
648 // this variable can be set to true to suppress "assert failure" messages
649 static bool s_bNoAsserts
= FALSE
;
654 // make life easier for people using VC++ IDE by using this format: like
655 // this, clicking on the message will take us immediately to the place of
657 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
661 msg
<< _T(": ") << szMsg
;
663 else // no message given
669 // if we are not in the main thread, output the assert directly and trap
670 // since dialogs cannot be displayed
671 if ( !wxThread::IsMain() )
673 msg
+= wxT(" [in child thread]");
675 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
677 OutputDebugString(msg
);
680 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
683 // He-e-e-e-elp!! we're asserting in a child thread
686 #endif // wxUSE_THREADS
690 // send it to the normal log destination
691 wxLogDebug(_T("%s"), msg
.c_str());
695 // delegate showing assert dialog (if possible) to that class
696 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
698 else // no traits object
700 // fall back to the function of last resort
701 s_bNoAsserts
= DoShowAssertDialog(msg
);
706 #endif // __WXDEBUG__