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