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
))
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
466 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
468 return DoShowAssertDialog(msg
);
471 #endif // __WXDEBUG__
473 // ============================================================================
474 // global functions implementation
475 // ============================================================================
485 // what else can we do?
494 wxTheApp
->WakeUpIdle();
496 //else: do nothing, what can we do?
502 bool wxAssertIsEqual(int x
, int y
)
507 // break into the debugger
510 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
512 #elif defined(__WXMAC__) && !defined(__DARWIN__)
518 #elif defined(__UNIX__)
525 void wxAssert(int cond
,
526 const wxChar
*szFile
,
528 const wxChar
*szCond
,
532 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
535 // this function is called when an assert fails
536 void wxOnAssert(const wxChar
*szFile
,
538 const wxChar
*szCond
,
542 static bool s_bInAssert
= FALSE
;
546 // He-e-e-e-elp!! we're trapped in endless loop
558 // by default, show the assert dialog box -- we can't customize this
560 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
564 // let the app process it as it wants
565 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
571 #endif // __WXDEBUG__
573 // ============================================================================
574 // private functions implementation
575 // ============================================================================
579 static void LINKAGEMODE
SetTraceMasks()
583 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
585 wxStringTokenizer
tkn(mask
, wxT(",;:"));
586 while ( tkn
.HasMoreTokens() )
587 wxLog::AddTraceMask(tkn
.GetNextToken());
592 bool DoShowAssertDialog(const wxString
& msg
)
594 // under MSW we can show the dialog even in the console mode
595 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
596 wxString
msgDlg(msg
);
598 // this message is intentionally not translated -- it is for
600 msgDlg
+= wxT("\nDo you want to stop the program?\n")
601 wxT("You can also choose [Cancel] to suppress ")
602 wxT("further warnings.");
604 switch ( ::MessageBox(NULL
, msgDlg
, _T("wxWindows Debug Alert"),
605 MB_YESNOCANCEL
| MB_ICONSTOP
) )
615 //case IDNO: nothing to do
618 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
621 // TODO: ask the user to enter "Y" or "N" on the console?
623 #endif // __WXMSW__/!__WXMSW__
625 // continue with the asserts
629 // show the assert modal dialog
631 void ShowAssertDialog(const wxChar
*szFile
,
633 const wxChar
*szCond
,
637 // this variable can be set to true to suppress "assert failure" messages
638 static bool s_bNoAsserts
= FALSE
;
643 // make life easier for people using VC++ IDE by using this format: like
644 // this, clicking on the message will take us immediately to the place of
646 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
650 msg
<< _T(": ") << szMsg
;
652 else // no message given
658 // if we are not in the main thread, output the assert directly and trap
659 // since dialogs cannot be displayed
660 if ( !wxThread::IsMain() )
662 msg
+= wxT(" [in child thread]");
664 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
666 OutputDebugString(msg
);
669 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
672 // He-e-e-e-elp!! we're asserting in a child thread
675 #endif // wxUSE_THREADS
679 // send it to the normal log destination
680 wxLogDebug(_T("%s"), msg
.c_str());
684 // delegate showing assert dialog (if possible) to that class
685 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
687 else // no traits object
689 // fall back to the function of last resort
690 s_bNoAsserts
= DoShowAssertDialog(msg
);
695 #endif // __WXDEBUG__