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