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