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 // ===========================================================================
54 // ===========================================================================
56 // ----------------------------------------------------------------------------
57 // initialization and termination
58 // ----------------------------------------------------------------------------
60 wxAppBase::wxAppBase()
62 wxTheApp
= (wxApp
*)this;
64 // VZ: what's this? is it obsolete?
65 m_wantDebugOutput
= FALSE
;
68 m_topWindow
= (wxWindow
*)NULL
;
69 m_useBestVisual
= FALSE
;
70 m_exitOnFrameDelete
= TRUE
;
76 bool wxAppBase::OnInitGui()
78 #ifdef __WXUNIVERSAL__
79 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
81 #endif // __WXUNIVERSAL__
87 int wxAppBase::OnExit()
90 // delete the config object if any (don't use Get() here, but Set()
91 // because Get() could create a new config object)
92 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
93 #endif // wxUSE_CONFIG
95 #ifdef __WXUNIVERSAL__
96 delete wxTheme::Set(NULL
);
97 #endif // __WXUNIVERSAL__
102 // ---------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 void wxAppBase::ProcessPendingEvents()
108 // ensure that we're the only thread to modify the pending events list
109 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
111 if ( !wxPendingEvents
)
113 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
117 // iterate until the list becomes empty
118 wxNode
*node
= wxPendingEvents
->First();
121 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
124 // In ProcessPendingEvents(), new handlers might be add
125 // and we can safely leave the critical section here.
126 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
127 handler
->ProcessPendingEvents();
128 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
130 node
= wxPendingEvents
->First();
133 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
142 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
144 if ( active
== m_isActive
)
149 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
150 event
.SetEventObject(this);
152 (void)ProcessEvent(event
);
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 bool wxAppBase::OnInit()
163 #if wxUSE_CMDLINE_PARSER
164 wxCmdLineParser
parser(argc
, argv
);
166 OnInitCmdLine(parser
);
169 switch ( parser
.Parse() )
172 cont
= OnCmdLineHelp(parser
);
176 cont
= OnCmdLineParsed(parser
);
180 cont
= OnCmdLineError(parser
);
186 #endif // wxUSE_CMDLINE_PARSER
191 #if wxUSE_CMDLINE_PARSER
193 #define OPTION_VERBOSE _T("verbose")
194 #define OPTION_THEME _T("theme")
195 #define OPTION_MODE _T("mode")
197 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
199 // the standard command line options
200 static const wxCmdLineEntryDesc cmdLineDesc
[] =
206 gettext_noop("show this help message"),
208 wxCMD_LINE_OPTION_HELP
216 gettext_noop("generate verbose log messages")
220 #ifdef __WXUNIVERSAL__
225 gettext_noop("specify the theme to use"),
226 wxCMD_LINE_VAL_STRING
228 #endif // __WXUNIVERSAL__
230 #if defined(__WXMGL__)
231 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
232 // should provide this option. That's why it is in common/appcmn.cpp
233 // and not mgl/app.cpp
238 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
239 wxCMD_LINE_VAL_STRING
247 parser
.SetDesc(cmdLineDesc
);
250 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
253 if ( parser
.Found(OPTION_VERBOSE
) )
255 wxLog::SetVerbose(TRUE
);
259 #ifdef __WXUNIVERSAL__
261 if ( parser
.Found(OPTION_THEME
, &themeName
) )
263 wxTheme
*theme
= wxTheme::Create(themeName
);
266 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
273 #endif // __WXUNIVERSAL__
275 #if defined(__WXMGL__)
277 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
280 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
282 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
287 if ( !SetDisplayMode(wxDisplayModeInfo(wxSize(w
, h
), bpp
)) )
295 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
302 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
309 #endif // wxUSE_CMDLINE_PARSER
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
318 bool wxAssertIsEqual(int x
, int y
)
323 // break into the debugger
326 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
328 #elif defined(__WXMAC__) && !defined(__DARWIN__)
334 #elif defined(__UNIX__)
341 // show the assert modal dialog
343 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
345 // this variable can be set to true to suppress "assert failure" messages
346 static bool s_bNoAsserts
= FALSE
;
350 // make life easier for people using VC++ IDE: clicking on the message
351 // will take us immediately to the place of the failed assert
352 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
354 wxT("%s(%d): assert failed"),
356 // make the error message more clear for all the others
357 wxT("Assert failed in file %s at line %d"),
363 wxStrcat(szBuf
, wxT(": "));
364 wxStrcat(szBuf
, szMsg
);
366 else // no message given
368 wxStrcat(szBuf
, wxT("."));
373 // send it to the normal log destination
376 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
377 // this message is intentionally not translated - it is for
379 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
381 // use the native message box if available: this is more robust than
383 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
384 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
385 MB_YESNOCANCEL
| MB_ICONSTOP
) )
395 //case IDNO: nothing to do
398 switch ( wxMessageBox(szBuf
, wxT("Debug"),
399 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
409 //case wxNO: nothing to do
419 // this function is called when an assert fails
420 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
423 static bool s_bInAssert
= FALSE
;
427 // He-e-e-e-elp!! we're trapped in endless loop
439 // by default, show the assert dialog box - we can't customize this
441 ShowAssertDialog(szFile
, nLine
, szMsg
);
445 // let the app process it as it wants
446 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
452 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
454 ShowAssertDialog(file
, line
, msg
);