]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
correct test for wxDEBUG_LEVEL in last commit
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if defined(__BORLANDC__)
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/window.h"
30 #include "wx/bitmap.h"
31 #include "wx/log.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
34 #include "wx/utils.h"
35 #include "wx/wxcrtvararg.h"
36 #endif
37
38 #include "wx/apptrait.h"
39 #include "wx/cmdline.h"
40 #include "wx/msgout.h"
41 #include "wx/thread.h"
42 #include "wx/vidmode.h"
43 #include "wx/evtloop.h"
44
45 #if wxUSE_FONTMAP
46 #include "wx/fontmap.h"
47 #endif // wxUSE_FONTMAP
48
49 // DLL options compatibility check:
50 #include "wx/build.h"
51 WX_CHECK_BUILD_OPTIONS("wxCore")
52
53 WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete;
54
55 // ============================================================================
56 // wxAppBase implementation
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // initialization
61 // ----------------------------------------------------------------------------
62
63 wxAppBase::wxAppBase()
64 {
65 m_topWindow = NULL;
66
67 m_useBestVisual = false;
68 m_forceTrueColour = false;
69
70 m_isActive = true;
71
72 // We don't want to exit the app if the user code shows a dialog from its
73 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
74 // to Yes initially as this dialog would be the last top level window.
75 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
76 // when we enter our OnRun() because we do want the default behaviour from
77 // then on. But this would be a problem if the user code calls
78 // SetExitOnFrameDelete(false) from OnInit().
79 //
80 // So we use the special "Later" value which is such that
81 // GetExitOnFrameDelete() returns false for it but which we know we can
82 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
83 // call) overwrite in OnRun()
84 m_exitOnFrameDelete = Later;
85 }
86
87 bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
88 {
89 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
90 return false;
91
92 wxInitializeStockLists();
93
94 wxBitmap::InitStandardHandlers();
95
96 // for compatibility call the old initialization function too
97 if ( !OnInitGui() )
98 return false;
99
100 return true;
101 }
102
103 // ----------------------------------------------------------------------------
104 // cleanup
105 // ----------------------------------------------------------------------------
106
107 wxAppBase::~wxAppBase()
108 {
109 // this destructor is required for Darwin
110 }
111
112 void wxAppBase::CleanUp()
113 {
114 // clean up all the pending objects
115 DeletePendingObjects();
116
117 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
118 // when destroyed, so iterate until none are left)
119 while ( !wxTopLevelWindows.empty() )
120 {
121 // do not use Destroy() here as it only puts the TLW in pending list
122 // but we want to delete them now
123 delete wxTopLevelWindows.GetFirst()->GetData();
124 }
125
126 // undo everything we did in Initialize() above
127 wxBitmap::CleanUpHandlers();
128
129 wxStockGDI::DeleteAll();
130
131 wxDeleteStockLists();
132
133 delete wxTheColourDatabase;
134 wxTheColourDatabase = NULL;
135
136 wxAppConsole::CleanUp();
137 }
138
139 // ----------------------------------------------------------------------------
140 // various accessors
141 // ----------------------------------------------------------------------------
142
143 wxWindow* wxAppBase::GetTopWindow() const
144 {
145 wxWindow* window = m_topWindow;
146 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
147 window = wxTopLevelWindows.GetFirst()->GetData();
148 return window;
149 }
150
151 wxVideoMode wxAppBase::GetDisplayMode() const
152 {
153 return wxVideoMode();
154 }
155
156 wxLayoutDirection wxAppBase::GetLayoutDirection() const
157 {
158 #if wxUSE_INTL
159 const wxLocale *const locale = wxGetLocale();
160 if ( locale )
161 {
162 const wxLanguageInfo *const
163 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
164
165 if ( info )
166 return info->LayoutDirection;
167 }
168 #endif // wxUSE_INTL
169
170 // we don't know
171 return wxLayout_Default;
172 }
173
174 #if wxUSE_CMDLINE_PARSER
175
176 // ----------------------------------------------------------------------------
177 // GUI-specific command line options handling
178 // ----------------------------------------------------------------------------
179
180 #define OPTION_THEME "theme"
181 #define OPTION_MODE "mode"
182
183 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
184 {
185 // first add the standard non GUI options
186 wxAppConsole::OnInitCmdLine(parser);
187
188 // the standard command line options
189 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
190 {
191 #ifdef __WXUNIVERSAL__
192 {
193 wxCMD_LINE_OPTION,
194 NULL,
195 OPTION_THEME,
196 gettext_noop("specify the theme to use"),
197 wxCMD_LINE_VAL_STRING,
198 0x0
199 },
200 #endif // __WXUNIVERSAL__
201
202 #if defined(__WXMGL__)
203 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
204 // should provide this option. That's why it is in common/appcmn.cpp
205 // and not mgl/app.cpp
206 {
207 wxCMD_LINE_OPTION,
208 NULL,
209 OPTION_MODE,
210 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
211 wxCMD_LINE_VAL_STRING,
212 0x0
213 },
214 #endif // __WXMGL__
215
216 // terminator
217 wxCMD_LINE_DESC_END
218 };
219
220 parser.SetDesc(cmdLineGUIDesc);
221 }
222
223 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
224 {
225 #ifdef __WXUNIVERSAL__
226 wxString themeName;
227 if ( parser.Found(OPTION_THEME, &themeName) )
228 {
229 wxTheme *theme = wxTheme::Create(themeName);
230 if ( !theme )
231 {
232 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
233 return false;
234 }
235
236 // Delete the defaultly created theme and set the new theme.
237 delete wxTheme::Get();
238 wxTheme::Set(theme);
239 }
240 #endif // __WXUNIVERSAL__
241
242 #if defined(__WXMGL__)
243 wxString modeDesc;
244 if ( parser.Found(OPTION_MODE, &modeDesc) )
245 {
246 unsigned w, h, bpp;
247 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
248 {
249 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
250 return false;
251 }
252
253 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
254 return false;
255 }
256 #endif // __WXMGL__
257
258 return wxAppConsole::OnCmdLineParsed(parser);
259 }
260
261 #endif // wxUSE_CMDLINE_PARSER
262
263 // ----------------------------------------------------------------------------
264 // OnXXX() hooks
265 // ----------------------------------------------------------------------------
266
267 bool wxAppBase::OnInitGui()
268 {
269 #ifdef __WXUNIVERSAL__
270 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
271 return false;
272 #endif // __WXUNIVERSAL__
273
274 return true;
275 }
276
277 int wxAppBase::OnRun()
278 {
279 // see the comment in ctor: if the initial value hasn't been changed, use
280 // the default Yes from now on
281 if ( m_exitOnFrameDelete == Later )
282 {
283 m_exitOnFrameDelete = Yes;
284 }
285 //else: it has been changed, assume the user knows what he is doing
286
287 return wxAppConsole::OnRun();
288 }
289
290 int wxAppBase::OnExit()
291 {
292 #ifdef __WXUNIVERSAL__
293 delete wxTheme::Set(NULL);
294 #endif // __WXUNIVERSAL__
295
296 return wxAppConsole::OnExit();
297 }
298
299 wxAppTraits *wxAppBase::CreateTraits()
300 {
301 return new wxGUIAppTraits;
302 }
303
304 // ----------------------------------------------------------------------------
305 // misc
306 // ----------------------------------------------------------------------------
307
308 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
309 {
310 if ( active == m_isActive )
311 return;
312
313 m_isActive = active;
314
315 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
316 event.SetEventObject(this);
317
318 (void)ProcessEvent(event);
319 }
320
321 bool wxAppBase::SafeYield(wxWindow *win, bool onlyIfNeeded)
322 {
323 wxWindowDisabler wd(win);
324
325 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
326
327 return loop && loop->Yield(onlyIfNeeded);
328 }
329
330 bool wxAppBase::SafeYieldFor(wxWindow *win, long eventsToProcess)
331 {
332 wxWindowDisabler wd(win);
333
334 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
335
336 return loop && loop->YieldFor(eventsToProcess);
337 }
338
339
340 // ----------------------------------------------------------------------------
341 // idle handling
342 // ----------------------------------------------------------------------------
343
344 void wxAppBase::DeletePendingObjects()
345 {
346 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
347 while (node)
348 {
349 wxObject *obj = node->GetData();
350
351 // remove it from the list first so that if we get back here somehow
352 // during the object deletion (e.g. wxYield called from its dtor) we
353 // wouldn't try to delete it the second time
354 if ( wxPendingDelete.Member(obj) )
355 wxPendingDelete.Erase(node);
356
357 delete obj;
358
359 // Deleting one object may have deleted other pending
360 // objects, so start from beginning of list again.
361 node = wxPendingDelete.GetFirst();
362 }
363 }
364
365 // Returns true if more time is needed.
366 bool wxAppBase::ProcessIdle()
367 {
368 // call the base class version first, it will process the pending events
369 // (which should be done before the idle events generation) and send the
370 // idle event to wxTheApp itself
371 bool needMore = wxAppConsoleBase::ProcessIdle();
372 wxIdleEvent event;
373 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
374 while (node)
375 {
376 wxWindow* win = node->GetData();
377 if (SendIdleEvents(win, event))
378 needMore = true;
379 node = node->GetNext();
380 }
381
382 // 'Garbage' collection of windows deleted with Close().
383 DeletePendingObjects();
384
385 #if wxUSE_LOG
386 // flush the logged messages if any
387 wxLog::FlushActive();
388 #endif
389
390 wxUpdateUIEvent::ResetUpdateTime();
391
392 return needMore;
393 }
394
395 // Send idle event to window and all subwindows
396 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
397 {
398 bool needMore = false;
399
400 win->OnInternalIdle();
401
402 // should we send idle event to this window?
403 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
404 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
405 {
406 event.SetEventObject(win);
407 win->HandleWindowEvent(event);
408
409 if (event.MoreRequested())
410 needMore = true;
411 }
412 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
413 while ( node )
414 {
415 wxWindow *child = node->GetData();
416 if (SendIdleEvents(child, event))
417 needMore = true;
418
419 node = node->GetNext();
420 }
421
422 return needMore;
423 }
424
425 // ----------------------------------------------------------------------------
426 // wxGUIAppTraitsBase
427 // ----------------------------------------------------------------------------
428
429 #if wxUSE_LOG
430
431 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
432 {
433 #if wxUSE_LOGGUI
434 return new wxLogGui;
435 #else
436 // we must have something!
437 return new wxLogStderr;
438 #endif
439 }
440
441 #endif // wxUSE_LOG
442
443 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
444 {
445 // The standard way of printing help on command line arguments (app --help)
446 // is (according to common practice):
447 // - console apps: to stderr (on any platform)
448 // - GUI apps: stderr on Unix platforms (!)
449 // stderr if available and message box otherwise on others
450 // (currently stderr only Windows if app running from console)
451 #ifdef __UNIX__
452 return new wxMessageOutputStderr;
453 #else // !__UNIX__
454 // wxMessageOutputMessageBox doesn't work under Motif
455 #ifdef __WXMOTIF__
456 return new wxMessageOutputLog;
457 #elif wxUSE_MSGDLG
458 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR);
459 #else
460 return new wxMessageOutputStderr;
461 #endif
462 #endif // __UNIX__/!__UNIX__
463 }
464
465 #if wxUSE_FONTMAP
466
467 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
468 {
469 return new wxFontMapper;
470 }
471
472 #endif // wxUSE_FONTMAP
473
474 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
475 {
476 // use the default native renderer by default
477 return NULL;
478 }
479
480 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
481 {
482 // under MSW we prefer to use the base class version using ::MessageBox()
483 // even if wxMessageBox() is available because it has less chances to
484 // double fault our app than our wxMessageBox()
485 //
486 // under DFB the message dialog is not always functional right now
487 //
488 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
489 // course
490 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
491 return wxAppTraitsBase::ShowAssertDialog(msg);
492 #else // wxUSE_MSGDLG
493 #if wxDEBUG_LEVEL
494 wxString msgDlg = msg;
495
496 #if wxUSE_STACKWALKER
497 // on Unix stack frame generation may take some time, depending on the
498 // size of the executable mainly... warn the user that we are working
499 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
500 fflush(stderr);
501
502 const wxString stackTrace = GetAssertStackTrace();
503 if ( !stackTrace.empty() )
504 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
505 #endif // wxUSE_STACKWALKER
506
507 // this message is intentionally not translated -- it is for
508 // developpers only
509 msgDlg += wxT("\nDo you want to stop the program?\n")
510 wxT("You can also choose [Cancel] to suppress ")
511 wxT("further warnings.");
512
513 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
514 wxYES_NO | wxCANCEL | wxICON_STOP ) )
515 {
516 case wxYES:
517 wxTrap();
518 break;
519
520 case wxCANCEL:
521 // no more asserts
522 return true;
523
524 //case wxNO: nothing to do
525 }
526 #else // !wxDEBUG_LEVEL
527 // this function always exists (for ABI compatibility) but is never called
528 // if debug level is 0 and so can simply do nothing then
529 wxUnusedVar(msg);
530 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
531
532 return false;
533 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
534 }
535
536 bool wxGUIAppTraitsBase::HasStderr()
537 {
538 // we consider that under Unix stderr always goes somewhere, even if the
539 // user doesn't always see it under GUI desktops
540 #ifdef __UNIX__
541 return true;
542 #else
543 return false;
544 #endif
545 }
546
547 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
548 {
549 if ( !wxPendingDelete.Member(object) )
550 wxPendingDelete.Append(object);
551 }
552
553 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
554 {
555 wxPendingDelete.DeleteObject(object);
556 }
557