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