]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
a53be00281a6a70525a58c3dfe2a90992a83493e
[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 },
255 #endif // wxUSE_LOG
256
257 #ifdef __WXUNIVERSAL__
258 {
259 wxCMD_LINE_OPTION,
260 _T(""),
261 OPTION_THEME,
262 gettext_noop("specify the theme to use"),
263 wxCMD_LINE_VAL_STRING
264 },
265 #endif // __WXUNIVERSAL__
266
267 #if defined(__WXMGL__)
268 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
269 // should provide this option. That's why it is in common/appcmn.cpp
270 // and not mgl/app.cpp
271 {
272 wxCMD_LINE_OPTION,
273 _T(""),
274 OPTION_MODE,
275 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
276 wxCMD_LINE_VAL_STRING
277 },
278 #endif // __WXMGL__
279
280 // terminator
281 { wxCMD_LINE_NONE }
282 };
283
284 parser.SetDesc(cmdLineDesc);
285 }
286
287 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
288 {
289 #if wxUSE_LOG
290 if ( parser.Found(OPTION_VERBOSE) )
291 {
292 wxLog::SetVerbose(TRUE);
293 }
294 #endif // wxUSE_LOG
295
296 #ifdef __WXUNIVERSAL__
297 wxString themeName;
298 if ( parser.Found(OPTION_THEME, &themeName) )
299 {
300 wxTheme *theme = wxTheme::Create(themeName);
301 if ( !theme )
302 {
303 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
304
305 return FALSE;
306 }
307
308 wxTheme::Set(theme);
309 }
310 #endif // __WXUNIVERSAL__
311
312 #if defined(__WXMGL__)
313 wxString modeDesc;
314 if ( parser.Found(OPTION_MODE, &modeDesc) )
315 {
316 unsigned w, h, bpp;
317 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
318 {
319 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
320
321 return FALSE;
322 }
323
324 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
325 return FALSE;
326 }
327 #endif // __WXMGL__
328
329 return TRUE;
330 }
331
332 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
333 {
334 parser.Usage();
335
336 return FALSE;
337 }
338
339 bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
340 {
341 parser.Usage();
342
343 return FALSE;
344 }
345
346 #endif // wxUSE_CMDLINE_PARSER
347
348 // ----------------------------------------------------------------------------
349 // debugging support
350 // ----------------------------------------------------------------------------
351
352 /* static */
353 bool wxAppBase::CheckBuildOptions(const wxBuildOptions& opts)
354 {
355 #define wxCMP(what) (what == opts.m_ ## what)
356
357 bool
358 #ifdef __WXDEBUG__
359 isDebug = TRUE;
360 #else
361 isDebug = FALSE;
362 #endif
363
364 int verMaj = wxMAJOR_VERSION,
365 verMin = wxMINOR_VERSION;
366
367 return wxCMP(isDebug) && wxCMP(verMaj) && wxCMP(verMin);
368
369 #undef wxCMP
370 }
371
372 #ifdef __WXDEBUG__
373
374 static void LINKAGEMODE SetTraceMasks()
375 {
376 wxString mask;
377 if ( wxGetEnv(wxT("WXTRACE"), &mask) )
378 {
379 wxStringTokenizer tkn(mask, wxT(","));
380 while ( tkn.HasMoreTokens() )
381 wxLog::AddTraceMask(tkn.GetNextToken());
382 }
383 }
384
385 // wxASSERT() helper
386 bool wxAssertIsEqual(int x, int y)
387 {
388 return x == y;
389 }
390
391 // break into the debugger
392 void wxTrap()
393 {
394 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
395 DebugBreak();
396 #elif defined(__WXMAC__) && !defined(__DARWIN__)
397 #if __powerc
398 Debugger();
399 #else
400 SysBreak();
401 #endif
402 #elif defined(__UNIX__)
403 raise(SIGTRAP);
404 #else
405 // TODO
406 #endif // Win/Unix
407 }
408
409 // show the assert modal dialog
410 static
411 void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg)
412 {
413 // this variable can be set to true to suppress "assert failure" messages
414 static bool s_bNoAsserts = FALSE;
415
416 wxChar szBuf[4096];
417
418 // make life easier for people using VC++ IDE: clicking on the message
419 // will take us immediately to the place of the failed assert
420 wxSnprintf(szBuf, WXSIZEOF(szBuf),
421 #ifdef __VISUALC__
422 wxT("%s(%d): assert failed"),
423 #else // !VC++
424 // make the error message more clear for all the others
425 wxT("Assert failed in file %s at line %d"),
426 #endif // VC/!VC
427 szFile, nLine);
428
429 if ( szMsg != NULL )
430 {
431 wxStrcat(szBuf, wxT(": "));
432 wxStrcat(szBuf, szMsg);
433 }
434 else // no message given
435 {
436 wxStrcat(szBuf, wxT("."));
437 }
438
439 if ( !s_bNoAsserts )
440 {
441 // send it to the normal log destination
442 wxLogDebug(szBuf);
443
444 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
445 // this message is intentionally not translated - it is for
446 // developpers only
447 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
448
449 // use the native message box if available: this is more robust than
450 // using our own
451 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
452 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
453 MB_YESNOCANCEL | MB_ICONSTOP ) )
454 {
455 case IDYES:
456 wxTrap();
457 break;
458
459 case IDCANCEL:
460 s_bNoAsserts = TRUE;
461 break;
462
463 //case IDNO: nothing to do
464 }
465 #else // !MSW
466 switch ( wxMessageBox(szBuf, wxT("Debug"),
467 wxYES_NO | wxCANCEL | wxICON_STOP ) )
468 {
469 case wxYES:
470 wxTrap();
471 break;
472
473 case wxCANCEL:
474 s_bNoAsserts = TRUE;
475 break;
476
477 //case wxNO: nothing to do
478 }
479 #endif // GUI or MSW
480
481 #else // !GUI
482 wxTrap();
483 #endif // GUI/!GUI
484 }
485 }
486
487 // this function is called when an assert fails
488 void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
489 {
490 // FIXME MT-unsafe
491 static bool s_bInAssert = FALSE;
492
493 if ( s_bInAssert )
494 {
495 // He-e-e-e-elp!! we're trapped in endless loop
496 wxTrap();
497
498 s_bInAssert = FALSE;
499
500 return;
501 }
502
503 s_bInAssert = TRUE;
504
505 if ( !wxTheApp )
506 {
507 // by default, show the assert dialog box - we can't customize this
508 // behaviour
509 ShowAssertDialog(szFile, nLine, szMsg);
510 }
511 else
512 {
513 // let the app process it as it wants
514 wxTheApp->OnAssert(szFile, nLine, szMsg);
515 }
516
517 s_bInAssert = FALSE;
518 }
519
520 void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg)
521 {
522 ShowAssertDialog(file, line, msg);
523 }
524
525 #endif //WXDEBUG
526