]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
#ifed wxLog occurrences
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/appcmn.cpp
3 // Purpose: wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 18.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "appbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/intl.h"
34 #include "wx/list.h"
35 #if wxUSE_LOG
36 #include "wx/log.h"
37 #endif // wxUSE_LOG
38 #if wxUSE_GUI
39 #include "wx/msgdlg.h"
40 #endif // wxUSE_GUI
41 #endif
42
43 #include "wx/cmdline.h"
44 #include "wx/thread.h"
45 #include "wx/confbase.h"
46 #include "wx/tokenzr.h"
47 #include "wx/utils.h"
48 #include "wx/msgout.h"
49
50 #if wxUSE_GUI
51 #include "wx/artprov.h"
52 #endif // wxUSE_GUI
53
54 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
55 #include <signal.h> // for SIGTRAP used by wxTrap()
56 #endif //Win/Unix
57
58 #if defined(__WXMSW__)
59 #include "wx/msw/private.h" // includes windows.h for MessageBox()
60 #endif
61
62 #if defined(__WXMAC__)
63 #include "wx/mac/private.h" // includes mac headers
64 #endif
65
66 // private functions prototypes
67 #ifdef __WXDEBUG__
68 static void LINKAGEMODE SetTraceMasks();
69 #endif // __WXDEBUG__
70
71 // ===========================================================================
72 // implementation
73 // ===========================================================================
74
75 // ----------------------------------------------------------------------------
76 // initialization and termination
77 // ----------------------------------------------------------------------------
78
79 wxAppBase::wxAppBase()
80 {
81 wxTheApp = (wxApp *)this;
82
83 #if WXWIN_COMPATIBILITY_2_2
84 m_wantDebugOutput = FALSE;
85 #endif // WXWIN_COMPATIBILITY_2_2
86
87 #if wxUSE_GUI
88 m_topWindow = (wxWindow *)NULL;
89 m_useBestVisual = FALSE;
90 m_isActive = TRUE;
91
92 // We don't want to exit the app if the user code shows a dialog from its
93 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
94 // to Yes initially as this dialog would be the last top level window.
95 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
96 // when we enter our OnRun() because we do want the default behaviour from
97 // then on. But this would be a problem if the user code calls
98 // SetExitOnFrameDelete(FALSE) from OnInit().
99 //
100 // So we use the special "Later" value which is such that
101 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
102 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
103 // call) overwrite in OnRun()
104 m_exitOnFrameDelete = Later;
105 #endif // wxUSE_GUI
106
107 #ifdef __WXDEBUG__
108 SetTraceMasks();
109 #endif
110 }
111
112 wxAppBase::~wxAppBase()
113 {
114 // this destructor is required for Darwin
115 }
116
117 #if wxUSE_GUI
118
119 bool wxAppBase::OnInitGui()
120 {
121 #ifdef __WXUNIVERSAL__
122 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
123 return FALSE;
124 #endif // __WXUNIVERSAL__
125
126 return TRUE;
127 }
128
129 int wxAppBase::OnRun()
130 {
131 // see the comment in ctor: if the initial value hasn't been changed, use
132 // the default Yes from now on
133 if ( m_exitOnFrameDelete == Later )
134 {
135 m_exitOnFrameDelete = Yes;
136 }
137 //else: it has been changed, assume the user knows what he is doing
138
139 return MainLoop();
140 }
141
142 #endif // wxUSE_GUI
143
144 int wxAppBase::OnExit()
145 {
146 #if wxUSE_CONFIG
147 // delete the config object if any (don't use Get() here, but Set()
148 // because Get() could create a new config object)
149 delete wxConfigBase::Set((wxConfigBase *) NULL);
150 #endif // wxUSE_CONFIG
151
152 #ifdef __WXUNIVERSAL__
153 delete wxTheme::Set(NULL);
154 #endif // __WXUNIVERSAL__
155
156 // use Set(NULL) and not Get() to avoid creating a message output object on
157 // demand when we just want to delete it
158 delete wxMessageOutput::Set(NULL);
159
160 return 0;
161 }
162
163 // ----------------------------------------------------------------------------
164 // customization hooks
165 // ----------------------------------------------------------------------------
166
167 #if wxUSE_LOG
168
169 wxLog *wxAppBase::CreateLogTarget()
170 {
171 #if wxUSE_GUI && wxUSE_LOGGUI && !defined(__WXMICROWIN__)
172 return new wxLogGui;
173 #else // !GUI
174 return new wxLogStderr;
175 #endif // wxUSE_GUI
176 }
177
178 #endif // wxUSE_LOG
179
180 wxMessageOutput *wxAppBase::CreateMessageOutput()
181 {
182 // The standard way of printing help on command line arguments (app --help)
183 // is (according to common practice):
184 // - console apps: to stderr (on any platform)
185 // - GUI apps: stderr on Unix platforms (!)
186 // message box under Windows and others
187 #if wxUSE_GUI && !defined(__UNIX__)
188 // wxMessageOutputMessageBox doesn't work under Motif
189 #ifdef __WXMOTIF__
190 return new wxMessageOutputLog;
191 #else
192 return new wxMessageOutputMessageBox;
193 #endif
194 #else // !wxUSE_GUI || __UNIX__
195 return new wxMessageOutputStderr;
196 #endif
197 }
198
199 // ---------------------------------------------------------------------------
200 // wxAppBase
201 // ----------------------------------------------------------------------------
202
203 void wxAppBase::ProcessPendingEvents()
204 {
205 // ensure that we're the only thread to modify the pending events list
206 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
207
208 if ( !wxPendingEvents )
209 {
210 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
211 return;
212 }
213
214 // iterate until the list becomes empty
215 wxNode *node = wxPendingEvents->GetFirst();
216 while (node)
217 {
218 wxEvtHandler *handler = (wxEvtHandler *)node->GetData();
219 delete node;
220
221 // In ProcessPendingEvents(), new handlers might be add
222 // and we can safely leave the critical section here.
223 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
224 handler->ProcessPendingEvents();
225 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
226
227 node = wxPendingEvents->GetFirst();
228 }
229
230 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
231 }
232
233 // ----------------------------------------------------------------------------
234 // misc
235 // ----------------------------------------------------------------------------
236
237 #if wxUSE_GUI
238
239 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
240 {
241 if ( active == m_isActive )
242 return;
243
244 m_isActive = active;
245
246 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
247 event.SetEventObject(this);
248
249 (void)ProcessEvent(event);
250 }
251
252 #endif // wxUSE_GUI
253
254 int wxAppBase::FilterEvent(wxEvent& WXUNUSED(event))
255 {
256 // process the events normally by default
257 return -1;
258 }
259
260 // ----------------------------------------------------------------------------
261 // cmd line parsing
262 // ----------------------------------------------------------------------------
263
264 bool wxAppBase::OnInit()
265 {
266 #if wxUSE_CMDLINE_PARSER
267 wxCmdLineParser parser(argc, argv);
268
269 OnInitCmdLine(parser);
270
271 bool cont;
272 switch ( parser.Parse(FALSE /* don't show usage */) )
273 {
274 case -1:
275 cont = OnCmdLineHelp(parser);
276 break;
277
278 case 0:
279 cont = OnCmdLineParsed(parser);
280 break;
281
282 default:
283 cont = OnCmdLineError(parser);
284 break;
285 }
286
287 if ( !cont )
288 return FALSE;
289 #endif // wxUSE_CMDLINE_PARSER
290
291 return TRUE;
292 }
293
294 #if wxUSE_CMDLINE_PARSER
295
296 #define OPTION_VERBOSE _T("verbose")
297 #define OPTION_THEME _T("theme")
298 #define OPTION_MODE _T("mode")
299
300 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
301 {
302 // the standard command line options
303 static const wxCmdLineEntryDesc cmdLineDesc[] =
304 {
305 {
306 wxCMD_LINE_SWITCH,
307 _T("h"),
308 _T("help"),
309 gettext_noop("show this help message"),
310 wxCMD_LINE_VAL_NONE,
311 wxCMD_LINE_OPTION_HELP
312 },
313
314 #if wxUSE_LOG
315 {
316 wxCMD_LINE_SWITCH,
317 _T(""),
318 OPTION_VERBOSE,
319 gettext_noop("generate verbose log messages"),
320 wxCMD_LINE_VAL_NONE,
321 0x0
322 },
323 #endif // wxUSE_LOG
324
325 #ifdef __WXUNIVERSAL__
326 {
327 wxCMD_LINE_OPTION,
328 _T(""),
329 OPTION_THEME,
330 gettext_noop("specify the theme to use"),
331 wxCMD_LINE_VAL_STRING,
332 0x0
333 },
334 #endif // __WXUNIVERSAL__
335
336 #if defined(__WXMGL__)
337 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
338 // should provide this option. That's why it is in common/appcmn.cpp
339 // and not mgl/app.cpp
340 {
341 wxCMD_LINE_OPTION,
342 _T(""),
343 OPTION_MODE,
344 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
345 wxCMD_LINE_VAL_STRING,
346 0x0
347 },
348 #endif // __WXMGL__
349
350 // terminator
351 {
352 wxCMD_LINE_NONE,
353 _T(""),
354 _T(""),
355 _T(""),
356 wxCMD_LINE_VAL_NONE,
357 0x0
358 }
359 };
360
361 parser.SetDesc(cmdLineDesc);
362 }
363
364 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
365 {
366 #if wxUSE_LOG
367 if ( parser.Found(OPTION_VERBOSE) )
368 {
369 wxLog::SetVerbose(TRUE);
370 }
371 #endif // wxUSE_LOG
372
373 #ifdef __WXUNIVERSAL__
374 wxString themeName;
375 if ( parser.Found(OPTION_THEME, &themeName) )
376 {
377 wxTheme *theme = wxTheme::Create(themeName);
378 if ( !theme )
379 {
380 #if wxUSE_LOG
381 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
382 #endif
383
384 return FALSE;
385 }
386
387 // Delete the defaultly created theme and set the new theme.
388 delete wxTheme::Get();
389 wxTheme::Set(theme);
390 }
391 #endif // __WXUNIVERSAL__
392
393 #if defined(__WXMGL__)
394 wxString modeDesc;
395 if ( parser.Found(OPTION_MODE, &modeDesc) )
396 {
397 unsigned w, h, bpp;
398 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
399 {
400 #if wxUSE_LOG
401 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
402 #endif
403 return FALSE;
404 }
405
406 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
407 return FALSE;
408 }
409 #endif // __WXMGL__
410
411 return TRUE;
412 }
413
414 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
415 {
416 parser.Usage();
417
418 return FALSE;
419 }
420
421 bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
422 {
423 parser.Usage();
424
425 return FALSE;
426 }
427
428 #endif // wxUSE_CMDLINE_PARSER
429
430 // ----------------------------------------------------------------------------
431 // debugging support
432 // ----------------------------------------------------------------------------
433
434 /* static */
435 bool wxAppBase::CheckBuildOptions(const wxBuildOptions& opts)
436 {
437 #define wxCMP(what) (what == opts.m_ ## what)
438
439 bool
440 #ifdef __WXDEBUG__
441 isDebug = TRUE;
442 #else
443 isDebug = FALSE;
444 #endif
445
446 int verMaj = wxMAJOR_VERSION,
447 verMin = wxMINOR_VERSION;
448
449 if ( !(wxCMP(isDebug) && wxCMP(verMaj) && wxCMP(verMin)) )
450 {
451 wxString msg;
452 wxString libDebug, progDebug;
453
454 if (isDebug)
455 libDebug = wxT("debug");
456 else
457 libDebug = wxT("no debug");
458
459 if (opts.m_isDebug)
460 progDebug = wxT("debug");
461 else
462 progDebug = wxT("no debug");
463
464 msg.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %d.%d (%s), and your program used %d.%d (%s)."),
465 verMaj, verMin, libDebug.c_str(), opts.m_verMaj, opts.m_verMin, progDebug.c_str());
466
467 #if wxUSE_LOG
468 wxLogFatalError(msg);
469 #endif
470
471 // normally wxLogFatalError doesn't return
472 return FALSE;
473 }
474 #undef wxCMP
475
476 return TRUE;
477 }
478
479 #ifdef __WXDEBUG__
480
481 static void LINKAGEMODE SetTraceMasks()
482 {
483 #if wxUSE_LOG
484 wxString mask;
485 if ( wxGetEnv(wxT("WXTRACE"), &mask) )
486 {
487 wxStringTokenizer tkn(mask, wxT(","));
488 while ( tkn.HasMoreTokens() )
489 wxLog::AddTraceMask(tkn.GetNextToken());
490 }
491 #endif // wxUSE_LOG
492 }
493
494 // wxASSERT() helper
495 bool wxAssertIsEqual(int x, int y)
496 {
497 return x == y;
498 }
499
500 // break into the debugger
501 void wxTrap()
502 {
503 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
504 DebugBreak();
505 #elif defined(__WXMAC__) && !defined(__DARWIN__)
506 #if __powerc
507 Debugger();
508 #else
509 SysBreak();
510 #endif
511 #elif defined(__UNIX__)
512 raise(SIGTRAP);
513 #else
514 // TODO
515 #endif // Win/Unix
516 }
517
518
519 void wxAssert(int cond,
520 const wxChar *szFile,
521 int nLine,
522 const wxChar *szCond,
523 const wxChar *szMsg)
524 {
525 if ( !cond )
526 wxOnAssert(szFile, nLine, szCond, szMsg);
527 }
528
529 // show the assert modal dialog
530 static
531 void ShowAssertDialog(const wxChar *szFile,
532 int nLine,
533 const wxChar *szCond,
534 const wxChar *szMsg)
535 {
536 // this variable can be set to true to suppress "assert failure" messages
537 static bool s_bNoAsserts = FALSE;
538
539 wxChar szBuf[4096];
540
541 // make life easier for people using VC++ IDE by using this format: like
542 // this, clicking on the message will take us immediately to the place of
543 // the failed assert
544 wxSnprintf(szBuf, WXSIZEOF(szBuf),
545 wxT("%s(%d): assert \"%s\" failed"),
546 szFile, nLine, szCond);
547
548 if ( szMsg != NULL )
549 {
550 wxStrcat(szBuf, wxT(": "));
551 wxStrcat(szBuf, szMsg);
552 }
553 else // no message given
554 {
555 wxStrcat(szBuf, wxT("."));
556 }
557
558 if ( !s_bNoAsserts )
559 {
560 #if wxUSE_LOG
561 // send it to the normal log destination
562 wxLogDebug(szBuf);
563 #endif
564
565 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
566 // this message is intentionally not translated - it is for
567 // developpers only
568 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
569
570 // use the native message box if available: this is more robust than
571 // using our own
572 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
573 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
574 MB_YESNOCANCEL | MB_ICONSTOP ) )
575 {
576 case IDYES:
577 wxTrap();
578 break;
579
580 case IDCANCEL:
581 s_bNoAsserts = TRUE;
582 break;
583
584 //case IDNO: nothing to do
585 }
586 #else // !MSW
587 switch ( wxMessageBox(szBuf, wxT("Debug"),
588 wxYES_NO | wxCANCEL | wxICON_STOP ) )
589 {
590 case wxYES:
591 wxTrap();
592 break;
593
594 case wxCANCEL:
595 s_bNoAsserts = TRUE;
596 break;
597
598 //case wxNO: nothing to do
599 }
600 #endif // GUI or MSW
601
602 #else // !GUI
603 wxTrap();
604 #endif // GUI/!GUI
605 }
606 }
607
608 // this function is called when an assert fails
609 void wxOnAssert(const wxChar *szFile,
610 int nLine,
611 const wxChar *szCond,
612 const wxChar *szMsg)
613 {
614 // FIXME MT-unsafe
615 static bool s_bInAssert = FALSE;
616
617 if ( s_bInAssert )
618 {
619 // He-e-e-e-elp!! we're trapped in endless loop
620 wxTrap();
621
622 s_bInAssert = FALSE;
623
624 return;
625 }
626
627 s_bInAssert = TRUE;
628
629 if ( !wxTheApp )
630 {
631 // by default, show the assert dialog box - we can't customize this
632 // behaviour
633 ShowAssertDialog(szFile, nLine, szCond, szMsg);
634 }
635 else
636 {
637 // let the app process it as it wants
638 wxTheApp->OnAssert(szFile, nLine, szCond, szMsg);
639 }
640
641 s_bInAssert = FALSE;
642 }
643
644 void wxAppBase::OnAssert(const wxChar *file,
645 int line,
646 const wxChar *cond,
647 const wxChar *msg)
648 {
649 ShowAssertDialog(file, line, cond, msg);
650 }
651
652 #endif //WXDEBUG
653