1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/appcmn.cpp
3 // Purpose: wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "appbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
36 #include "wx/msgdlg.h"
40 #include "wx/cmdline.h"
41 #include "wx/thread.h"
42 #include "wx/confbase.h"
43 #include "wx/tokenzr.h"
45 #include "wx/msgout.h"
48 #include "wx/artprov.h"
51 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
52 #include <signal.h> // for SIGTRAP used by wxTrap()
55 #if defined(__WXMSW__)
56 #include "wx/msw/private.h" // includes windows.h for MessageBox()
59 #if defined(__WXMAC__)
60 #include "wx/mac/private.h" // includes mac headers
63 // private functions prototypes
65 static void LINKAGEMODE
SetTraceMasks();
68 // ===========================================================================
70 // ===========================================================================
72 // ----------------------------------------------------------------------------
73 // initialization and termination
74 // ----------------------------------------------------------------------------
76 wxAppBase::wxAppBase()
78 wxTheApp
= (wxApp
*)this;
80 #if WXWIN_COMPATIBILITY_2_2
81 m_wantDebugOutput
= FALSE
;
82 #endif // WXWIN_COMPATIBILITY_2_2
85 m_topWindow
= (wxWindow
*)NULL
;
86 m_useBestVisual
= FALSE
;
87 m_exitOnFrameDelete
= TRUE
;
96 wxAppBase::~wxAppBase()
98 // this destructor is required for Darwin
102 bool wxAppBase::OnInitGui()
104 #ifdef __WXUNIVERSAL__
105 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
107 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
109 wxArtProvider::PushProvider(art
);
110 #endif // __WXUNIVERSAL__
116 int wxAppBase::OnExit()
119 // delete the config object if any (don't use Get() here, but Set()
120 // because Get() could create a new config object)
121 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
122 #endif // wxUSE_CONFIG
124 #ifdef __WXUNIVERSAL__
125 delete wxTheme::Set(NULL
);
126 #endif // __WXUNIVERSAL__
131 // ---------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 void wxAppBase::ProcessPendingEvents()
137 // ensure that we're the only thread to modify the pending events list
138 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
140 if ( !wxPendingEvents
)
142 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
146 // iterate until the list becomes empty
147 wxNode
*node
= wxPendingEvents
->First();
150 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
153 // In ProcessPendingEvents(), new handlers might be add
154 // and we can safely leave the critical section here.
155 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
156 handler
->ProcessPendingEvents();
157 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
159 node
= wxPendingEvents
->First();
162 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
171 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
173 if ( active
== m_isActive
)
178 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
179 event
.SetEventObject(this);
181 (void)ProcessEvent(event
);
186 int wxAppBase::FilterEvent(wxEvent
& WXUNUSED(event
))
188 // process the events normally by default
192 void wxAppBase::DoInit()
194 if(wxMessageOutput::Get()) return;
197 wxMessageOutput::Set(new wxMessageOutputLog
);
199 wxMessageOutput::Set(new wxMessageOutputMessageBox
);
202 wxMessageOutput::Set(new wxMessageOutputStderr
);
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 bool wxAppBase::OnInit()
213 #if wxUSE_CMDLINE_PARSER
214 wxCmdLineParser
parser(argc
, argv
);
216 OnInitCmdLine(parser
);
219 switch ( parser
.Parse(FALSE
/* don't show usage */) )
222 cont
= OnCmdLineHelp(parser
);
226 cont
= OnCmdLineParsed(parser
);
230 cont
= OnCmdLineError(parser
);
236 #endif // wxUSE_CMDLINE_PARSER
241 #if wxUSE_CMDLINE_PARSER
243 #define OPTION_VERBOSE _T("verbose")
244 #define OPTION_THEME _T("theme")
245 #define OPTION_MODE _T("mode")
247 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
249 // the standard command line options
250 static const wxCmdLineEntryDesc cmdLineDesc
[] =
256 gettext_noop("show this help message"),
258 wxCMD_LINE_OPTION_HELP
266 gettext_noop("generate verbose log messages"),
272 #ifdef __WXUNIVERSAL__
277 gettext_noop("specify the theme to use"),
278 wxCMD_LINE_VAL_STRING
,
281 #endif // __WXUNIVERSAL__
283 #if defined(__WXMGL__)
284 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
285 // should provide this option. That's why it is in common/appcmn.cpp
286 // and not mgl/app.cpp
291 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
292 wxCMD_LINE_VAL_STRING
,
308 parser
.SetDesc(cmdLineDesc
);
311 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
314 if ( parser
.Found(OPTION_VERBOSE
) )
316 wxLog::SetVerbose(TRUE
);
320 #ifdef __WXUNIVERSAL__
322 if ( parser
.Found(OPTION_THEME
, &themeName
) )
324 wxTheme
*theme
= wxTheme::Create(themeName
);
327 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
334 #endif // __WXUNIVERSAL__
336 #if defined(__WXMGL__)
338 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
341 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
343 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
348 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
356 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
363 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
370 #endif // wxUSE_CMDLINE_PARSER
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
377 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
379 #define wxCMP(what) (what == opts.m_ ## what)
388 int verMaj
= wxMAJOR_VERSION
,
389 verMin
= wxMINOR_VERSION
;
391 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
393 wxLogFatalError(_T("Mismatch between the program and library build ")
394 _T("versions detected."));
396 // normally wxLogFatalError doesn't return
406 static void LINKAGEMODE
SetTraceMasks()
409 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
411 wxStringTokenizer
tkn(mask
, wxT(","));
412 while ( tkn
.HasMoreTokens() )
413 wxLog::AddTraceMask(tkn
.GetNextToken());
418 bool wxAssertIsEqual(int x
, int y
)
423 // break into the debugger
426 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
428 #elif defined(__WXMAC__) && !defined(__DARWIN__)
434 #elif defined(__UNIX__)
441 // show the assert modal dialog
443 void ShowAssertDialog(const wxChar
*szFile
,
445 const wxChar
*szCond
,
448 // this variable can be set to true to suppress "assert failure" messages
449 static bool s_bNoAsserts
= FALSE
;
453 // make life easier for people using VC++ IDE by using this format: like
454 // this, clicking on the message will take us immediately to the place of
456 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
457 wxT("%s(%d): assert \"%s\" failed"),
458 szFile
, nLine
, szCond
);
462 wxStrcat(szBuf
, wxT(": "));
463 wxStrcat(szBuf
, szMsg
);
465 else // no message given
467 wxStrcat(szBuf
, wxT("."));
472 // send it to the normal log destination
475 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
476 // this message is intentionally not translated - it is for
478 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
480 // use the native message box if available: this is more robust than
482 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
483 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
484 MB_YESNOCANCEL
| MB_ICONSTOP
) )
494 //case IDNO: nothing to do
497 switch ( wxMessageBox(szBuf
, wxT("Debug"),
498 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
508 //case wxNO: nothing to do
518 // this function is called when an assert fails
519 void wxOnAssert(const wxChar
*szFile
,
521 const wxChar
*szCond
,
525 static bool s_bInAssert
= FALSE
;
529 // He-e-e-e-elp!! we're trapped in endless loop
541 // by default, show the assert dialog box - we can't customize this
543 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
547 // let the app process it as it wants
548 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
554 void wxAppBase::OnAssert(const wxChar
*file
,
559 ShowAssertDialog(file
, line
, cond
, msg
);