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__)
39 #include "wx/msgdlg.h"
43 #include "wx/cmdline.h"
44 #include "wx/thread.h"
45 #include "wx/confbase.h"
46 #include "wx/tokenzr.h"
48 #include "wx/msgout.h"
51 #include "wx/artprov.h"
54 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
55 #include <signal.h> // for SIGTRAP used by wxTrap()
58 #if defined(__WXMSW__)
59 #include "wx/msw/private.h" // includes windows.h for MessageBox()
62 #if defined(__WXMAC__)
63 #include "wx/mac/private.h" // includes mac headers
66 // private functions prototypes
68 static void LINKAGEMODE
SetTraceMasks();
71 // ===========================================================================
73 // ===========================================================================
75 // ----------------------------------------------------------------------------
76 // initialization and termination
77 // ----------------------------------------------------------------------------
79 wxAppBase::wxAppBase()
81 wxTheApp
= (wxApp
*)this;
83 #if WXWIN_COMPATIBILITY_2_2
84 m_wantDebugOutput
= FALSE
;
85 #endif // WXWIN_COMPATIBILITY_2_2
88 m_topWindow
= (wxWindow
*)NULL
;
89 m_useBestVisual
= FALSE
;
92 // We don't want to exit the app if the user code shows a dialog from its
93 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
94 // to Yes initially as this dialog would be the last top level window.
95 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
96 // when we enter our OnRun() because we do want the default behaviour from
97 // then on. But this would be a problem if the user code calls
98 // SetExitOnFrameDelete(FALSE) from OnInit().
100 // So we use the special "Later" value which is such that
101 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
102 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
103 // call) overwrite in OnRun()
104 m_exitOnFrameDelete
= Later
;
112 wxAppBase::~wxAppBase()
114 // this destructor is required for Darwin
119 bool wxAppBase::OnInitGui()
121 #ifdef __WXUNIVERSAL__
122 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
124 wxArtProvider
*art
= wxTheme::Get()->GetArtProvider();
126 wxArtProvider::PushProvider(art
);
127 #endif // __WXUNIVERSAL__
132 int wxAppBase::OnRun()
134 // see the comment in ctor: if the initial value hasn't been changed, use
135 // the default Yes from now on
136 if ( m_exitOnFrameDelete
== Later
)
138 m_exitOnFrameDelete
= Yes
;
140 //else: it has been changed, assume the user knows what he is doing
147 int wxAppBase::OnExit()
150 // delete the config object if any (don't use Get() here, but Set()
151 // because Get() could create a new config object)
152 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
153 #endif // wxUSE_CONFIG
155 #ifdef __WXUNIVERSAL__
156 delete wxTheme::Set(NULL
);
157 #endif // __WXUNIVERSAL__
159 // use Set(NULL) and not Get() to avoid creating a message output object on
160 // demand when we just want to delete it
161 delete wxMessageOutput::Set(NULL
);
166 // ----------------------------------------------------------------------------
167 // customization hooks
168 // ----------------------------------------------------------------------------
172 wxLog
*wxAppBase::CreateLogTarget()
174 #if wxUSE_GUI && wxUSE_LOGGUI && !defined(__WXMICROWIN__)
177 return new wxLogStderr
;
183 wxMessageOutput
*wxAppBase::CreateMessageOutput()
185 // The standard way of printing help on command line arguments (app --help)
186 // is (according to common practice):
187 // - console apps: to stderr (on any platform)
188 // - GUI apps: stderr on Unix platforms (!)
189 // message box under Windows and others
190 #if wxUSE_GUI && !defined(__UNIX__)
191 // wxMessageOutputMessageBox doesn't work under Motif
193 return new wxMessageOutputLog
;
195 return new wxMessageOutputMessageBox
;
197 #else // !wxUSE_GUI || __UNIX__
198 return new wxMessageOutputStderr
;
202 // ---------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 void wxAppBase::ProcessPendingEvents()
208 // ensure that we're the only thread to modify the pending events list
209 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
211 if ( !wxPendingEvents
)
213 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
217 // iterate until the list becomes empty
218 wxNode
*node
= wxPendingEvents
->First();
221 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
224 // In ProcessPendingEvents(), new handlers might be add
225 // and we can safely leave the critical section here.
226 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
227 handler
->ProcessPendingEvents();
228 wxENTER_CRIT_SECT( *wxPendingEventsLocker
);
230 node
= wxPendingEvents
->First();
233 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker
);
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
242 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
244 if ( active
== m_isActive
)
249 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
250 event
.SetEventObject(this);
252 (void)ProcessEvent(event
);
257 int wxAppBase::FilterEvent(wxEvent
& WXUNUSED(event
))
259 // process the events normally by default
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 bool wxAppBase::OnInit()
269 #if wxUSE_CMDLINE_PARSER
270 wxCmdLineParser
parser(argc
, argv
);
272 OnInitCmdLine(parser
);
275 switch ( parser
.Parse(FALSE
/* don't show usage */) )
278 cont
= OnCmdLineHelp(parser
);
282 cont
= OnCmdLineParsed(parser
);
286 cont
= OnCmdLineError(parser
);
292 #endif // wxUSE_CMDLINE_PARSER
297 #if wxUSE_CMDLINE_PARSER
299 #define OPTION_VERBOSE _T("verbose")
300 #define OPTION_THEME _T("theme")
301 #define OPTION_MODE _T("mode")
303 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
305 // the standard command line options
306 static const wxCmdLineEntryDesc cmdLineDesc
[] =
312 gettext_noop("show this help message"),
314 wxCMD_LINE_OPTION_HELP
322 gettext_noop("generate verbose log messages"),
328 #ifdef __WXUNIVERSAL__
333 gettext_noop("specify the theme to use"),
334 wxCMD_LINE_VAL_STRING
,
337 #endif // __WXUNIVERSAL__
339 #if defined(__WXMGL__)
340 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
341 // should provide this option. That's why it is in common/appcmn.cpp
342 // and not mgl/app.cpp
347 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
348 wxCMD_LINE_VAL_STRING
,
364 parser
.SetDesc(cmdLineDesc
);
367 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
370 if ( parser
.Found(OPTION_VERBOSE
) )
372 wxLog::SetVerbose(TRUE
);
376 #ifdef __WXUNIVERSAL__
378 if ( parser
.Found(OPTION_THEME
, &themeName
) )
380 wxTheme
*theme
= wxTheme::Create(themeName
);
383 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
390 #endif // __WXUNIVERSAL__
392 #if defined(__WXMGL__)
394 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
397 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
399 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
404 if ( !SetDisplayMode(wxDisplayModeInfo(w
, h
, bpp
)) )
412 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
419 bool wxAppBase::OnCmdLineError(wxCmdLineParser
& parser
)
426 #endif // wxUSE_CMDLINE_PARSER
428 // ----------------------------------------------------------------------------
430 // ----------------------------------------------------------------------------
433 bool wxAppBase::CheckBuildOptions(const wxBuildOptions
& opts
)
435 #define wxCMP(what) (what == opts.m_ ## what)
444 int verMaj
= wxMAJOR_VERSION
,
445 verMin
= wxMINOR_VERSION
;
447 if ( !(wxCMP(isDebug
) && wxCMP(verMaj
) && wxCMP(verMin
)) )
450 wxString libDebug
, progDebug
;
453 libDebug
= wxT("debug");
455 libDebug
= wxT("no debug");
458 progDebug
= wxT("debug");
460 progDebug
= wxT("no debug");
462 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %d.%d (%s), and your program used %d.%d (%s)."),
463 verMaj
, verMin
, libDebug
.c_str(), opts
.m_verMaj
, opts
.m_verMin
, progDebug
.c_str());
465 wxLogFatalError(msg
);
467 // normally wxLogFatalError doesn't return
477 static void LINKAGEMODE
SetTraceMasks()
480 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
482 wxStringTokenizer
tkn(mask
, wxT(","));
483 while ( tkn
.HasMoreTokens() )
484 wxLog::AddTraceMask(tkn
.GetNextToken());
489 bool wxAssertIsEqual(int x
, int y
)
494 // break into the debugger
497 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
499 #elif defined(__WXMAC__) && !defined(__DARWIN__)
505 #elif defined(__UNIX__)
513 void wxAssert(int cond
,
514 const wxChar
*szFile
,
516 const wxChar
*szCond
,
520 wxOnAssert(szFile
, nLine
, szCond
, szMsg
);
523 // show the assert modal dialog
525 void ShowAssertDialog(const wxChar
*szFile
,
527 const wxChar
*szCond
,
530 // this variable can be set to true to suppress "assert failure" messages
531 static bool s_bNoAsserts
= FALSE
;
535 // make life easier for people using VC++ IDE by using this format: like
536 // this, clicking on the message will take us immediately to the place of
538 wxSnprintf(szBuf
, WXSIZEOF(szBuf
),
539 wxT("%s(%d): assert \"%s\" failed"),
540 szFile
, nLine
, szCond
);
544 wxStrcat(szBuf
, wxT(": "));
545 wxStrcat(szBuf
, szMsg
);
547 else // no message given
549 wxStrcat(szBuf
, wxT("."));
554 // send it to the normal log destination
557 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
558 // this message is intentionally not translated - it is for
560 wxStrcat(szBuf
, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
562 // use the native message box if available: this is more robust than
564 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
565 switch ( ::MessageBox(NULL
, szBuf
, _T("Debug"),
566 MB_YESNOCANCEL
| MB_ICONSTOP
) )
576 //case IDNO: nothing to do
579 switch ( wxMessageBox(szBuf
, wxT("Debug"),
580 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
590 //case wxNO: nothing to do
600 // this function is called when an assert fails
601 void wxOnAssert(const wxChar
*szFile
,
603 const wxChar
*szCond
,
607 static bool s_bInAssert
= FALSE
;
611 // He-e-e-e-elp!! we're trapped in endless loop
623 // by default, show the assert dialog box - we can't customize this
625 ShowAssertDialog(szFile
, nLine
, szCond
, szMsg
);
629 // let the app process it as it wants
630 wxTheApp
->OnAssert(szFile
, nLine
, szCond
, szMsg
);
636 void wxAppBase::OnAssert(const wxChar
*file
,
641 ShowAssertDialog(file
, line
, cond
, msg
);