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