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 // ============================================================================
54 // wxAppBase implementation
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 wxAppBase::wxAppBase()
65 m_useBestVisual
= false;
66 m_forceTrueColour
= false;
70 // We don't want to exit the app if the user code shows a dialog from its
71 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
72 // to Yes initially as this dialog would be the last top level window.
73 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
74 // when we enter our OnRun() because we do want the default behaviour from
75 // then on. But this would be a problem if the user code calls
76 // SetExitOnFrameDelete(false) from OnInit().
78 // So we use the special "Later" value which is such that
79 // GetExitOnFrameDelete() returns false for it but which we know we can
80 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
81 // call) overwrite in OnRun()
82 m_exitOnFrameDelete
= Later
;
85 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
87 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
90 wxInitializeStockLists();
92 wxBitmap::InitStandardHandlers();
94 // for compatibility call the old initialization function too
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 wxAppBase::~wxAppBase()
107 // this destructor is required for Darwin
110 void wxAppBase::CleanUp()
112 // clean up all the pending objects
113 DeletePendingObjects();
115 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
116 // when destroyed, so iterate until none are left)
117 while ( !wxTopLevelWindows
.empty() )
119 // do not use Destroy() here as it only puts the TLW in pending list
120 // but we want to delete them now
121 delete wxTopLevelWindows
.GetFirst()->GetData();
124 // undo everything we did in Initialize() above
125 wxBitmap::CleanUpHandlers();
127 wxStockGDI::DeleteAll();
129 wxDeleteStockLists();
131 wxDELETE(wxTheColourDatabase
);
133 wxAppConsole::CleanUp();
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 wxWindow
* wxAppBase::GetTopWindow() const
142 wxWindow
* window
= m_topWindow
;
143 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
144 window
= wxTopLevelWindows
.GetFirst()->GetData();
148 wxVideoMode
wxAppBase::GetDisplayMode() const
150 return wxVideoMode();
153 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
156 const wxLocale
*const locale
= wxGetLocale();
159 const wxLanguageInfo
*const
160 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
163 return info
->LayoutDirection
;
168 return wxLayout_Default
;
171 #if wxUSE_CMDLINE_PARSER
173 // ----------------------------------------------------------------------------
174 // GUI-specific command line options handling
175 // ----------------------------------------------------------------------------
177 #define OPTION_THEME "theme"
178 #define OPTION_MODE "mode"
180 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
182 // first add the standard non GUI options
183 wxAppConsole::OnInitCmdLine(parser
);
185 // the standard command line options
186 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
188 #ifdef __WXUNIVERSAL__
193 gettext_noop("specify the theme to use"),
194 wxCMD_LINE_VAL_STRING
,
197 #endif // __WXUNIVERSAL__
199 #if defined(__WXMGL__)
200 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
201 // should provide this option. That's why it is in common/appcmn.cpp
202 // and not mgl/app.cpp
207 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
208 wxCMD_LINE_VAL_STRING
,
217 parser
.SetDesc(cmdLineGUIDesc
);
220 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
222 #ifdef __WXUNIVERSAL__
224 if ( parser
.Found(OPTION_THEME
, &themeName
) )
226 wxTheme
*theme
= wxTheme::Create(themeName
);
229 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
233 // Delete the defaultly created theme and set the new theme.
234 delete wxTheme::Get();
237 #endif // __WXUNIVERSAL__
239 #if defined(__WXMGL__)
241 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
244 if ( wxSscanf(modeDesc
.c_str(), wxT("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
246 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
250 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
255 return wxAppConsole::OnCmdLineParsed(parser
);
258 #endif // wxUSE_CMDLINE_PARSER
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 bool wxAppBase::OnInitGui()
266 #ifdef __WXUNIVERSAL__
267 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
269 #endif // __WXUNIVERSAL__
274 int wxAppBase::OnRun()
276 // see the comment in ctor: if the initial value hasn't been changed, use
277 // the default Yes from now on
278 if ( m_exitOnFrameDelete
== Later
)
280 m_exitOnFrameDelete
= Yes
;
282 //else: it has been changed, assume the user knows what he is doing
284 return wxAppConsole::OnRun();
287 int wxAppBase::OnExit()
289 #ifdef __WXUNIVERSAL__
290 delete wxTheme::Set(NULL
);
291 #endif // __WXUNIVERSAL__
293 return wxAppConsole::OnExit();
296 wxAppTraits
*wxAppBase::CreateTraits()
298 return new wxGUIAppTraits
;
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
307 if ( active
== m_isActive
)
312 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
313 event
.SetEventObject(this);
315 (void)ProcessEvent(event
);
318 bool wxAppBase::SafeYield(wxWindow
*win
, bool onlyIfNeeded
)
320 wxWindowDisabler
wd(win
);
322 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
324 return loop
&& loop
->Yield(onlyIfNeeded
);
327 bool wxAppBase::SafeYieldFor(wxWindow
*win
, long eventsToProcess
)
329 wxWindowDisabler
wd(win
);
331 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
333 return loop
&& loop
->YieldFor(eventsToProcess
);
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 // Returns true if more time is needed.
342 bool wxAppBase::ProcessIdle()
344 // call the base class version first to send the idle event to wxTheApp
346 bool needMore
= wxAppConsoleBase::ProcessIdle();
348 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
351 wxWindow
* win
= node
->GetData();
352 if (SendIdleEvents(win
, event
))
354 node
= node
->GetNext();
357 wxUpdateUIEvent::ResetUpdateTime();
362 // Send idle event to window and all subwindows
363 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
365 bool needMore
= false;
367 win
->OnInternalIdle();
369 // should we send idle event to this window?
370 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
371 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
373 event
.SetEventObject(win
);
374 win
->HandleWindowEvent(event
);
376 if (event
.MoreRequested())
379 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
382 wxWindow
*child
= node
->GetData();
383 if (SendIdleEvents(child
, event
))
386 node
= node
->GetNext();
392 // ----------------------------------------------------------------------------
393 // wxGUIAppTraitsBase
394 // ----------------------------------------------------------------------------
398 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
401 #ifndef __WXOSX_IPHONE__
404 return new wxLogStderr
;
407 // we must have something!
408 return new wxLogStderr
;
414 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
416 // The standard way of printing help on command line arguments (app --help)
417 // is (according to common practice):
418 // - console apps: to stderr (on any platform)
419 // - GUI apps: stderr on Unix platforms (!)
420 // stderr if available and message box otherwise on others
421 // (currently stderr only Windows if app running from console)
423 return new wxMessageOutputStderr
;
425 // wxMessageOutputMessageBox doesn't work under Motif
427 return new wxMessageOutputLog
;
429 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
431 return new wxMessageOutputStderr
;
433 #endif // __UNIX__/!__UNIX__
438 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
440 return new wxFontMapper
;
443 #endif // wxUSE_FONTMAP
445 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
447 // use the default native renderer by default
451 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
454 // under MSW we prefer to use the base class version using ::MessageBox()
455 // even if wxMessageBox() is available because it has less chances to
456 // double fault our app than our wxMessageBox()
458 // under DFB the message dialog is not always functional right now
460 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
462 #if !defined(__WXMSW__) && !defined(__WXDFB__) && wxUSE_MSGDLG
464 // we can't (safely) show the GUI dialog from another thread, only do it
465 // for the asserts in the main thread
466 if ( wxIsMainThread() )
468 wxString msgDlg
= msg
;
470 #if wxUSE_STACKWALKER
471 const wxString stackTrace
= GetAssertStackTrace();
472 if ( !stackTrace
.empty() )
473 msgDlg
<< wxT("\n\nCall stack:\n") << stackTrace
;
474 #endif // wxUSE_STACKWALKER
476 // this message is intentionally not translated -- it is for
478 msgDlg
+= wxT("\nDo you want to stop the program?\n")
479 wxT("You can also choose [Cancel] to suppress ")
480 wxT("further warnings.");
482 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
483 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
493 //case wxNO: nothing to do
498 #endif // wxUSE_MSGDLG
499 #endif // wxDEBUG_LEVEL
501 return wxAppTraitsBase::ShowAssertDialog(msg
);
504 bool wxGUIAppTraitsBase::HasStderr()
506 // we consider that under Unix stderr always goes somewhere, even if the
507 // user doesn't always see it under GUI desktops