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