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