]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
implemented menu drawing in the GTK theme
[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 #if WXWIN_COMPATIBILITY_2_2
84 m_wantDebugOutput = FALSE;
85 #endif // WXWIN_COMPATIBILITY_2_2
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
93
94 #ifdef __WXDEBUG__
95 SetTraceMasks();
96 #endif
97 }
98
99 wxAppBase::~wxAppBase()
100 {
101 // this destructor is required for Darwin
102 }
103
104 #if wxUSE_GUI
105 bool wxAppBase::OnInitGui()
106 {
107 #ifdef __WXUNIVERSAL__
108 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
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
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 // ----------------------------------------------------------------------------
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;
198 switch ( parser.Parse(FALSE /* don't show usage */) )
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")
224 #define OPTION_MODE _T("mode")
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 },
247 #endif // wxUSE_LOG
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
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
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
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 {
311 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
312
313 return FALSE;
314 }
315
316 if ( !SetDisplayMode(wxDisplayModeInfo(w, h, bpp)) )
317 return FALSE;
318 }
319 #endif // __WXMGL__
320
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
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();
357 #elif defined(__WXMAC__) && !defined(__DARWIN__)
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;
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 }
446 }
447
448 // this function is called when an assert fails
449 void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
450 {
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
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 }
477
478 s_bInAssert = FALSE;
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