]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
Don't call a function that might do drawing when setting a null image list
[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 int wxAppBase::FilterEvent(wxEvent& WXUNUSED(event))
186 {
187 // process the events normally by default
188 return -1;
189 }
190
191 // ----------------------------------------------------------------------------
192 // cmd line parsing
193 // ----------------------------------------------------------------------------
194
195 bool wxAppBase::OnInit()
196 {
197 #if wxUSE_CMDLINE_PARSER
198 wxCmdLineParser parser(argc, argv);
199
200 OnInitCmdLine(parser);
201
202 bool cont;
203 switch ( parser.Parse(FALSE /* don't show usage */) )
204 {
205 case -1:
206 cont = OnCmdLineHelp(parser);
207 break;
208
209 case 0:
210 cont = OnCmdLineParsed(parser);
211 break;
212
213 default:
214 cont = OnCmdLineError(parser);
215 break;
216 }
217
218 if ( !cont )
219 return FALSE;
220 #endif // wxUSE_CMDLINE_PARSER
221
222 return TRUE;
223 }
224
225 #if wxUSE_CMDLINE_PARSER
226
227 #define OPTION_VERBOSE _T("verbose")
228 #define OPTION_THEME _T("theme")
229 #define OPTION_MODE _T("mode")
230
231 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
232 {
233 // the standard command line options
234 static const wxCmdLineEntryDesc cmdLineDesc[] =
235 {
236 {
237 wxCMD_LINE_SWITCH,
238 _T("h"),
239 _T("help"),
240 gettext_noop("show this help message"),
241 wxCMD_LINE_VAL_NONE,
242 wxCMD_LINE_OPTION_HELP
243 },
244
245 #if wxUSE_LOG
246 {
247 wxCMD_LINE_SWITCH,
248 _T(""),
249 OPTION_VERBOSE,
250 gettext_noop("generate verbose log messages"),
251 wxCMD_LINE_VAL_NONE,
252 0x0
253 },
254 #endif // wxUSE_LOG
255
256 #ifdef __WXUNIVERSAL__
257 {
258 wxCMD_LINE_OPTION,
259 _T(""),
260 OPTION_THEME,
261 gettext_noop("specify the theme to use"),
262 wxCMD_LINE_VAL_STRING,
263 0x0
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 0x0
278 },
279 #endif // __WXMGL__
280
281 // terminator
282 {
283 wxCMD_LINE_NONE,
284 _T(""),
285 _T(""),
286 _T(""),
287 wxCMD_LINE_VAL_NONE,
288 0x0
289 }
290 };
291
292 parser.SetDesc(cmdLineDesc);
293 }
294
295 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
296 {
297 #if wxUSE_LOG
298 if ( parser.Found(OPTION_VERBOSE) )
299 {
300 wxLog::SetVerbose(TRUE);
301 }
302 #endif // wxUSE_LOG
303
304 #ifdef __WXUNIVERSAL__
305 wxString themeName;
306 if ( parser.Found(OPTION_THEME, &themeName) )
307 {
308 wxTheme *theme = wxTheme::Create(themeName);
309 if ( !theme )
310 {
311 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
312
313 return FALSE;
314 }
315
316 wxTheme::Set(theme);
317 }
318 #endif // __WXUNIVERSAL__
319
320 #if defined(__WXMGL__)
321 wxString modeDesc;
322 if ( parser.Found(OPTION_MODE, &modeDesc) )
323 {
324 unsigned w, h, bpp;
325 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
326 {
327 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
328
329 return FALSE;
330 }
331
332 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
333 return FALSE;
334 }
335 #endif // __WXMGL__
336
337 return TRUE;
338 }
339
340 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
341 {
342 parser.Usage();
343
344 return FALSE;
345 }
346
347 bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
348 {
349 parser.Usage();
350
351 return FALSE;
352 }
353
354 #endif // wxUSE_CMDLINE_PARSER
355
356 // ----------------------------------------------------------------------------
357 // debugging support
358 // ----------------------------------------------------------------------------
359
360 /* static */
361 bool wxAppBase::CheckBuildOptions(const wxBuildOptions& opts)
362 {
363 #define wxCMP(what) (what == opts.m_ ## what)
364
365 bool
366 #ifdef __WXDEBUG__
367 isDebug = TRUE;
368 #else
369 isDebug = FALSE;
370 #endif
371
372 int verMaj = wxMAJOR_VERSION,
373 verMin = wxMINOR_VERSION;
374
375 if ( !(wxCMP(isDebug) && wxCMP(verMaj) && wxCMP(verMin)) )
376 {
377 wxLogFatalError(_T("Mismatch between the program and library build ")
378 _T("versions detected."));
379
380 // normally wxLogFatalError doesn't return
381 return FALSE;
382 }
383 #undef wxCMP
384
385 return TRUE;
386 }
387
388 #ifdef __WXDEBUG__
389
390 static void LINKAGEMODE SetTraceMasks()
391 {
392 wxString mask;
393 if ( wxGetEnv(wxT("WXTRACE"), &mask) )
394 {
395 wxStringTokenizer tkn(mask, wxT(","));
396 while ( tkn.HasMoreTokens() )
397 wxLog::AddTraceMask(tkn.GetNextToken());
398 }
399 }
400
401 // wxASSERT() helper
402 bool wxAssertIsEqual(int x, int y)
403 {
404 return x == y;
405 }
406
407 // break into the debugger
408 void wxTrap()
409 {
410 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
411 DebugBreak();
412 #elif defined(__WXMAC__) && !defined(__DARWIN__)
413 #if __powerc
414 Debugger();
415 #else
416 SysBreak();
417 #endif
418 #elif defined(__UNIX__)
419 raise(SIGTRAP);
420 #else
421 // TODO
422 #endif // Win/Unix
423 }
424
425 // show the assert modal dialog
426 static
427 void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg)
428 {
429 // this variable can be set to true to suppress "assert failure" messages
430 static bool s_bNoAsserts = FALSE;
431
432 wxChar szBuf[4096];
433
434 // make life easier for people using VC++ IDE: clicking on the message
435 // will take us immediately to the place of the failed assert
436 wxSnprintf(szBuf, WXSIZEOF(szBuf),
437 #ifdef __VISUALC__
438 wxT("%s(%d): assert failed"),
439 #else // !VC++
440 // make the error message more clear for all the others
441 wxT("Assert failed in file %s at line %d"),
442 #endif // VC/!VC
443 szFile, nLine);
444
445 if ( szMsg != NULL )
446 {
447 wxStrcat(szBuf, wxT(": "));
448 wxStrcat(szBuf, szMsg);
449 }
450 else // no message given
451 {
452 wxStrcat(szBuf, wxT("."));
453 }
454
455 if ( !s_bNoAsserts )
456 {
457 // send it to the normal log destination
458 wxLogDebug(szBuf);
459
460 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
461 // this message is intentionally not translated - it is for
462 // developpers only
463 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
464
465 // use the native message box if available: this is more robust than
466 // using our own
467 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
468 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
469 MB_YESNOCANCEL | MB_ICONSTOP ) )
470 {
471 case IDYES:
472 wxTrap();
473 break;
474
475 case IDCANCEL:
476 s_bNoAsserts = TRUE;
477 break;
478
479 //case IDNO: nothing to do
480 }
481 #else // !MSW
482 switch ( wxMessageBox(szBuf, wxT("Debug"),
483 wxYES_NO | wxCANCEL | wxICON_STOP ) )
484 {
485 case wxYES:
486 wxTrap();
487 break;
488
489 case wxCANCEL:
490 s_bNoAsserts = TRUE;
491 break;
492
493 //case wxNO: nothing to do
494 }
495 #endif // GUI or MSW
496
497 #else // !GUI
498 wxTrap();
499 #endif // GUI/!GUI
500 }
501 }
502
503 // this function is called when an assert fails
504 void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
505 {
506 // FIXME MT-unsafe
507 static bool s_bInAssert = FALSE;
508
509 if ( s_bInAssert )
510 {
511 // He-e-e-e-elp!! we're trapped in endless loop
512 wxTrap();
513
514 s_bInAssert = FALSE;
515
516 return;
517 }
518
519 s_bInAssert = TRUE;
520
521 if ( !wxTheApp )
522 {
523 // by default, show the assert dialog box - we can't customize this
524 // behaviour
525 ShowAssertDialog(szFile, nLine, szMsg);
526 }
527 else
528 {
529 // let the app process it as it wants
530 wxTheApp->OnAssert(szFile, nLine, szMsg);
531 }
532
533 s_bInAssert = FALSE;
534 }
535
536 void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg)
537 {
538 ShowAssertDialog(file, line, msg);
539 }
540
541 #endif //WXDEBUG
542