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