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