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"
45 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
46 #include <signal.h> // for SIGTRAP used by wxTrap()
49 #if defined(__WXMSW__)
50 #include "wx/msw/private.h" // includes windows.h for MessageBox()
53 #if defined(__WXMAC__)
54 #include "wx/mac/private.h" // includes mac headers
57 // ===========================================================================
59 // ===========================================================================
61 // ----------------------------------------------------------------------------
62 // initialization and termination
63 // ----------------------------------------------------------------------------
66 static void LINKAGEMODE
SetTraceMasks()
69 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
71 wxStringTokenizer
tkn(mask
, wxT(","));
72 while ( tkn
.HasMoreTokens() )
73 wxLog::AddTraceMask(tkn
.GetNextToken());
78 wxAppBase::wxAppBase()
80 wxTheApp
= (wxApp
*)this;
82 // VZ: what's this? is it obsolete?
83 m_wantDebugOutput
= FALSE
;
86 m_topWindow
= (wxWindow
*)NULL
;
87 m_useBestVisual
= FALSE
;
88 m_exitOnFrameDelete
= TRUE
;
97 wxAppBase::~wxAppBase()
99 // this destructor is required for Darwin
103 bool wxAppBase::OnInitGui()
105 #ifdef __WXUNIVERSAL__
106 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
108 #endif // __WXUNIVERSAL__
114 int wxAppBase::OnExit()
117 // delete the config object if any (don't use Get() here, but Set()
118 // because Get() could create a new config object)
119 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
120 #endif // wxUSE_CONFIG
122 #ifdef __WXUNIVERSAL__
123 delete wxTheme::Set(NULL
);
124 #endif // __WXUNIVERSAL__
129 // ---------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 void wxAppBase::ProcessPendingEvents()
135 // ensure that we're the only thread to modify the pending events list
136 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
138 if ( !wxPendingEvents
)
140 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
144 // iterate until the list becomes empty
145 wxNode
*node
= wxPendingEvents
->First();
148 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
151 // In ProcessPendingEvents(), new handlers might be add
152 // and we can safely leave the critical section here.
153 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
154 handler
->ProcessPendingEvents();
155 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
157 node
= wxPendingEvents
->First();
160 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
169 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
171 if ( active
== m_isActive
)
176 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
177 event
.SetEventObject(this);
179 (void)ProcessEvent(event
);
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 bool wxAppBase::OnInit()
190 #if wxUSE_CMDLINE_PARSER
191 wxCmdLineParser
parser(argc
, argv
);
193 OnInitCmdLine(parser
);
196 switch ( parser
.Parse(FALSE
/* don't show usage */) )
199 cont
= OnCmdLineHelp(parser
);
203 cont
= OnCmdLineParsed(parser
);
207 cont
= OnCmdLineError(parser
);
213 #endif // wxUSE_CMDLINE_PARSER
218 #if wxUSE_CMDLINE_PARSER
220 #define OPTION_VERBOSE _T("verbose")
221 #define OPTION_THEME _T("theme")
222 #define OPTION_MODE _T("mode")
224 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
226 // the standard command line options
227 static const wxCmdLineEntryDesc cmdLineDesc
[] =
233 gettext_noop("show this help message"),
235 wxCMD_LINE_OPTION_HELP
243 gettext_noop("generate verbose log messages")
247 #ifdef __WXUNIVERSAL__
252 gettext_noop("specify the theme to use"),
253 wxCMD_LINE_VAL_STRING
255 #endif // __WXUNIVERSAL__
257 #if defined(__WXMGL__)
258 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
259 // should provide this option. That's why it is in common/appcmn.cpp
260 // and not mgl/app.cpp
265 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
266 wxCMD_LINE_VAL_STRING
274 parser
.SetDesc(cmdLineDesc
);
277 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
280 if ( parser
.Found(OPTION_VERBOSE
) )
282 wxLog::SetVerbose(TRUE
);
286 #ifdef __WXUNIVERSAL__
288 if ( parser
.Found(OPTION_THEME
, &themeName
) )
290 wxTheme
*theme
= wxTheme::Create(themeName
);
293 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
300 #endif // __WXUNIVERSAL__
302 #if defined(__WXMGL__)
304 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
307 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
309 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
314 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
322 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
329 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
336 #endif // wxUSE_CMDLINE_PARSER
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
345 bool wxAssertIsEqual(int x
, int y
)
350 // break into the debugger
353 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
355 #elif defined(__WXMAC__) && !defined(__DARWIN__)
361 #elif defined(__UNIX__)
368 // show the assert modal dialog
370 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
372 // this variable can be set to true to suppress "assert failure" messages
373 static bool s_bNoAsserts
= FALSE
;
377 // make life easier for people using VC++ IDE: clicking on the message
378 // will take us immediately to the place of the failed assert
379 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
381 wxT("%s(%d): assert failed"),
383 // make the error message more clear for all the others
384 wxT("Assert failed in file %s at line %d"),
390 wxStrcat(szBuf
, wxT(": "));
391 wxStrcat(szBuf
, szMsg
);
393 else // no message given
395 wxStrcat(szBuf
, wxT("."));
400 // send it to the normal log destination
403 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
404 // this message is intentionally not translated - it is for
406 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
408 // use the native message box if available: this is more robust than
410 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
411 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
412 MB_YESNOCANCEL
| MB_ICONSTOP
) )
422 //case IDNO: nothing to do
425 switch ( wxMessageBox(szBuf
, wxT("Debug"),
426 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
436 //case wxNO: nothing to do
446 // this function is called when an assert fails
447 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
450 static bool s_bInAssert
= FALSE
;
454 // He-e-e-e-elp!! we're trapped in endless loop
466 // by default, show the assert dialog box - we can't customize this
468 ShowAssertDialog(szFile
, nLine
, szMsg
);
472 // let the app process it as it wants
473 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
479 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
481 ShowAssertDialog(file
, line
, msg
);