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
116 bool wxAppBase::OnInitGui()
118 #ifdef __WXUNIVERSAL__
119 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
121 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
123 wxArtProvider::PushProvider(art
);
124 #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
144 int wxAppBase::OnExit()
147 // delete the config object if any (don't use Get() here, but Set()
148 // because Get() could create a new config object)
149 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
150 #endif // wxUSE_CONFIG
152 #ifdef __WXUNIVERSAL__
153 delete wxTheme::Set(NULL
);
154 #endif // __WXUNIVERSAL__
159 // ---------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxAppBase::ProcessPendingEvents()
165 // ensure that we're the only thread to modify the pending events list
166 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
168 if ( !wxPendingEvents
)
170 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
174 // iterate until the list becomes empty
175 wxNode
*node
= wxPendingEvents
->First();
178 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
181 // In ProcessPendingEvents(), new handlers might be add
182 // and we can safely leave the critical section here.
183 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
184 handler
->ProcessPendingEvents();
185 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
187 node
= wxPendingEvents
->First();
190 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
199 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
201 if ( active
== m_isActive
)
206 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
207 event
.SetEventObject(this);
209 (void)ProcessEvent(event
);
214 int wxAppBase::FilterEvent(wxEvent
& WXUNUSED(event
))
216 // process the events normally by default
220 void wxAppBase::DoInit()
222 if (wxMessageOutput::Get())
225 // NB: The standard way of printing help on command line arguments (app --help)
226 // is (according to common practice):
227 // - console apps: to stderr (on any platform)
228 // - GUI apps: stderr on Unix platforms (!)
229 // message box under Windows and others
230 #if wxUSE_GUI && !defined(__UNIX__)
232 wxMessageOutput::Set(new wxMessageOutputLog
);
234 wxMessageOutput::Set(new wxMessageOutputMessageBox
);
237 wxMessageOutput::Set(new wxMessageOutputStderr
);
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 bool wxAppBase::OnInit()
248 #if wxUSE_CMDLINE_PARSER
249 wxCmdLineParser
parser(argc
, argv
);
251 OnInitCmdLine(parser
);
254 switch ( parser
.Parse(FALSE
/* don't show usage */) )
257 cont
= OnCmdLineHelp(parser
);
261 cont
= OnCmdLineParsed(parser
);
265 cont
= OnCmdLineError(parser
);
271 #endif // wxUSE_CMDLINE_PARSER
276 #if wxUSE_CMDLINE_PARSER
278 #define OPTION_VERBOSE _T("verbose")
279 #define OPTION_THEME _T("theme")
280 #define OPTION_MODE _T("mode")
282 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
284 // the standard command line options
285 static const wxCmdLineEntryDesc cmdLineDesc
[] =
291 gettext_noop("show this help message"),
293 wxCMD_LINE_OPTION_HELP
301 gettext_noop("generate verbose log messages"),
307 #ifdef __WXUNIVERSAL__
312 gettext_noop("specify the theme to use"),
313 wxCMD_LINE_VAL_STRING
,
316 #endif // __WXUNIVERSAL__
318 #if defined(__WXMGL__)
319 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
320 // should provide this option. That's why it is in common/appcmn.cpp
321 // and not mgl/app.cpp
326 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
327 wxCMD_LINE_VAL_STRING
,
343 parser
.SetDesc(cmdLineDesc
);
346 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
349 if ( parser
.Found(OPTION_VERBOSE
) )
351 wxLog::SetVerbose(TRUE
);
355 #ifdef __WXUNIVERSAL__
357 if ( parser
.Found(OPTION_THEME
, &themeName
) )
359 wxTheme
*theme
= wxTheme::Create(themeName
);
362 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
369 #endif // __WXUNIVERSAL__
371 #if defined(__WXMGL__)
373 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
376 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
378 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
383 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
391 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
398 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
405 #endif // wxUSE_CMDLINE_PARSER
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
412 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
414 #define wxCMP(what) (what == opts.m_ ## what)
423 int verMaj
= wxMAJOR_VERSION
,
424 verMin
= wxMINOR_VERSION
;
426 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
428 wxLogFatalError(_T("Mismatch between the program and library build ")
429 _T("versions detected."));
431 // normally wxLogFatalError doesn't return
441 static void LINKAGEMODE
SetTraceMasks()
444 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
446 wxStringTokenizer
tkn(mask
, wxT(","));
447 while ( tkn
.HasMoreTokens() )
448 wxLog::AddTraceMask(tkn
.GetNextToken());
453 bool wxAssertIsEqual(int x
, int y
)
458 // break into the debugger
461 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
463 #elif defined(__WXMAC__) && !defined(__DARWIN__)
469 #elif defined(__UNIX__)
476 // show the assert modal dialog
478 void ShowAssertDialog(const wxChar
*szFile
,
480 const wxChar
*szCond
,
483 // this variable can be set to true to suppress "assert failure" messages
484 static bool s_bNoAsserts
= FALSE
;
488 // make life easier for people using VC++ IDE by using this format: like
489 // this, clicking on the message will take us immediately to the place of
491 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
492 wxT("%s(%d): assert \"%s\" failed"),
493 szFile
, nLine
, szCond
);
497 wxStrcat(szBuf
, wxT(": "));
498 wxStrcat(szBuf
, szMsg
);
500 else // no message given
502 wxStrcat(szBuf
, wxT("."));
507 // send it to the normal log destination
510 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
511 // this message is intentionally not translated - it is for
513 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
515 // use the native message box if available: this is more robust than
517 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
518 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
519 MB_YESNOCANCEL
| MB_ICONSTOP
) )
529 //case IDNO: nothing to do
532 switch ( wxMessageBox(szBuf
, wxT("Debug"),
533 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
543 //case wxNO: nothing to do
553 // this function is called when an assert fails
554 void wxOnAssert(const wxChar
*szFile
,
556 const wxChar
*szCond
,
560 static bool s_bInAssert
= FALSE
;
564 // He-e-e-e-elp!! we're trapped in endless loop
576 // by default, show the assert dialog box - we can't customize this
578 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
582 // let the app process it as it wants
583 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
589 void wxAppBase::OnAssert(const wxChar
*file
,
594 ShowAssertDialog(file
, line
, cond
, msg
);