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