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