]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
controls sample is now able to use default command line arguments
[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 {
282 wxLogError(_("Unsupported display mode '%s'."), themeName.c_str());
283
284 return FALSE;
285 }
286
287 SetDisplayMode(wxDisplayModeInfo(wxSize(w, h), bpp));
288 }
289#endif
290
bf188f1a
VZ
291 return TRUE;
292}
293
294bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
295{
296 parser.Usage();
297
298 return FALSE;
299}
300
301bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
302{
303 parser.Usage();
304
305 return FALSE;
306}
307
308#endif // wxUSE_CMDLINE_PARSER
309
a5f1fd3e
VZ
310// ----------------------------------------------------------------------------
311// debugging support
312// ----------------------------------------------------------------------------
313
314#ifdef __WXDEBUG__
315
316// wxASSERT() helper
317bool wxAssertIsEqual(int x, int y)
318{
319 return x == y;
320}
321
322// break into the debugger
323void wxTrap()
324{
325#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
326 DebugBreak();
327#elif defined(__WXMAC__)
328#if __powerc
329 Debugger();
330#else
331 SysBreak();
332#endif
333#elif defined(__UNIX__)
334 raise(SIGTRAP);
335#else
336 // TODO
337#endif // Win/Unix
338}
339
340// show the assert modal dialog
341static
342void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg)
343{
344 // this variable can be set to true to suppress "assert failure" messages
345 static bool s_bNoAsserts = FALSE;
a5f1fd3e
VZ
346
347 wxChar szBuf[4096];
348
349 // make life easier for people using VC++ IDE: clicking on the message
350 // will take us immediately to the place of the failed assert
351 wxSnprintf(szBuf, WXSIZEOF(szBuf),
352#ifdef __VISUALC__
353 wxT("%s(%d): assert failed"),
354#else // !VC++
355 // make the error message more clear for all the others
356 wxT("Assert failed in file %s at line %d"),
357#endif // VC/!VC
358 szFile, nLine);
359
360 if ( szMsg != NULL )
361 {
362 wxStrcat(szBuf, wxT(": "));
363 wxStrcat(szBuf, szMsg);
364 }
365 else // no message given
366 {
367 wxStrcat(szBuf, wxT("."));
368 }
369
370 if ( !s_bNoAsserts )
371 {
372 // send it to the normal log destination
373 wxLogDebug(szBuf);
374
375#if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
376 // this message is intentionally not translated - it is for
377 // developpers only
378 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
379
380 // use the native message box if available: this is more robust than
381 // using our own
382#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
383 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
384 MB_YESNOCANCEL | MB_ICONSTOP ) )
385 {
386 case IDYES:
387 wxTrap();
388 break;
389
390 case IDCANCEL:
391 s_bNoAsserts = TRUE;
392 break;
393
394 //case IDNO: nothing to do
395 }
396#else // !MSW
397 switch ( wxMessageBox(szBuf, wxT("Debug"),
398 wxYES_NO | wxCANCEL | wxICON_STOP ) )
399 {
400 case wxYES:
401 wxTrap();
402 break;
403
404 case wxCANCEL:
405 s_bNoAsserts = TRUE;
406 break;
407
408 //case wxNO: nothing to do
409 }
410#endif // GUI or MSW
411
412#else // !GUI
413 wxTrap();
414#endif // GUI/!GUI
415 }
a5f1fd3e
VZ
416}
417
418// this function is called when an assert fails
419void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
420{
76456676
VZ
421 // FIXME MT-unsafe
422 static bool s_bInAssert = FALSE;
423
424 if ( s_bInAssert )
425 {
426 // He-e-e-e-elp!! we're trapped in endless loop
427 wxTrap();
428
429 s_bInAssert = FALSE;
430
431 return;
432 }
433
434 s_bInAssert = TRUE;
435
a5f1fd3e
VZ
436 if ( !wxTheApp )
437 {
438 // by default, show the assert dialog box - we can't customize this
439 // behaviour
440 ShowAssertDialog(szFile, nLine, szMsg);
441 }
442 else
443 {
444 // let the app process it as it wants
445 wxTheApp->OnAssert(szFile, nLine, szMsg);
446 }
76456676
VZ
447
448 s_bInAssert = FALSE;
a5f1fd3e
VZ
449}
450
451void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg)
452{
453 ShowAssertDialog(file, line, msg);
454}
455
456#endif //WXDEBUG
457