]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
Some doc tweaks
[wxWidgets.git] / src / common / appcmn.cpp
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"
33 #include "wx/intl.h"
34 #include "wx/list.h"
35 #if wxUSE_GUI
36 #include "wx/msgdlg.h"
37 #endif // wxUSE_GUI
38 #endif
39
40 #include "wx/cmdline.h"
41 #include "wx/thread.h"
42 #include "wx/confbase.h"
43 #include "wx/tokenzr.h"
44 #include "wx/utils.h"
45
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
54 #if defined(__WXMAC__)
55 #include "wx/mac/private.h" // includes mac headers
56 #endif
57
58 // ===========================================================================
59 // implementation
60 // ===========================================================================
61
62 // ----------------------------------------------------------------------------
63 // initialization and termination
64 // ----------------------------------------------------------------------------
65
66 #ifdef __WXDEBUG__
67 static 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
79 wxAppBase::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
92
93 #ifdef __WXDEBUG__
94 SetTraceMasks();
95 #endif
96 }
97
98 wxAppBase::~wxAppBase()
99 {
100 // this destructor is required for Darwin
101 }
102
103 #if wxUSE_GUI
104 bool wxAppBase::OnInitGui()
105 {
106 #ifdef __WXUNIVERSAL__
107 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
108 return FALSE;
109 #endif // __WXUNIVERSAL__
110
111 return TRUE;
112 }
113 #endif // wxUSE_GUI
114
115 int 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
130 // ---------------------------------------------------------------------------
131 // wxAppBase
132 // ----------------------------------------------------------------------------
133
134 void wxAppBase::ProcessPendingEvents()
135 {
136 // ensure that we're the only thread to modify the pending events list
137 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
138
139 if ( !wxPendingEvents )
140 {
141 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
142 return;
143 }
144
145 // iterate until the list becomes empty
146 wxNode *node = wxPendingEvents->First();
147 while (node)
148 {
149 wxEvtHandler *handler = (wxEvtHandler *)node->Data();
150 delete node;
151
152 // In ProcessPendingEvents(), new handlers might be add
153 // and we can safely leave the critical section here.
154 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
155 handler->ProcessPendingEvents();
156 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
157
158 node = wxPendingEvents->First();
159 }
160
161 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
162 }
163
164 // ----------------------------------------------------------------------------
165 // misc
166 // ----------------------------------------------------------------------------
167
168 #if wxUSE_GUI
169
170 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
171 {
172 if ( active == m_isActive )
173 return;
174
175 m_isActive = active;
176
177 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
178 event.SetEventObject(this);
179
180 (void)ProcessEvent(event);
181 }
182
183 #endif // wxUSE_GUI
184
185 // ----------------------------------------------------------------------------
186 // cmd line parsing
187 // ----------------------------------------------------------------------------
188
189 bool wxAppBase::OnInit()
190 {
191 #if wxUSE_CMDLINE_PARSER
192 wxCmdLineParser parser(argc, argv);
193
194 OnInitCmdLine(parser);
195
196 bool cont;
197 switch ( parser.Parse(FALSE /* don't show usage */) )
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")
223 #define OPTION_MODE _T("mode")
224
225 void 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 },
246 #endif // wxUSE_LOG
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
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
271 // terminator
272 { wxCMD_LINE_NONE }
273 };
274
275 parser.SetDesc(cmdLineDesc);
276 }
277
278 bool 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
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 {
310 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
311
312 return FALSE;
313 }
314
315 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
316 return FALSE;
317 }
318 #endif // __WXMGL__
319
320 return TRUE;
321 }
322
323 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
324 {
325 parser.Usage();
326
327 return FALSE;
328 }
329
330 bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
331 {
332 parser.Usage();
333
334 return FALSE;
335 }
336
337 #endif // wxUSE_CMDLINE_PARSER
338
339 // ----------------------------------------------------------------------------
340 // debugging support
341 // ----------------------------------------------------------------------------
342
343 #ifdef __WXDEBUG__
344
345 // wxASSERT() helper
346 bool wxAssertIsEqual(int x, int y)
347 {
348 return x == y;
349 }
350
351 // break into the debugger
352 void wxTrap()
353 {
354 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
355 DebugBreak();
356 #elif defined(__WXMAC__) && !defined(__DARWIN__)
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
370 static
371 void 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;
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 }
445 }
446
447 // this function is called when an assert fails
448 void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
449 {
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
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 }
476
477 s_bInAssert = FALSE;
478 }
479
480 void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg)
481 {
482 ShowAssertDialog(file, line, msg);
483 }
484
485 #endif //WXDEBUG
486