]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
don't write the strings to the stream one char at a time, it's *horribly* slow
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/appcmn.cpp
3// Purpose: wxAppBase methods common to all platforms
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 18.10.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "appbase.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
bf188f1a 33 #include "wx/intl.h"
e87271f3 34 #include "wx/list.h"
a5f1fd3e
VZ
35 #if wxUSE_GUI
36 #include "wx/msgdlg.h"
37 #endif // wxUSE_GUI
72cdf4c9
VZ
38#endif
39
bf188f1a 40#include "wx/cmdline.h"
72cdf4c9 41#include "wx/thread.h"
7beba2fc 42#include "wx/confbase.h"
697c5f51 43#include "wx/tokenzr.h"
bebc39e3 44#include "wx/utils.h"
8d38cdb7
VZ
45
46#if wxUSE_GUI
47 #include "wx/artprov.h"
48#endif // wxUSE_GUI
e1ee679c 49
a5f1fd3e
VZ
50#if !defined(__WXMSW__) || defined(__WXMICROWIN__)
51 #include <signal.h> // for SIGTRAP used by wxTrap()
52#endif //Win/Unix
53
54#if defined(__WXMSW__)
55 #include "wx/msw/private.h" // includes windows.h for MessageBox()
56#endif
57
5128e3be
SC
58#if defined(__WXMAC__)
59 #include "wx/mac/private.h" // includes mac headers
60#endif
61
090a6d7a
VZ
62// private functions prototypes
63#ifdef __WXDEBUG__
64 static void LINKAGEMODE SetTraceMasks();
65#endif // __WXDEBUG__
66
d54598dd
VZ
67// ===========================================================================
68// implementation
69// ===========================================================================
70
bf188f1a
VZ
71// ----------------------------------------------------------------------------
72// initialization and termination
73// ----------------------------------------------------------------------------
74
090a6d7a 75wxAppBase::wxAppBase()
697c5f51 76{
1e6feb95
VZ
77 wxTheApp = (wxApp *)this;
78
73deed44 79#if WXWIN_COMPATIBILITY_2_2
1e6feb95 80 m_wantDebugOutput = FALSE;
73deed44 81#endif // WXWIN_COMPATIBILITY_2_2
1e6feb95
VZ
82
83#if wxUSE_GUI
84 m_topWindow = (wxWindow *)NULL;
85 m_useBestVisual = FALSE;
86 m_exitOnFrameDelete = TRUE;
87 m_isActive = TRUE;
88#endif // wxUSE_GUI
697c5f51
VS
89
90#ifdef __WXDEBUG__
91 SetTraceMasks();
92#endif
1e6feb95
VZ
93}
94
799ea011
GD
95wxAppBase::~wxAppBase()
96{
97 // this destructor is required for Darwin
98}
99
1e6feb95
VZ
100#if wxUSE_GUI
101bool wxAppBase::OnInitGui()
102{
103#ifdef __WXUNIVERSAL__
bf188f1a 104 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
1e6feb95 105 return FALSE;
536b70ac
VS
106 wxArtProvider *art = wxTheme::Get()->GetArtProvider();
107 if ( art )
108 wxArtProvider::PushProvider(art);
1e6feb95
VZ
109#endif // __WXUNIVERSAL__
110
111 return TRUE;
112}
113#endif // wxUSE_GUI
114
115int wxAppBase::OnExit()
116{
117#if wxUSE_CONFIG
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
122
123#ifdef __WXUNIVERSAL__
124 delete wxTheme::Set(NULL);
125#endif // __WXUNIVERSAL__
126
127 return 0;
128}
129
72cdf4c9
VZ
130// ---------------------------------------------------------------------------
131// wxAppBase
132// ----------------------------------------------------------------------------
133
134void wxAppBase::ProcessPendingEvents()
135{
136 // ensure that we're the only thread to modify the pending events list
16c1f79c 137 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
138
139 if ( !wxPendingEvents )
16c1f79c
RR
140 {
141 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 142 return;
16c1f79c 143 }
72cdf4c9
VZ
144
145 // iterate until the list becomes empty
146 wxNode *node = wxPendingEvents->First();
147 while (node)
148 {
149 wxEvtHandler *handler = (wxEvtHandler *)node->Data();
16c1f79c 150 delete node;
72cdf4c9 151
16c1f79c 152 // In ProcessPendingEvents(), new handlers might be add
1d910ac1 153 // and we can safely leave the critical section here.
16c1f79c 154 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 155 handler->ProcessPendingEvents();
16c1f79c 156 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 157
72cdf4c9
VZ
158 node = wxPendingEvents->First();
159 }
1d910ac1 160
16c1f79c 161 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
162}
163
1e6feb95
VZ
164// ----------------------------------------------------------------------------
165// misc
166// ----------------------------------------------------------------------------
167
168#if wxUSE_GUI
169
6e169cf3 170void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 171{
66dfed9b
VZ
172 if ( active == m_isActive )
173 return;
174
1e6feb95 175 m_isActive = active;
66dfed9b
VZ
176
177 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
178 event.SetEventObject(this);
179
180 (void)ProcessEvent(event);
7beba2fc 181}
1e6feb95
VZ
182
183#endif // wxUSE_GUI
bf188f1a 184
9154d8cf
VZ
185int wxAppBase::FilterEvent(wxEvent& WXUNUSED(event))
186{
187 // process the events normally by default
188 return -1;
189}
190
bf188f1a
VZ
191// ----------------------------------------------------------------------------
192// cmd line parsing
193// ----------------------------------------------------------------------------
194
195bool wxAppBase::OnInit()
196{
197#if wxUSE_CMDLINE_PARSER
198 wxCmdLineParser parser(argc, argv);
199
200 OnInitCmdLine(parser);
201
202 bool cont;
be03c0ec 203 switch ( parser.Parse(FALSE /* don't show usage */) )
bf188f1a
VZ
204 {
205 case -1:
206 cont = OnCmdLineHelp(parser);
207 break;
208
209 case 0:
210 cont = OnCmdLineParsed(parser);
211 break;
212
213 default:
214 cont = OnCmdLineError(parser);
215 break;
216 }
217
218 if ( !cont )
219 return FALSE;
220#endif // wxUSE_CMDLINE_PARSER
221
222 return TRUE;
223}
224
225#if wxUSE_CMDLINE_PARSER
226
227#define OPTION_VERBOSE _T("verbose")
228#define OPTION_THEME _T("theme")
c358c660 229#define OPTION_MODE _T("mode")
bf188f1a
VZ
230
231void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
232{
233 // the standard command line options
234 static const wxCmdLineEntryDesc cmdLineDesc[] =
235 {
236 {
237 wxCMD_LINE_SWITCH,
238 _T("h"),
239 _T("help"),
240 gettext_noop("show this help message"),
241 wxCMD_LINE_VAL_NONE,
242 wxCMD_LINE_OPTION_HELP
243 },
244
245#if wxUSE_LOG
246 {
247 wxCMD_LINE_SWITCH,
248 _T(""),
249 OPTION_VERBOSE,
ba161d7e
GD
250 gettext_noop("generate verbose log messages"),
251 wxCMD_LINE_VAL_NONE,
252 0x0
bf188f1a 253 },
0f02d3d0 254#endif // wxUSE_LOG
bf188f1a
VZ
255
256#ifdef __WXUNIVERSAL__
257 {
258 wxCMD_LINE_OPTION,
259 _T(""),
260 OPTION_THEME,
261 gettext_noop("specify the theme to use"),
ba161d7e
GD
262 wxCMD_LINE_VAL_STRING,
263 0x0
bf188f1a
VZ
264 },
265#endif // __WXUNIVERSAL__
266
c358c660
VS
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
271 {
272 wxCMD_LINE_OPTION,
273 _T(""),
274 OPTION_MODE,
275 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
ba161d7e
GD
276 wxCMD_LINE_VAL_STRING,
277 0x0
c358c660
VS
278 },
279#endif // __WXMGL__
280
bf188f1a 281 // terminator
ba161d7e
GD
282 {
283 wxCMD_LINE_NONE,
284 _T(""),
285 _T(""),
286 _T(""),
287 wxCMD_LINE_VAL_NONE,
288 0x0
289 }
bf188f1a
VZ
290 };
291
292 parser.SetDesc(cmdLineDesc);
293}
294
295bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
296{
297#if wxUSE_LOG
298 if ( parser.Found(OPTION_VERBOSE) )
299 {
300 wxLog::SetVerbose(TRUE);
301 }
302#endif // wxUSE_LOG
303
304#ifdef __WXUNIVERSAL__
305 wxString themeName;
306 if ( parser.Found(OPTION_THEME, &themeName) )
307 {
308 wxTheme *theme = wxTheme::Create(themeName);
309 if ( !theme )
310 {
311 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
312
313 return FALSE;
314 }
315
316 wxTheme::Set(theme);
317 }
318#endif // __WXUNIVERSAL__
319
c358c660
VS
320#if defined(__WXMGL__)
321 wxString modeDesc;
322 if ( parser.Found(OPTION_MODE, &modeDesc) )
323 {
324 unsigned w, h, bpp;
325 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
326 {
49e885f8 327 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
c358c660
VS
328
329 return FALSE;
330 }
331
07082b28 332 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
49e885f8 333 return FALSE;
c358c660 334 }
be03c0ec 335#endif // __WXMGL__
c358c660 336
bf188f1a
VZ
337 return TRUE;
338}
339
340bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
341{
342 parser.Usage();
343
344 return FALSE;
345}
346
347bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
348{
349 parser.Usage();
350
351 return FALSE;
352}
353
354#endif // wxUSE_CMDLINE_PARSER
355
a5f1fd3e
VZ
356// ----------------------------------------------------------------------------
357// debugging support
358// ----------------------------------------------------------------------------
359
090a6d7a
VZ
360/* static */
361bool wxAppBase::CheckBuildOptions(const wxBuildOptions& opts)
362{
363#define wxCMP(what) (what == opts.m_ ## what)
364
365 bool
366#ifdef __WXDEBUG__
367 isDebug = TRUE;
368#else
369 isDebug = FALSE;
370#endif
371
372 int verMaj = wxMAJOR_VERSION,
373 verMin = wxMINOR_VERSION;
374
e6e6fcc9
VZ
375 if ( !(wxCMP(isDebug) && wxCMP(verMaj) && wxCMP(verMin)) )
376 {
377 wxLogFatalError(_T("Mismatch between the program and library build ")
378 _T("versions detected."));
090a6d7a 379
e6e6fcc9
VZ
380 // normally wxLogFatalError doesn't return
381 return FALSE;
382 }
090a6d7a 383#undef wxCMP
e6e6fcc9
VZ
384
385 return TRUE;
090a6d7a
VZ
386}
387
a5f1fd3e
VZ
388#ifdef __WXDEBUG__
389
090a6d7a
VZ
390static void LINKAGEMODE SetTraceMasks()
391{
392 wxString mask;
393 if ( wxGetEnv(wxT("WXTRACE"), &mask) )
394 {
395 wxStringTokenizer tkn(mask, wxT(","));
396 while ( tkn.HasMoreTokens() )
397 wxLog::AddTraceMask(tkn.GetNextToken());
398 }
399}
400
a5f1fd3e
VZ
401// wxASSERT() helper
402bool wxAssertIsEqual(int x, int y)
403{
404 return x == y;
405}
406
407// break into the debugger
408void wxTrap()
409{
410#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
411 DebugBreak();
c31ad41d 412#elif defined(__WXMAC__) && !defined(__DARWIN__)
a5f1fd3e
VZ
413#if __powerc
414 Debugger();
415#else
416 SysBreak();
417#endif
418#elif defined(__UNIX__)
419 raise(SIGTRAP);
420#else
421 // TODO
422#endif // Win/Unix
423}
424
425// show the assert modal dialog
426static
aad65f13
VZ
427void ShowAssertDialog(const wxChar *szFile,
428 int nLine,
429 const wxChar *szCond,
430 const wxChar *szMsg)
a5f1fd3e
VZ
431{
432 // this variable can be set to true to suppress "assert failure" messages
433 static bool s_bNoAsserts = FALSE;
a5f1fd3e
VZ
434
435 wxChar szBuf[4096];
436
aad65f13
VZ
437 // make life easier for people using VC++ IDE by using this format: like
438 // this, clicking on the message will take us immediately to the place of
439 // the failed assert
a5f1fd3e 440 wxSnprintf(szBuf, WXSIZEOF(szBuf),
aad65f13
VZ
441 wxT("%s(%d): assert \"%s\" failed"),
442 szFile, nLine, szCond);
a5f1fd3e
VZ
443
444 if ( szMsg != NULL )
445 {
446 wxStrcat(szBuf, wxT(": "));
447 wxStrcat(szBuf, szMsg);
448 }
449 else // no message given
450 {
451 wxStrcat(szBuf, wxT("."));
452 }
453
454 if ( !s_bNoAsserts )
455 {
456 // send it to the normal log destination
457 wxLogDebug(szBuf);
458
459#if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
460 // this message is intentionally not translated - it is for
461 // developpers only
462 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
463
464 // use the native message box if available: this is more robust than
465 // using our own
466#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
467 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
468 MB_YESNOCANCEL | MB_ICONSTOP ) )
469 {
470 case IDYES:
471 wxTrap();
472 break;
473
474 case IDCANCEL:
475 s_bNoAsserts = TRUE;
476 break;
477
478 //case IDNO: nothing to do
479 }
480#else // !MSW
481 switch ( wxMessageBox(szBuf, wxT("Debug"),
482 wxYES_NO | wxCANCEL | wxICON_STOP ) )
483 {
484 case wxYES:
485 wxTrap();
486 break;
487
488 case wxCANCEL:
489 s_bNoAsserts = TRUE;
490 break;
491
492 //case wxNO: nothing to do
493 }
494#endif // GUI or MSW
495
496#else // !GUI
497 wxTrap();
498#endif // GUI/!GUI
499 }
a5f1fd3e
VZ
500}
501
502// this function is called when an assert fails
aad65f13
VZ
503void wxOnAssert(const wxChar *szFile,
504 int nLine,
505 const wxChar *szCond,
506 const wxChar *szMsg)
a5f1fd3e 507{
76456676
VZ
508 // FIXME MT-unsafe
509 static bool s_bInAssert = FALSE;
510
511 if ( s_bInAssert )
512 {
513 // He-e-e-e-elp!! we're trapped in endless loop
514 wxTrap();
515
516 s_bInAssert = FALSE;
517
518 return;
519 }
520
521 s_bInAssert = TRUE;
522
a5f1fd3e
VZ
523 if ( !wxTheApp )
524 {
525 // by default, show the assert dialog box - we can't customize this
526 // behaviour
aad65f13 527 ShowAssertDialog(szFile, nLine, szCond, szMsg);
a5f1fd3e
VZ
528 }
529 else
530 {
531 // let the app process it as it wants
aad65f13 532 wxTheApp->OnAssert(szFile, nLine, szCond, szMsg);
a5f1fd3e 533 }
76456676
VZ
534
535 s_bInAssert = FALSE;
a5f1fd3e
VZ
536}
537
aad65f13
VZ
538void wxAppBase::OnAssert(const wxChar *file,
539 int line,
540 const wxChar *cond,
541 const wxChar *msg)
a5f1fd3e 542{
aad65f13 543 ShowAssertDialog(file, line, cond, msg);
a5f1fd3e
VZ
544}
545
546#endif //WXDEBUG
547