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 // VZ: what's this? is it obsolete?
84 m_wantDebugOutput
= FALSE
;
87 m_topWindow
= (wxWindow
*)NULL
;
88 m_useBestVisual
= FALSE
;
89 m_exitOnFrameDelete
= TRUE
;
98 wxAppBase::~wxAppBase()
100 // this destructor is required for Darwin
104 bool wxAppBase::OnInitGui()
106 #ifdef __WXUNIVERSAL__
107 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
109 #endif // __WXUNIVERSAL__
115 int wxAppBase::OnExit()
118 // delete the config object if any (don't use Get() here, but Set()
119 // because Get() could create a new config object)
120 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
121 #endif // wxUSE_CONFIG
123 #ifdef __WXUNIVERSAL__
124 delete wxTheme::Set(NULL
);
125 #endif // __WXUNIVERSAL__
130 // ---------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 void wxAppBase::ProcessPendingEvents()
136 // ensure that we're the only thread to modify the pending events list
137 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
139 if ( !wxPendingEvents
)
141 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
145 // iterate until the list becomes empty
146 wxNode
*node
= wxPendingEvents
->First();
149 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
152 // In ProcessPendingEvents(), new handlers might be add
153 // and we can safely leave the critical section here.
154 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
155 handler
->ProcessPendingEvents();
156 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
158 node
= wxPendingEvents
->First();
161 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
170 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
172 if ( active
== m_isActive
)
177 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
178 event
.SetEventObject(this);
180 (void)ProcessEvent(event
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 bool wxAppBase::OnInit()
191 #if wxUSE_CMDLINE_PARSER
192 wxCmdLineParser
parser(argc
, argv
);
194 OnInitCmdLine(parser
);
197 switch ( parser
.Parse(FALSE
/* don't show usage */) )
200 cont
= OnCmdLineHelp(parser
);
204 cont
= OnCmdLineParsed(parser
);
208 cont
= OnCmdLineError(parser
);
214 #endif // wxUSE_CMDLINE_PARSER
219 #if wxUSE_CMDLINE_PARSER
221 #define OPTION_VERBOSE _T("verbose")
222 #define OPTION_THEME _T("theme")
223 #define OPTION_MODE _T("mode")
225 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
227 // the standard command line options
228 static const wxCmdLineEntryDesc cmdLineDesc
[] =
234 gettext_noop("show this help message"),
236 wxCMD_LINE_OPTION_HELP
244 gettext_noop("generate verbose log messages")
248 #ifdef __WXUNIVERSAL__
253 gettext_noop("specify the theme to use"),
254 wxCMD_LINE_VAL_STRING
256 #endif // __WXUNIVERSAL__
258 #if defined(__WXMGL__)
259 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
260 // should provide this option. That's why it is in common/appcmn.cpp
261 // and not mgl/app.cpp
266 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
267 wxCMD_LINE_VAL_STRING
275 parser
.SetDesc(cmdLineDesc
);
278 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
281 if ( parser
.Found(OPTION_VERBOSE
) )
283 wxLog::SetVerbose(TRUE
);
287 #ifdef __WXUNIVERSAL__
289 if ( parser
.Found(OPTION_THEME
, &themeName
) )
291 wxTheme
*theme
= wxTheme::Create(themeName
);
294 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
301 #endif // __WXUNIVERSAL__
303 #if defined(__WXMGL__)
305 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
308 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
310 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
315 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
323 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
330 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
337 #endif // wxUSE_CMDLINE_PARSER
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
346 bool wxAssertIsEqual(int x
, int y
)
351 // break into the debugger
354 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
356 #elif defined(__WXMAC__) && !defined(__DARWIN__)
362 #elif defined(__UNIX__)
369 // show the assert modal dialog
371 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
373 // this variable can be set to true to suppress "assert failure" messages
374 static bool s_bNoAsserts
= FALSE
;
378 // make life easier for people using VC++ IDE: clicking on the message
379 // will take us immediately to the place of the failed assert
380 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
382 wxT("%s(%d): assert failed"),
384 // make the error message more clear for all the others
385 wxT("Assert failed in file %s at line %d"),
391 wxStrcat(szBuf
, wxT(": "));
392 wxStrcat(szBuf
, szMsg
);
394 else // no message given
396 wxStrcat(szBuf
, wxT("."));
401 // send it to the normal log destination
404 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
405 // this message is intentionally not translated - it is for
407 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
409 // use the native message box if available: this is more robust than
411 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
412 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
413 MB_YESNOCANCEL
| MB_ICONSTOP
) )
423 //case IDNO: nothing to do
426 switch ( wxMessageBox(szBuf
, wxT("Debug"),
427 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
437 //case wxNO: nothing to do
447 // this function is called when an assert fails
448 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
451 static bool s_bInAssert
= FALSE
;
455 // He-e-e-e-elp!! we're trapped in endless loop
467 // by default, show the assert dialog box - we can't customize this
469 ShowAssertDialog(szFile
, nLine
, szMsg
);
473 // let the app process it as it wants
474 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
480 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
482 ShowAssertDialog(file
, line
, msg
);