]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
check for NULL szLocale in Init()
[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"
a69be60b
VZ
35 #if wxUSE_LOG
36 #include "wx/log.h"
37 #endif // wxUSE_LOG
a5f1fd3e
VZ
38 #if wxUSE_GUI
39 #include "wx/msgdlg.h"
40 #endif // wxUSE_GUI
72cdf4c9
VZ
41#endif
42
bf188f1a 43#include "wx/cmdline.h"
72cdf4c9 44#include "wx/thread.h"
7beba2fc 45#include "wx/confbase.h"
697c5f51 46#include "wx/tokenzr.h"
bebc39e3 47#include "wx/utils.h"
74698d3a 48#include "wx/msgout.h"
8d38cdb7
VZ
49
50#if wxUSE_GUI
51 #include "wx/artprov.h"
52#endif // wxUSE_GUI
e1ee679c 53
a5f1fd3e
VZ
54#if !defined(__WXMSW__) || defined(__WXMICROWIN__)
55 #include <signal.h> // for SIGTRAP used by wxTrap()
56#endif //Win/Unix
57
58#if defined(__WXMSW__)
59 #include "wx/msw/private.h" // includes windows.h for MessageBox()
60#endif
61
5128e3be
SC
62#if defined(__WXMAC__)
63 #include "wx/mac/private.h" // includes mac headers
64#endif
65
090a6d7a
VZ
66// private functions prototypes
67#ifdef __WXDEBUG__
68 static void LINKAGEMODE SetTraceMasks();
69#endif // __WXDEBUG__
70
d54598dd
VZ
71// ===========================================================================
72// implementation
73// ===========================================================================
74
bf188f1a
VZ
75// ----------------------------------------------------------------------------
76// initialization and termination
77// ----------------------------------------------------------------------------
78
090a6d7a 79wxAppBase::wxAppBase()
697c5f51 80{
1e6feb95
VZ
81 wxTheApp = (wxApp *)this;
82
73deed44 83#if WXWIN_COMPATIBILITY_2_2
1e6feb95 84 m_wantDebugOutput = FALSE;
73deed44 85#endif // WXWIN_COMPATIBILITY_2_2
1e6feb95
VZ
86
87#if wxUSE_GUI
88 m_topWindow = (wxWindow *)NULL;
89 m_useBestVisual = FALSE;
1e6feb95 90 m_isActive = TRUE;
1cbee0b4
VZ
91
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().
99 //
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;
1e6feb95 105#endif // wxUSE_GUI
697c5f51
VS
106
107#ifdef __WXDEBUG__
108 SetTraceMasks();
109#endif
1e6feb95
VZ
110}
111
799ea011
GD
112wxAppBase::~wxAppBase()
113{
114 // this destructor is required for Darwin
115}
116
1e6feb95 117#if wxUSE_GUI
9e5b30eb 118
1e6feb95
VZ
119bool wxAppBase::OnInitGui()
120{
121#ifdef __WXUNIVERSAL__
bf188f1a 122 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
1e6feb95
VZ
123 return FALSE;
124#endif // __WXUNIVERSAL__
125
126 return TRUE;
127}
1e6feb95 128
1cbee0b4
VZ
129int wxAppBase::OnRun()
130{
131 // see the comment in ctor: if the initial value hasn't been changed, use
132 // the default Yes from now on
133 if ( m_exitOnFrameDelete == Later )
134 {
135 m_exitOnFrameDelete = Yes;
136 }
137 //else: it has been changed, assume the user knows what he is doing
138
139 return MainLoop();
140}
141
9e5b30eb
VZ
142#endif // wxUSE_GUI
143
1e6feb95
VZ
144int wxAppBase::OnExit()
145{
146#if wxUSE_CONFIG
147 // delete the config object if any (don't use Get() here, but Set()
148 // because Get() could create a new config object)
149 delete wxConfigBase::Set((wxConfigBase *) NULL);
150#endif // wxUSE_CONFIG
151
152#ifdef __WXUNIVERSAL__
153 delete wxTheme::Set(NULL);
154#endif // __WXUNIVERSAL__
155
a69be60b
VZ
156 // use Set(NULL) and not Get() to avoid creating a message output object on
157 // demand when we just want to delete it
158 delete wxMessageOutput::Set(NULL);
159
1e6feb95
VZ
160 return 0;
161}
162
a69be60b
VZ
163// ----------------------------------------------------------------------------
164// customization hooks
165// ----------------------------------------------------------------------------
166
167#if wxUSE_LOG
168
169wxLog *wxAppBase::CreateLogTarget()
170{
171#if wxUSE_GUI && wxUSE_LOGGUI && !defined(__WXMICROWIN__)
172 return new wxLogGui;
173#else // !GUI
174 return new wxLogStderr;
175#endif // wxUSE_GUI
176}
177
178#endif // wxUSE_LOG
179
180wxMessageOutput *wxAppBase::CreateMessageOutput()
181{
182 // The standard way of printing help on command line arguments (app --help)
183 // is (according to common practice):
184 // - console apps: to stderr (on any platform)
185 // - GUI apps: stderr on Unix platforms (!)
186 // message box under Windows and others
187#if wxUSE_GUI && !defined(__UNIX__)
188 // wxMessageOutputMessageBox doesn't work under Motif
189 #ifdef __WXMOTIF__
190 return new wxMessageOutputLog;
191 #else
192 return new wxMessageOutputMessageBox;
193 #endif
194#else // !wxUSE_GUI || __UNIX__
195 return new wxMessageOutputStderr;
196#endif
197}
198
72cdf4c9
VZ
199// ---------------------------------------------------------------------------
200// wxAppBase
201// ----------------------------------------------------------------------------
202
203void wxAppBase::ProcessPendingEvents()
204{
205 // ensure that we're the only thread to modify the pending events list
16c1f79c 206 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
207
208 if ( !wxPendingEvents )
16c1f79c
RR
209 {
210 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 211 return;
16c1f79c 212 }
72cdf4c9
VZ
213
214 // iterate until the list becomes empty
b1d4dd7a 215 wxNode *node = wxPendingEvents->GetFirst();
72cdf4c9
VZ
216 while (node)
217 {
b1d4dd7a 218 wxEvtHandler *handler = (wxEvtHandler *)node->GetData();
16c1f79c 219 delete node;
72cdf4c9 220
16c1f79c 221 // In ProcessPendingEvents(), new handlers might be add
1d910ac1 222 // and we can safely leave the critical section here.
16c1f79c 223 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 224 handler->ProcessPendingEvents();
16c1f79c 225 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 226
b1d4dd7a 227 node = wxPendingEvents->GetFirst();
72cdf4c9 228 }
1d910ac1 229
16c1f79c 230 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
231}
232
1e6feb95
VZ
233// ----------------------------------------------------------------------------
234// misc
235// ----------------------------------------------------------------------------
236
237#if wxUSE_GUI
238
6e169cf3 239void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 240{
66dfed9b
VZ
241 if ( active == m_isActive )
242 return;
243
1e6feb95 244 m_isActive = active;
66dfed9b
VZ
245
246 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
247 event.SetEventObject(this);
248
249 (void)ProcessEvent(event);
7beba2fc 250}
1e6feb95
VZ
251
252#endif // wxUSE_GUI
bf188f1a 253
9154d8cf
VZ
254int wxAppBase::FilterEvent(wxEvent& WXUNUSED(event))
255{
256 // process the events normally by default
257 return -1;
258}
259
bf188f1a
VZ
260// ----------------------------------------------------------------------------
261// cmd line parsing
262// ----------------------------------------------------------------------------
263
264bool wxAppBase::OnInit()
265{
266#if wxUSE_CMDLINE_PARSER
267 wxCmdLineParser parser(argc, argv);
268
269 OnInitCmdLine(parser);
270
271 bool cont;
be03c0ec 272 switch ( parser.Parse(FALSE /* don't show usage */) )
bf188f1a
VZ
273 {
274 case -1:
275 cont = OnCmdLineHelp(parser);
276 break;
277
278 case 0:
279 cont = OnCmdLineParsed(parser);
280 break;
281
282 default:
283 cont = OnCmdLineError(parser);
284 break;
285 }
286
287 if ( !cont )
288 return FALSE;
289#endif // wxUSE_CMDLINE_PARSER
290
291 return TRUE;
292}
293
294#if wxUSE_CMDLINE_PARSER
295
296#define OPTION_VERBOSE _T("verbose")
297#define OPTION_THEME _T("theme")
c358c660 298#define OPTION_MODE _T("mode")
bf188f1a
VZ
299
300void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
301{
302 // the standard command line options
303 static const wxCmdLineEntryDesc cmdLineDesc[] =
304 {
305 {
306 wxCMD_LINE_SWITCH,
307 _T("h"),
308 _T("help"),
309 gettext_noop("show this help message"),
310 wxCMD_LINE_VAL_NONE,
311 wxCMD_LINE_OPTION_HELP
312 },
313
314#if wxUSE_LOG
315 {
316 wxCMD_LINE_SWITCH,
317 _T(""),
318 OPTION_VERBOSE,
ba161d7e
GD
319 gettext_noop("generate verbose log messages"),
320 wxCMD_LINE_VAL_NONE,
321 0x0
bf188f1a 322 },
0f02d3d0 323#endif // wxUSE_LOG
bf188f1a
VZ
324
325#ifdef __WXUNIVERSAL__
326 {
327 wxCMD_LINE_OPTION,
328 _T(""),
329 OPTION_THEME,
330 gettext_noop("specify the theme to use"),
ba161d7e
GD
331 wxCMD_LINE_VAL_STRING,
332 0x0
bf188f1a
VZ
333 },
334#endif // __WXUNIVERSAL__
335
c358c660
VS
336#if defined(__WXMGL__)
337 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
338 // should provide this option. That's why it is in common/appcmn.cpp
339 // and not mgl/app.cpp
340 {
341 wxCMD_LINE_OPTION,
342 _T(""),
343 OPTION_MODE,
344 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
ba161d7e
GD
345 wxCMD_LINE_VAL_STRING,
346 0x0
c358c660
VS
347 },
348#endif // __WXMGL__
349
bf188f1a 350 // terminator
ba161d7e
GD
351 {
352 wxCMD_LINE_NONE,
353 _T(""),
354 _T(""),
355 _T(""),
356 wxCMD_LINE_VAL_NONE,
357 0x0
358 }
bf188f1a
VZ
359 };
360
361 parser.SetDesc(cmdLineDesc);
362}
363
364bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
365{
366#if wxUSE_LOG
367 if ( parser.Found(OPTION_VERBOSE) )
368 {
369 wxLog::SetVerbose(TRUE);
370 }
371#endif // wxUSE_LOG
372
373#ifdef __WXUNIVERSAL__
374 wxString themeName;
375 if ( parser.Found(OPTION_THEME, &themeName) )
376 {
377 wxTheme *theme = wxTheme::Create(themeName);
378 if ( !theme )
379 {
380 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
381
382 return FALSE;
383 }
384
815b393a
VZ
385 // Delete the defaultly created theme and set the new theme.
386 delete wxTheme::Get();
bf188f1a
VZ
387 wxTheme::Set(theme);
388 }
389#endif // __WXUNIVERSAL__
390
c358c660
VS
391#if defined(__WXMGL__)
392 wxString modeDesc;
393 if ( parser.Found(OPTION_MODE, &modeDesc) )
394 {
395 unsigned w, h, bpp;
396 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
397 {
49e885f8 398 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
c358c660
VS
399
400 return FALSE;
401 }
402
07082b28 403 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
49e885f8 404 return FALSE;
c358c660 405 }
be03c0ec 406#endif // __WXMGL__
c358c660 407
bf188f1a
VZ
408 return TRUE;
409}
410
411bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
412{
413 parser.Usage();
414
415 return FALSE;
416}
417
418bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
419{
420 parser.Usage();
421
422 return FALSE;
423}
424
425#endif // wxUSE_CMDLINE_PARSER
426
a5f1fd3e
VZ
427// ----------------------------------------------------------------------------
428// debugging support
429// ----------------------------------------------------------------------------
430
090a6d7a
VZ
431/* static */
432bool wxAppBase::CheckBuildOptions(const wxBuildOptions& opts)
433{
434#define wxCMP(what) (what == opts.m_ ## what)
435
436 bool
437#ifdef __WXDEBUG__
438 isDebug = TRUE;
439#else
440 isDebug = FALSE;
441#endif
442
443 int verMaj = wxMAJOR_VERSION,
444 verMin = wxMINOR_VERSION;
445
e6e6fcc9
VZ
446 if ( !(wxCMP(isDebug) && wxCMP(verMaj) && wxCMP(verMin)) )
447 {
2b5f62a0
VZ
448 wxString msg;
449 wxString libDebug, progDebug;
450
451 if (isDebug)
452 libDebug = wxT("debug");
453 else
454 libDebug = wxT("no debug");
455
456 if (opts.m_isDebug)
457 progDebug = wxT("debug");
458 else
459 progDebug = wxT("no debug");
460
461 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)."),
462 verMaj, verMin, libDebug.c_str(), opts.m_verMaj, opts.m_verMin, progDebug.c_str());
463
464 wxLogFatalError(msg);
090a6d7a 465
e6e6fcc9
VZ
466 // normally wxLogFatalError doesn't return
467 return FALSE;
468 }
090a6d7a 469#undef wxCMP
e6e6fcc9
VZ
470
471 return TRUE;
090a6d7a
VZ
472}
473
a5f1fd3e
VZ
474#ifdef __WXDEBUG__
475
090a6d7a
VZ
476static void LINKAGEMODE SetTraceMasks()
477{
e30285ab 478#if wxUSE_LOG
090a6d7a
VZ
479 wxString mask;
480 if ( wxGetEnv(wxT("WXTRACE"), &mask) )
481 {
482 wxStringTokenizer tkn(mask, wxT(","));
483 while ( tkn.HasMoreTokens() )
484 wxLog::AddTraceMask(tkn.GetNextToken());
485 }
e30285ab 486#endif // wxUSE_LOG
090a6d7a
VZ
487}
488
a5f1fd3e
VZ
489// wxASSERT() helper
490bool wxAssertIsEqual(int x, int y)
491{
492 return x == y;
493}
494
495// break into the debugger
496void wxTrap()
497{
498#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
499 DebugBreak();
c31ad41d 500#elif defined(__WXMAC__) && !defined(__DARWIN__)
a5f1fd3e
VZ
501#if __powerc
502 Debugger();
503#else
504 SysBreak();
505#endif
506#elif defined(__UNIX__)
507 raise(SIGTRAP);
508#else
509 // TODO
510#endif // Win/Unix
511}
512
333e110d
CE
513
514void wxAssert(int cond,
515 const wxChar *szFile,
516 int nLine,
517 const wxChar *szCond,
518 const wxChar *szMsg)
519{
520 if ( !cond )
521 wxOnAssert(szFile, nLine, szCond, szMsg);
522}
523
a5f1fd3e
VZ
524// show the assert modal dialog
525static
aad65f13
VZ
526void ShowAssertDialog(const wxChar *szFile,
527 int nLine,
528 const wxChar *szCond,
529 const wxChar *szMsg)
a5f1fd3e
VZ
530{
531 // this variable can be set to true to suppress "assert failure" messages
532 static bool s_bNoAsserts = FALSE;
a5f1fd3e
VZ
533
534 wxChar szBuf[4096];
535
aad65f13
VZ
536 // make life easier for people using VC++ IDE by using this format: like
537 // this, clicking on the message will take us immediately to the place of
538 // the failed assert
a5f1fd3e 539 wxSnprintf(szBuf, WXSIZEOF(szBuf),
aad65f13
VZ
540 wxT("%s(%d): assert \"%s\" failed"),
541 szFile, nLine, szCond);
a5f1fd3e
VZ
542
543 if ( szMsg != NULL )
544 {
545 wxStrcat(szBuf, wxT(": "));
546 wxStrcat(szBuf, szMsg);
547 }
548 else // no message given
549 {
550 wxStrcat(szBuf, wxT("."));
551 }
552
553 if ( !s_bNoAsserts )
554 {
555 // send it to the normal log destination
556 wxLogDebug(szBuf);
557
558#if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
559 // this message is intentionally not translated - it is for
560 // developpers only
561 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
562
563 // use the native message box if available: this is more robust than
564 // using our own
565#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
566 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
567 MB_YESNOCANCEL | MB_ICONSTOP ) )
568 {
569 case IDYES:
570 wxTrap();
571 break;
572
573 case IDCANCEL:
574 s_bNoAsserts = TRUE;
575 break;
576
577 //case IDNO: nothing to do
578 }
579#else // !MSW
580 switch ( wxMessageBox(szBuf, wxT("Debug"),
581 wxYES_NO | wxCANCEL | wxICON_STOP ) )
582 {
583 case wxYES:
584 wxTrap();
585 break;
586
587 case wxCANCEL:
588 s_bNoAsserts = TRUE;
589 break;
590
591 //case wxNO: nothing to do
592 }
593#endif // GUI or MSW
594
595#else // !GUI
596 wxTrap();
597#endif // GUI/!GUI
598 }
a5f1fd3e
VZ
599}
600
601// this function is called when an assert fails
aad65f13
VZ
602void wxOnAssert(const wxChar *szFile,
603 int nLine,
604 const wxChar *szCond,
605 const wxChar *szMsg)
a5f1fd3e 606{
76456676
VZ
607 // FIXME MT-unsafe
608 static bool s_bInAssert = FALSE;
609
610 if ( s_bInAssert )
611 {
612 // He-e-e-e-elp!! we're trapped in endless loop
613 wxTrap();
614
615 s_bInAssert = FALSE;
616
617 return;
618 }
619
620 s_bInAssert = TRUE;
621
a5f1fd3e
VZ
622 if ( !wxTheApp )
623 {
624 // by default, show the assert dialog box - we can't customize this
625 // behaviour
aad65f13 626 ShowAssertDialog(szFile, nLine, szCond, szMsg);
a5f1fd3e
VZ
627 }
628 else
629 {
630 // let the app process it as it wants
aad65f13 631 wxTheApp->OnAssert(szFile, nLine, szCond, szMsg);
a5f1fd3e 632 }
76456676
VZ
633
634 s_bInAssert = FALSE;
a5f1fd3e
VZ
635}
636
aad65f13
VZ
637void wxAppBase::OnAssert(const wxChar *file,
638 int line,
639 const wxChar *cond,
640 const wxChar *msg)
a5f1fd3e 641{
aad65f13 642 ShowAssertDialog(file, line, cond, msg);
a5f1fd3e
VZ
643}
644
645#endif //WXDEBUG
646