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();
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 wxAppBase::~wxAppBase()
115 // this destructor is required for Darwin
118 void wxAppBase::CleanUp()
120 // clean up all the pending objects
121 DeletePendingObjects();
123 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
124 // when destroyed, so iterate until none are left)
125 while ( !wxTopLevelWindows
.empty() )
127 // do not use Destroy() here as it only puts the TLW in pending list
128 // but we want to delete them now
129 delete wxTopLevelWindows
.GetFirst()->GetData();
132 // undo everything we did in Initialize() above
133 wxBitmap::CleanUpHandlers();
135 wxStockGDI::DeleteAll();
137 wxDeleteStockLists();
139 delete wxTheColourDatabase
;
140 wxTheColourDatabase
= NULL
;
142 wxAppConsole::CleanUp();
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 wxWindow
* wxAppBase::GetTopWindow() const
151 wxWindow
* window
= m_topWindow
;
152 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
153 window
= wxTopLevelWindows
.GetFirst()->GetData();
157 wxVideoMode
wxAppBase::GetDisplayMode() const
159 return wxVideoMode();
162 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
165 const wxLocale
*const locale
= wxGetLocale();
168 const wxLanguageInfo
*const
169 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
172 return info
->LayoutDirection
;
177 return wxLayout_Default
;
180 #if wxUSE_CMDLINE_PARSER
182 // ----------------------------------------------------------------------------
183 // GUI-specific command line options handling
184 // ----------------------------------------------------------------------------
186 #define OPTION_THEME "theme"
187 #define OPTION_MODE "mode"
189 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
191 // first add the standard non GUI options
192 wxAppConsole::OnInitCmdLine(parser
);
194 // the standard command line options
195 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
197 #ifdef __WXUNIVERSAL__
202 gettext_noop("specify the theme to use"),
203 wxCMD_LINE_VAL_STRING
,
206 #endif // __WXUNIVERSAL__
208 #if defined(__WXMGL__)
209 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
210 // should provide this option. That's why it is in common/appcmn.cpp
211 // and not mgl/app.cpp
216 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
217 wxCMD_LINE_VAL_STRING
,
226 parser
.SetDesc(cmdLineGUIDesc
);
229 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
231 #ifdef __WXUNIVERSAL__
233 if ( parser
.Found(OPTION_THEME
, &themeName
) )
235 wxTheme
*theme
= wxTheme::Create(themeName
);
238 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
242 // Delete the defaultly created theme and set the new theme.
243 delete wxTheme::Get();
246 #endif // __WXUNIVERSAL__
248 #if defined(__WXMGL__)
250 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
253 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
255 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
259 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
264 return wxAppConsole::OnCmdLineParsed(parser
);
267 #endif // wxUSE_CMDLINE_PARSER
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 bool wxAppBase::OnInitGui()
275 #ifdef __WXUNIVERSAL__
276 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
278 #endif // __WXUNIVERSAL__
283 int wxAppBase::OnRun()
285 // see the comment in ctor: if the initial value hasn't been changed, use
286 // the default Yes from now on
287 if ( m_exitOnFrameDelete
== Later
)
289 m_exitOnFrameDelete
= Yes
;
291 //else: it has been changed, assume the user knows what he is doing
293 return wxAppConsole::OnRun();
296 int wxAppBase::OnExit()
298 #ifdef __WXUNIVERSAL__
299 delete wxTheme::Set(NULL
);
300 #endif // __WXUNIVERSAL__
302 return wxAppConsole::OnExit();
305 wxAppTraits
*wxAppBase::CreateTraits()
307 return new wxGUIAppTraits
;
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
316 if ( active
== m_isActive
)
321 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
322 event
.SetEventObject(this);
324 (void)ProcessEvent(event
);
327 bool wxAppBase::SafeYield(wxWindow
*win
, bool onlyIfNeeded
)
329 wxWindowDisabler
wd(win
);
331 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
333 return loop
&& loop
->Yield(onlyIfNeeded
);
336 bool wxAppBase::SafeYieldFor(wxWindow
*win
, long eventsToProcess
)
338 wxWindowDisabler
wd(win
);
340 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
342 return loop
&& loop
->YieldFor(eventsToProcess
);
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 void wxAppBase::DeletePendingObjects()
352 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
355 wxObject
*obj
= node
->GetData();
357 // remove it from the list first so that if we get back here somehow
358 // during the object deletion (e.g. wxYield called from its dtor) we
359 // wouldn't try to delete it the second time
360 if ( wxPendingDelete
.Member(obj
) )
361 wxPendingDelete
.Erase(node
);
365 // Deleting one object may have deleted other pending
366 // objects, so start from beginning of list again.
367 node
= wxPendingDelete
.GetFirst();
371 // Returns true if more time is needed.
372 bool wxAppBase::ProcessIdle()
374 // call the base class version first, it will process the pending events
375 // (which should be done before the idle events generation) and send the
376 // idle event to wxTheApp itself
377 bool needMore
= wxAppConsoleBase::ProcessIdle();
379 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
382 wxWindow
* win
= node
->GetData();
383 if (SendIdleEvents(win
, event
))
385 node
= node
->GetNext();
388 // 'Garbage' collection of windows deleted with Close().
389 DeletePendingObjects();
392 // flush the logged messages if any
393 wxLog::FlushActive();
396 wxUpdateUIEvent::ResetUpdateTime();
401 // Send idle event to window and all subwindows
402 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
404 bool needMore
= false;
406 win
->OnInternalIdle();
408 // should we send idle event to this window?
409 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
410 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
412 event
.SetEventObject(win
);
413 win
->HandleWindowEvent(event
);
415 if (event
.MoreRequested())
418 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
421 wxWindow
*child
= node
->GetData();
422 if (SendIdleEvents(child
, event
))
425 node
= node
->GetNext();
431 // ----------------------------------------------------------------------------
432 // wxGUIAppTraitsBase
433 // ----------------------------------------------------------------------------
437 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
442 // we must have something!
443 return new wxLogStderr
;
449 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
451 // The standard way of printing help on command line arguments (app --help)
452 // is (according to common practice):
453 // - console apps: to stderr (on any platform)
454 // - GUI apps: stderr on Unix platforms (!)
455 // stderr if available and message box otherwise on others
456 // (currently stderr only Windows if app running from console)
458 return new wxMessageOutputStderr
;
460 // wxMessageOutputMessageBox doesn't work under Motif
462 return new wxMessageOutputLog
;
464 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
466 return new wxMessageOutputStderr
;
468 #endif // __UNIX__/!__UNIX__
473 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
475 return new wxFontMapper
;
478 #endif // wxUSE_FONTMAP
480 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
482 // use the default native renderer by default
488 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
490 // under MSW we prefer to use the base class version using ::MessageBox()
491 // even if wxMessageBox() is available because it has less chances to
492 // double fault our app than our wxMessageBox()
494 // under DFB the message dialog is not always functional right now
496 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
498 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
499 return wxAppTraitsBase::ShowAssertDialog(msg
);
500 #else // wxUSE_MSGDLG
501 wxString msgDlg
= msg
;
503 #if wxUSE_STACKWALKER
504 // on Unix stack frame generation may take some time, depending on the
505 // size of the executable mainly... warn the user that we are working
506 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
509 const wxString stackTrace
= GetAssertStackTrace();
510 if ( !stackTrace
.empty() )
511 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
512 #endif // wxUSE_STACKWALKER
514 // this message is intentionally not translated -- it is for
516 msgDlg
+= wxT("\nDo you want to stop the program?\n")
517 wxT("You can also choose [Cancel] to suppress ")
518 wxT("further warnings.");
520 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
521 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
531 //case wxNO: nothing to do
535 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
538 #endif // __WXDEBUG__
540 bool wxGUIAppTraitsBase::HasStderr()
542 // we consider that under Unix stderr always goes somewhere, even if the
543 // user doesn't always see it under GUI desktops
551 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
553 if ( !wxPendingDelete
.Member(object
) )
554 wxPendingDelete
.Append(object
);
557 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
559 wxPendingDelete
.DeleteObject(object
);