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"
46 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
47 #include <signal.h> // for SIGTRAP used by wxTrap()
50 #if defined(__WXMSW__)
51 #include "wx/msw/private.h" // includes windows.h for MessageBox()
54 #if defined(__WXMAC__)
55 #include "wx/mac/private.h" // includes mac headers
58 // ===========================================================================
60 // ===========================================================================
62 // ----------------------------------------------------------------------------
63 // initialization and termination
64 // ----------------------------------------------------------------------------
67 static void LINKAGEMODE
SetTraceMasks()
70 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
72 wxStringTokenizer
tkn(mask
, wxT(","));
73 while ( tkn
.HasMoreTokens() )
74 wxLog::AddTraceMask(tkn
.GetNextToken());
79 wxAppBase::wxAppBase()
81 wxTheApp
= (wxApp
*)this;
83 #if WXWIN_COMPATIBILITY_2_2
84 m_wantDebugOutput
= FALSE
;
85 #endif // WXWIN_COMPATIBILITY_2_2
88 m_topWindow
= (wxWindow
*)NULL
;
89 m_useBestVisual
= FALSE
;
90 m_exitOnFrameDelete
= TRUE
;
99 wxAppBase::~wxAppBase()
101 // this destructor is required for Darwin
105 bool wxAppBase::OnInitGui()
107 #ifdef __WXUNIVERSAL__
108 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
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 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 bool wxAppBase::OnInit()
192 #if wxUSE_CMDLINE_PARSER
193 wxCmdLineParser
parser(argc
, argv
);
195 OnInitCmdLine(parser
);
198 switch ( parser
.Parse(FALSE
/* don't show usage */) )
201 cont
= OnCmdLineHelp(parser
);
205 cont
= OnCmdLineParsed(parser
);
209 cont
= OnCmdLineError(parser
);
215 #endif // wxUSE_CMDLINE_PARSER
220 #if wxUSE_CMDLINE_PARSER
222 #define OPTION_VERBOSE _T("verbose")
223 #define OPTION_THEME _T("theme")
224 #define OPTION_MODE _T("mode")
226 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
228 // the standard command line options
229 static const wxCmdLineEntryDesc cmdLineDesc
[] =
235 gettext_noop("show this help message"),
237 wxCMD_LINE_OPTION_HELP
245 gettext_noop("generate verbose log messages")
249 #ifdef __WXUNIVERSAL__
254 gettext_noop("specify the theme to use"),
255 wxCMD_LINE_VAL_STRING
257 #endif // __WXUNIVERSAL__
259 #if defined(__WXMGL__)
260 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
261 // should provide this option. That's why it is in common/appcmn.cpp
262 // and not mgl/app.cpp
267 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
268 wxCMD_LINE_VAL_STRING
276 parser
.SetDesc(cmdLineDesc
);
279 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
282 if ( parser
.Found(OPTION_VERBOSE
) )
284 wxLog::SetVerbose(TRUE
);
288 #ifdef __WXUNIVERSAL__
290 if ( parser
.Found(OPTION_THEME
, &themeName
) )
292 wxTheme
*theme
= wxTheme::Create(themeName
);
295 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
302 #endif // __WXUNIVERSAL__
304 #if defined(__WXMGL__)
306 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
309 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
311 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
316 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
324 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
331 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
338 #endif // wxUSE_CMDLINE_PARSER
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
347 bool wxAssertIsEqual(int x
, int y
)
352 // break into the debugger
355 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
357 #elif defined(__WXMAC__) && !defined(__DARWIN__)
363 #elif defined(__UNIX__)
370 // show the assert modal dialog
372 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
374 // this variable can be set to true to suppress "assert failure" messages
375 static bool s_bNoAsserts
= FALSE
;
379 // make life easier for people using VC++ IDE: clicking on the message
380 // will take us immediately to the place of the failed assert
381 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
383 wxT("%s(%d): assert failed"),
385 // make the error message more clear for all the others
386 wxT("Assert failed in file %s at line %d"),
392 wxStrcat(szBuf
, wxT(": "));
393 wxStrcat(szBuf
, szMsg
);
395 else // no message given
397 wxStrcat(szBuf
, wxT("."));
402 // send it to the normal log destination
405 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
406 // this message is intentionally not translated - it is for
408 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
410 // use the native message box if available: this is more robust than
412 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
413 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
414 MB_YESNOCANCEL
| MB_ICONSTOP
) )
424 //case IDNO: nothing to do
427 switch ( wxMessageBox(szBuf
, wxT("Debug"),
428 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
438 //case wxNO: nothing to do
448 // this function is called when an assert fails
449 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
452 static bool s_bInAssert
= FALSE
;
456 // He-e-e-e-elp!! we're trapped in endless loop
468 // by default, show the assert dialog box - we can't customize this
470 ShowAssertDialog(szFile
, nLine
, szMsg
);
474 // let the app process it as it wants
475 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
481 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
483 ShowAssertDialog(file
, line
, msg
);