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