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 wxTheApp
= (wxApp
*)this;
79 #if WXWIN_COMPATIBILITY_2_2
80 m_wantDebugOutput
= FALSE
;
81 #endif // WXWIN_COMPATIBILITY_2_2
84 m_topWindow
= (wxWindow
*)NULL
;
85 m_useBestVisual
= FALSE
;
86 m_exitOnFrameDelete
= TRUE
;
95 wxAppBase::~wxAppBase()
97 // this destructor is required for Darwin
101 bool wxAppBase::OnInitGui()
103 #ifdef __WXUNIVERSAL__
104 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
106 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
108 wxArtProvider::PushProvider(art
);
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 int wxAppBase::FilterEvent(wxEvent
& WXUNUSED(event
))
187 // process the events normally by default
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 bool wxAppBase::OnInit()
197 #if wxUSE_CMDLINE_PARSER
198 wxCmdLineParser
parser(argc
, argv
);
200 OnInitCmdLine(parser
);
203 switch ( parser
.Parse(FALSE
/* don't show usage */) )
206 cont
= OnCmdLineHelp(parser
);
210 cont
= OnCmdLineParsed(parser
);
214 cont
= OnCmdLineError(parser
);
220 #endif // wxUSE_CMDLINE_PARSER
225 #if wxUSE_CMDLINE_PARSER
227 #define OPTION_VERBOSE _T("verbose")
228 #define OPTION_THEME _T("theme")
229 #define OPTION_MODE _T("mode")
231 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
233 // the standard command line options
234 static const wxCmdLineEntryDesc cmdLineDesc
[] =
240 gettext_noop("show this help message"),
242 wxCMD_LINE_OPTION_HELP
250 gettext_noop("generate verbose log messages"),
256 #ifdef __WXUNIVERSAL__
261 gettext_noop("specify the theme to use"),
262 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
,
292 parser
.SetDesc(cmdLineDesc
);
295 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
298 if ( parser
.Found(OPTION_VERBOSE
) )
300 wxLog::SetVerbose(TRUE
);
304 #ifdef __WXUNIVERSAL__
306 if ( parser
.Found(OPTION_THEME
, &themeName
) )
308 wxTheme
*theme
= wxTheme::Create(themeName
);
311 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
318 #endif // __WXUNIVERSAL__
320 #if defined(__WXMGL__)
322 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
325 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
327 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
332 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
340 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
347 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
354 #endif // wxUSE_CMDLINE_PARSER
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
361 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
363 #define wxCMP(what) (what == opts.m_ ## what)
372 int verMaj
= wxMAJOR_VERSION
,
373 verMin
= wxMINOR_VERSION
;
375 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
377 wxLogFatalError(_T("Mismatch between the program and library build ")
378 _T("versions detected."));
380 // normally wxLogFatalError doesn't return
390 static void LINKAGEMODE
SetTraceMasks()
393 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
395 wxStringTokenizer
tkn(mask
, wxT(","));
396 while ( tkn
.HasMoreTokens() )
397 wxLog::AddTraceMask(tkn
.GetNextToken());
402 bool wxAssertIsEqual(int x
, int y
)
407 // break into the debugger
410 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
412 #elif defined(__WXMAC__) && !defined(__DARWIN__)
418 #elif defined(__UNIX__)
425 // show the assert modal dialog
427 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
429 // this variable can be set to true to suppress "assert failure" messages
430 static bool s_bNoAsserts
= FALSE
;
434 // make life easier for people using VC++ IDE: clicking on the message
435 // will take us immediately to the place of the failed assert
436 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
438 wxT("%s(%d): assert failed"),
440 // make the error message more clear for all the others
441 wxT("Assert failed in file %s at line %d"),
447 wxStrcat(szBuf
, wxT(": "));
448 wxStrcat(szBuf
, szMsg
);
450 else // no message given
452 wxStrcat(szBuf
, wxT("."));
457 // send it to the normal log destination
460 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
461 // this message is intentionally not translated - it is for
463 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
465 // use the native message box if available: this is more robust than
467 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
468 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
469 MB_YESNOCANCEL
| MB_ICONSTOP
) )
479 //case IDNO: nothing to do
482 switch ( wxMessageBox(szBuf
, wxT("Debug"),
483 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
493 //case wxNO: nothing to do
503 // this function is called when an assert fails
504 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
507 static bool s_bInAssert
= FALSE
;
511 // He-e-e-e-elp!! we're trapped in endless loop
523 // by default, show the assert dialog box - we can't customize this
525 ShowAssertDialog(szFile
, nLine
, szMsg
);
529 // let the app process it as it wants
530 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
536 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
538 ShowAssertDialog(file
, line
, msg
);