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