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