]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/appcmn.cpp
compilation fixes for wxUniv (moved wxDlgProc back to toplevel.cpp)
[wxWidgets.git] / src / common / appcmn.cpp
... / ...
CommitLineData
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
75wxAppBase::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
95wxAppBase::~wxAppBase()
96{
97 // this destructor is required for Darwin
98}
99
100#if wxUSE_GUI
101bool 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
115int 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
134void 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
170void 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
185int 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
195bool 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
231void 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
295bool 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
340bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
341{
342 parser.Usage();
343
344 return FALSE;
345}
346
347bool 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 */
361bool 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
390static 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
402bool wxAssertIsEqual(int x, int y)
403{
404 return x == y;
405}
406
407// break into the debugger
408void 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
426static
427void ShowAssertDialog(const wxChar *szFile,
428 int nLine,
429 const wxChar *szCond,
430 const wxChar *szMsg)
431{
432 // this variable can be set to true to suppress "assert failure" messages
433 static bool s_bNoAsserts = FALSE;
434
435 wxChar szBuf[4096];
436
437 // make life easier for people using VC++ IDE by using this format: like
438 // this, clicking on the message will take us immediately to the place of
439 // the failed assert
440 wxSnprintf(szBuf, WXSIZEOF(szBuf),
441 wxT("%s(%d): assert \"%s\" failed"),
442 szFile, nLine, szCond);
443
444 if ( szMsg != NULL )
445 {
446 wxStrcat(szBuf, wxT(": "));
447 wxStrcat(szBuf, szMsg);
448 }
449 else // no message given
450 {
451 wxStrcat(szBuf, wxT("."));
452 }
453
454 if ( !s_bNoAsserts )
455 {
456 // send it to the normal log destination
457 wxLogDebug(szBuf);
458
459#if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
460 // this message is intentionally not translated - it is for
461 // developpers only
462 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
463
464 // use the native message box if available: this is more robust than
465 // using our own
466#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
467 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
468 MB_YESNOCANCEL | MB_ICONSTOP ) )
469 {
470 case IDYES:
471 wxTrap();
472 break;
473
474 case IDCANCEL:
475 s_bNoAsserts = TRUE;
476 break;
477
478 //case IDNO: nothing to do
479 }
480#else // !MSW
481 switch ( wxMessageBox(szBuf, wxT("Debug"),
482 wxYES_NO | wxCANCEL | wxICON_STOP ) )
483 {
484 case wxYES:
485 wxTrap();
486 break;
487
488 case wxCANCEL:
489 s_bNoAsserts = TRUE;
490 break;
491
492 //case wxNO: nothing to do
493 }
494#endif // GUI or MSW
495
496#else // !GUI
497 wxTrap();
498#endif // GUI/!GUI
499 }
500}
501
502// this function is called when an assert fails
503void wxOnAssert(const wxChar *szFile,
504 int nLine,
505 const wxChar *szCond,
506 const wxChar *szMsg)
507{
508 // FIXME MT-unsafe
509 static bool s_bInAssert = FALSE;
510
511 if ( s_bInAssert )
512 {
513 // He-e-e-e-elp!! we're trapped in endless loop
514 wxTrap();
515
516 s_bInAssert = FALSE;
517
518 return;
519 }
520
521 s_bInAssert = TRUE;
522
523 if ( !wxTheApp )
524 {
525 // by default, show the assert dialog box - we can't customize this
526 // behaviour
527 ShowAssertDialog(szFile, nLine, szCond, szMsg);
528 }
529 else
530 {
531 // let the app process it as it wants
532 wxTheApp->OnAssert(szFile, nLine, szCond, szMsg);
533 }
534
535 s_bInAssert = FALSE;
536}
537
538void wxAppBase::OnAssert(const wxChar *file,
539 int line,
540 const wxChar *cond,
541 const wxChar *msg)
542{
543 ShowAssertDialog(file, line, cond, msg);
544}
545
546#endif //WXDEBUG
547