]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
added missing header for not PCH case
[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{
090a6d7a
VZ
77 // this function is defined by IMPLEMENT_APP() macro in the user code
78 extern const wxBuildOptions& wxGetBuildOptions();
79
80 if ( !CheckBuildOptions(wxGetBuildOptions()) )
697c5f51 81 {
090a6d7a
VZ
82 wxLogFatalError(_T("Mismatch between the program and library build ")
83 _T("versions detected."));
697c5f51 84 }
697c5f51 85
1e6feb95
VZ
86 wxTheApp = (wxApp *)this;
87
73deed44 88#if WXWIN_COMPATIBILITY_2_2
1e6feb95 89 m_wantDebugOutput = FALSE;
73deed44 90#endif // WXWIN_COMPATIBILITY_2_2
1e6feb95
VZ
91
92#if wxUSE_GUI
93 m_topWindow = (wxWindow *)NULL;
94 m_useBestVisual = FALSE;
95 m_exitOnFrameDelete = TRUE;
96 m_isActive = TRUE;
97#endif // wxUSE_GUI
697c5f51
VS
98
99#ifdef __WXDEBUG__
100 SetTraceMasks();
101#endif
1e6feb95
VZ
102}
103
799ea011
GD
104wxAppBase::~wxAppBase()
105{
106 // this destructor is required for Darwin
107}
108
1e6feb95
VZ
109#if wxUSE_GUI
110bool wxAppBase::OnInitGui()
111{
112#ifdef __WXUNIVERSAL__
bf188f1a 113 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
1e6feb95 114 return FALSE;
536b70ac
VS
115 wxArtProvider *art = wxTheme::Get()->GetArtProvider();
116 if ( art )
117 wxArtProvider::PushProvider(art);
1e6feb95
VZ
118#endif // __WXUNIVERSAL__
119
120 return TRUE;
121}
122#endif // wxUSE_GUI
123
124int wxAppBase::OnExit()
125{
126#if wxUSE_CONFIG
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
131
132#ifdef __WXUNIVERSAL__
133 delete wxTheme::Set(NULL);
134#endif // __WXUNIVERSAL__
135
136 return 0;
137}
138
72cdf4c9
VZ
139// ---------------------------------------------------------------------------
140// wxAppBase
141// ----------------------------------------------------------------------------
142
143void wxAppBase::ProcessPendingEvents()
144{
145 // ensure that we're the only thread to modify the pending events list
16c1f79c 146 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
147
148 if ( !wxPendingEvents )
16c1f79c
RR
149 {
150 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 151 return;
16c1f79c 152 }
72cdf4c9
VZ
153
154 // iterate until the list becomes empty
155 wxNode *node = wxPendingEvents->First();
156 while (node)
157 {
158 wxEvtHandler *handler = (wxEvtHandler *)node->Data();
16c1f79c 159 delete node;
72cdf4c9 160
16c1f79c 161 // In ProcessPendingEvents(), new handlers might be add
1d910ac1 162 // and we can safely leave the critical section here.
16c1f79c 163 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 164 handler->ProcessPendingEvents();
16c1f79c 165 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 166
72cdf4c9
VZ
167 node = wxPendingEvents->First();
168 }
1d910ac1 169
16c1f79c 170 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
171}
172
1e6feb95
VZ
173// ----------------------------------------------------------------------------
174// misc
175// ----------------------------------------------------------------------------
176
177#if wxUSE_GUI
178
6e169cf3 179void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 180{
66dfed9b
VZ
181 if ( active == m_isActive )
182 return;
183
1e6feb95 184 m_isActive = active;
66dfed9b
VZ
185
186 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
187 event.SetEventObject(this);
188
189 (void)ProcessEvent(event);
7beba2fc 190}
1e6feb95
VZ
191
192#endif // wxUSE_GUI
bf188f1a
VZ
193
194// ----------------------------------------------------------------------------
195// cmd line parsing
196// ----------------------------------------------------------------------------
197
198bool wxAppBase::OnInit()
199{
200#if wxUSE_CMDLINE_PARSER
201 wxCmdLineParser parser(argc, argv);
202
203 OnInitCmdLine(parser);
204
205 bool cont;
be03c0ec 206 switch ( parser.Parse(FALSE /* don't show usage */) )
bf188f1a
VZ
207 {
208 case -1:
209 cont = OnCmdLineHelp(parser);
210 break;
211
212 case 0:
213 cont = OnCmdLineParsed(parser);
214 break;
215
216 default:
217 cont = OnCmdLineError(parser);
218 break;
219 }
220
221 if ( !cont )
222 return FALSE;
223#endif // wxUSE_CMDLINE_PARSER
224
225 return TRUE;
226}
227
228#if wxUSE_CMDLINE_PARSER
229
230#define OPTION_VERBOSE _T("verbose")
231#define OPTION_THEME _T("theme")
c358c660 232#define OPTION_MODE _T("mode")
bf188f1a
VZ
233
234void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
235{
236 // the standard command line options
237 static const wxCmdLineEntryDesc cmdLineDesc[] =
238 {
239 {
240 wxCMD_LINE_SWITCH,
241 _T("h"),
242 _T("help"),
243 gettext_noop("show this help message"),
244 wxCMD_LINE_VAL_NONE,
245 wxCMD_LINE_OPTION_HELP
246 },
247
248#if wxUSE_LOG
249 {
250 wxCMD_LINE_SWITCH,
251 _T(""),
252 OPTION_VERBOSE,
253 gettext_noop("generate verbose log messages")
254 },
0f02d3d0 255#endif // wxUSE_LOG
bf188f1a
VZ
256
257#ifdef __WXUNIVERSAL__
258 {
259 wxCMD_LINE_OPTION,
260 _T(""),
261 OPTION_THEME,
262 gettext_noop("specify the theme to use"),
263 wxCMD_LINE_VAL_STRING
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)"),
276 wxCMD_LINE_VAL_STRING
277 },
278#endif // __WXMGL__
279
bf188f1a
VZ
280 // terminator
281 { wxCMD_LINE_NONE }
282 };
283
284 parser.SetDesc(cmdLineDesc);
285}
286
287bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
288{
289#if wxUSE_LOG
290 if ( parser.Found(OPTION_VERBOSE) )
291 {
292 wxLog::SetVerbose(TRUE);
293 }
294#endif // wxUSE_LOG
295
296#ifdef __WXUNIVERSAL__
297 wxString themeName;
298 if ( parser.Found(OPTION_THEME, &themeName) )
299 {
300 wxTheme *theme = wxTheme::Create(themeName);
301 if ( !theme )
302 {
303 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
304
305 return FALSE;
306 }
307
308 wxTheme::Set(theme);
309 }
310#endif // __WXUNIVERSAL__
311
c358c660
VS
312#if defined(__WXMGL__)
313 wxString modeDesc;
314 if ( parser.Found(OPTION_MODE, &modeDesc) )
315 {
316 unsigned w, h, bpp;
317 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
318 {
49e885f8 319 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
c358c660
VS
320
321 return FALSE;
322 }
323
07082b28 324 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
49e885f8 325 return FALSE;
c358c660 326 }
be03c0ec 327#endif // __WXMGL__
c358c660 328
bf188f1a
VZ
329 return TRUE;
330}
331
332bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
333{
334 parser.Usage();
335
336 return FALSE;
337}
338
339bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
340{
341 parser.Usage();
342
343 return FALSE;
344}
345
346#endif // wxUSE_CMDLINE_PARSER
347
a5f1fd3e
VZ
348// ----------------------------------------------------------------------------
349// debugging support
350// ----------------------------------------------------------------------------
351
090a6d7a
VZ
352/* static */
353bool wxAppBase::CheckBuildOptions(const wxBuildOptions& opts)
354{
355#define wxCMP(what) (what == opts.m_ ## what)
356
357 bool
358#ifdef __WXDEBUG__
359 isDebug = TRUE;
360#else
361 isDebug = FALSE;
362#endif
363
364 int verMaj = wxMAJOR_VERSION,
365 verMin = wxMINOR_VERSION;
366
367 return wxCMP(isDebug) && wxCMP(verMaj) && wxCMP(verMin);
368
369#undef wxCMP
370}
371
a5f1fd3e
VZ
372#ifdef __WXDEBUG__
373
090a6d7a
VZ
374static void LINKAGEMODE SetTraceMasks()
375{
376 wxString mask;
377 if ( wxGetEnv(wxT("WXTRACE"), &mask) )
378 {
379 wxStringTokenizer tkn(mask, wxT(","));
380 while ( tkn.HasMoreTokens() )
381 wxLog::AddTraceMask(tkn.GetNextToken());
382 }
383}
384
a5f1fd3e
VZ
385// wxASSERT() helper
386bool wxAssertIsEqual(int x, int y)
387{
388 return x == y;
389}
390
391// break into the debugger
392void wxTrap()
393{
394#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
395 DebugBreak();
c31ad41d 396#elif defined(__WXMAC__) && !defined(__DARWIN__)
a5f1fd3e
VZ
397#if __powerc
398 Debugger();
399#else
400 SysBreak();
401#endif
402#elif defined(__UNIX__)
403 raise(SIGTRAP);
404#else
405 // TODO
406#endif // Win/Unix
407}
408
409// show the assert modal dialog
410static
411void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg)
412{
413 // this variable can be set to true to suppress "assert failure" messages
414 static bool s_bNoAsserts = FALSE;
a5f1fd3e
VZ
415
416 wxChar szBuf[4096];
417
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),
421#ifdef __VISUALC__
422 wxT("%s(%d): assert failed"),
423#else // !VC++
424 // make the error message more clear for all the others
425 wxT("Assert failed in file %s at line %d"),
426#endif // VC/!VC
427 szFile, nLine);
428
429 if ( szMsg != NULL )
430 {
431 wxStrcat(szBuf, wxT(": "));
432 wxStrcat(szBuf, szMsg);
433 }
434 else // no message given
435 {
436 wxStrcat(szBuf, wxT("."));
437 }
438
439 if ( !s_bNoAsserts )
440 {
441 // send it to the normal log destination
442 wxLogDebug(szBuf);
443
444#if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
445 // this message is intentionally not translated - it is for
446 // developpers only
447 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
448
449 // use the native message box if available: this is more robust than
450 // using our own
451#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
452 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
453 MB_YESNOCANCEL | MB_ICONSTOP ) )
454 {
455 case IDYES:
456 wxTrap();
457 break;
458
459 case IDCANCEL:
460 s_bNoAsserts = TRUE;
461 break;
462
463 //case IDNO: nothing to do
464 }
465#else // !MSW
466 switch ( wxMessageBox(szBuf, wxT("Debug"),
467 wxYES_NO | wxCANCEL | wxICON_STOP ) )
468 {
469 case wxYES:
470 wxTrap();
471 break;
472
473 case wxCANCEL:
474 s_bNoAsserts = TRUE;
475 break;
476
477 //case wxNO: nothing to do
478 }
479#endif // GUI or MSW
480
481#else // !GUI
482 wxTrap();
483#endif // GUI/!GUI
484 }
a5f1fd3e
VZ
485}
486
487// this function is called when an assert fails
488void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
489{
76456676
VZ
490 // FIXME MT-unsafe
491 static bool s_bInAssert = FALSE;
492
493 if ( s_bInAssert )
494 {
495 // He-e-e-e-elp!! we're trapped in endless loop
496 wxTrap();
497
498 s_bInAssert = FALSE;
499
500 return;
501 }
502
503 s_bInAssert = TRUE;
504
a5f1fd3e
VZ
505 if ( !wxTheApp )
506 {
507 // by default, show the assert dialog box - we can't customize this
508 // behaviour
509 ShowAssertDialog(szFile, nLine, szMsg);
510 }
511 else
512 {
513 // let the app process it as it wants
514 wxTheApp->OnAssert(szFile, nLine, szMsg);
515 }
76456676
VZ
516
517 s_bInAssert = FALSE;
a5f1fd3e
VZ
518}
519
520void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg)
521{
522 ShowAssertDialog(file, line, msg);
523}
524
525#endif //WXDEBUG
526