]>
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" |
b46b1d59 | 45 | #include "wx/ptr_scpd.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 | ||
6c8f8d92 | 66 | #ifdef __WXDEBUG__ |
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" | |
6c8f8d92 VZ |
75 | #endif // __WXDEBUG__ |
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 | ||
88 | #ifdef __WXDEBUG__ | |
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 | |
0accd1cf | 95 | void ShowAssertDialog(const wxString& szFile, |
e2478fde | 96 | int nLine, |
0accd1cf VS |
97 | const wxString& szFunc, |
98 | const wxString& szCond, | |
99 | const wxString& szMsg, | |
e2478fde VZ |
100 | wxAppTraits *traits = NULL); |
101 | ||
102 | // turn on the trace masks specified in the env variable WXTRACE | |
103 | static void LINKAGEMODE SetTraceMasks(); | |
104 | #endif // __WXDEBUG__ | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // global vars | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
e0954e72 | 110 | wxAppConsole *wxAppConsoleBase::ms_appInstance = NULL; |
e2478fde | 111 | |
e0954e72 | 112 | wxAppInitializerFunction wxAppConsoleBase::ms_appInitFn = NULL; |
e2478fde | 113 | |
51fe4b60 VZ |
114 | wxSocketManager *wxAppTraitsBase::ms_manager = NULL; |
115 | ||
b46b1d59 VZ |
116 | // ---------------------------------------------------------------------------- |
117 | // wxEventLoopPtr | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
120 | // this defines wxEventLoopPtr | |
2ddff00c | 121 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase) |
b46b1d59 | 122 | |
e2478fde | 123 | // ============================================================================ |
e0954e72 | 124 | // wxAppConsoleBase implementation |
e2478fde VZ |
125 | // ============================================================================ |
126 | ||
127 | // ---------------------------------------------------------------------------- | |
128 | // ctor/dtor | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
e0954e72 | 131 | wxAppConsoleBase::wxAppConsoleBase() |
e2478fde VZ |
132 | { |
133 | m_traits = NULL; | |
b46b1d59 | 134 | m_mainLoop = NULL; |
e2478fde | 135 | |
5c33522f | 136 | ms_appInstance = static_cast<wxAppConsole *>(this); |
e2478fde VZ |
137 | |
138 | #ifdef __WXDEBUG__ | |
139 | SetTraceMasks(); | |
bc334f39 RD |
140 | #if wxUSE_UNICODE |
141 | // In unicode mode the SetTraceMasks call can cause an apptraits to be | |
142 | // created, but since we are still in the constructor the wrong kind will | |
143 | // be created for GUI apps. Destroy it so it can be created again later. | |
144 | delete m_traits; | |
145 | m_traits = NULL; | |
146 | #endif | |
e2478fde VZ |
147 | #endif |
148 | } | |
149 | ||
e0954e72 | 150 | wxAppConsoleBase::~wxAppConsoleBase() |
e2478fde VZ |
151 | { |
152 | delete m_traits; | |
153 | } | |
154 | ||
94826170 VZ |
155 | // ---------------------------------------------------------------------------- |
156 | // initilization/cleanup | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
a7d980f6 | 159 | bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc), wxChar **argv) |
94826170 | 160 | { |
d774f916 VZ |
161 | #if wxUSE_INTL |
162 | GetTraits()->SetLocale(); | |
163 | #endif // wxUSE_INTL | |
164 | ||
b46b1d59 | 165 | #if wxUSE_THREADS |
079f4130 | 166 | wxHandlersWithPendingEventsLocker = new wxCriticalSection; |
b46b1d59 VZ |
167 | #endif |
168 | ||
4055ed82 | 169 | #ifndef __WXPALMOS__ |
a7d980f6 | 170 | if ( m_appName.empty() && argv && argv[0] ) |
94826170 VZ |
171 | { |
172 | // the application name is, by default, the name of its executable file | |
94826170 | 173 | wxFileName::SplitPath(argv[0], NULL, &m_appName, NULL); |
94826170 | 174 | } |
d774f916 | 175 | #endif // !__WXPALMOS__ |
94826170 VZ |
176 | |
177 | return true; | |
178 | } | |
179 | ||
e0954e72 | 180 | wxEventLoopBase *wxAppConsoleBase::CreateMainLoop() |
b46b1d59 VZ |
181 | { |
182 | return GetTraits()->CreateEventLoop(); | |
183 | } | |
184 | ||
e0954e72 | 185 | void wxAppConsoleBase::CleanUp() |
94826170 | 186 | { |
aea33a3e VZ |
187 | if ( m_mainLoop ) |
188 | { | |
189 | delete m_mainLoop; | |
190 | m_mainLoop = NULL; | |
191 | } | |
82a4f054 | 192 | |
079f4130 FM |
193 | delete wxHandlersWithPendingEvents; |
194 | wxHandlersWithPendingEvents = NULL; | |
b46b1d59 VZ |
195 | |
196 | #if wxUSE_THREADS | |
079f4130 FM |
197 | delete wxHandlersWithPendingEventsLocker; |
198 | wxHandlersWithPendingEventsLocker = NULL; | |
b46b1d59 | 199 | #endif // wxUSE_THREADS |
94826170 VZ |
200 | } |
201 | ||
e2478fde VZ |
202 | // ---------------------------------------------------------------------------- |
203 | // OnXXX() callbacks | |
204 | // ---------------------------------------------------------------------------- | |
205 | ||
e0954e72 | 206 | bool wxAppConsoleBase::OnInit() |
e2478fde VZ |
207 | { |
208 | #if wxUSE_CMDLINE_PARSER | |
209 | wxCmdLineParser parser(argc, argv); | |
210 | ||
211 | OnInitCmdLine(parser); | |
212 | ||
213 | bool cont; | |
4629016d | 214 | switch ( parser.Parse(false /* don't show usage */) ) |
e2478fde VZ |
215 | { |
216 | case -1: | |
217 | cont = OnCmdLineHelp(parser); | |
218 | break; | |
219 | ||
220 | case 0: | |
221 | cont = OnCmdLineParsed(parser); | |
222 | break; | |
223 | ||
224 | default: | |
225 | cont = OnCmdLineError(parser); | |
226 | break; | |
227 | } | |
228 | ||
229 | if ( !cont ) | |
4629016d | 230 | return false; |
e2478fde VZ |
231 | #endif // wxUSE_CMDLINE_PARSER |
232 | ||
4629016d | 233 | return true; |
e2478fde VZ |
234 | } |
235 | ||
e0954e72 | 236 | int wxAppConsoleBase::OnRun() |
b46b1d59 VZ |
237 | { |
238 | return MainLoop(); | |
8ac09e52 | 239 | } |
b46b1d59 | 240 | |
e0954e72 | 241 | int wxAppConsoleBase::OnExit() |
e2478fde VZ |
242 | { |
243 | #if wxUSE_CONFIG | |
244 | // delete the config object if any (don't use Get() here, but Set() | |
245 | // because Get() could create a new config object) | |
246 | delete wxConfigBase::Set((wxConfigBase *) NULL); | |
247 | #endif // wxUSE_CONFIG | |
248 | ||
e2478fde VZ |
249 | return 0; |
250 | } | |
251 | ||
e0954e72 | 252 | void wxAppConsoleBase::Exit() |
e2478fde | 253 | { |
b46b1d59 VZ |
254 | if (m_mainLoop != NULL) |
255 | ExitMainLoop(); | |
256 | else | |
257 | exit(-1); | |
e2478fde VZ |
258 | } |
259 | ||
260 | // ---------------------------------------------------------------------------- | |
261 | // traits stuff | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
e0954e72 | 264 | wxAppTraits *wxAppConsoleBase::CreateTraits() |
e2478fde | 265 | { |
7843d11b | 266 | return new wxConsoleAppTraits; |
e2478fde VZ |
267 | } |
268 | ||
e0954e72 | 269 | wxAppTraits *wxAppConsoleBase::GetTraits() |
e2478fde VZ |
270 | { |
271 | // FIXME-MT: protect this with a CS? | |
272 | if ( !m_traits ) | |
273 | { | |
274 | m_traits = CreateTraits(); | |
275 | ||
276 | wxASSERT_MSG( m_traits, _T("wxApp::CreateTraits() failed?") ); | |
277 | } | |
278 | ||
279 | return m_traits; | |
280 | } | |
281 | ||
96b2cbe8 VZ |
282 | /* static */ |
283 | wxAppTraits *wxAppConsoleBase::GetTraitsIfExists() | |
284 | { | |
285 | wxAppConsole * const app = GetInstance(); | |
286 | return app ? app->GetTraits() : NULL; | |
287 | } | |
288 | ||
e2478fde VZ |
289 | // ---------------------------------------------------------------------------- |
290 | // event processing | |
291 | // ---------------------------------------------------------------------------- | |
292 | ||
e0954e72 | 293 | int wxAppConsoleBase::MainLoop() |
e2478fde | 294 | { |
2ddff00c | 295 | wxEventLoopBaseTiedPtr mainLoop(&m_mainLoop, CreateMainLoop()); |
b46b1d59 VZ |
296 | |
297 | return m_mainLoop ? m_mainLoop->Run() : -1; | |
298 | } | |
299 | ||
e0954e72 | 300 | void wxAppConsoleBase::ExitMainLoop() |
b46b1d59 VZ |
301 | { |
302 | // we should exit from the main event loop, not just any currently active | |
303 | // (e.g. modal dialog) event loop | |
304 | if ( m_mainLoop && m_mainLoop->IsRunning() ) | |
305 | { | |
306 | m_mainLoop->Exit(0); | |
307 | } | |
308 | } | |
309 | ||
e0954e72 | 310 | bool wxAppConsoleBase::Pending() |
b46b1d59 VZ |
311 | { |
312 | // use the currently active message loop here, not m_mainLoop, because if | |
313 | // we're showing a modal dialog (with its own event loop) currently the | |
314 | // main event loop is not running anyhow | |
2ddff00c | 315 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); |
b46b1d59 VZ |
316 | |
317 | return loop && loop->Pending(); | |
318 | } | |
319 | ||
e0954e72 | 320 | bool wxAppConsoleBase::Dispatch() |
b46b1d59 VZ |
321 | { |
322 | // see comment in Pending() | |
2ddff00c | 323 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); |
b46b1d59 VZ |
324 | |
325 | return loop && loop->Dispatch(); | |
326 | } | |
8ecff181 | 327 | |
e0954e72 | 328 | bool wxAppConsoleBase::HasPendingEvents() const |
b46b1d59 | 329 | { |
079f4130 | 330 | wxENTER_CRIT_SECT( *wxHandlersWithPendingEventsLocker ); |
e2478fde | 331 | |
079f4130 | 332 | bool has = wxHandlersWithPendingEvents && !wxHandlersWithPendingEvents->IsEmpty(); |
a1312bc8 | 333 | |
079f4130 | 334 | wxLEAVE_CRIT_SECT( *wxHandlersWithPendingEventsLocker ); |
a1312bc8 PC |
335 | |
336 | return has; | |
8ac09e52 | 337 | } |
b46b1d59 | 338 | |
e0954e72 VZ |
339 | /* static */ |
340 | bool wxAppConsoleBase::IsMainLoopRunning() | |
341 | { | |
342 | const wxAppConsole * const app = GetInstance(); | |
343 | ||
344 | return app && app->m_mainLoop != NULL; | |
345 | } | |
346 | ||
347 | void wxAppConsoleBase::ProcessPendingEvents() | |
b46b1d59 VZ |
348 | { |
349 | #if wxUSE_THREADS | |
079f4130 | 350 | if ( !wxHandlersWithPendingEventsLocker ) |
b46b1d59 VZ |
351 | return; |
352 | #endif | |
353 | ||
079f4130 | 354 | wxENTER_CRIT_SECT( *wxHandlersWithPendingEventsLocker ); |
c0bfefb6 | 355 | |
079f4130 | 356 | if (wxHandlersWithPendingEvents) |
e2478fde | 357 | { |
cd844951 VZ |
358 | // iterate until the list becomes empty: the handlers remove themselves |
359 | // from it when they don't have any more pending events | |
079f4130 | 360 | wxList::compatibility_iterator node = wxHandlersWithPendingEvents->GetFirst(); |
a1312bc8 PC |
361 | while (node) |
362 | { | |
a1312bc8 PC |
363 | // In ProcessPendingEvents(), new handlers might be add |
364 | // and we can safely leave the critical section here. | |
079f4130 | 365 | wxLEAVE_CRIT_SECT( *wxHandlersWithPendingEventsLocker ); |
8ecff181 | 366 | |
cd844951 | 367 | wxEvtHandler *handler = (wxEvtHandler *)node->GetData(); |
a1312bc8 | 368 | handler->ProcessPendingEvents(); |
de4de64f | 369 | |
079f4130 | 370 | wxENTER_CRIT_SECT( *wxHandlersWithPendingEventsLocker ); |
e2478fde | 371 | |
cd844951 | 372 | // restart as the iterators could have been invalidated |
079f4130 | 373 | node = wxHandlersWithPendingEvents->GetFirst(); |
a1312bc8 | 374 | } |
e2478fde VZ |
375 | } |
376 | ||
079f4130 | 377 | wxLEAVE_CRIT_SECT( *wxHandlersWithPendingEventsLocker ); |
e2478fde VZ |
378 | } |
379 | ||
e0954e72 | 380 | void wxAppConsoleBase::WakeUpIdle() |
b46b1d59 VZ |
381 | { |
382 | if ( m_mainLoop ) | |
383 | m_mainLoop->WakeUp(); | |
384 | } | |
385 | ||
e0954e72 | 386 | bool wxAppConsoleBase::ProcessIdle() |
b46b1d59 | 387 | { |
14eb37a0 VZ |
388 | // process pending wx events before sending idle events |
389 | ProcessPendingEvents(); | |
390 | ||
b46b1d59 VZ |
391 | wxIdleEvent event; |
392 | ||
393 | event.SetEventObject(this); | |
394 | ProcessEvent(event); | |
395 | return event.MoreRequested(); | |
396 | } | |
397 | ||
e0954e72 | 398 | int wxAppConsoleBase::FilterEvent(wxEvent& WXUNUSED(event)) |
e2478fde VZ |
399 | { |
400 | // process the events normally by default | |
401 | return -1; | |
402 | } | |
403 | ||
78361a0e VZ |
404 | // ---------------------------------------------------------------------------- |
405 | // exception handling | |
406 | // ---------------------------------------------------------------------------- | |
407 | ||
6f054ac5 VZ |
408 | #if wxUSE_EXCEPTIONS |
409 | ||
410 | void | |
e0954e72 VZ |
411 | wxAppConsoleBase::HandleEvent(wxEvtHandler *handler, |
412 | wxEventFunction func, | |
413 | wxEvent& event) const | |
6f054ac5 | 414 | { |
96d38c7e VS |
415 | // by default, simply call the handler |
416 | (handler->*func)(event); | |
6f054ac5 VZ |
417 | } |
418 | ||
1663c655 VZ |
419 | void wxAppConsoleBase::OnUnhandledException() |
420 | { | |
421 | #ifdef __WXDEBUG__ | |
422 | // we're called from an exception handler so we can re-throw the exception | |
423 | // to recover its type | |
424 | wxString what; | |
425 | try | |
426 | { | |
427 | throw; | |
428 | } | |
429 | #if wxUSE_STL | |
430 | catch ( std::exception& e ) | |
431 | { | |
432 | what.Printf("std::exception of type \"%s\", what() = \"%s\"", | |
433 | typeid(e).name(), e.what()); | |
434 | } | |
435 | #endif // wxUSE_STL | |
436 | catch ( ... ) | |
437 | { | |
438 | what = "unknown exception"; | |
439 | } | |
440 | ||
441 | wxMessageOutputBest().Printf( | |
442 | "*** Caught unhandled %s; terminating\n", what | |
443 | ); | |
444 | #endif // __WXDEBUG__ | |
445 | } | |
446 | ||
b46b1d59 VZ |
447 | // ---------------------------------------------------------------------------- |
448 | // exceptions support | |
449 | // ---------------------------------------------------------------------------- | |
450 | ||
e0954e72 | 451 | bool wxAppConsoleBase::OnExceptionInMainLoop() |
b46b1d59 VZ |
452 | { |
453 | throw; | |
454 | ||
455 | // some compilers are too stupid to know that we never return after throw | |
456 | #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200) | |
457 | return false; | |
458 | #endif | |
459 | } | |
460 | ||
6f054ac5 VZ |
461 | #endif // wxUSE_EXCEPTIONS |
462 | ||
e2478fde VZ |
463 | // ---------------------------------------------------------------------------- |
464 | // cmd line parsing | |
465 | // ---------------------------------------------------------------------------- | |
466 | ||
467 | #if wxUSE_CMDLINE_PARSER | |
468 | ||
e6d4038a | 469 | #define OPTION_VERBOSE "verbose" |
e2478fde | 470 | |
e0954e72 | 471 | void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser& parser) |
e2478fde VZ |
472 | { |
473 | // the standard command line options | |
474 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
475 | { | |
476 | { | |
477 | wxCMD_LINE_SWITCH, | |
e6d4038a VZ |
478 | "h", |
479 | "help", | |
e2478fde VZ |
480 | gettext_noop("show this help message"), |
481 | wxCMD_LINE_VAL_NONE, | |
482 | wxCMD_LINE_OPTION_HELP | |
483 | }, | |
484 | ||
485 | #if wxUSE_LOG | |
486 | { | |
487 | wxCMD_LINE_SWITCH, | |
0d5ab92f | 488 | NULL, |
e2478fde VZ |
489 | OPTION_VERBOSE, |
490 | gettext_noop("generate verbose log messages"), | |
491 | wxCMD_LINE_VAL_NONE, | |
492 | 0x0 | |
493 | }, | |
494 | #endif // wxUSE_LOG | |
495 | ||
e2478fde | 496 | // terminator |
0d5ab92f | 497 | wxCMD_LINE_DESC_END |
e2478fde VZ |
498 | }; |
499 | ||
500 | parser.SetDesc(cmdLineDesc); | |
501 | } | |
502 | ||
e0954e72 | 503 | bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser& parser) |
e2478fde VZ |
504 | { |
505 | #if wxUSE_LOG | |
506 | if ( parser.Found(OPTION_VERBOSE) ) | |
507 | { | |
fa0d3447 | 508 | wxLog::SetVerbose(true); |
e2478fde | 509 | } |
fa0d3447 WS |
510 | #else |
511 | wxUnusedVar(parser); | |
e2478fde VZ |
512 | #endif // wxUSE_LOG |
513 | ||
fa0d3447 | 514 | return true; |
e2478fde VZ |
515 | } |
516 | ||
e0954e72 | 517 | bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser& parser) |
e2478fde VZ |
518 | { |
519 | parser.Usage(); | |
520 | ||
4629016d | 521 | return false; |
e2478fde VZ |
522 | } |
523 | ||
e0954e72 | 524 | bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser& parser) |
e2478fde VZ |
525 | { |
526 | parser.Usage(); | |
527 | ||
4629016d | 528 | return false; |
e2478fde VZ |
529 | } |
530 | ||
531 | #endif // wxUSE_CMDLINE_PARSER | |
532 | ||
533 | // ---------------------------------------------------------------------------- | |
534 | // debugging support | |
535 | // ---------------------------------------------------------------------------- | |
536 | ||
537 | /* static */ | |
e0954e72 VZ |
538 | bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature, |
539 | const char *componentName) | |
e2478fde | 540 | { |
2a7c7605 VS |
541 | #if 0 // can't use wxLogTrace, not up and running yet |
542 | printf("checking build options object '%s' (ptr %p) in '%s'\n", | |
543 | optionsSignature, optionsSignature, componentName); | |
e2478fde VZ |
544 | #endif |
545 | ||
2a7c7605 | 546 | if ( strcmp(optionsSignature, WX_BUILD_OPTIONS_SIGNATURE) != 0 ) |
e2478fde | 547 | { |
2a7c7605 VS |
548 | wxString lib = wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE); |
549 | wxString prog = wxString::FromAscii(optionsSignature); | |
550 | wxString progName = wxString::FromAscii(componentName); | |
e2478fde | 551 | wxString msg; |
2dbc444a | 552 | |
b880adec | 553 | msg.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."), |
2a7c7605 | 554 | lib.c_str(), progName.c_str(), prog.c_str()); |
2dbc444a | 555 | |
095d49f2 | 556 | wxLogFatalError(msg.c_str()); |
e2478fde VZ |
557 | |
558 | // normally wxLogFatalError doesn't return | |
4629016d | 559 | return false; |
e2478fde VZ |
560 | } |
561 | #undef wxCMP | |
562 | ||
4629016d | 563 | return true; |
e2478fde VZ |
564 | } |
565 | ||
566 | #ifdef __WXDEBUG__ | |
567 | ||
e0954e72 VZ |
568 | void wxAppConsoleBase::OnAssertFailure(const wxChar *file, |
569 | int line, | |
570 | const wxChar *func, | |
571 | const wxChar *cond, | |
572 | const wxChar *msg) | |
dfa0b52f VZ |
573 | { |
574 | ShowAssertDialog(file, line, func, cond, msg, GetTraits()); | |
575 | } | |
576 | ||
e0954e72 VZ |
577 | void wxAppConsoleBase::OnAssert(const wxChar *file, |
578 | int line, | |
579 | const wxChar *cond, | |
580 | const wxChar *msg) | |
e2478fde | 581 | { |
acc476c5 | 582 | OnAssertFailure(file, line, NULL, cond, msg); |
e2478fde VZ |
583 | } |
584 | ||
585 | #endif // __WXDEBUG__ | |
586 | ||
587 | // ============================================================================ | |
588 | // other classes implementations | |
589 | // ============================================================================ | |
590 | ||
591 | // ---------------------------------------------------------------------------- | |
592 | // wxConsoleAppTraitsBase | |
593 | // ---------------------------------------------------------------------------- | |
594 | ||
595 | #if wxUSE_LOG | |
596 | ||
597 | wxLog *wxConsoleAppTraitsBase::CreateLogTarget() | |
598 | { | |
599 | return new wxLogStderr; | |
600 | } | |
601 | ||
602 | #endif // wxUSE_LOG | |
603 | ||
604 | wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput() | |
605 | { | |
606 | return new wxMessageOutputStderr; | |
607 | } | |
608 | ||
609 | #if wxUSE_FONTMAP | |
610 | ||
611 | wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper() | |
612 | { | |
613 | return (wxFontMapper *)new wxFontMapperBase; | |
614 | } | |
615 | ||
616 | #endif // wxUSE_FONTMAP | |
617 | ||
f0244295 VZ |
618 | wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() |
619 | { | |
620 | // console applications don't use renderers | |
621 | return NULL; | |
622 | } | |
623 | ||
8df6de97 | 624 | #ifdef __WXDEBUG__ |
e2478fde VZ |
625 | bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) |
626 | { | |
627 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
628 | } | |
8df6de97 | 629 | #endif |
e2478fde VZ |
630 | |
631 | bool wxConsoleAppTraitsBase::HasStderr() | |
632 | { | |
633 | // console applications always have stderr, even under Mac/Windows | |
634 | return true; | |
635 | } | |
636 | ||
637 | void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
638 | { | |
639 | delete object; | |
640 | } | |
641 | ||
642 | void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject * WXUNUSED(object)) | |
643 | { | |
644 | // nothing to do | |
645 | } | |
2dbc444a | 646 | |
e2478fde VZ |
647 | // ---------------------------------------------------------------------------- |
648 | // wxAppTraits | |
649 | // ---------------------------------------------------------------------------- | |
650 | ||
6c4f5ea5 | 651 | #if wxUSE_INTL |
d774f916 VZ |
652 | void wxAppTraitsBase::SetLocale() |
653 | { | |
d6f2a891 | 654 | wxSetlocale(LC_ALL, ""); |
cb352236 | 655 | wxUpdateLocaleIsUtf8(); |
d774f916 | 656 | } |
6c4f5ea5 | 657 | #endif |
d774f916 | 658 | |
d254213e PC |
659 | #if wxUSE_THREADS |
660 | void wxMutexGuiEnterImpl(); | |
661 | void wxMutexGuiLeaveImpl(); | |
662 | ||
663 | void wxAppTraitsBase::MutexGuiEnter() | |
664 | { | |
665 | wxMutexGuiEnterImpl(); | |
666 | } | |
667 | ||
668 | void wxAppTraitsBase::MutexGuiLeave() | |
669 | { | |
670 | wxMutexGuiLeaveImpl(); | |
671 | } | |
672 | ||
673 | void WXDLLIMPEXP_BASE wxMutexGuiEnter() | |
674 | { | |
96b2cbe8 VZ |
675 | wxAppTraits * const traits = wxAppConsoleBase::GetTraitsIfExists(); |
676 | if ( traits ) | |
677 | traits->MutexGuiEnter(); | |
d254213e PC |
678 | } |
679 | ||
680 | void WXDLLIMPEXP_BASE wxMutexGuiLeave() | |
681 | { | |
96b2cbe8 VZ |
682 | wxAppTraits * const traits = wxAppConsoleBase::GetTraitsIfExists(); |
683 | if ( traits ) | |
684 | traits->MutexGuiLeave(); | |
d254213e PC |
685 | } |
686 | #endif // wxUSE_THREADS | |
687 | ||
e2478fde VZ |
688 | #ifdef __WXDEBUG__ |
689 | ||
db9febdf | 690 | bool wxAppTraitsBase::ShowAssertDialog(const wxString& msgOriginal) |
e2478fde | 691 | { |
db9febdf RR |
692 | wxString msg = msgOriginal; |
693 | ||
694 | #if wxUSE_STACKWALKER | |
695 | #if !defined(__WXMSW__) | |
696 | // on Unix stack frame generation may take some time, depending on the | |
697 | // size of the executable mainly... warn the user that we are working | |
698 | wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait")); | |
699 | fflush(stderr); | |
700 | #endif | |
701 | ||
702 | const wxString stackTrace = GetAssertStackTrace(); | |
703 | if ( !stackTrace.empty() ) | |
704 | msg << _T("\n\nCall stack:\n") << stackTrace; | |
705 | #endif // wxUSE_STACKWALKER | |
706 | ||
e2478fde VZ |
707 | return DoShowAssertDialog(msg); |
708 | } | |
709 | ||
db9febdf RR |
710 | #if wxUSE_STACKWALKER |
711 | wxString wxAppTraitsBase::GetAssertStackTrace() | |
712 | { | |
713 | wxString stackTrace; | |
714 | ||
715 | class StackDump : public wxStackWalker | |
716 | { | |
717 | public: | |
718 | StackDump() { } | |
719 | ||
720 | const wxString& GetStackTrace() const { return m_stackTrace; } | |
721 | ||
722 | protected: | |
723 | virtual void OnStackFrame(const wxStackFrame& frame) | |
724 | { | |
725 | m_stackTrace << wxString::Format | |
726 | ( | |
727 | _T("[%02d] "), | |
728 | wx_truncate_cast(int, frame.GetLevel()) | |
729 | ); | |
730 | ||
731 | wxString name = frame.GetName(); | |
732 | if ( !name.empty() ) | |
733 | { | |
734 | m_stackTrace << wxString::Format(_T("%-40s"), name.c_str()); | |
735 | } | |
736 | else | |
737 | { | |
738 | m_stackTrace << wxString::Format(_T("%p"), frame.GetAddress()); | |
739 | } | |
740 | ||
741 | if ( frame.HasSourceLocation() ) | |
742 | { | |
743 | m_stackTrace << _T('\t') | |
744 | << frame.GetFileName() | |
745 | << _T(':') | |
746 | << frame.GetLine(); | |
747 | } | |
748 | ||
749 | m_stackTrace << _T('\n'); | |
750 | } | |
751 | ||
752 | private: | |
753 | wxString m_stackTrace; | |
754 | }; | |
755 | ||
756 | // don't show more than maxLines or we could get a dialog too tall to be | |
757 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
758 | // characters it is still only 300 pixels... | |
759 | static const int maxLines = 20; | |
760 | ||
761 | StackDump dump; | |
762 | dump.Walk(2, maxLines); // don't show OnAssert() call itself | |
763 | stackTrace = dump.GetStackTrace(); | |
764 | ||
765 | const int count = stackTrace.Freq(wxT('\n')); | |
766 | for ( int i = 0; i < count - maxLines; i++ ) | |
767 | stackTrace = stackTrace.BeforeLast(wxT('\n')); | |
768 | ||
769 | return stackTrace; | |
770 | } | |
771 | #endif // wxUSE_STACKWALKER | |
772 | ||
773 | ||
e2478fde VZ |
774 | #endif // __WXDEBUG__ |
775 | ||
e2478fde VZ |
776 | // ============================================================================ |
777 | // global functions implementation | |
778 | // ============================================================================ | |
779 | ||
780 | void wxExit() | |
781 | { | |
782 | if ( wxTheApp ) | |
783 | { | |
784 | wxTheApp->Exit(); | |
785 | } | |
786 | else | |
787 | { | |
788 | // what else can we do? | |
789 | exit(-1); | |
790 | } | |
791 | } | |
792 | ||
793 | void wxWakeUpIdle() | |
794 | { | |
795 | if ( wxTheApp ) | |
796 | { | |
797 | wxTheApp->WakeUpIdle(); | |
798 | } | |
799 | //else: do nothing, what can we do? | |
800 | } | |
801 | ||
802 | #ifdef __WXDEBUG__ | |
803 | ||
804 | // wxASSERT() helper | |
805 | bool wxAssertIsEqual(int x, int y) | |
806 | { | |
807 | return x == y; | |
808 | } | |
809 | ||
810 | // break into the debugger | |
811 | void wxTrap() | |
812 | { | |
813 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
814 | DebugBreak(); | |
f0756afe DE |
815 | #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS |
816 | Debugger(); | |
e2478fde VZ |
817 | #elif defined(__UNIX__) |
818 | raise(SIGTRAP); | |
819 | #else | |
820 | // TODO | |
821 | #endif // Win/Unix | |
822 | } | |
823 | ||
e2478fde | 824 | // this function is called when an assert fails |
0accd1cf VS |
825 | static void wxDoOnAssert(const wxString& szFile, |
826 | int nLine, | |
827 | const wxString& szFunc, | |
828 | const wxString& szCond, | |
829 | const wxString& szMsg = wxEmptyString) | |
e2478fde VZ |
830 | { |
831 | // FIXME MT-unsafe | |
000eea7a | 832 | static int s_bInAssert = 0; |
e2478fde | 833 | |
000eea7a VZ |
834 | wxRecursionGuard guard(s_bInAssert); |
835 | if ( guard.IsInside() ) | |
e2478fde | 836 | { |
000eea7a | 837 | // can't use assert here to avoid infinite loops, so just trap |
e2478fde VZ |
838 | wxTrap(); |
839 | ||
e2478fde VZ |
840 | return; |
841 | } | |
842 | ||
e2478fde VZ |
843 | if ( !wxTheApp ) |
844 | { | |
845 | // by default, show the assert dialog box -- we can't customize this | |
846 | // behaviour | |
0accd1cf | 847 | ShowAssertDialog(szFile, nLine, szFunc, szCond, szMsg); |
e2478fde VZ |
848 | } |
849 | else | |
850 | { | |
851 | // let the app process it as it wants | |
0accd1cf VS |
852 | // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed |
853 | wxTheApp->OnAssertFailure(szFile.c_str(), nLine, szFunc.c_str(), | |
854 | szCond.c_str(), szMsg.c_str()); | |
e2478fde | 855 | } |
e2478fde VZ |
856 | } |
857 | ||
0accd1cf VS |
858 | void wxOnAssert(const wxString& szFile, |
859 | int nLine, | |
860 | const wxString& szFunc, | |
861 | const wxString& szCond, | |
862 | const wxString& szMsg) | |
863 | { | |
864 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
865 | } | |
866 | ||
867 | void wxOnAssert(const wxString& szFile, | |
868 | int nLine, | |
869 | const wxString& szFunc, | |
870 | const wxString& szCond) | |
871 | { | |
872 | wxDoOnAssert(szFile, nLine, szFunc, szCond); | |
873 | } | |
874 | ||
875 | void wxOnAssert(const wxChar *szFile, | |
876 | int nLine, | |
877 | const char *szFunc, | |
878 | const wxChar *szCond, | |
879 | const wxChar *szMsg) | |
880 | { | |
881 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
882 | } | |
883 | ||
fbaf7d45 VS |
884 | void wxOnAssert(const char *szFile, |
885 | int nLine, | |
886 | const char *szFunc, | |
887 | const char *szCond, | |
888 | const wxString& szMsg) | |
889 | { | |
890 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
891 | } | |
892 | ||
2b232d20 VZ |
893 | void wxOnAssert(const char *szFile, |
894 | int nLine, | |
895 | const char *szFunc, | |
896 | const char *szCond, | |
897 | const wxCStrData& msg) | |
898 | { | |
899 | wxDoOnAssert(szFile, nLine, szFunc, szCond, msg); | |
900 | } | |
901 | ||
0accd1cf VS |
902 | #if wxUSE_UNICODE |
903 | void wxOnAssert(const char *szFile, | |
904 | int nLine, | |
905 | const char *szFunc, | |
906 | const char *szCond) | |
907 | { | |
908 | wxDoOnAssert(szFile, nLine, szFunc, szCond); | |
909 | } | |
910 | ||
911 | void wxOnAssert(const char *szFile, | |
912 | int nLine, | |
913 | const char *szFunc, | |
914 | const char *szCond, | |
915 | const char *szMsg) | |
916 | { | |
917 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
918 | } | |
919 | ||
920 | void wxOnAssert(const char *szFile, | |
921 | int nLine, | |
922 | const char *szFunc, | |
923 | const char *szCond, | |
924 | const wxChar *szMsg) | |
925 | { | |
926 | wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); | |
927 | } | |
928 | #endif // wxUSE_UNICODE | |
929 | ||
e2478fde VZ |
930 | #endif // __WXDEBUG__ |
931 | ||
932 | // ============================================================================ | |
933 | // private functions implementation | |
934 | // ============================================================================ | |
935 | ||
936 | #ifdef __WXDEBUG__ | |
937 | ||
938 | static void LINKAGEMODE SetTraceMasks() | |
939 | { | |
940 | #if wxUSE_LOG | |
941 | wxString mask; | |
942 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
943 | { | |
944 | wxStringTokenizer tkn(mask, wxT(",;:")); | |
945 | while ( tkn.HasMoreTokens() ) | |
946 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
947 | } | |
948 | #endif // wxUSE_LOG | |
949 | } | |
950 | ||
6ef1b2a1 | 951 | static |
e2478fde VZ |
952 | bool DoShowAssertDialog(const wxString& msg) |
953 | { | |
954 | // under MSW we can show the dialog even in the console mode | |
955 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
956 | wxString msgDlg(msg); | |
957 | ||
958 | // this message is intentionally not translated -- it is for | |
959 | // developpers only | |
960 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
961 | wxT("You can also choose [Cancel] to suppress ") | |
962 | wxT("further warnings."); | |
963 | ||
e0a050e3 | 964 | switch ( ::MessageBox(NULL, msgDlg.wx_str(), _T("wxWidgets Debug Alert"), |
e2478fde VZ |
965 | MB_YESNOCANCEL | MB_ICONSTOP ) ) |
966 | { | |
967 | case IDYES: | |
968 | wxTrap(); | |
969 | break; | |
970 | ||
971 | case IDCANCEL: | |
972 | // stop the asserts | |
973 | return true; | |
974 | ||
975 | //case IDNO: nothing to do | |
976 | } | |
977 | #else // !__WXMSW__ | |
978 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
979 | fflush(stderr); | |
980 | ||
981 | // TODO: ask the user to enter "Y" or "N" on the console? | |
982 | wxTrap(); | |
983 | #endif // __WXMSW__/!__WXMSW__ | |
984 | ||
985 | // continue with the asserts | |
986 | return false; | |
987 | } | |
988 | ||
4a92d4bc VZ |
989 | // show the assert modal dialog |
990 | static | |
0accd1cf | 991 | void ShowAssertDialog(const wxString& szFile, |
4a92d4bc | 992 | int nLine, |
0accd1cf VS |
993 | const wxString& szFunc, |
994 | const wxString& szCond, | |
995 | const wxString& szMsg, | |
4a92d4bc VZ |
996 | wxAppTraits *traits) |
997 | { | |
998 | // this variable can be set to true to suppress "assert failure" messages | |
999 | static bool s_bNoAsserts = false; | |
1000 | ||
1001 | wxString msg; | |
1002 | msg.reserve(2048); | |
1003 | ||
1004 | // make life easier for people using VC++ IDE by using this format: like | |
1005 | // this, clicking on the message will take us immediately to the place of | |
1006 | // the failed assert | |
1007 | msg.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile, nLine, szCond); | |
1008 | ||
dfa0b52f | 1009 | // add the function name, if any |
0accd1cf | 1010 | if ( !szFunc.empty() ) |
dfa0b52f VZ |
1011 | msg << _T(" in ") << szFunc << _T("()"); |
1012 | ||
1013 | // and the message itself | |
0accd1cf | 1014 | if ( !szMsg.empty() ) |
2c9b3131 | 1015 | { |
4a92d4bc | 1016 | msg << _T(": ") << szMsg; |
2c9b3131 | 1017 | } |
4a92d4bc VZ |
1018 | else // no message given |
1019 | { | |
1020 | msg << _T('.'); | |
1021 | } | |
1022 | ||
e2478fde VZ |
1023 | #if wxUSE_THREADS |
1024 | // if we are not in the main thread, output the assert directly and trap | |
1025 | // since dialogs cannot be displayed | |
1026 | if ( !wxThread::IsMain() ) | |
1027 | { | |
1028 | msg += wxT(" [in child thread]"); | |
1029 | ||
1030 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
1031 | msg << wxT("\r\n"); | |
e0a050e3 | 1032 | OutputDebugString(msg.wx_str()); |
e2478fde VZ |
1033 | #else |
1034 | // send to stderr | |
1035 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
1036 | fflush(stderr); | |
1037 | #endif | |
1038 | // He-e-e-e-elp!! we're asserting in a child thread | |
1039 | wxTrap(); | |
1040 | } | |
6c8f8d92 | 1041 | else |
e2478fde VZ |
1042 | #endif // wxUSE_THREADS |
1043 | ||
1044 | if ( !s_bNoAsserts ) | |
1045 | { | |
1046 | // send it to the normal log destination | |
56fae7b8 | 1047 | wxLogDebug(_T("%s"), msg.c_str()); |
e2478fde VZ |
1048 | |
1049 | if ( traits ) | |
1050 | { | |
1051 | // delegate showing assert dialog (if possible) to that class | |
1052 | s_bNoAsserts = traits->ShowAssertDialog(msg); | |
1053 | } | |
1054 | else // no traits object | |
1055 | { | |
1056 | // fall back to the function of last resort | |
1057 | s_bNoAsserts = DoShowAssertDialog(msg); | |
1058 | } | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | #endif // __WXDEBUG__ |