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"
47 #include "wx/artprov.h"
50 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
51 #include <signal.h> // for SIGTRAP used by wxTrap()
54 #if defined(__WXMSW__)
55 #include "wx/msw/private.h" // includes windows.h for MessageBox()
58 #if defined(__WXMAC__)
59 #include "wx/mac/private.h" // includes mac headers
62 // ===========================================================================
64 // ===========================================================================
66 // ----------------------------------------------------------------------------
67 // initialization and termination
68 // ----------------------------------------------------------------------------
71 static void LINKAGEMODE
SetTraceMasks()
74 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
76 wxStringTokenizer
tkn(mask
, wxT(","));
77 while ( tkn
.HasMoreTokens() )
78 wxLog::AddTraceMask(tkn
.GetNextToken());
83 wxAppBase::wxAppBase()
85 wxTheApp
= (wxApp
*)this;
87 #if WXWIN_COMPATIBILITY_2_2
88 m_wantDebugOutput
= FALSE
;
89 #endif // WXWIN_COMPATIBILITY_2_2
92 m_topWindow
= (wxWindow
*)NULL
;
93 m_useBestVisual
= FALSE
;
94 m_exitOnFrameDelete
= TRUE
;
103 wxAppBase::~wxAppBase()
105 // this destructor is required for Darwin
109 bool wxAppBase::OnInitGui()
111 #ifdef __WXUNIVERSAL__
112 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
114 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
116 wxArtProvider::PushProvider(art
);
117 #endif // __WXUNIVERSAL__
123 int wxAppBase::OnExit()
126 // delete the config object if any (don't use Get() here, but Set()
127 // because Get() could create a new config object)
128 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
129 #endif // wxUSE_CONFIG
131 #ifdef __WXUNIVERSAL__
132 delete wxTheme::Set(NULL
);
133 #endif // __WXUNIVERSAL__
138 // ---------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 void wxAppBase::ProcessPendingEvents()
144 // ensure that we're the only thread to modify the pending events list
145 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
147 if ( !wxPendingEvents
)
149 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
153 // iterate until the list becomes empty
154 wxNode
*node
= wxPendingEvents
->First();
157 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
160 // In ProcessPendingEvents(), new handlers might be add
161 // and we can safely leave the critical section here.
162 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
163 handler
->ProcessPendingEvents();
164 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
166 node
= wxPendingEvents
->First();
169 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
178 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
180 if ( active
== m_isActive
)
185 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
186 event
.SetEventObject(this);
188 (void)ProcessEvent(event
);
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 bool wxAppBase::OnInit()
199 #if wxUSE_CMDLINE_PARSER
200 wxCmdLineParser
parser(argc
, argv
);
202 OnInitCmdLine(parser
);
205 switch ( parser
.Parse(FALSE
/* don't show usage */) )
208 cont
= OnCmdLineHelp(parser
);
212 cont
= OnCmdLineParsed(parser
);
216 cont
= OnCmdLineError(parser
);
222 #endif // wxUSE_CMDLINE_PARSER
227 #if wxUSE_CMDLINE_PARSER
229 #define OPTION_VERBOSE _T("verbose")
230 #define OPTION_THEME _T("theme")
231 #define OPTION_MODE _T("mode")
233 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
235 // the standard command line options
236 static const wxCmdLineEntryDesc cmdLineDesc
[] =
242 gettext_noop("show this help message"),
244 wxCMD_LINE_OPTION_HELP
252 gettext_noop("generate verbose log messages")
256 #ifdef __WXUNIVERSAL__
261 gettext_noop("specify the theme to use"),
262 wxCMD_LINE_VAL_STRING
264 #endif // __WXUNIVERSAL__
266 #if defined(__WXMGL__)
267 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
268 // should provide this option. That's why it is in common/appcmn.cpp
269 // and not mgl/app.cpp
274 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
275 wxCMD_LINE_VAL_STRING
283 parser
.SetDesc(cmdLineDesc
);
286 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
289 if ( parser
.Found(OPTION_VERBOSE
) )
291 wxLog::SetVerbose(TRUE
);
295 #ifdef __WXUNIVERSAL__
297 if ( parser
.Found(OPTION_THEME
, &themeName
) )
299 wxTheme
*theme
= wxTheme::Create(themeName
);
302 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
309 #endif // __WXUNIVERSAL__
311 #if defined(__WXMGL__)
313 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
316 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
318 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
323 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
331 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
338 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
345 #endif // wxUSE_CMDLINE_PARSER
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
354 bool wxAssertIsEqual(int x
, int y
)
359 // break into the debugger
362 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
364 #elif defined(__WXMAC__) && !defined(__DARWIN__)
370 #elif defined(__UNIX__)
377 // show the assert modal dialog
379 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
381 // this variable can be set to true to suppress "assert failure" messages
382 static bool s_bNoAsserts
= FALSE
;
386 // make life easier for people using VC++ IDE: clicking on the message
387 // will take us immediately to the place of the failed assert
388 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
390 wxT("%s(%d): assert failed"),
392 // make the error message more clear for all the others
393 wxT("Assert failed in file %s at line %d"),
399 wxStrcat(szBuf
, wxT(": "));
400 wxStrcat(szBuf
, szMsg
);
402 else // no message given
404 wxStrcat(szBuf
, wxT("."));
409 // send it to the normal log destination
412 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
413 // this message is intentionally not translated - it is for
415 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
417 // use the native message box if available: this is more robust than
419 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
420 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
421 MB_YESNOCANCEL
| MB_ICONSTOP
) )
431 //case IDNO: nothing to do
434 switch ( wxMessageBox(szBuf
, wxT("Debug"),
435 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
445 //case wxNO: nothing to do
455 // this function is called when an assert fails
456 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
459 static bool s_bInAssert
= FALSE
;
463 // He-e-e-e-elp!! we're trapped in endless loop
475 // by default, show the assert dialog box - we can't customize this
477 ShowAssertDialog(szFile
, nLine
, szMsg
);
481 // let the app process it as it wants
482 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
488 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
490 ShowAssertDialog(file
, line
, msg
);