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 // ----------------------------------------------------------------------------
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"),
250 #ifdef __WXUNIVERSAL__
255 gettext_noop("specify the theme to use"),
256 wxCMD_LINE_VAL_STRING
,
259 #endif // __WXUNIVERSAL__
261 #if defined(__WXMGL__)
262 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
263 // should provide this option. That's why it is in common/appcmn.cpp
264 // and not mgl/app.cpp
269 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
270 wxCMD_LINE_VAL_STRING
,
286 parser
.SetDesc(cmdLineDesc
);
289 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
292 if ( parser
.Found(OPTION_VERBOSE
) )
294 wxLog::SetVerbose(TRUE
);
298 #ifdef __WXUNIVERSAL__
300 if ( parser
.Found(OPTION_THEME
, &themeName
) )
302 wxTheme
*theme
= wxTheme::Create(themeName
);
305 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
312 #endif // __WXUNIVERSAL__
314 #if defined(__WXMGL__)
316 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
319 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
321 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
326 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
334 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
341 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
348 #endif // wxUSE_CMDLINE_PARSER
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
355 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
357 #define wxCMP(what) (what == opts.m_ ## what)
366 int verMaj
= wxMAJOR_VERSION
,
367 verMin
= wxMINOR_VERSION
;
369 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
371 wxLogFatalError(_T("Mismatch between the program and library build ")
372 _T("versions detected."));
374 // normally wxLogFatalError doesn't return
384 static void LINKAGEMODE
SetTraceMasks()
387 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
389 wxStringTokenizer
tkn(mask
, wxT(","));
390 while ( tkn
.HasMoreTokens() )
391 wxLog::AddTraceMask(tkn
.GetNextToken());
396 bool wxAssertIsEqual(int x
, int y
)
401 // break into the debugger
404 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
406 #elif defined(__WXMAC__) && !defined(__DARWIN__)
412 #elif defined(__UNIX__)
419 // show the assert modal dialog
421 void ShowAssertDialog(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
423 // this variable can be set to true to suppress "assert failure" messages
424 static bool s_bNoAsserts
= FALSE
;
428 // make life easier for people using VC++ IDE: clicking on the message
429 // will take us immediately to the place of the failed assert
430 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
432 wxT("%s(%d): assert failed"),
434 // make the error message more clear for all the others
435 wxT("Assert failed in file %s at line %d"),
441 wxStrcat(szBuf
, wxT(": "));
442 wxStrcat(szBuf
, szMsg
);
444 else // no message given
446 wxStrcat(szBuf
, wxT("."));
451 // send it to the normal log destination
454 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
455 // this message is intentionally not translated - it is for
457 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
459 // use the native message box if available: this is more robust than
461 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
462 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
463 MB_YESNOCANCEL
| MB_ICONSTOP
) )
473 //case IDNO: nothing to do
476 switch ( wxMessageBox(szBuf
, wxT("Debug"),
477 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
487 //case wxNO: nothing to do
497 // this function is called when an assert fails
498 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
501 static bool s_bInAssert
= FALSE
;
505 // He-e-e-e-elp!! we're trapped in endless loop
517 // by default, show the assert dialog box - we can't customize this
519 ShowAssertDialog(szFile
, nLine
, szMsg
);
523 // let the app process it as it wants
524 wxTheApp
->OnAssert(szFile
, nLine
, szMsg
);
530 void wxAppBase::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
532 ShowAssertDialog(file
, line
, msg
);