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