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 // private functions prototypes
64 static void LINKAGEMODE
SetTraceMasks();
67 // ===========================================================================
69 // ===========================================================================
71 // ----------------------------------------------------------------------------
72 // initialization and termination
73 // ----------------------------------------------------------------------------
75 wxAppBase::wxAppBase()
77 // this function is defined by IMPLEMENT_APP() macro in the user code
78 extern const wxBuildOptions
& wxGetBuildOptions();
80 if ( !CheckBuildOptions(wxGetBuildOptions()) )
82 wxLogFatalError(_T("Mismatch between the program and library build ")
83 _T("versions detected."));
86 wxTheApp
= (wxApp
*)this;
88 #if WXWIN_COMPATIBILITY_2_2
89 m_wantDebugOutput
= FALSE
;
90 #endif // WXWIN_COMPATIBILITY_2_2
93 m_topWindow
= (wxWindow
*)NULL
;
94 m_useBestVisual
= FALSE
;
95 m_exitOnFrameDelete
= TRUE
;
104 wxAppBase::~wxAppBase()
106 // this destructor is required for Darwin
110 bool wxAppBase::OnInitGui()
112 #ifdef __WXUNIVERSAL__
113 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
115 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
117 wxArtProvider::PushProvider(art
);
118 #endif // __WXUNIVERSAL__
124 int wxAppBase::OnExit()
127 // delete the config object if any (don't use Get() here, but Set()
128 // because Get() could create a new config object)
129 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
130 #endif // wxUSE_CONFIG
132 #ifdef __WXUNIVERSAL__
133 delete wxTheme::Set(NULL
);
134 #endif // __WXUNIVERSAL__
139 // ---------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 void wxAppBase::ProcessPendingEvents()
145 // ensure that we're the only thread to modify the pending events list
146 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
148 if ( !wxPendingEvents
)
150 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
154 // iterate until the list becomes empty
155 wxNode
*node
= wxPendingEvents
->First();
158 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
161 // In ProcessPendingEvents(), new handlers might be add
162 // and we can safely leave the critical section here.
163 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
164 handler
->ProcessPendingEvents();
165 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
167 node
= wxPendingEvents
->First();
170 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
179 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
181 if ( active
== m_isActive
)
186 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
187 event
.SetEventObject(this);
189 (void)ProcessEvent(event
);
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 bool wxAppBase::OnInit()
200 #if wxUSE_CMDLINE_PARSER
201 wxCmdLineParser
parser(argc
, argv
);
203 OnInitCmdLine(parser
);
206 switch ( parser
.Parse(FALSE
/* don't show usage */) )
209 cont
= OnCmdLineHelp(parser
);
213 cont
= OnCmdLineParsed(parser
);
217 cont
= OnCmdLineError(parser
);
223 #endif // wxUSE_CMDLINE_PARSER
228 #if wxUSE_CMDLINE_PARSER
230 #define OPTION_VERBOSE _T("verbose")
231 #define OPTION_THEME _T("theme")
232 #define OPTION_MODE _T("mode")
234 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
236 // the standard command line options
237 static const wxCmdLineEntryDesc cmdLineDesc
[] =
243 gettext_noop("show this help message"),
245 wxCMD_LINE_OPTION_HELP
253 gettext_noop("generate verbose log messages")
257 #ifdef __WXUNIVERSAL__
262 gettext_noop("specify the theme to use"),
263 wxCMD_LINE_VAL_STRING
265 #endif // __WXUNIVERSAL__
267 #if defined(__WXMGL__)
268 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
269 // should provide this option. That's why it is in common/appcmn.cpp
270 // and not mgl/app.cpp
275 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
276 wxCMD_LINE_VAL_STRING
284 parser
.SetDesc(cmdLineDesc
);
287 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
290 if ( parser
.Found(OPTION_VERBOSE
) )
292 wxLog::SetVerbose(TRUE
);
296 #ifdef __WXUNIVERSAL__
298 if ( parser
.Found(OPTION_THEME
, &themeName
) )
300 wxTheme
*theme
= wxTheme::Create(themeName
);
303 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
310 #endif // __WXUNIVERSAL__
312 #if defined(__WXMGL__)
314 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
317 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
319 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
324 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
332 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
339 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
346 #endif // wxUSE_CMDLINE_PARSER
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
353 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
355 #define wxCMP(what) (what == opts.m_ ## what)
364 int verMaj
= wxMAJOR_VERSION
,
365 verMin
= wxMINOR_VERSION
;
367 return wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
);
374 static void LINKAGEMODE
SetTraceMasks()
377 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
379 wxStringTokenizer
tkn(mask
, wxT(","));
380 while ( tkn
.HasMoreTokens() )
381 wxLog::AddTraceMask(tkn
.GetNextToken());
386 bool wxAssertIsEqual(int x
, int y
)
391 // break into the debugger
394 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
396 #elif defined(__WXMAC__) && !defined(__DARWIN__)
402 #elif defined(__UNIX__)
409 // show the assert modal dialog
411 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
413 // this variable can be set to true to suppress "assert failure" messages
414 static bool s_bNoAsserts
= FALSE
;
418 // make life easier for people using VC++ IDE: clicking on the message
419 // will take us immediately to the place of the failed assert
420 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
422 wxT("%s(%d): assert failed"),
424 // make the error message more clear for all the others
425 wxT("Assert failed in file %s at line %d"),
431 wxStrcat(szBuf
, wxT(": "));
432 wxStrcat(szBuf
, szMsg
);
434 else // no message given
436 wxStrcat(szBuf
, wxT("."));
441 // send it to the normal log destination
444 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
445 // this message is intentionally not translated - it is for
447 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
449 // use the native message box if available: this is more robust than
451 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
452 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
453 MB_YESNOCANCEL
| MB_ICONSTOP
) )
463 //case IDNO: nothing to do
466 switch ( wxMessageBox(szBuf
, wxT("Debug"),
467 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
477 //case wxNO: nothing to do
487 // this function is called when an assert fails
488 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
491 static bool s_bInAssert
= FALSE
;
495 // He-e-e-e-elp!! we're trapped in endless loop
507 // by default, show the assert dialog box - we can't customize this
509 ShowAssertDialog(szFile
, nLine
, szMsg
);
513 // let the app process it as it wants
514 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
520 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
522 ShowAssertDialog(file
, line
, msg
);