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"
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()
52 #if defined(__WXMAC__)
53 #include "wx/mac/private.h" // includes mac headers
56 // ===========================================================================
58 // ===========================================================================
60 // ----------------------------------------------------------------------------
61 // initialization and termination
62 // ----------------------------------------------------------------------------
64 wxAppBase::wxAppBase()
66 wxTheApp
= (wxApp
*)this;
68 // VZ: what's this? is it obsolete?
69 m_wantDebugOutput
= FALSE
;
72 m_topWindow
= (wxWindow
*)NULL
;
73 m_useBestVisual
= FALSE
;
74 m_exitOnFrameDelete
= TRUE
;
80 bool wxAppBase::OnInitGui()
82 #ifdef __WXUNIVERSAL__
83 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
85 #endif // __WXUNIVERSAL__
91 int wxAppBase::OnExit()
94 // delete the config object if any (don't use Get() here, but Set()
95 // because Get() could create a new config object)
96 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
97 #endif // wxUSE_CONFIG
99 #ifdef __WXUNIVERSAL__
100 delete wxTheme::Set(NULL
);
101 #endif // __WXUNIVERSAL__
106 // ---------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 void wxAppBase::ProcessPendingEvents()
112 // ensure that we're the only thread to modify the pending events list
113 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
115 if ( !wxPendingEvents
)
117 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
121 // iterate until the list becomes empty
122 wxNode
*node
= wxPendingEvents
->First();
125 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
128 // In ProcessPendingEvents(), new handlers might be add
129 // and we can safely leave the critical section here.
130 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
131 handler
->ProcessPendingEvents();
132 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
134 node
= wxPendingEvents
->First();
137 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
146 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
148 if ( active
== m_isActive
)
153 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
154 event
.SetEventObject(this);
156 (void)ProcessEvent(event
);
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 bool wxAppBase::OnInit()
167 #if wxUSE_CMDLINE_PARSER
168 wxCmdLineParser
parser(argc
, argv
);
170 OnInitCmdLine(parser
);
173 switch ( parser
.Parse(FALSE
/* don't show usage */) )
176 cont
= OnCmdLineHelp(parser
);
180 cont
= OnCmdLineParsed(parser
);
184 cont
= OnCmdLineError(parser
);
190 #endif // wxUSE_CMDLINE_PARSER
195 #if wxUSE_CMDLINE_PARSER
197 #define OPTION_VERBOSE _T("verbose")
198 #define OPTION_THEME _T("theme")
199 #define OPTION_MODE _T("mode")
201 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
203 // the standard command line options
204 static const wxCmdLineEntryDesc cmdLineDesc
[] =
210 gettext_noop("show this help message"),
212 wxCMD_LINE_OPTION_HELP
220 gettext_noop("generate verbose log messages")
224 #ifdef __WXUNIVERSAL__
229 gettext_noop("specify the theme to use"),
230 wxCMD_LINE_VAL_STRING
232 #endif // __WXUNIVERSAL__
234 #if defined(__WXMGL__)
235 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
236 // should provide this option. That's why it is in common/appcmn.cpp
237 // and not mgl/app.cpp
242 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
243 wxCMD_LINE_VAL_STRING
251 parser
.SetDesc(cmdLineDesc
);
254 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
257 if ( parser
.Found(OPTION_VERBOSE
) )
259 wxLog::SetVerbose(TRUE
);
263 #ifdef __WXUNIVERSAL__
265 if ( parser
.Found(OPTION_THEME
, &themeName
) )
267 wxTheme
*theme
= wxTheme::Create(themeName
);
270 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
277 #endif // __WXUNIVERSAL__
279 #if defined(__WXMGL__)
281 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
284 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
286 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
291 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
299 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
306 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
313 #endif // wxUSE_CMDLINE_PARSER
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
322 bool wxAssertIsEqual(int x
, int y
)
327 // break into the debugger
330 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
332 #elif defined(__WXMAC__) && !defined(__DARWIN__)
338 #elif defined(__UNIX__)
345 // show the assert modal dialog
347 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
349 // this variable can be set to true to suppress "assert failure" messages
350 static bool s_bNoAsserts
= FALSE
;
354 // make life easier for people using VC++ IDE: clicking on the message
355 // will take us immediately to the place of the failed assert
356 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
358 wxT("%s(%d): assert failed"),
360 // make the error message more clear for all the others
361 wxT("Assert failed in file %s at line %d"),
367 wxStrcat(szBuf
, wxT(": "));
368 wxStrcat(szBuf
, szMsg
);
370 else // no message given
372 wxStrcat(szBuf
, wxT("."));
377 // send it to the normal log destination
380 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
381 // this message is intentionally not translated - it is for
383 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
385 // use the native message box if available: this is more robust than
387 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
388 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
389 MB_YESNOCANCEL
| MB_ICONSTOP
) )
399 //case IDNO: nothing to do
402 switch ( wxMessageBox(szBuf
, wxT("Debug"),
403 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
413 //case wxNO: nothing to do
423 // this function is called when an assert fails
424 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
427 static bool s_bInAssert
= FALSE
;
431 // He-e-e-e-elp!! we're trapped in endless loop
443 // by default, show the assert dialog box - we can't customize this
445 ShowAssertDialog(szFile
, nLine
, szMsg
);
449 // let the app process it as it wants
450 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
456 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
458 ShowAssertDialog(file
, line
, msg
);