]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
fixed memory leak in wxDocManager::CreateDocument (patch 494842)
[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 if ( active == m_isActive )
145 return;
146
147 m_isActive = active;
148
149 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
150 event.SetEventObject(this);
151
152 (void)ProcessEvent(event);
153 }
154
155 #endif // wxUSE_GUI
156
157 // ----------------------------------------------------------------------------
158 // cmd line parsing
159 // ----------------------------------------------------------------------------
160
161 bool wxAppBase::OnInit()
162 {
163 #if wxUSE_CMDLINE_PARSER
164 wxCmdLineParser parser(argc, argv);
165
166 OnInitCmdLine(parser);
167
168 bool cont;
169 switch ( parser.Parse(FALSE /* don't show usage */) )
170 {
171 case -1:
172 cont = OnCmdLineHelp(parser);
173 break;
174
175 case 0:
176 cont = OnCmdLineParsed(parser);
177 break;
178
179 default:
180 cont = OnCmdLineError(parser);
181 break;
182 }
183
184 if ( !cont )
185 return FALSE;
186 #endif // wxUSE_CMDLINE_PARSER
187
188 return TRUE;
189 }
190
191 #if wxUSE_CMDLINE_PARSER
192
193 #define OPTION_VERBOSE _T("verbose")
194 #define OPTION_THEME _T("theme")
195 #define OPTION_MODE _T("mode")
196
197 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
198 {
199 // the standard command line options
200 static const wxCmdLineEntryDesc cmdLineDesc[] =
201 {
202 {
203 wxCMD_LINE_SWITCH,
204 _T("h"),
205 _T("help"),
206 gettext_noop("show this help message"),
207 wxCMD_LINE_VAL_NONE,
208 wxCMD_LINE_OPTION_HELP
209 },
210
211 #if wxUSE_LOG
212 {
213 wxCMD_LINE_SWITCH,
214 _T(""),
215 OPTION_VERBOSE,
216 gettext_noop("generate verbose log messages")
217 },
218 #endif // wxUSE_LOG
219
220 #ifdef __WXUNIVERSAL__
221 {
222 wxCMD_LINE_OPTION,
223 _T(""),
224 OPTION_THEME,
225 gettext_noop("specify the theme to use"),
226 wxCMD_LINE_VAL_STRING
227 },
228 #endif // __WXUNIVERSAL__
229
230 #if defined(__WXMGL__)
231 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
232 // should provide this option. That's why it is in common/appcmn.cpp
233 // and not mgl/app.cpp
234 {
235 wxCMD_LINE_OPTION,
236 _T(""),
237 OPTION_MODE,
238 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
239 wxCMD_LINE_VAL_STRING
240 },
241 #endif // __WXMGL__
242
243 // terminator
244 { wxCMD_LINE_NONE }
245 };
246
247 parser.SetDesc(cmdLineDesc);
248 }
249
250 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
251 {
252 #if wxUSE_LOG
253 if ( parser.Found(OPTION_VERBOSE) )
254 {
255 wxLog::SetVerbose(TRUE);
256 }
257 #endif // wxUSE_LOG
258
259 #ifdef __WXUNIVERSAL__
260 wxString themeName;
261 if ( parser.Found(OPTION_THEME, &themeName) )
262 {
263 wxTheme *theme = wxTheme::Create(themeName);
264 if ( !theme )
265 {
266 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
267
268 return FALSE;
269 }
270
271 wxTheme::Set(theme);
272 }
273 #endif // __WXUNIVERSAL__
274
275 #if defined(__WXMGL__)
276 wxString modeDesc;
277 if ( parser.Found(OPTION_MODE, &modeDesc) )
278 {
279 unsigned w, h, bpp;
280 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
281 {
282 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
283
284 return FALSE;
285 }
286
287 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
288 return FALSE;
289 }
290 #endif // __WXMGL__
291
292 return TRUE;
293 }
294
295 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
296 {
297 parser.Usage();
298
299 return FALSE;
300 }
301
302 bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
303 {
304 parser.Usage();
305
306 return FALSE;
307 }
308
309 #endif // wxUSE_CMDLINE_PARSER
310
311 // ----------------------------------------------------------------------------
312 // debugging support
313 // ----------------------------------------------------------------------------
314
315 #ifdef __WXDEBUG__
316
317 // wxASSERT() helper
318 bool wxAssertIsEqual(int x, int y)
319 {
320 return x == y;
321 }
322
323 // break into the debugger
324 void wxTrap()
325 {
326 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
327 DebugBreak();
328 #elif defined(__WXMAC__) && !defined(__DARWIN__)
329 #if __powerc
330 Debugger();
331 #else
332 SysBreak();
333 #endif
334 #elif defined(__UNIX__)
335 raise(SIGTRAP);
336 #else
337 // TODO
338 #endif // Win/Unix
339 }
340
341 // show the assert modal dialog
342 static
343 void ShowAssertDialog(const wxChar *szFile, int nLine, const wxChar *szMsg)
344 {
345 // this variable can be set to true to suppress "assert failure" messages
346 static bool s_bNoAsserts = FALSE;
347
348 wxChar szBuf[4096];
349
350 // make life easier for people using VC++ IDE: clicking on the message
351 // will take us immediately to the place of the failed assert
352 wxSnprintf(szBuf, WXSIZEOF(szBuf),
353 #ifdef __VISUALC__
354 wxT("%s(%d): assert failed"),
355 #else // !VC++
356 // make the error message more clear for all the others
357 wxT("Assert failed in file %s at line %d"),
358 #endif // VC/!VC
359 szFile, nLine);
360
361 if ( szMsg != NULL )
362 {
363 wxStrcat(szBuf, wxT(": "));
364 wxStrcat(szBuf, szMsg);
365 }
366 else // no message given
367 {
368 wxStrcat(szBuf, wxT("."));
369 }
370
371 if ( !s_bNoAsserts )
372 {
373 // send it to the normal log destination
374 wxLogDebug(szBuf);
375
376 #if (wxUSE_GUI && wxUSE_MSGDLG) || defined(__WXMSW__)
377 // this message is intentionally not translated - it is for
378 // developpers only
379 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
380
381 // use the native message box if available: this is more robust than
382 // using our own
383 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
384 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
385 MB_YESNOCANCEL | MB_ICONSTOP ) )
386 {
387 case IDYES:
388 wxTrap();
389 break;
390
391 case IDCANCEL:
392 s_bNoAsserts = TRUE;
393 break;
394
395 //case IDNO: nothing to do
396 }
397 #else // !MSW
398 switch ( wxMessageBox(szBuf, wxT("Debug"),
399 wxYES_NO | wxCANCEL | wxICON_STOP ) )
400 {
401 case wxYES:
402 wxTrap();
403 break;
404
405 case wxCANCEL:
406 s_bNoAsserts = TRUE;
407 break;
408
409 //case wxNO: nothing to do
410 }
411 #endif // GUI or MSW
412
413 #else // !GUI
414 wxTrap();
415 #endif // GUI/!GUI
416 }
417 }
418
419 // this function is called when an assert fails
420 void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
421 {
422 // FIXME MT-unsafe
423 static bool s_bInAssert = FALSE;
424
425 if ( s_bInAssert )
426 {
427 // He-e-e-e-elp!! we're trapped in endless loop
428 wxTrap();
429
430 s_bInAssert = FALSE;
431
432 return;
433 }
434
435 s_bInAssert = TRUE;
436
437 if ( !wxTheApp )
438 {
439 // by default, show the assert dialog box - we can't customize this
440 // behaviour
441 ShowAssertDialog(szFile, nLine, szMsg);
442 }
443 else
444 {
445 // let the app process it as it wants
446 wxTheApp->OnAssert(szFile, nLine, szMsg);
447 }
448
449 s_bInAssert = FALSE;
450 }
451
452 void wxAppBase::OnAssert(const wxChar *file, int line, const wxChar *msg)
453 {
454 ShowAssertDialog(file, line, msg);
455 }
456
457 #endif //WXDEBUG
458