]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
change to /usr/share/wx/version
[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"
e1ee679c 43
a5f1fd3e
VZ
44#if !defined(__WXMSW__) || defined(__WXMICROWIN__)
45 #include <signal.h> // for SIGTRAP used by wxTrap()
46#endif //Win/Unix
47
48#if defined(__WXMSW__)
49 #include "wx/msw/private.h" // includes windows.h for MessageBox()
50#endif
51
d54598dd
VZ
52// ===========================================================================
53// implementation
54// ===========================================================================
55
bf188f1a
VZ
56// ----------------------------------------------------------------------------
57// initialization and termination
58// ----------------------------------------------------------------------------
59
1e6feb95
VZ
60wxAppBase::wxAppBase()
61{
62 wxTheApp = (wxApp *)this;
63
64 // VZ: what's this? is it obsolete?
65 m_wantDebugOutput = FALSE;
66
67#if wxUSE_GUI
68 m_topWindow = (wxWindow *)NULL;
69 m_useBestVisual = FALSE;
70 m_exitOnFrameDelete = TRUE;
71 m_isActive = TRUE;
72#endif // wxUSE_GUI
73}
74
1e6feb95
VZ
75#if wxUSE_GUI
76bool wxAppBase::OnInitGui()
77{
78#ifdef __WXUNIVERSAL__
bf188f1a 79 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
1e6feb95
VZ
80 return FALSE;
81#endif // __WXUNIVERSAL__
82
83 return TRUE;
84}
85#endif // wxUSE_GUI
86
87int wxAppBase::OnExit()
88{
89#if wxUSE_CONFIG
90 // delete the config object if any (don't use Get() here, but Set()
91 // because Get() could create a new config object)
92 delete wxConfigBase::Set((wxConfigBase *) NULL);
93#endif // wxUSE_CONFIG
94
95#ifdef __WXUNIVERSAL__
96 delete wxTheme::Set(NULL);
97#endif // __WXUNIVERSAL__
98
99 return 0;
100}
101
72cdf4c9
VZ
102// ---------------------------------------------------------------------------
103// wxAppBase
104// ----------------------------------------------------------------------------
105
106void wxAppBase::ProcessPendingEvents()
107{
108 // ensure that we're the only thread to modify the pending events list
16c1f79c 109 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
110
111 if ( !wxPendingEvents )
16c1f79c
RR
112 {
113 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 114 return;
16c1f79c 115 }
72cdf4c9
VZ
116
117 // iterate until the list becomes empty
118 wxNode *node = wxPendingEvents->First();
119 while (node)
120 {
121 wxEvtHandler *handler = (wxEvtHandler *)node->Data();
16c1f79c 122 delete node;
72cdf4c9 123
16c1f79c 124 // In ProcessPendingEvents(), new handlers might be add
1d910ac1 125 // and we can safely leave the critical section here.
16c1f79c 126 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 127 handler->ProcessPendingEvents();
16c1f79c 128 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9 129
72cdf4c9
VZ
130 node = wxPendingEvents->First();
131 }
1d910ac1 132
16c1f79c 133 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
72cdf4c9
VZ
134}
135
1e6feb95
VZ
136// ----------------------------------------------------------------------------
137// misc
138// ----------------------------------------------------------------------------
139
140#if wxUSE_GUI
141
6e169cf3 142void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 143{
66dfed9b
VZ
144 if ( active == m_isActive )
145 return;
146
1e6feb95 147 m_isActive = active;
66dfed9b
VZ
148
149 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
150 event.SetEventObject(this);
151
152 (void)ProcessEvent(event);
7beba2fc 153}
1e6feb95
VZ
154
155#endif // wxUSE_GUI
bf188f1a
VZ
156
157// ----------------------------------------------------------------------------
158// cmd line parsing
159// ----------------------------------------------------------------------------
160
161bool wxAppBase::OnInit()
162{
163#if wxUSE_CMDLINE_PARSER
164 wxCmdLineParser parser(argc, argv);
165
166 OnInitCmdLine(parser);
167
168 bool cont;
169 switch ( parser.Parse() )
170 {
171 case -1:
172 cont = OnCmdLineHelp(parser);
173 break;
174
175 case 0:
176 cont = OnCmdLineParsed(parser);
177 break;
178
179 default:
180 cont = OnCmdLineError(parser);
181 break;
182 }
183
184 if ( !cont )
185 return FALSE;
186#endif // wxUSE_CMDLINE_PARSER
187
188 return TRUE;
189}
190
191#if wxUSE_CMDLINE_PARSER
192
193#define OPTION_VERBOSE _T("verbose")
194#define OPTION_THEME _T("theme")
c358c660 195#define OPTION_MODE _T("mode")
bf188f1a
VZ
196
197void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
198{
199 // the standard command line options
200 static const wxCmdLineEntryDesc cmdLineDesc[] =
201 {
202 {
203 wxCMD_LINE_SWITCH,
204 _T("h"),
205 _T("help"),
206 gettext_noop("show this help message"),
207 wxCMD_LINE_VAL_NONE,
208 wxCMD_LINE_OPTION_HELP
209 },
210
211#if wxUSE_LOG
212 {
213 wxCMD_LINE_SWITCH,
214 _T(""),
215 OPTION_VERBOSE,
216 gettext_noop("generate verbose log messages")
217 },
0f02d3d0 218#endif // wxUSE_LOG
bf188f1a
VZ
219
220#ifdef __WXUNIVERSAL__
221 {
222 wxCMD_LINE_OPTION,
223 _T(""),
224 OPTION_THEME,
225 gettext_noop("specify the theme to use"),
226 wxCMD_LINE_VAL_STRING
227 },
228#endif // __WXUNIVERSAL__
229
c358c660
VS
230#if defined(__WXMGL__)
231 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
232 // should provide this option. That's why it is in common/appcmn.cpp
233 // and not mgl/app.cpp
234 {
235 wxCMD_LINE_OPTION,
236 _T(""),
237 OPTION_MODE,
238 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
239 wxCMD_LINE_VAL_STRING
240 },
241#endif // __WXMGL__
242
bf188f1a
VZ
243 // terminator
244 { wxCMD_LINE_NONE }
245 };
246
247 parser.SetDesc(cmdLineDesc);
248}
249
250bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
251{
252#if wxUSE_LOG
253 if ( parser.Found(OPTION_VERBOSE) )
254 {
255 wxLog::SetVerbose(TRUE);
256 }
257#endif // wxUSE_LOG
258
259#ifdef __WXUNIVERSAL__
260 wxString themeName;
261 if ( parser.Found(OPTION_THEME, &themeName) )
262 {
263 wxTheme *theme = wxTheme::Create(themeName);
264 if ( !theme )
265 {
266 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
267
268 return FALSE;
269 }
270
271 wxTheme::Set(theme);
272 }
273#endif // __WXUNIVERSAL__
274
c358c660
VS
275#if defined(__WXMGL__)
276 wxString modeDesc;
277 if ( parser.Found(OPTION_MODE, &modeDesc) )
278 {
279 unsigned w, h, bpp;
280 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
281 {
49e885f8 282 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
c358c660
VS
283
284 return FALSE;
285 }
286
07082b28 287 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
49e885f8 288 return FALSE;
c358c660
VS
289 }
290#endif
291
bf188f1a
VZ
292 return TRUE;
293}
294
295bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
296{
297 parser.Usage();
298
299 return FALSE;
300}
301
302bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
303{
304 parser.Usage();
305
306 return FALSE;
307}
308
309#endif // wxUSE_CMDLINE_PARSER
310
a5f1fd3e
VZ
311// ----------------------------------------------------------------------------
312// debugging support
313// ----------------------------------------------------------------------------
314
315#ifdef __WXDEBUG__
316
317// wxASSERT() helper
318bool wxAssertIsEqual(int x, int y)
319{
320 return x == y;
321}
322
323// break into the debugger
324void wxTrap()
325{
326#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
327 DebugBreak();
c31ad41d 328#elif defined(__WXMAC__) && !defined(__DARWIN__)
a5f1fd3e
VZ
329#if __powerc
330 Debugger();
331#else
332 SysBreak();
333#endif
334#elif defined(__UNIX__)
335 raise(SIGTRAP);
336#else
337 // TODO
338#endif // Win/Unix
339}
340
341// show the assert modal dialog
342static
343void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg)
344{
345 // this variable can be set to true to suppress "assert failure" messages
346 static bool s_bNoAsserts = FALSE;
a5f1fd3e
VZ
347
348 wxChar szBuf[4096];
349
350 // make life easier for people using VC++ IDE: clicking on the message
351 // will take us immediately to the place of the failed assert
352 wxSnprintf(szBuf, WXSIZEOF(szBuf),
353#ifdef __VISUALC__
354 wxT("%s(%d): assert failed"),
355#else // !VC++
356 // make the error message more clear for all the others
357 wxT("Assert failed in file %s at line %d"),
358#endif // VC/!VC
359 szFile, nLine);
360
361 if ( szMsg != NULL )
362 {
363 wxStrcat(szBuf, wxT(": "));
364 wxStrcat(szBuf, szMsg);
365 }
366 else // no message given
367 {
368 wxStrcat(szBuf, wxT("."));
369 }
370
371 if ( !s_bNoAsserts )
372 {
373 // send it to the normal log destination
374 wxLogDebug(szBuf);
375
376#if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
377 // this message is intentionally not translated - it is for
378 // developpers only
379 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
380
381 // use the native message box if available: this is more robust than
382 // using our own
383#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
384 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
385 MB_YESNOCANCEL | MB_ICONSTOP ) )
386 {
387 case IDYES:
388 wxTrap();
389 break;
390
391 case IDCANCEL:
392 s_bNoAsserts = TRUE;
393 break;
394
395 //case IDNO: nothing to do
396 }
397#else // !MSW
398 switch ( wxMessageBox(szBuf, wxT("Debug"),
399 wxYES_NO | wxCANCEL | wxICON_STOP ) )
400 {
401 case wxYES:
402 wxTrap();
403 break;
404
405 case wxCANCEL:
406 s_bNoAsserts = TRUE;
407 break;
408
409 //case wxNO: nothing to do
410 }
411#endif // GUI or MSW
412
413#else // !GUI
414 wxTrap();
415#endif // GUI/!GUI
416 }
a5f1fd3e
VZ
417}
418
419// this function is called when an assert fails
420void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
421{
76456676
VZ
422 // FIXME MT-unsafe
423 static bool s_bInAssert = FALSE;
424
425 if ( s_bInAssert )
426 {
427 // He-e-e-e-elp!! we're trapped in endless loop
428 wxTrap();
429
430 s_bInAssert = FALSE;
431
432 return;
433 }
434
435 s_bInAssert = TRUE;
436
a5f1fd3e
VZ
437 if ( !wxTheApp )
438 {
439 // by default, show the assert dialog box - we can't customize this
440 // behaviour
441 ShowAssertDialog(szFile, nLine, szMsg);
442 }
443 else
444 {
445 // let the app process it as it wants
446 wxTheApp->OnAssert(szFile, nLine, szMsg);
447 }
76456676
VZ
448
449 s_bInAssert = FALSE;
a5f1fd3e
VZ
450}
451
452void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg)
453{
454 ShowAssertDialog(file, line, msg);
455}
456
457#endif //WXDEBUG
458