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