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())
197 // NB: The standard way of printing help on command line arguments (app --help)
198 // is (according to common practice):
199 // - console apps: to stderr (on any platform)
200 // - GUI apps: stderr on Unix platforms (!)
201 // message box under Windows and others
202 #if wxUSE_GUI && !defined(__UNIX__)
204 wxMessageOutput::Set(new wxMessageOutputLog
);
206 wxMessageOutput::Set(new wxMessageOutputMessageBox
);
209 wxMessageOutput::Set(new wxMessageOutputStderr
);
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 bool wxAppBase::OnInit()
220 #if wxUSE_CMDLINE_PARSER
221 wxCmdLineParser
parser(argc
, argv
);
223 OnInitCmdLine(parser
);
226 switch ( parser
.Parse(FALSE
/* don't show usage */) )
229 cont
= OnCmdLineHelp(parser
);
233 cont
= OnCmdLineParsed(parser
);
237 cont
= OnCmdLineError(parser
);
243 #endif // wxUSE_CMDLINE_PARSER
248 #if wxUSE_CMDLINE_PARSER
250 #define OPTION_VERBOSE _T("verbose")
251 #define OPTION_THEME _T("theme")
252 #define OPTION_MODE _T("mode")
254 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
256 // the standard command line options
257 static const wxCmdLineEntryDesc cmdLineDesc
[] =
263 gettext_noop("show this help message"),
265 wxCMD_LINE_OPTION_HELP
273 gettext_noop("generate verbose log messages"),
279 #ifdef __WXUNIVERSAL__
284 gettext_noop("specify the theme to use"),
285 wxCMD_LINE_VAL_STRING
,
288 #endif // __WXUNIVERSAL__
290 #if defined(__WXMGL__)
291 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
292 // should provide this option. That's why it is in common/appcmn.cpp
293 // and not mgl/app.cpp
298 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
299 wxCMD_LINE_VAL_STRING
,
315 parser
.SetDesc(cmdLineDesc
);
318 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
321 if ( parser
.Found(OPTION_VERBOSE
) )
323 wxLog::SetVerbose(TRUE
);
327 #ifdef __WXUNIVERSAL__
329 if ( parser
.Found(OPTION_THEME
, &themeName
) )
331 wxTheme
*theme
= wxTheme::Create(themeName
);
334 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
341 #endif // __WXUNIVERSAL__
343 #if defined(__WXMGL__)
345 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
348 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
350 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
355 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
363 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
370 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
377 #endif // wxUSE_CMDLINE_PARSER
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
384 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
386 #define wxCMP(what) (what == opts.m_ ## what)
395 int verMaj
= wxMAJOR_VERSION
,
396 verMin
= wxMINOR_VERSION
;
398 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
400 wxLogFatalError(_T("Mismatch between the program and library build ")
401 _T("versions detected."));
403 // normally wxLogFatalError doesn't return
413 static void LINKAGEMODE
SetTraceMasks()
416 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
418 wxStringTokenizer
tkn(mask
, wxT(","));
419 while ( tkn
.HasMoreTokens() )
420 wxLog::AddTraceMask(tkn
.GetNextToken());
425 bool wxAssertIsEqual(int x
, int y
)
430 // break into the debugger
433 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
435 #elif defined(__WXMAC__) && !defined(__DARWIN__)
441 #elif defined(__UNIX__)
448 // show the assert modal dialog
450 void ShowAssertDialog(const wxChar
*szFile
,
452 const wxChar
*szCond
,
455 // this variable can be set to true to suppress "assert failure" messages
456 static bool s_bNoAsserts
= FALSE
;
460 // make life easier for people using VC++ IDE by using this format: like
461 // this, clicking on the message will take us immediately to the place of
463 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
464 wxT("%s(%d): assert \"%s\" failed"),
465 szFile
, nLine
, szCond
);
469 wxStrcat(szBuf
, wxT(": "));
470 wxStrcat(szBuf
, szMsg
);
472 else // no message given
474 wxStrcat(szBuf
, wxT("."));
479 // send it to the normal log destination
482 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
483 // this message is intentionally not translated - it is for
485 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
487 // use the native message box if available: this is more robust than
489 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
490 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
491 MB_YESNOCANCEL
| MB_ICONSTOP
) )
501 //case IDNO: nothing to do
504 switch ( wxMessageBox(szBuf
, wxT("Debug"),
505 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
515 //case wxNO: nothing to do
525 // this function is called when an assert fails
526 void wxOnAssert(const wxChar
*szFile
,
528 const wxChar
*szCond
,
532 static bool s_bInAssert
= FALSE
;
536 // He-e-e-e-elp!! we're trapped in endless loop
548 // by default, show the assert dialog box - we can't customize this
550 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
554 // let the app process it as it wants
555 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
561 void wxAppBase::OnAssert(const wxChar
*file
,
566 ShowAssertDialog(file
, line
, cond
, msg
);