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
;
79 wxAppBase::~wxAppBase()
81 // this destructor is required for Darwin
85 bool wxAppBase::OnInitGui()
87 #ifdef __WXUNIVERSAL__
88 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
90 #endif // __WXUNIVERSAL__
96 int wxAppBase::OnExit()
99 // delete the config object if any (don't use Get() here, but Set()
100 // because Get() could create a new config object)
101 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
102 #endif // wxUSE_CONFIG
104 #ifdef __WXUNIVERSAL__
105 delete wxTheme::Set(NULL
);
106 #endif // __WXUNIVERSAL__
111 // ---------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 void wxAppBase::ProcessPendingEvents()
117 // ensure that we're the only thread to modify the pending events list
118 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
120 if ( !wxPendingEvents
)
122 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
126 // iterate until the list becomes empty
127 wxNode
*node
= wxPendingEvents
->First();
130 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
133 // In ProcessPendingEvents(), new handlers might be add
134 // and we can safely leave the critical section here.
135 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
136 handler
->ProcessPendingEvents();
137 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
139 node
= wxPendingEvents
->First();
142 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
151 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
153 if ( active
== m_isActive
)
158 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
159 event
.SetEventObject(this);
161 (void)ProcessEvent(event
);
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 bool wxAppBase::OnInit()
172 #if wxUSE_CMDLINE_PARSER
173 wxCmdLineParser
parser(argc
, argv
);
175 OnInitCmdLine(parser
);
178 switch ( parser
.Parse(FALSE
/* don't show usage */) )
181 cont
= OnCmdLineHelp(parser
);
185 cont
= OnCmdLineParsed(parser
);
189 cont
= OnCmdLineError(parser
);
195 #endif // wxUSE_CMDLINE_PARSER
200 #if wxUSE_CMDLINE_PARSER
202 #define OPTION_VERBOSE _T("verbose")
203 #define OPTION_THEME _T("theme")
204 #define OPTION_MODE _T("mode")
206 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
208 // the standard command line options
209 static const wxCmdLineEntryDesc cmdLineDesc
[] =
215 gettext_noop("show this help message"),
217 wxCMD_LINE_OPTION_HELP
225 gettext_noop("generate verbose log messages")
229 #ifdef __WXUNIVERSAL__
234 gettext_noop("specify the theme to use"),
235 wxCMD_LINE_VAL_STRING
237 #endif // __WXUNIVERSAL__
239 #if defined(__WXMGL__)
240 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
241 // should provide this option. That's why it is in common/appcmn.cpp
242 // and not mgl/app.cpp
247 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
248 wxCMD_LINE_VAL_STRING
256 parser
.SetDesc(cmdLineDesc
);
259 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
262 if ( parser
.Found(OPTION_VERBOSE
) )
264 wxLog::SetVerbose(TRUE
);
268 #ifdef __WXUNIVERSAL__
270 if ( parser
.Found(OPTION_THEME
, &themeName
) )
272 wxTheme
*theme
= wxTheme::Create(themeName
);
275 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
282 #endif // __WXUNIVERSAL__
284 #if defined(__WXMGL__)
286 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
289 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
291 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
296 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
304 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
311 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
318 #endif // wxUSE_CMDLINE_PARSER
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
327 bool wxAssertIsEqual(int x
, int y
)
332 // break into the debugger
335 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
337 #elif defined(__WXMAC__) && !defined(__DARWIN__)
343 #elif defined(__UNIX__)
350 // show the assert modal dialog
352 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
354 // this variable can be set to true to suppress "assert failure" messages
355 static bool s_bNoAsserts
= FALSE
;
359 // make life easier for people using VC++ IDE: clicking on the message
360 // will take us immediately to the place of the failed assert
361 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
363 wxT("%s(%d): assert failed"),
365 // make the error message more clear for all the others
366 wxT("Assert failed in file %s at line %d"),
372 wxStrcat(szBuf
, wxT(": "));
373 wxStrcat(szBuf
, szMsg
);
375 else // no message given
377 wxStrcat(szBuf
, wxT("."));
382 // send it to the normal log destination
385 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
386 // this message is intentionally not translated - it is for
388 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
390 // use the native message box if available: this is more robust than
392 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
393 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
394 MB_YESNOCANCEL
| MB_ICONSTOP
) )
404 //case IDNO: nothing to do
407 switch ( wxMessageBox(szBuf
, wxT("Debug"),
408 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
418 //case wxNO: nothing to do
428 // this function is called when an assert fails
429 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
432 static bool s_bInAssert
= FALSE
;
436 // He-e-e-e-elp!! we're trapped in endless loop
448 // by default, show the assert dialog box - we can't customize this
450 ShowAssertDialog(szFile
, nLine
, szMsg
);
454 // let the app process it as it wants
455 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
461 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
463 ShowAssertDialog(file
, line
, msg
);