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