1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
29 #include "wx/window.h"
30 #include "wx/bitmap.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
35 #include "wx/wxcrtvararg.h"
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"
46 #include "wx/fontmap.h"
47 #endif // wxUSE_FONTMAP
49 // DLL options compatibility check:
51 WX_CHECK_BUILD_OPTIONS("wxCore")
53 WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
55 // ============================================================================
56 // wxAppBase implementation
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 wxAppBase::wxAppBase()
67 m_useBestVisual
= false;
68 m_forceTrueColour
= false;
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().
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
;
87 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
89 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
92 wxInitializeStockLists();
94 wxBitmap::InitStandardHandlers();
96 // for compatibility call the old initialization function too
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 wxAppBase::~wxAppBase()
109 // this destructor is required for Darwin
112 void wxAppBase::CleanUp()
114 // clean up all the pending objects
115 DeletePendingObjects();
117 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
118 // when destroyed, so iterate until none are left)
119 while ( !wxTopLevelWindows
.empty() )
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();
126 // undo everything we did in Initialize() above
127 wxBitmap::CleanUpHandlers();
129 wxStockGDI::DeleteAll();
131 wxDeleteStockLists();
133 delete wxTheColourDatabase
;
134 wxTheColourDatabase
= NULL
;
136 wxAppConsole::CleanUp();
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 wxWindow
* wxAppBase::GetTopWindow() const
145 wxWindow
* window
= m_topWindow
;
146 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
147 window
= wxTopLevelWindows
.GetFirst()->GetData();
151 wxVideoMode
wxAppBase::GetDisplayMode() const
153 return wxVideoMode();
156 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
159 const wxLocale
*const locale
= wxGetLocale();
162 const wxLanguageInfo
*const
163 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
166 return info
->LayoutDirection
;
171 return wxLayout_Default
;
174 #if wxUSE_CMDLINE_PARSER
176 // ----------------------------------------------------------------------------
177 // GUI-specific command line options handling
178 // ----------------------------------------------------------------------------
180 #define OPTION_THEME "theme"
181 #define OPTION_MODE "mode"
183 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
185 // first add the standard non GUI options
186 wxAppConsole::OnInitCmdLine(parser
);
188 // the standard command line options
189 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
191 #ifdef __WXUNIVERSAL__
196 gettext_noop("specify the theme to use"),
197 wxCMD_LINE_VAL_STRING
,
200 #endif // __WXUNIVERSAL__
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
210 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
211 wxCMD_LINE_VAL_STRING
,
220 parser
.SetDesc(cmdLineGUIDesc
);
223 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
225 #ifdef __WXUNIVERSAL__
227 if ( parser
.Found(OPTION_THEME
, &themeName
) )
229 wxTheme
*theme
= wxTheme::Create(themeName
);
232 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
236 // Delete the defaultly created theme and set the new theme.
237 delete wxTheme::Get();
240 #endif // __WXUNIVERSAL__
242 #if defined(__WXMGL__)
244 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
247 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
249 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
253 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
258 return wxAppConsole::OnCmdLineParsed(parser
);
261 #endif // wxUSE_CMDLINE_PARSER
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 bool wxAppBase::OnInitGui()
269 #ifdef __WXUNIVERSAL__
270 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
272 #endif // __WXUNIVERSAL__
277 int wxAppBase::OnRun()
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
)
283 m_exitOnFrameDelete
= Yes
;
285 //else: it has been changed, assume the user knows what he is doing
287 return wxAppConsole::OnRun();
290 int wxAppBase::OnExit()
292 #ifdef __WXUNIVERSAL__
293 delete wxTheme::Set(NULL
);
294 #endif // __WXUNIVERSAL__
296 return wxAppConsole::OnExit();
299 wxAppTraits
*wxAppBase::CreateTraits()
301 return new wxGUIAppTraits
;
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
310 if ( active
== m_isActive
)
315 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
316 event
.SetEventObject(this);
318 (void)ProcessEvent(event
);
321 bool wxAppBase::SafeYield(wxWindow
*win
, bool onlyIfNeeded
)
323 wxWindowDisabler
wd(win
);
325 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
327 return loop
&& loop
->Yield(onlyIfNeeded
);
330 bool wxAppBase::SafeYieldFor(wxWindow
*win
, long eventsToProcess
)
332 wxWindowDisabler
wd(win
);
334 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
336 return loop
&& loop
->YieldFor(eventsToProcess
);
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 void wxAppBase::DeletePendingObjects()
346 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
349 wxObject
*obj
= node
->GetData();
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
);
359 // Deleting one object may have deleted other pending
360 // objects, so start from beginning of list again.
361 node
= wxPendingDelete
.GetFirst();
365 // Returns true if more time is needed.
366 bool wxAppBase::ProcessIdle()
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();
373 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
376 wxWindow
* win
= node
->GetData();
377 if (SendIdleEvents(win
, event
))
379 node
= node
->GetNext();
382 // 'Garbage' collection of windows deleted with Close().
383 DeletePendingObjects();
386 // flush the logged messages if any
387 wxLog::FlushActive();
390 wxUpdateUIEvent::ResetUpdateTime();
395 // Send idle event to window and all subwindows
396 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
398 bool needMore
= false;
400 win
->OnInternalIdle();
402 // should we send idle event to this window?
403 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
404 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
406 event
.SetEventObject(win
);
407 win
->HandleWindowEvent(event
);
409 if (event
.MoreRequested())
412 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
415 wxWindow
*child
= node
->GetData();
416 if (SendIdleEvents(child
, event
))
419 node
= node
->GetNext();
425 // ----------------------------------------------------------------------------
426 // wxGUIAppTraitsBase
427 // ----------------------------------------------------------------------------
431 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
436 // we must have something!
437 return new wxLogStderr
;
443 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
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)
452 return new wxMessageOutputStderr
;
454 // wxMessageOutputMessageBox doesn't work under Motif
456 return new wxMessageOutputLog
;
458 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
460 return new wxMessageOutputStderr
;
462 #endif // __UNIX__/!__UNIX__
467 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
469 return new wxFontMapper
;
472 #endif // wxUSE_FONTMAP
474 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
476 // use the default native renderer by default
480 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
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()
486 // under DFB the message dialog is not always functional right now
488 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
490 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
491 return wxAppTraitsBase::ShowAssertDialog(msg
);
492 #else // wxUSE_MSGDLG
494 wxString msgDlg
= msg
;
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"));
502 const wxString stackTrace
= GetAssertStackTrace();
503 if ( !stackTrace
.empty() )
504 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
505 #endif // wxUSE_STACKWALKER
507 // this message is intentionally not translated -- it is for
509 msgDlg
+= wxT("\nDo you want to stop the program?\n")
510 wxT("You can also choose [Cancel] to suppress ")
511 wxT("further warnings.");
513 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
514 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
524 //case wxNO: nothing to do
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
530 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
533 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
536 bool wxGUIAppTraitsBase::HasStderr()
538 // we consider that under Unix stderr always goes somewhere, even if the
539 // user doesn't always see it under GUI desktops
547 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
549 if ( !wxPendingDelete
.Member(object
) )
550 wxPendingDelete
.Append(object
);
553 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
555 wxPendingDelete
.DeleteObject(object
);