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