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"
47 #include "wx/stackwalk.h"
48 #endif // wxUSE_STACKWALKER
51 #if defined(__WXMSW__)
52 #include "wx/msw/private.h" // includes windows.h for LOGFONT
56 #include "wx/fontmap.h"
57 #endif // wxUSE_FONTMAP
59 // DLL options compatibility check:
61 WX_CHECK_BUILD_OPTIONS("wxCore")
63 WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
65 // ============================================================================
66 // wxAppBase implementation
67 // ============================================================================
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 wxAppBase::wxAppBase()
77 m_useBestVisual
= false;
78 m_forceTrueColour
= false;
82 // We don't want to exit the app if the user code shows a dialog from its
83 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
84 // to Yes initially as this dialog would be the last top level window.
85 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
86 // when we enter our OnRun() because we do want the default behaviour from
87 // then on. But this would be a problem if the user code calls
88 // SetExitOnFrameDelete(false) from OnInit().
90 // So we use the special "Later" value which is such that
91 // GetExitOnFrameDelete() returns false for it but which we know we can
92 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
93 // call) overwrite in OnRun()
94 m_exitOnFrameDelete
= Later
;
97 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
99 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
102 wxInitializeStockLists();
104 wxBitmap::InitStandardHandlers();
106 // for compatibility call the old initialization function too
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 wxAppBase::~wxAppBase()
119 // this destructor is required for Darwin
122 void wxAppBase::CleanUp()
124 // clean up all the pending objects
125 DeletePendingObjects();
127 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
128 // when destroyed, so iterate until none are left)
129 while ( !wxTopLevelWindows
.empty() )
131 // do not use Destroy() here as it only puts the TLW in pending list
132 // but we want to delete them now
133 delete wxTopLevelWindows
.GetFirst()->GetData();
136 // undo everything we did in Initialize() above
137 wxBitmap::CleanUpHandlers();
139 wxStockGDI::DeleteAll();
141 wxDeleteStockLists();
143 delete wxTheColourDatabase
;
144 wxTheColourDatabase
= NULL
;
146 wxAppConsole::CleanUp();
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 wxWindow
* wxAppBase::GetTopWindow() const
155 wxWindow
* window
= m_topWindow
;
156 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
157 window
= wxTopLevelWindows
.GetFirst()->GetData();
161 wxVideoMode
wxAppBase::GetDisplayMode() const
163 return wxVideoMode();
166 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
169 const wxLocale
*const locale
= wxGetLocale();
172 const wxLanguageInfo
*const
173 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
176 return info
->LayoutDirection
;
181 return wxLayout_Default
;
184 #if wxUSE_CMDLINE_PARSER
186 // ----------------------------------------------------------------------------
187 // GUI-specific command line options handling
188 // ----------------------------------------------------------------------------
190 #define OPTION_THEME "theme"
191 #define OPTION_MODE "mode"
193 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
195 // first add the standard non GUI options
196 wxAppConsole::OnInitCmdLine(parser
);
198 // the standard command line options
199 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
201 #ifdef __WXUNIVERSAL__
206 gettext_noop("specify the theme to use"),
207 wxCMD_LINE_VAL_STRING
,
210 #endif // __WXUNIVERSAL__
212 #if defined(__WXMGL__)
213 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
214 // should provide this option. That's why it is in common/appcmn.cpp
215 // and not mgl/app.cpp
220 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
221 wxCMD_LINE_VAL_STRING
,
230 parser
.SetDesc(cmdLineGUIDesc
);
233 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
235 #ifdef __WXUNIVERSAL__
237 if ( parser
.Found(OPTION_THEME
, &themeName
) )
239 wxTheme
*theme
= wxTheme::Create(themeName
);
242 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
246 // Delete the defaultly created theme and set the new theme.
247 delete wxTheme::Get();
250 #endif // __WXUNIVERSAL__
252 #if defined(__WXMGL__)
254 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
257 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
259 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
263 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
268 return wxAppConsole::OnCmdLineParsed(parser
);
271 #endif // wxUSE_CMDLINE_PARSER
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 bool wxAppBase::OnInitGui()
279 #ifdef __WXUNIVERSAL__
280 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
282 #endif // __WXUNIVERSAL__
287 int wxAppBase::OnRun()
289 // see the comment in ctor: if the initial value hasn't been changed, use
290 // the default Yes from now on
291 if ( m_exitOnFrameDelete
== Later
)
293 m_exitOnFrameDelete
= Yes
;
295 //else: it has been changed, assume the user knows what he is doing
297 return wxAppConsole::OnRun();
300 int wxAppBase::OnExit()
302 #ifdef __WXUNIVERSAL__
303 delete wxTheme::Set(NULL
);
304 #endif // __WXUNIVERSAL__
306 return wxAppConsole::OnExit();
309 wxAppTraits
*wxAppBase::CreateTraits()
311 return new wxGUIAppTraits
;
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
320 if ( active
== m_isActive
)
325 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
326 event
.SetEventObject(this);
328 (void)ProcessEvent(event
);
331 bool wxAppBase::SafeYield(wxWindow
*win
, bool onlyIfNeeded
)
333 wxWindowDisabler
wd(win
);
335 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
337 return loop
&& loop
->Yield(onlyIfNeeded
);
340 bool wxAppBase::SafeYieldFor(wxWindow
*win
, long eventsToProcess
)
342 wxWindowDisabler
wd(win
);
344 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
346 return loop
&& loop
->YieldFor(eventsToProcess
);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 void wxAppBase::DeletePendingObjects()
356 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
359 wxObject
*obj
= node
->GetData();
361 // remove it from the list first so that if we get back here somehow
362 // during the object deletion (e.g. wxYield called from its dtor) we
363 // wouldn't try to delete it the second time
364 if ( wxPendingDelete
.Member(obj
) )
365 wxPendingDelete
.Erase(node
);
369 // Deleting one object may have deleted other pending
370 // objects, so start from beginning of list again.
371 node
= wxPendingDelete
.GetFirst();
375 // Returns true if more time is needed.
376 bool wxAppBase::ProcessIdle()
378 // call the base class version first, it will process the pending events
379 // (which should be done before the idle events generation) and send the
380 // idle event to wxTheApp itself
381 bool needMore
= wxAppConsoleBase::ProcessIdle();
383 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
386 wxWindow
* win
= node
->GetData();
387 if (SendIdleEvents(win
, event
))
389 node
= node
->GetNext();
392 // 'Garbage' collection of windows deleted with Close().
393 DeletePendingObjects();
396 // flush the logged messages if any
397 wxLog::FlushActive();
400 wxUpdateUIEvent::ResetUpdateTime();
405 // Send idle event to window and all subwindows
406 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
408 bool needMore
= false;
410 win
->OnInternalIdle();
412 // should we send idle event to this window?
413 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
414 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
416 event
.SetEventObject(win
);
417 win
->HandleWindowEvent(event
);
419 if (event
.MoreRequested())
422 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
425 wxWindow
*child
= node
->GetData();
426 if (SendIdleEvents(child
, event
))
429 node
= node
->GetNext();
435 // ----------------------------------------------------------------------------
436 // wxGUIAppTraitsBase
437 // ----------------------------------------------------------------------------
441 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
446 // we must have something!
447 return new wxLogStderr
;
453 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
455 // The standard way of printing help on command line arguments (app --help)
456 // is (according to common practice):
457 // - console apps: to stderr (on any platform)
458 // - GUI apps: stderr on Unix platforms (!)
459 // stderr if available and message box otherwise on others
460 // (currently stderr only Windows if app running from console)
462 return new wxMessageOutputStderr
;
464 // wxMessageOutputMessageBox doesn't work under Motif
466 return new wxMessageOutputLog
;
468 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
470 return new wxMessageOutputStderr
;
472 #endif // __UNIX__/!__UNIX__
477 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
479 return new wxFontMapper
;
482 #endif // wxUSE_FONTMAP
484 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
486 // use the default native renderer by default
492 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
494 // under MSW we prefer to use the base class version using ::MessageBox()
495 // even if wxMessageBox() is available because it has less chances to
496 // double fault our app than our wxMessageBox()
498 // under DFB the message dialog is not always functional right now
500 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
502 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
503 return wxAppTraitsBase::ShowAssertDialog(msg
);
504 #else // wxUSE_MSGDLG
505 wxString msgDlg
= msg
;
507 #if wxUSE_STACKWALKER
508 // on Unix stack frame generation may take some time, depending on the
509 // size of the executable mainly... warn the user that we are working
510 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
513 const wxString stackTrace
= GetAssertStackTrace();
514 if ( !stackTrace
.empty() )
515 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
516 #endif // wxUSE_STACKWALKER
518 // this message is intentionally not translated -- it is for
520 msgDlg
+= wxT("\nDo you want to stop the program?\n")
521 wxT("You can also choose [Cancel] to suppress ")
522 wxT("further warnings.");
524 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
525 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
535 //case wxNO: nothing to do
539 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
542 #endif // __WXDEBUG__
544 bool wxGUIAppTraitsBase::HasStderr()
546 // we consider that under Unix stderr always goes somewhere, even if the
547 // user doesn't always see it under GUI desktops
555 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
557 if ( !wxPendingDelete
.Member(object
) )
558 wxPendingDelete
.Append(object
);
561 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
563 wxPendingDelete
.DeleteObject(object
);