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