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
;
89 // We don't want to exit the app if the user code shows a dialog from its
90 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
91 // to Yes initially as this dialog would be the last top level window.
92 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
93 // when we enter our OnRun() because we do want the default behaviour from
94 // then on. But this would be a problem if the user code calls
95 // SetExitOnFrameDelete(FALSE) from OnInit().
97 // So we use the special "Later" value which is such that
98 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
99 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
100 // call) overwrite in OnRun()
101 m_exitOnFrameDelete
= Later
;
109 wxAppBase::~wxAppBase()
111 // this destructor is required for Darwin
115 bool wxAppBase::OnInitGui()
117 #ifdef __WXUNIVERSAL__
118 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
120 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
122 wxArtProvider::PushProvider(art
);
123 #endif // __WXUNIVERSAL__
129 int wxAppBase::OnRun()
131 // see the comment in ctor: if the initial value hasn't been changed, use
132 // the default Yes from now on
133 if ( m_exitOnFrameDelete
== Later
)
135 m_exitOnFrameDelete
= Yes
;
137 //else: it has been changed, assume the user knows what he is doing
142 int wxAppBase::OnExit()
145 // delete the config object if any (don't use Get() here, but Set()
146 // because Get() could create a new config object)
147 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
148 #endif // wxUSE_CONFIG
150 #ifdef __WXUNIVERSAL__
151 delete wxTheme::Set(NULL
);
152 #endif // __WXUNIVERSAL__
157 // ---------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void wxAppBase::ProcessPendingEvents()
163 // ensure that we're the only thread to modify the pending events list
164 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
166 if ( !wxPendingEvents
)
168 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
172 // iterate until the list becomes empty
173 wxNode
*node
= wxPendingEvents
->First();
176 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
179 // In ProcessPendingEvents(), new handlers might be add
180 // and we can safely leave the critical section here.
181 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
182 handler
->ProcessPendingEvents();
183 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
185 node
= wxPendingEvents
->First();
188 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
197 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
199 if ( active
== m_isActive
)
204 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
205 event
.SetEventObject(this);
207 (void)ProcessEvent(event
);
212 int wxAppBase::FilterEvent(wxEvent
& WXUNUSED(event
))
214 // process the events normally by default
218 void wxAppBase::DoInit()
220 if (wxMessageOutput::Get())
223 // NB: The standard way of printing help on command line arguments (app --help)
224 // is (according to common practice):
225 // - console apps: to stderr (on any platform)
226 // - GUI apps: stderr on Unix platforms (!)
227 // message box under Windows and others
228 #if wxUSE_GUI && !defined(__UNIX__)
230 wxMessageOutput::Set(new wxMessageOutputLog
);
232 wxMessageOutput::Set(new wxMessageOutputMessageBox
);
235 wxMessageOutput::Set(new wxMessageOutputStderr
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 bool wxAppBase::OnInit()
246 #if wxUSE_CMDLINE_PARSER
247 wxCmdLineParser
parser(argc
, argv
);
249 OnInitCmdLine(parser
);
252 switch ( parser
.Parse(FALSE
/* don't show usage */) )
255 cont
= OnCmdLineHelp(parser
);
259 cont
= OnCmdLineParsed(parser
);
263 cont
= OnCmdLineError(parser
);
269 #endif // wxUSE_CMDLINE_PARSER
274 #if wxUSE_CMDLINE_PARSER
276 #define OPTION_VERBOSE _T("verbose")
277 #define OPTION_THEME _T("theme")
278 #define OPTION_MODE _T("mode")
280 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
282 // the standard command line options
283 static const wxCmdLineEntryDesc cmdLineDesc
[] =
289 gettext_noop("show this help message"),
291 wxCMD_LINE_OPTION_HELP
299 gettext_noop("generate verbose log messages"),
305 #ifdef __WXUNIVERSAL__
310 gettext_noop("specify the theme to use"),
311 wxCMD_LINE_VAL_STRING
,
314 #endif // __WXUNIVERSAL__
316 #if defined(__WXMGL__)
317 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
318 // should provide this option. That's why it is in common/appcmn.cpp
319 // and not mgl/app.cpp
324 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
325 wxCMD_LINE_VAL_STRING
,
341 parser
.SetDesc(cmdLineDesc
);
344 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
347 if ( parser
.Found(OPTION_VERBOSE
) )
349 wxLog::SetVerbose(TRUE
);
353 #ifdef __WXUNIVERSAL__
355 if ( parser
.Found(OPTION_THEME
, &themeName
) )
357 wxTheme
*theme
= wxTheme::Create(themeName
);
360 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
367 #endif // __WXUNIVERSAL__
369 #if defined(__WXMGL__)
371 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
374 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
376 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
381 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
389 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
396 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
403 #endif // wxUSE_CMDLINE_PARSER
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
410 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
412 #define wxCMP(what) (what == opts.m_ ## what)
421 int verMaj
= wxMAJOR_VERSION
,
422 verMin
= wxMINOR_VERSION
;
424 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
426 wxLogFatalError(_T("Mismatch between the program and library build ")
427 _T("versions detected."));
429 // normally wxLogFatalError doesn't return
439 static void LINKAGEMODE
SetTraceMasks()
442 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
444 wxStringTokenizer
tkn(mask
, wxT(","));
445 while ( tkn
.HasMoreTokens() )
446 wxLog::AddTraceMask(tkn
.GetNextToken());
451 bool wxAssertIsEqual(int x
, int y
)
456 // break into the debugger
459 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
461 #elif defined(__WXMAC__) && !defined(__DARWIN__)
467 #elif defined(__UNIX__)
474 // show the assert modal dialog
476 void ShowAssertDialog(const wxChar
*szFile
,
478 const wxChar
*szCond
,
481 // this variable can be set to true to suppress "assert failure" messages
482 static bool s_bNoAsserts
= FALSE
;
486 // make life easier for people using VC++ IDE by using this format: like
487 // this, clicking on the message will take us immediately to the place of
489 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
490 wxT("%s(%d): assert \"%s\" failed"),
491 szFile
, nLine
, szCond
);
495 wxStrcat(szBuf
, wxT(": "));
496 wxStrcat(szBuf
, szMsg
);
498 else // no message given
500 wxStrcat(szBuf
, wxT("."));
505 // send it to the normal log destination
508 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
509 // this message is intentionally not translated - it is for
511 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
513 // use the native message box if available: this is more robust than
515 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
516 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
517 MB_YESNOCANCEL
| MB_ICONSTOP
) )
527 //case IDNO: nothing to do
530 switch ( wxMessageBox(szBuf
, wxT("Debug"),
531 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
541 //case wxNO: nothing to do
551 // this function is called when an assert fails
552 void wxOnAssert(const wxChar
*szFile
,
554 const wxChar
*szCond
,
558 static bool s_bInAssert
= FALSE
;
562 // He-e-e-e-elp!! we're trapped in endless loop
574 // by default, show the assert dialog box - we can't customize this
576 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
580 // let the app process it as it wants
581 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
587 void wxAppBase::OnAssert(const wxChar
*file
,
592 ShowAssertDialog(file
, line
, cond
, msg
);