]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/appbase.cpp | |
3 | // Purpose: implements wxAppConsoleBase class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 19.06.2003 (extracted from common/appcmn.cpp) | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #ifdef __WINDOWS__ | |
29 | #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox() | |
30 | #endif | |
31 | #include "wx/list.h" | |
32 | #include "wx/app.h" | |
33 | #include "wx/intl.h" | |
34 | #include "wx/log.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/wxcrtvararg.h" | |
37 | #endif //WX_PRECOMP | |
38 | ||
39 | #include "wx/apptrait.h" | |
40 | #include "wx/cmdline.h" | |
41 | #include "wx/confbase.h" | |
42 | #include "wx/evtloop.h" | |
43 | #include "wx/filename.h" | |
44 | #include "wx/msgout.h" | |
45 | #include "wx/scopedptr.h" | |
46 | #include "wx/sysopt.h" | |
47 | #include "wx/tokenzr.h" | |
48 | #include "wx/thread.h" | |
49 | ||
50 | #if wxUSE_STL | |
51 | #if wxUSE_EXCEPTIONS | |
52 | #include <exception> | |
53 | #include <typeinfo> | |
54 | #endif | |
55 | #if wxUSE_INTL | |
56 | #include <locale> | |
57 | #endif | |
58 | #endif // wxUSE_STL | |
59 | ||
60 | #if !defined(__WINDOWS__) || defined(__WXMICROWIN__) | |
61 | #include <signal.h> // for SIGTRAP used by wxTrap() | |
62 | #endif //Win/Unix | |
63 | ||
64 | #include <locale.h> | |
65 | ||
66 | #if wxUSE_FONTMAP | |
67 | #include "wx/fontmap.h" | |
68 | #endif // wxUSE_FONTMAP | |
69 | ||
70 | #if wxDEBUG_LEVEL | |
71 | #if wxUSE_STACKWALKER | |
72 | #include "wx/stackwalk.h" | |
73 | #ifdef __WINDOWS__ | |
74 | #include "wx/msw/debughlp.h" | |
75 | #endif | |
76 | #endif // wxUSE_STACKWALKER | |
77 | ||
78 | #include "wx/recguard.h" | |
79 | #endif // wxDEBUG_LEVEL | |
80 | ||
81 | // wxABI_VERSION can be defined when compiling applications but it should be | |
82 | // left undefined when compiling the library itself, it is then set to its | |
83 | // default value in version.h | |
84 | #if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99 | |
85 | #error "wxABI_VERSION should not be defined when compiling the library" | |
86 | #endif | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // private functions prototypes | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | #if wxDEBUG_LEVEL | |
93 | // really just show the assert dialog | |
94 | static bool DoShowAssertDialog(const wxString& msg); | |
95 | ||
96 | // prepare for showing the assert dialog, use the given traits or | |
97 | // DoShowAssertDialog() as last fallback to really show it | |
98 | static | |
99 | void ShowAssertDialog(const wxString& file, | |
100 | int line, | |
101 | const wxString& func, | |
102 | const wxString& cond, | |
103 | const wxString& msg, | |
104 | wxAppTraits *traits = NULL); | |
105 | #endif // wxDEBUG_LEVEL | |
106 | ||
107 | #ifdef __WXDEBUG__ | |
108 | // turn on the trace masks specified in the env variable WXTRACE | |
109 | static void LINKAGEMODE SetTraceMasks(); | |
110 | #endif // __WXDEBUG__ | |
111 | ||
112 | // ---------------------------------------------------------------------------- | |
113 | // global vars | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | wxAppConsole *wxAppConsoleBase::ms_appInstance = NULL; | |
117 | ||
118 | wxAppInitializerFunction wxAppConsoleBase::ms_appInitFn = NULL; | |
119 | ||
120 | wxSocketManager *wxAppTraitsBase::ms_manager = NULL; | |
121 | ||
122 | WXDLLIMPEXP_DATA_BASE(wxList) wxPendingDelete; | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // wxEventLoopPtr | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | // this defines wxEventLoopPtr | |
129 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase) | |
130 | ||
131 | // ============================================================================ | |
132 | // wxAppConsoleBase implementation | |
133 | // ============================================================================ | |
134 | ||
135 | // ---------------------------------------------------------------------------- | |
136 | // ctor/dtor | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
139 | wxAppConsoleBase::wxAppConsoleBase() | |
140 | { | |
141 | m_traits = NULL; | |
142 | m_mainLoop = NULL; | |
143 | m_bDoPendingEventProcessing = true; | |
144 | ||
145 | ms_appInstance = static_cast<wxAppConsole *>(this); | |
146 | ||
147 | #ifdef __WXDEBUG__ | |
148 | SetTraceMasks(); | |
149 | #if wxUSE_UNICODE | |
150 | // In unicode mode the SetTraceMasks call can cause an apptraits to be | |
151 | // created, but since we are still in the constructor the wrong kind will | |
152 | // be created for GUI apps. Destroy it so it can be created again later. | |
153 | wxDELETE(m_traits); | |
154 | #endif | |
155 | #endif | |
156 | ||
157 | wxEvtHandler::AddFilter(this); | |
158 | } | |
159 | ||
160 | wxAppConsoleBase::~wxAppConsoleBase() | |
161 | { | |
162 | wxEvtHandler::RemoveFilter(this); | |
163 | ||
164 | // we're being destroyed and using this object from now on may not work or | |
165 | // even crash so don't leave dangling pointers to it | |
166 | ms_appInstance = NULL; | |
167 | ||
168 | delete m_traits; | |
169 | } | |
170 | ||
171 | // ---------------------------------------------------------------------------- | |
172 | // initialization/cleanup | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc), wxChar **WXUNUSED(argv)) | |
176 | { | |
177 | return true; | |
178 | } | |
179 | ||
180 | wxString wxAppConsoleBase::GetAppName() const | |
181 | { | |
182 | wxString name = m_appName; | |
183 | if ( name.empty() ) | |
184 | { | |
185 | if ( argv ) | |
186 | { | |
187 | // the application name is, by default, the name of its executable file | |
188 | wxFileName::SplitPath(argv[0], NULL, &name, NULL); | |
189 | } | |
190 | } | |
191 | return name; | |
192 | } | |
193 | ||
194 | wxString wxAppConsoleBase::GetAppDisplayName() const | |
195 | { | |
196 | // use the explicitly provided display name, if any | |
197 | if ( !m_appDisplayName.empty() ) | |
198 | return m_appDisplayName; | |
199 | ||
200 | // if the application name was explicitly set, use it as is as capitalizing | |
201 | // it won't always produce good results | |
202 | if ( !m_appName.empty() ) | |
203 | return m_appName; | |
204 | ||
205 | // if neither is set, use the capitalized version of the program file as | |
206 | // it's the most reasonable default | |
207 | return GetAppName().Capitalize(); | |
208 | } | |
209 | ||
210 | wxEventLoopBase *wxAppConsoleBase::CreateMainLoop() | |
211 | { | |
212 | return GetTraits()->CreateEventLoop(); | |
213 | } | |
214 | ||
215 | void wxAppConsoleBase::CleanUp() | |
216 | { | |
217 | wxDELETE(m_mainLoop); | |
218 | } | |
219 | ||
220 | // ---------------------------------------------------------------------------- | |
221 | // OnXXX() callbacks | |
222 | // ---------------------------------------------------------------------------- | |
223 | ||
224 | bool wxAppConsoleBase::OnInit() | |
225 | { | |
226 | #if wxUSE_CMDLINE_PARSER | |
227 | wxCmdLineParser parser(argc, argv); | |
228 | ||
229 | OnInitCmdLine(parser); | |
230 | ||
231 | bool cont; | |
232 | switch ( parser.Parse(false /* don't show usage */) ) | |
233 | { | |
234 | case -1: | |
235 | cont = OnCmdLineHelp(parser); | |
236 | break; | |
237 | ||
238 | case 0: | |
239 | cont = OnCmdLineParsed(parser); | |
240 | break; | |
241 | ||
242 | default: | |
243 | cont = OnCmdLineError(parser); | |
244 | break; | |
245 | } | |
246 | ||
247 | if ( !cont ) | |
248 | return false; | |
249 | #endif // wxUSE_CMDLINE_PARSER | |
250 | ||
251 | return true; | |
252 | } | |
253 | ||
254 | int wxAppConsoleBase::OnRun() | |
255 | { | |
256 | return MainLoop(); | |
257 | } | |
258 | ||
259 | int wxAppConsoleBase::OnExit() | |
260 | { | |
261 | #if wxUSE_CONFIG | |
262 | // delete the config object if any (don't use Get() here, but Set() | |
263 | // because Get() could create a new config object) | |
264 | delete wxConfigBase::Set(NULL); | |
265 | #endif // wxUSE_CONFIG | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
270 | void wxAppConsoleBase::Exit() | |
271 | { | |
272 | if (m_mainLoop != NULL) | |
273 | ExitMainLoop(); | |
274 | else | |
275 | exit(-1); | |
276 | } | |
277 | ||
278 | // ---------------------------------------------------------------------------- | |
279 | // traits stuff | |
280 | // ---------------------------------------------------------------------------- | |
281 | ||
282 | wxAppTraits *wxAppConsoleBase::CreateTraits() | |
283 | { | |
284 | return new wxConsoleAppTraits; | |
285 | } | |
286 | ||
287 | wxAppTraits *wxAppConsoleBase::GetTraits() | |
288 | { | |
289 | // FIXME-MT: protect this with a CS? | |
290 | if ( !m_traits ) | |
291 | { | |
292 | m_traits = CreateTraits(); | |
293 | ||
294 | wxASSERT_MSG( m_traits, wxT("wxApp::CreateTraits() failed?") ); | |
295 | } | |
296 | ||
297 | return m_traits; | |
298 | } | |
299 | ||
300 | /* static */ | |
301 | wxAppTraits *wxAppConsoleBase::GetTraitsIfExists() | |
302 | { | |
303 | wxAppConsole * const app = GetInstance(); | |
304 | return app ? app->GetTraits() : NULL; | |
305 | } | |
306 | ||
307 | /* static */ | |
308 | wxAppTraits& wxAppConsoleBase::GetValidTraits() | |
309 | { | |
310 | static wxConsoleAppTraits s_traitsConsole; | |
311 | wxAppTraits* const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
312 | ||
313 | return traits ? *traits : s_traitsConsole; | |
314 | } | |
315 | ||
316 | // ---------------------------------------------------------------------------- | |
317 | // wxEventLoop redirection | |
318 | // ---------------------------------------------------------------------------- | |
319 | ||
320 | int wxAppConsoleBase::MainLoop() | |
321 | { | |
322 | wxEventLoopBaseTiedPtr mainLoop(&m_mainLoop, CreateMainLoop()); | |
323 | ||
324 | return m_mainLoop ? m_mainLoop->Run() : -1; | |
325 | } | |
326 | ||
327 | void wxAppConsoleBase::ExitMainLoop() | |
328 | { | |
329 | // we should exit from the main event loop, not just any currently active | |
330 | // (e.g. modal dialog) event loop | |
331 | if ( m_mainLoop && m_mainLoop->IsRunning() ) | |
332 | { | |
333 | m_mainLoop->Exit(0); | |
334 | } | |
335 | } | |
336 | ||
337 | bool wxAppConsoleBase::Pending() | |
338 | { | |
339 | // use the currently active message loop here, not m_mainLoop, because if | |
340 | // we're showing a modal dialog (with its own event loop) currently the | |
341 | // main event loop is not running anyhow | |
342 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
343 | ||
344 | return loop && loop->Pending(); | |
345 | } | |
346 | ||
347 | bool wxAppConsoleBase::Dispatch() | |
348 | { | |
349 | // see comment in Pending() | |
350 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
351 | ||
352 | return loop && loop->Dispatch(); | |
353 | } | |
354 | ||
355 | bool wxAppConsoleBase::Yield(bool onlyIfNeeded) | |
356 | { | |
357 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
358 | if ( loop ) | |
359 | return loop->Yield(onlyIfNeeded); | |
360 | ||
361 | wxScopedPtr<wxEventLoopBase> tmpLoop(CreateMainLoop()); | |
362 | return tmpLoop->Yield(onlyIfNeeded); | |
363 | } | |
364 | ||
365 | void wxAppConsoleBase::WakeUpIdle() | |
366 | { | |
367 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
368 | ||
369 | if ( loop ) | |
370 | loop->WakeUp(); | |
371 | } | |
372 | ||
373 | bool wxAppConsoleBase::ProcessIdle() | |
374 | { | |
375 | // synthesize an idle event and check if more of them are needed | |
376 | wxIdleEvent event; | |
377 | event.SetEventObject(this); | |
378 | ProcessEvent(event); | |
379 | ||
380 | #if wxUSE_LOG | |
381 | // flush the logged messages if any (do this after processing the events | |
382 | // which could have logged new messages) | |
383 | wxLog::FlushActive(); | |
384 | #endif | |
385 | ||
386 | // Garbage collect all objects previously scheduled for destruction. | |
387 | DeletePendingObjects(); | |
388 | ||
389 | return event.MoreRequested(); | |
390 | } | |
391 | ||
392 | bool wxAppConsoleBase::UsesEventLoop() const | |
393 | { | |
394 | // in console applications we don't know whether we're going to have an | |
395 | // event loop so assume we won't -- unless we already have one running | |
396 | return wxEventLoopBase::GetActive() != NULL; | |
397 | } | |
398 | ||
399 | // ---------------------------------------------------------------------------- | |
400 | // events | |
401 | // ---------------------------------------------------------------------------- | |
402 | ||
403 | /* static */ | |
404 | bool wxAppConsoleBase::IsMainLoopRunning() | |
405 | { | |
406 | const wxAppConsole * const app = GetInstance(); | |
407 | ||
408 | return app && app->m_mainLoop != NULL; | |
409 | } | |
410 | ||
411 | int wxAppConsoleBase::FilterEvent(wxEvent& WXUNUSED(event)) | |
412 | { | |
413 | // process the events normally by default | |
414 | return Event_Skip; | |
415 | } | |
416 | ||
417 | void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler* toDelay) | |
418 | { | |
419 | wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
420 | ||
421 | // move the handler from the list of handlers with processable pending events | |
422 | // to the list of handlers with pending events which needs to be processed later | |
423 | m_handlersWithPendingEvents.Remove(toDelay); | |
424 | ||
425 | if (m_handlersWithPendingDelayedEvents.Index(toDelay) == wxNOT_FOUND) | |
426 | m_handlersWithPendingDelayedEvents.Add(toDelay); | |
427 | ||
428 | wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
429 | } | |
430 | ||
431 | void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler* toRemove) | |
432 | { | |
433 | wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
434 | ||
435 | if (m_handlersWithPendingEvents.Index(toRemove) != wxNOT_FOUND) | |
436 | { | |
437 | m_handlersWithPendingEvents.Remove(toRemove); | |
438 | ||
439 | // check that the handler was present only once in the list | |
440 | wxASSERT_MSG( m_handlersWithPendingEvents.Index(toRemove) == wxNOT_FOUND, | |
441 | "Handler occurs twice in the m_handlersWithPendingEvents list!" ); | |
442 | } | |
443 | //else: it wasn't in this list at all, it's ok | |
444 | ||
445 | if (m_handlersWithPendingDelayedEvents.Index(toRemove) != wxNOT_FOUND) | |
446 | { | |
447 | m_handlersWithPendingDelayedEvents.Remove(toRemove); | |
448 | ||
449 | // check that the handler was present only once in the list | |
450 | wxASSERT_MSG( m_handlersWithPendingDelayedEvents.Index(toRemove) == wxNOT_FOUND, | |
451 | "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" ); | |
452 | } | |
453 | //else: it wasn't in this list at all, it's ok | |
454 | ||
455 | wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
456 | } | |
457 | ||
458 | void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler* toAppend) | |
459 | { | |
460 | wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
461 | ||
462 | if ( m_handlersWithPendingEvents.Index(toAppend) == wxNOT_FOUND ) | |
463 | m_handlersWithPendingEvents.Add(toAppend); | |
464 | ||
465 | wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
466 | } | |
467 | ||
468 | bool wxAppConsoleBase::HasPendingEvents() const | |
469 | { | |
470 | wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase*>(this)->m_handlersWithPendingEventsLocker); | |
471 | ||
472 | bool has = !m_handlersWithPendingEvents.IsEmpty(); | |
473 | ||
474 | wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase*>(this)->m_handlersWithPendingEventsLocker); | |
475 | ||
476 | return has; | |
477 | } | |
478 | ||
479 | void wxAppConsoleBase::SuspendProcessingOfPendingEvents() | |
480 | { | |
481 | m_bDoPendingEventProcessing = false; | |
482 | } | |
483 | ||
484 | void wxAppConsoleBase::ResumeProcessingOfPendingEvents() | |
485 | { | |
486 | m_bDoPendingEventProcessing = true; | |
487 | } | |
488 | ||
489 | void wxAppConsoleBase::ProcessPendingEvents() | |
490 | { | |
491 | if ( m_bDoPendingEventProcessing ) | |
492 | { | |
493 | wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
494 | ||
495 | wxCHECK_RET( m_handlersWithPendingDelayedEvents.IsEmpty(), | |
496 | "this helper list should be empty" ); | |
497 | ||
498 | // iterate until the list becomes empty: the handlers remove themselves | |
499 | // from it when they don't have any more pending events | |
500 | while (!m_handlersWithPendingEvents.IsEmpty()) | |
501 | { | |
502 | // In ProcessPendingEvents(), new handlers might be added | |
503 | // and we can safely leave the critical section here. | |
504 | wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
505 | ||
506 | // NOTE: we always call ProcessPendingEvents() on the first event handler | |
507 | // with pending events because handlers auto-remove themselves | |
508 | // from this list (see RemovePendingEventHandler) if they have no | |
509 | // more pending events. | |
510 | m_handlersWithPendingEvents[0]->ProcessPendingEvents(); | |
511 | ||
512 | wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
513 | } | |
514 | ||
515 | // now the wxHandlersWithPendingEvents is surely empty; however some event | |
516 | // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents | |
517 | // because of a selective wxYield call in progress. | |
518 | // Now we need to move them back to wxHandlersWithPendingEvents so the next | |
519 | // call to this function has the chance of processing them: | |
520 | if (!m_handlersWithPendingDelayedEvents.IsEmpty()) | |
521 | { | |
522 | WX_APPEND_ARRAY(m_handlersWithPendingEvents, m_handlersWithPendingDelayedEvents); | |
523 | m_handlersWithPendingDelayedEvents.Clear(); | |
524 | } | |
525 | ||
526 | wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
527 | } | |
528 | } | |
529 | ||
530 | void wxAppConsoleBase::DeletePendingEvents() | |
531 | { | |
532 | wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
533 | ||
534 | wxCHECK_RET( m_handlersWithPendingDelayedEvents.IsEmpty(), | |
535 | "this helper list should be empty" ); | |
536 | ||
537 | for (unsigned int i=0; i<m_handlersWithPendingEvents.GetCount(); i++) | |
538 | m_handlersWithPendingEvents[i]->DeletePendingEvents(); | |
539 | ||
540 | m_handlersWithPendingEvents.Clear(); | |
541 | ||
542 | wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); | |
543 | } | |
544 | ||
545 | // ---------------------------------------------------------------------------- | |
546 | // delayed objects destruction | |
547 | // ---------------------------------------------------------------------------- | |
548 | ||
549 | bool wxAppConsoleBase::IsScheduledForDestruction(wxObject *object) const | |
550 | { | |
551 | return wxPendingDelete.Member(object); | |
552 | } | |
553 | ||
554 | void wxAppConsoleBase::ScheduleForDestruction(wxObject *object) | |
555 | { | |
556 | if ( !UsesEventLoop() ) | |
557 | { | |
558 | // we won't be able to delete it later so do it right now | |
559 | delete object; | |
560 | return; | |
561 | } | |
562 | //else: we either already have or will soon start an event loop | |
563 | ||
564 | if ( !wxPendingDelete.Member(object) ) | |
565 | wxPendingDelete.Append(object); | |
566 | } | |
567 | ||
568 | void wxAppConsoleBase::DeletePendingObjects() | |
569 | { | |
570 | wxList::compatibility_iterator node = wxPendingDelete.GetFirst(); | |
571 | while (node) | |
572 | { | |
573 | wxObject *obj = node->GetData(); | |
574 | ||
575 | // remove it from the list first so that if we get back here somehow | |
576 | // during the object deletion (e.g. wxYield called from its dtor) we | |
577 | // wouldn't try to delete it the second time | |
578 | if ( wxPendingDelete.Member(obj) ) | |
579 | wxPendingDelete.Erase(node); | |
580 | ||
581 | delete obj; | |
582 | ||
583 | // Deleting one object may have deleted other pending | |
584 | // objects, so start from beginning of list again. | |
585 | node = wxPendingDelete.GetFirst(); | |
586 | } | |
587 | } | |
588 | ||
589 | // ---------------------------------------------------------------------------- | |
590 | // exception handling | |
591 | // ---------------------------------------------------------------------------- | |
592 | ||
593 | #if wxUSE_EXCEPTIONS | |
594 | ||
595 | void | |
596 | wxAppConsoleBase::HandleEvent(wxEvtHandler *handler, | |
597 | wxEventFunction func, | |
598 | wxEvent& event) const | |
599 | { | |
600 | // by default, simply call the handler | |
601 | (handler->*func)(event); | |
602 | } | |
603 | ||
604 | void wxAppConsoleBase::CallEventHandler(wxEvtHandler *handler, | |
605 | wxEventFunctor& functor, | |
606 | wxEvent& event) const | |
607 | { | |
608 | // If the functor holds a method then, for backward compatibility, call | |
609 | // HandleEvent(): | |
610 | wxEventFunction eventFunction = functor.GetEvtMethod(); | |
611 | ||
612 | if ( eventFunction ) | |
613 | HandleEvent(handler, eventFunction, event); | |
614 | else | |
615 | functor(handler, event); | |
616 | } | |
617 | ||
618 | void wxAppConsoleBase::OnUnhandledException() | |
619 | { | |
620 | #ifdef __WXDEBUG__ | |
621 | // we're called from an exception handler so we can re-throw the exception | |
622 | // to recover its type | |
623 | wxString what; | |
624 | try | |
625 | { | |
626 | throw; | |
627 | } | |
628 | #if wxUSE_STL | |
629 | catch ( std::exception& e ) | |
630 | { | |
631 | what.Printf("std::exception of type \"%s\", what() = \"%s\"", | |
632 | typeid(e).name(), e.what()); | |
633 | } | |
634 | #endif // wxUSE_STL | |
635 | catch ( ... ) | |
636 | { | |
637 | what = "unknown exception"; | |
638 | } | |
639 | ||
640 | wxMessageOutputBest().Printf( | |
641 | "*** Caught unhandled %s; terminating\n", what | |
642 | ); | |
643 | #endif // __WXDEBUG__ | |
644 | } | |
645 | ||
646 | // ---------------------------------------------------------------------------- | |
647 | // exceptions support | |
648 | // ---------------------------------------------------------------------------- | |
649 | ||
650 | bool wxAppConsoleBase::OnExceptionInMainLoop() | |
651 | { | |
652 | throw; | |
653 | ||
654 | // some compilers are too stupid to know that we never return after throw | |
655 | #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200) | |
656 | return false; | |
657 | #endif | |
658 | } | |
659 | ||
660 | #endif // wxUSE_EXCEPTIONS | |
661 | ||
662 | // ---------------------------------------------------------------------------- | |
663 | // cmd line parsing | |
664 | // ---------------------------------------------------------------------------- | |
665 | ||
666 | #if wxUSE_CMDLINE_PARSER | |
667 | ||
668 | #define OPTION_VERBOSE "verbose" | |
669 | ||
670 | void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser& parser) | |
671 | { | |
672 | // the standard command line options | |
673 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
674 | { | |
675 | { | |
676 | wxCMD_LINE_SWITCH, | |
677 | "h", | |
678 | "help", | |
679 | gettext_noop("show this help message"), | |
680 | wxCMD_LINE_VAL_NONE, | |
681 | wxCMD_LINE_OPTION_HELP | |
682 | }, | |
683 | ||
684 | #if wxUSE_LOG | |
685 | { | |
686 | wxCMD_LINE_SWITCH, | |
687 | NULL, | |
688 | OPTION_VERBOSE, | |
689 | gettext_noop("generate verbose log messages"), | |
690 | wxCMD_LINE_VAL_NONE, | |
691 | 0x0 | |
692 | }, | |
693 | #endif // wxUSE_LOG | |
694 | ||
695 | // terminator | |
696 | wxCMD_LINE_DESC_END | |
697 | }; | |
698 | ||
699 | parser.SetDesc(cmdLineDesc); | |
700 | } | |
701 | ||
702 | bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser& parser) | |
703 | { | |
704 | #if wxUSE_LOG | |
705 | if ( parser.Found(OPTION_VERBOSE) ) | |
706 | { | |
707 | wxLog::SetVerbose(true); | |
708 | } | |
709 | #else | |
710 | wxUnusedVar(parser); | |
711 | #endif // wxUSE_LOG | |
712 | ||
713 | return true; | |
714 | } | |
715 | ||
716 | bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser& parser) | |
717 | { | |
718 | parser.Usage(); | |
719 | ||
720 | return false; | |
721 | } | |
722 | ||
723 | bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser& parser) | |
724 | { | |
725 | parser.Usage(); | |
726 | ||
727 | return false; | |
728 | } | |
729 | ||
730 | #endif // wxUSE_CMDLINE_PARSER | |
731 | ||
732 | // ---------------------------------------------------------------------------- | |
733 | // debugging support | |
734 | // ---------------------------------------------------------------------------- | |
735 | ||
736 | /* static */ | |
737 | bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature, | |
738 | const char *componentName) | |
739 | { | |
740 | #if 0 // can't use wxLogTrace, not up and running yet | |
741 | printf("checking build options object '%s' (ptr %p) in '%s'\n", | |
742 | optionsSignature, optionsSignature, componentName); | |
743 | #endif | |
744 | ||
745 | if ( strcmp(optionsSignature, WX_BUILD_OPTIONS_SIGNATURE) != 0 ) | |
746 | { | |
747 | wxString lib = wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE); | |
748 | wxString prog = wxString::FromAscii(optionsSignature); | |
749 | wxString progName = wxString::FromAscii(componentName); | |
750 | wxString msg; | |
751 | ||
752 | msg.Printf(wxT("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."), | |
753 | lib.c_str(), progName.c_str(), prog.c_str()); | |
754 | ||
755 | wxLogFatalError(msg.c_str()); | |
756 | ||
757 | // normally wxLogFatalError doesn't return | |
758 | return false; | |
759 | } | |
760 | ||
761 | return true; | |
762 | } | |
763 | ||
764 | void wxAppConsoleBase::OnAssertFailure(const wxChar *file, | |
765 | int line, | |
766 | const wxChar *func, | |
767 | const wxChar *cond, | |
768 | const wxChar *msg) | |
769 | { | |
770 | #if wxDEBUG_LEVEL | |
771 | ShowAssertDialog(file, line, func, cond, msg, GetTraits()); | |
772 | #else | |
773 | // this function is still present even in debug level 0 build for ABI | |
774 | // compatibility reasons but is never called there and so can simply do | |
775 | // nothing in it | |
776 | wxUnusedVar(file); | |
777 | wxUnusedVar(line); | |
778 | wxUnusedVar(func); | |
779 | wxUnusedVar(cond); | |
780 | wxUnusedVar(msg); | |
781 | #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL | |
782 | } | |
783 | ||
784 | void wxAppConsoleBase::OnAssert(const wxChar *file, | |
785 | int line, | |
786 | const wxChar *cond, | |
787 | const wxChar *msg) | |
788 | { | |
789 | OnAssertFailure(file, line, NULL, cond, msg); | |
790 | } | |
791 | ||
792 | // ---------------------------------------------------------------------------- | |
793 | // Miscellaneous other methods | |
794 | // ---------------------------------------------------------------------------- | |
795 | ||
796 | void wxAppConsoleBase::SetCLocale() | |
797 | { | |
798 | // We want to use the user locale by default in GUI applications in order | |
799 | // to show the numbers, dates &c in the familiar format -- and also accept | |
800 | // this format on input (especially important for decimal comma/dot). | |
801 | wxSetlocale(LC_ALL, ""); | |
802 | } | |
803 | ||
804 | // ============================================================================ | |
805 | // other classes implementations | |
806 | // ============================================================================ | |
807 | ||
808 | // ---------------------------------------------------------------------------- | |
809 | // wxConsoleAppTraitsBase | |
810 | // ---------------------------------------------------------------------------- | |
811 | ||
812 | #if wxUSE_LOG | |
813 | ||
814 | wxLog *wxConsoleAppTraitsBase::CreateLogTarget() | |
815 | { | |
816 | return new wxLogStderr; | |
817 | } | |
818 | ||
819 | #endif // wxUSE_LOG | |
820 | ||
821 | wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput() | |
822 | { | |
823 | return new wxMessageOutputStderr; | |
824 | } | |
825 | ||
826 | #if wxUSE_FONTMAP | |
827 | ||
828 | wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper() | |
829 | { | |
830 | return (wxFontMapper *)new wxFontMapperBase; | |
831 | } | |
832 | ||
833 | #endif // wxUSE_FONTMAP | |
834 | ||
835 | wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() | |
836 | { | |
837 | // console applications don't use renderers | |
838 | return NULL; | |
839 | } | |
840 | ||
841 | bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
842 | { | |
843 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
844 | } | |
845 | ||
846 | bool wxConsoleAppTraitsBase::HasStderr() | |
847 | { | |
848 | // console applications always have stderr, even under Mac/Windows | |
849 | return true; | |
850 | } | |
851 | ||
852 | // ---------------------------------------------------------------------------- | |
853 | // wxAppTraits | |
854 | // ---------------------------------------------------------------------------- | |
855 | ||
856 | #if wxUSE_THREADS | |
857 | void wxMutexGuiEnterImpl(); | |
858 | void wxMutexGuiLeaveImpl(); | |
859 | ||
860 | void wxAppTraitsBase::MutexGuiEnter() | |
861 | { | |
862 | wxMutexGuiEnterImpl(); | |
863 | } | |
864 | ||
865 | void wxAppTraitsBase::MutexGuiLeave() | |
866 | { | |
867 | wxMutexGuiLeaveImpl(); | |
868 | } | |
869 | ||
870 | void WXDLLIMPEXP_BASE wxMutexGuiEnter() | |
871 | { | |
872 | wxAppTraits * const traits = wxAppConsoleBase::GetTraitsIfExists(); | |
873 | if ( traits ) | |
874 | traits->MutexGuiEnter(); | |
875 | } | |
876 | ||
877 | void WXDLLIMPEXP_BASE wxMutexGuiLeave() | |
878 | { | |
879 | wxAppTraits * const traits = wxAppConsoleBase::GetTraitsIfExists(); | |
880 | if ( traits ) | |
881 | traits->MutexGuiLeave(); | |
882 | } | |
883 | #endif // wxUSE_THREADS | |
884 | ||
885 | bool wxAppTraitsBase::ShowAssertDialog(const wxString& msgOriginal) | |
886 | { | |
887 | #if wxDEBUG_LEVEL | |
888 | wxString msg; | |
889 | ||
890 | #if wxUSE_STACKWALKER | |
891 | const wxString stackTrace = GetAssertStackTrace(); | |
892 | if ( !stackTrace.empty() ) | |
893 | { | |
894 | msg << wxT("\n\nCall stack:\n") << stackTrace; | |
895 | ||
896 | wxMessageOutputDebug().Output(msg); | |
897 | } | |
898 | #endif // wxUSE_STACKWALKER | |
899 | ||
900 | return DoShowAssertDialog(msgOriginal + msg); | |
901 | #else // !wxDEBUG_LEVEL | |
902 | wxUnusedVar(msgOriginal); | |
903 | ||
904 | return false; | |
905 | #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL | |
906 | } | |
907 | ||
908 | #if wxUSE_STACKWALKER | |
909 | wxString wxAppTraitsBase::GetAssertStackTrace() | |
910 | { | |
911 | #if wxDEBUG_LEVEL | |
912 | ||
913 | #if !defined(__WINDOWS__) | |
914 | // on Unix stack frame generation may take some time, depending on the | |
915 | // size of the executable mainly... warn the user that we are working | |
916 | wxFprintf(stderr, "Collecting stack trace information, please wait..."); | |
917 | fflush(stderr); | |
918 | #endif // !__WINDOWS__ | |
919 | ||
920 | ||
921 | wxString stackTrace; | |
922 | ||
923 | class StackDump : public wxStackWalker | |
924 | { | |
925 | public: | |
926 | StackDump() { } | |
927 | ||
928 | const wxString& GetStackTrace() const { return m_stackTrace; } | |
929 | ||
930 | protected: | |
931 | virtual void OnStackFrame(const wxStackFrame& frame) | |
932 | { | |
933 | m_stackTrace << wxString::Format | |
934 | ( | |
935 | wxT("[%02d] "), | |
936 | wx_truncate_cast(int, frame.GetLevel()) | |
937 | ); | |
938 | ||
939 | wxString name = frame.GetName(); | |
940 | if ( !name.empty() ) | |
941 | { | |
942 | m_stackTrace << wxString::Format(wxT("%-40s"), name.c_str()); | |
943 | } | |
944 | else | |
945 | { | |
946 | m_stackTrace << wxString::Format(wxT("%p"), frame.GetAddress()); | |
947 | } | |
948 | ||
949 | if ( frame.HasSourceLocation() ) | |
950 | { | |
951 | m_stackTrace << wxT('\t') | |
952 | << frame.GetFileName() | |
953 | << wxT(':') | |
954 | << frame.GetLine(); | |
955 | } | |
956 | ||
957 | m_stackTrace << wxT('\n'); | |
958 | } | |
959 | ||
960 | private: | |
961 | wxString m_stackTrace; | |
962 | }; | |
963 | ||
964 | // don't show more than maxLines or we could get a dialog too tall to be | |
965 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
966 | // characters it is still only 300 pixels... | |
967 | static const int maxLines = 20; | |
968 | ||
969 | StackDump dump; | |
970 | dump.Walk(8, maxLines); // 8 is chosen to hide all OnAssert() calls | |
971 | stackTrace = dump.GetStackTrace(); | |
972 | ||
973 | const int count = stackTrace.Freq(wxT('\n')); | |
974 | for ( int i = 0; i < count - maxLines; i++ ) | |
975 | stackTrace = stackTrace.BeforeLast(wxT('\n')); | |
976 | ||
977 | return stackTrace; | |
978 | #else // !wxDEBUG_LEVEL | |
979 | // this function is still present for ABI-compatibility even in debug level | |
980 | // 0 build but is not used there and so can simply do nothing | |
981 | return wxString(); | |
982 | #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL | |
983 | } | |
984 | #endif // wxUSE_STACKWALKER | |
985 | ||
986 | ||
987 | // ============================================================================ | |
988 | // global functions implementation | |
989 | // ============================================================================ | |
990 | ||
991 | void wxExit() | |
992 | { | |
993 | if ( wxTheApp ) | |
994 | { | |
995 | wxTheApp->Exit(); | |
996 | } | |
997 | else | |
998 | { | |
999 | // what else can we do? | |
1000 | exit(-1); | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | void wxWakeUpIdle() | |
1005 | { | |
1006 | if ( wxTheApp ) | |
1007 | { | |
1008 | wxTheApp->WakeUpIdle(); | |
1009 | } | |
1010 | //else: do nothing, what can we do? | |
1011 | } | |
1012 | ||
1013 | // wxASSERT() helper | |
1014 | bool wxAssertIsEqual(int x, int y) | |
1015 | { | |
1016 | return x == y; | |
1017 | } | |
1018 | ||
1019 | void wxAbort() | |
1020 | { | |
1021 | #ifdef __WXWINCE__ | |
1022 | ExitThread(3); | |
1023 | #else | |
1024 | abort(); | |
1025 | #endif | |
1026 | } | |
1027 | ||
1028 | #if wxDEBUG_LEVEL | |
1029 | ||
1030 | // break into the debugger | |
1031 | #ifndef wxTrap | |
1032 | ||
1033 | void wxTrap() | |
1034 | { | |
1035 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
1036 | DebugBreak(); | |
1037 | #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS | |
1038 | Debugger(); | |
1039 | #elif defined(__UNIX__) | |
1040 | raise(SIGTRAP); | |
1041 | #else | |
1042 | // TODO | |
1043 | #endif // Win/Unix | |
1044 | } | |
1045 | ||
1046 | #endif // wxTrap already defined as a macro | |
1047 | ||
1048 | // default assert handler | |
1049 | static void | |
1050 | wxDefaultAssertHandler(const wxString& file, | |
1051 | int line, | |
1052 | const wxString& func, | |
1053 | const wxString& cond, | |
1054 | const wxString& msg) | |
1055 | { | |
1056 | // If this option is set, we should abort immediately when assert happens. | |
1057 | if ( wxSystemOptions::GetOptionInt("exit-on-assert") ) | |
1058 | wxAbort(); | |
1059 | ||
1060 | // FIXME MT-unsafe | |
1061 | static int s_bInAssert = 0; | |
1062 | ||
1063 | wxRecursionGuard guard(s_bInAssert); | |
1064 | if ( guard.IsInside() ) | |
1065 | { | |
1066 | // can't use assert here to avoid infinite loops, so just trap | |
1067 | wxTrap(); | |
1068 | ||
1069 | return; | |
1070 | } | |
1071 | ||
1072 | if ( !wxTheApp ) | |
1073 | { | |
1074 | // by default, show the assert dialog box -- we can't customize this | |
1075 | // behaviour | |
1076 | ShowAssertDialog(file, line, func, cond, msg); | |
1077 | } | |
1078 | else | |
1079 | { | |
1080 | // let the app process it as it wants | |
1081 | // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed | |
1082 | wxTheApp->OnAssertFailure(file.c_str(), line, func.c_str(), | |
1083 | cond.c_str(), msg.c_str()); | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | wxAssertHandler_t wxTheAssertHandler = wxDefaultAssertHandler; | |
1088 | ||
1089 | void wxSetDefaultAssertHandler() | |
1090 | { | |
1091 | wxTheAssertHandler = wxDefaultAssertHandler; | |
1092 | } | |
1093 | ||
1094 | void wxOnAssert(const wxString& file, | |
1095 | int line, | |
1096 | const wxString& func, | |
1097 | const wxString& cond, | |
1098 | const wxString& msg) | |
1099 | { | |
1100 | wxTheAssertHandler(file, line, func, cond, msg); | |
1101 | } | |
1102 | ||
1103 | void wxOnAssert(const wxString& file, | |
1104 | int line, | |
1105 | const wxString& func, | |
1106 | const wxString& cond) | |
1107 | { | |
1108 | wxTheAssertHandler(file, line, func, cond, wxString()); | |
1109 | } | |
1110 | ||
1111 | void wxOnAssert(const wxChar *file, | |
1112 | int line, | |
1113 | const char *func, | |
1114 | const wxChar *cond, | |
1115 | const wxChar *msg) | |
1116 | { | |
1117 | // this is the backwards-compatible version (unless we don't use Unicode) | |
1118 | // so it could be called directly from the user code and this might happen | |
1119 | // even when wxTheAssertHandler is NULL | |
1120 | #if wxUSE_UNICODE | |
1121 | if ( wxTheAssertHandler ) | |
1122 | #endif // wxUSE_UNICODE | |
1123 | wxTheAssertHandler(file, line, func, cond, msg); | |
1124 | } | |
1125 | ||
1126 | void wxOnAssert(const char *file, | |
1127 | int line, | |
1128 | const char *func, | |
1129 | const char *cond, | |
1130 | const wxString& msg) | |
1131 | { | |
1132 | wxTheAssertHandler(file, line, func, cond, msg); | |
1133 | } | |
1134 | ||
1135 | void wxOnAssert(const char *file, | |
1136 | int line, | |
1137 | const char *func, | |
1138 | const char *cond, | |
1139 | const wxCStrData& msg) | |
1140 | { | |
1141 | wxTheAssertHandler(file, line, func, cond, msg); | |
1142 | } | |
1143 | ||
1144 | #if wxUSE_UNICODE | |
1145 | void wxOnAssert(const char *file, | |
1146 | int line, | |
1147 | const char *func, | |
1148 | const char *cond) | |
1149 | { | |
1150 | wxTheAssertHandler(file, line, func, cond, wxString()); | |
1151 | } | |
1152 | ||
1153 | void wxOnAssert(const char *file, | |
1154 | int line, | |
1155 | const char *func, | |
1156 | const char *cond, | |
1157 | const char *msg) | |
1158 | { | |
1159 | wxTheAssertHandler(file, line, func, cond, msg); | |
1160 | } | |
1161 | ||
1162 | void wxOnAssert(const char *file, | |
1163 | int line, | |
1164 | const char *func, | |
1165 | const char *cond, | |
1166 | const wxChar *msg) | |
1167 | { | |
1168 | wxTheAssertHandler(file, line, func, cond, msg); | |
1169 | } | |
1170 | #endif // wxUSE_UNICODE | |
1171 | ||
1172 | #endif // wxDEBUG_LEVEL | |
1173 | ||
1174 | // ============================================================================ | |
1175 | // private functions implementation | |
1176 | // ============================================================================ | |
1177 | ||
1178 | #ifdef __WXDEBUG__ | |
1179 | ||
1180 | static void LINKAGEMODE SetTraceMasks() | |
1181 | { | |
1182 | #if wxUSE_LOG | |
1183 | wxString mask; | |
1184 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
1185 | { | |
1186 | wxStringTokenizer tkn(mask, wxT(",;:")); | |
1187 | while ( tkn.HasMoreTokens() ) | |
1188 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
1189 | } | |
1190 | #endif // wxUSE_LOG | |
1191 | } | |
1192 | ||
1193 | #endif // __WXDEBUG__ | |
1194 | ||
1195 | #if wxDEBUG_LEVEL | |
1196 | ||
1197 | bool wxTrapInAssert = false; | |
1198 | ||
1199 | static | |
1200 | bool DoShowAssertDialog(const wxString& msg) | |
1201 | { | |
1202 | // under Windows we can show the dialog even in the console mode | |
1203 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) | |
1204 | wxString msgDlg(msg); | |
1205 | ||
1206 | // this message is intentionally not translated -- it is for developers | |
1207 | // only -- and the less code we use here, less is the danger of recursively | |
1208 | // asserting and dying | |
1209 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
1210 | wxT("You can also choose [Cancel] to suppress ") | |
1211 | wxT("further warnings."); | |
1212 | ||
1213 | switch ( ::MessageBox(NULL, msgDlg.t_str(), wxT("wxWidgets Debug Alert"), | |
1214 | MB_YESNOCANCEL | MB_ICONSTOP ) ) | |
1215 | { | |
1216 | case IDYES: | |
1217 | // If we called wxTrap() directly from here, the programmer would | |
1218 | // see this function and a few more calls between his own code and | |
1219 | // it in the stack trace which would be perfectly useless and often | |
1220 | // confusing. So instead just set the flag here and let the macros | |
1221 | // defined in wx/debug.h call wxTrap() themselves, this ensures | |
1222 | // that the debugger will show the line in the user code containing | |
1223 | // the failing assert. | |
1224 | wxTrapInAssert = true; | |
1225 | break; | |
1226 | ||
1227 | case IDCANCEL: | |
1228 | // stop the asserts | |
1229 | return true; | |
1230 | ||
1231 | //case IDNO: nothing to do | |
1232 | } | |
1233 | #else // !__WINDOWS__ | |
1234 | wxUnusedVar(msg); | |
1235 | #endif // __WINDOWS__/!__WINDOWS__ | |
1236 | ||
1237 | // continue with the asserts by default | |
1238 | return false; | |
1239 | } | |
1240 | ||
1241 | // show the standard assert dialog | |
1242 | static | |
1243 | void ShowAssertDialog(const wxString& file, | |
1244 | int line, | |
1245 | const wxString& func, | |
1246 | const wxString& cond, | |
1247 | const wxString& msgUser, | |
1248 | wxAppTraits *traits) | |
1249 | { | |
1250 | // this variable can be set to true to suppress "assert failure" messages | |
1251 | static bool s_bNoAsserts = false; | |
1252 | ||
1253 | wxString msg; | |
1254 | msg.reserve(2048); | |
1255 | ||
1256 | // make life easier for people using VC++ IDE by using this format: like | |
1257 | // this, clicking on the message will take us immediately to the place of | |
1258 | // the failed assert | |
1259 | msg.Printf(wxT("%s(%d): assert \"%s\" failed"), file, line, cond); | |
1260 | ||
1261 | // add the function name, if any | |
1262 | if ( !func.empty() ) | |
1263 | msg << wxT(" in ") << func << wxT("()"); | |
1264 | ||
1265 | // and the message itself | |
1266 | if ( !msgUser.empty() ) | |
1267 | { | |
1268 | msg << wxT(": ") << msgUser; | |
1269 | } | |
1270 | else // no message given | |
1271 | { | |
1272 | msg << wxT('.'); | |
1273 | } | |
1274 | ||
1275 | #if wxUSE_THREADS | |
1276 | // if we are not in the main thread, output the assert directly and trap | |
1277 | // since dialogs cannot be displayed | |
1278 | if ( !wxThread::IsMain() ) | |
1279 | { | |
1280 | msg += wxString::Format(" [in thread %lx]", wxThread::GetCurrentId()); | |
1281 | } | |
1282 | #endif // wxUSE_THREADS | |
1283 | ||
1284 | // log the assert in any case | |
1285 | wxMessageOutputDebug().Output(msg); | |
1286 | ||
1287 | if ( !s_bNoAsserts ) | |
1288 | { | |
1289 | if ( traits ) | |
1290 | { | |
1291 | // delegate showing assert dialog (if possible) to that class | |
1292 | s_bNoAsserts = traits->ShowAssertDialog(msg); | |
1293 | } | |
1294 | else // no traits object | |
1295 | { | |
1296 | // fall back to the function of last resort | |
1297 | s_bNoAsserts = DoShowAssertDialog(msg); | |
1298 | } | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | #endif // wxDEBUG_LEVEL |