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