1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if defined(__BORLANDC__)
28 #include "wx/window.h"
29 #include "wx/bitmap.h"
31 #include "wx/msgdlg.h"
32 #include "wx/confbase.h"
34 #include "wx/wxcrtvararg.h"
37 #include "wx/apptrait.h"
38 #include "wx/cmdline.h"
39 #include "wx/msgout.h"
40 #include "wx/thread.h"
41 #include "wx/vidmode.h"
42 #include "wx/evtloop.h"
45 #include "wx/fontmap.h"
46 #endif // wxUSE_FONTMAP
48 // DLL options compatibility check:
50 WX_CHECK_BUILD_OPTIONS("wxCore")
52 // ============================================================================
53 // wxAppBase implementation
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxAppBase::wxAppBase()
64 m_useBestVisual
= false;
65 m_forceTrueColour
= false;
69 // We don't want to exit the app if the user code shows a dialog from its
70 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
71 // to Yes initially as this dialog would be the last top level window.
72 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
73 // when we enter our OnRun() because we do want the default behaviour from
74 // then on. But this would be a problem if the user code calls
75 // SetExitOnFrameDelete(false) from OnInit().
77 // So we use the special "Later" value which is such that
78 // GetExitOnFrameDelete() returns false for it but which we know we can
79 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
80 // call) overwrite in OnRun()
81 m_exitOnFrameDelete
= Later
;
84 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
86 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
89 wxInitializeStockLists();
91 wxBitmap::InitStandardHandlers();
93 // for compatibility call the old initialization function too
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 wxAppBase::~wxAppBase()
106 // this destructor is required for Darwin
109 void wxAppBase::CleanUp()
111 // clean up all the pending objects
112 DeletePendingObjects();
114 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
115 // when destroyed, so iterate until none are left)
116 while ( !wxTopLevelWindows
.empty() )
118 // do not use Destroy() here as it only puts the TLW in pending list
119 // but we want to delete them now
120 delete wxTopLevelWindows
.GetFirst()->GetData();
123 // undo everything we did in Initialize() above
124 wxBitmap::CleanUpHandlers();
126 wxStockGDI::DeleteAll();
128 wxDeleteStockLists();
130 wxDELETE(wxTheColourDatabase
);
132 wxAppConsole::CleanUp();
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 wxWindow
* wxAppBase::GetTopWindow() const
141 wxWindow
* window
= m_topWindow
;
142 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
143 window
= wxTopLevelWindows
.GetFirst()->GetData();
147 wxVideoMode
wxAppBase::GetDisplayMode() const
149 return wxVideoMode();
152 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
155 const wxLocale
*const locale
= wxGetLocale();
158 const wxLanguageInfo
*const
159 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
162 return info
->LayoutDirection
;
167 return wxLayout_Default
;
170 #if wxUSE_CMDLINE_PARSER
172 // ----------------------------------------------------------------------------
173 // GUI-specific command line options handling
174 // ----------------------------------------------------------------------------
176 #define OPTION_THEME "theme"
177 #define OPTION_MODE "mode"
179 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
181 // first add the standard non GUI options
182 wxAppConsole::OnInitCmdLine(parser
);
184 // the standard command line options
185 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
187 #ifdef __WXUNIVERSAL__
192 gettext_noop("specify the theme to use"),
193 wxCMD_LINE_VAL_STRING
,
196 #endif // __WXUNIVERSAL__
198 #if defined(__WXDFB__)
199 // VS: this is not specific to wxDFB, all fullscreen (framebuffer) ports
200 // should provide this option. That's why it is in common/appcmn.cpp
201 // and not dfb/app.cpp
206 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
207 wxCMD_LINE_VAL_STRING
,
216 parser
.SetDesc(cmdLineGUIDesc
);
219 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
221 #ifdef __WXUNIVERSAL__
223 if ( parser
.Found(OPTION_THEME
, &themeName
) )
225 wxTheme
*theme
= wxTheme::Create(themeName
);
228 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
232 // Delete the defaultly created theme and set the new theme.
233 delete wxTheme::Get();
236 #endif // __WXUNIVERSAL__
238 #if defined(__WXDFB__)
240 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
243 if ( wxSscanf(modeDesc
.c_str(), wxT("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
245 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
249 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
254 return wxAppConsole::OnCmdLineParsed(parser
);
257 #endif // wxUSE_CMDLINE_PARSER
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 bool wxAppBase::OnInitGui()
265 #ifdef __WXUNIVERSAL__
266 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
268 #endif // __WXUNIVERSAL__
273 int wxAppBase::OnRun()
275 // see the comment in ctor: if the initial value hasn't been changed, use
276 // the default Yes from now on
277 if ( m_exitOnFrameDelete
== Later
)
279 m_exitOnFrameDelete
= Yes
;
281 //else: it has been changed, assume the user knows what he is doing
283 return wxAppConsole::OnRun();
286 int wxAppBase::OnExit()
288 #ifdef __WXUNIVERSAL__
289 delete wxTheme::Set(NULL
);
290 #endif // __WXUNIVERSAL__
292 return wxAppConsole::OnExit();
295 wxAppTraits
*wxAppBase::CreateTraits()
297 return new wxGUIAppTraits
;
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
306 if ( active
== m_isActive
)
311 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
312 event
.SetEventObject(this);
314 (void)ProcessEvent(event
);
317 bool wxAppBase::SafeYield(wxWindow
*win
, bool onlyIfNeeded
)
319 wxWindowDisabler
wd(win
);
321 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
323 return loop
&& loop
->Yield(onlyIfNeeded
);
326 bool wxAppBase::SafeYieldFor(wxWindow
*win
, long eventsToProcess
)
328 wxWindowDisabler
wd(win
);
330 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
332 return loop
&& loop
->YieldFor(eventsToProcess
);
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 // Returns true if more time is needed.
341 bool wxAppBase::ProcessIdle()
343 // call the base class version first to send the idle event to wxTheApp
345 bool needMore
= wxAppConsoleBase::ProcessIdle();
347 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
350 wxWindow
* win
= node
->GetData();
352 // Don't send idle events to the windows that are about to be destroyed
353 // anyhow, this is wasteful and unexpected.
354 if ( !wxPendingDelete
.Member(win
) && win
->SendIdleEvents(event
) )
356 node
= node
->GetNext();
359 wxUpdateUIEvent::ResetUpdateTime();
364 // ----------------------------------------------------------------------------
365 // wxGUIAppTraitsBase
366 // ----------------------------------------------------------------------------
370 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
373 #ifndef __WXOSX_IPHONE__
376 return new wxLogStderr
;
379 // we must have something!
380 return new wxLogStderr
;
386 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
388 // The standard way of printing help on command line arguments (app --help)
389 // is (according to common practice):
390 // - console apps: to stderr (on any platform)
391 // - GUI apps: stderr on Unix platforms (!)
392 // stderr if available and message box otherwise on others
393 // (currently stderr only Windows if app running from console)
395 return new wxMessageOutputStderr
;
397 // wxMessageOutputMessageBox doesn't work under Motif
399 return new wxMessageOutputLog
;
401 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
403 return new wxMessageOutputStderr
;
405 #endif // __UNIX__/!__UNIX__
410 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
412 return new wxFontMapper
;
415 #endif // wxUSE_FONTMAP
417 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
419 // use the default native renderer by default
423 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
426 // under MSW we prefer to use the base class version using ::MessageBox()
427 // even if wxMessageBox() is available because it has less chances to
428 // double fault our app than our wxMessageBox()
430 // under DFB the message dialog is not always functional right now
432 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
434 #if !defined(__WXMSW__) && !defined(__WXDFB__) && wxUSE_MSGDLG
436 // we can't (safely) show the GUI dialog from another thread, only do it
437 // for the asserts in the main thread
438 if ( wxIsMainThread() )
440 wxString msgDlg
= msg
;
442 #if wxUSE_STACKWALKER
443 const wxString stackTrace
= GetAssertStackTrace();
444 if ( !stackTrace
.empty() )
445 msgDlg
<< wxT("\n\nCall stack:\n") << stackTrace
;
446 #endif // wxUSE_STACKWALKER
448 // this message is intentionally not translated -- it is for
450 msgDlg
+= wxT("\nDo you want to stop the program?\n")
451 wxT("You can also choose [Cancel] to suppress ")
452 wxT("further warnings.");
454 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
455 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
465 //case wxNO: nothing to do
470 #endif // wxUSE_MSGDLG
471 #endif // wxDEBUG_LEVEL
473 return wxAppTraitsBase::ShowAssertDialog(msg
);
476 bool wxGUIAppTraitsBase::HasStderr()
478 // we consider that under Unix stderr always goes somewhere, even if the
479 // user doesn't always see it under GUI desktops