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"
46 #include "wx/stackwalk.h"
47 #endif // wxUSE_STACKWALKER
50 #if defined(__WXMSW__)
51 #include "wx/msw/private.h" // includes windows.h for LOGFONT
55 #include "wx/fontmap.h"
56 #endif // wxUSE_FONTMAP
58 // DLL options compatibility check:
60 WX_CHECK_BUILD_OPTIONS("wxCore")
62 WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
64 // ============================================================================
65 // wxAppBase implementation
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 wxAppBase::wxAppBase()
76 m_useBestVisual
= false;
77 m_forceTrueColour
= false;
81 m_isInsideYield
= false;
83 // We don't want to exit the app if the user code shows a dialog from its
84 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
85 // to Yes initially as this dialog would be the last top level window.
86 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
87 // when we enter our OnRun() because we do want the default behaviour from
88 // then on. But this would be a problem if the user code calls
89 // SetExitOnFrameDelete(false) from OnInit().
91 // So we use the special "Later" value which is such that
92 // GetExitOnFrameDelete() returns false for it but which we know we can
93 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
94 // call) overwrite in OnRun()
95 m_exitOnFrameDelete
= Later
;
98 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
100 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
103 wxInitializeStockLists();
105 wxBitmap::InitStandardHandlers();
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 wxAppBase::~wxAppBase()
116 // this destructor is required for Darwin
119 void wxAppBase::CleanUp()
121 // clean up all the pending objects
122 DeletePendingObjects();
124 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
125 // when destroyed, so iterate until none are left)
126 while ( !wxTopLevelWindows
.empty() )
128 // do not use Destroy() here as it only puts the TLW in pending list
129 // but we want to delete them now
130 delete wxTopLevelWindows
.GetFirst()->GetData();
133 // undo everything we did in Initialize() above
134 wxBitmap::CleanUpHandlers();
136 wxStockGDI::DeleteAll();
138 wxDeleteStockLists();
140 delete wxTheColourDatabase
;
141 wxTheColourDatabase
= NULL
;
143 wxAppConsole::CleanUp();
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 wxWindow
* wxAppBase::GetTopWindow() const
152 wxWindow
* window
= m_topWindow
;
153 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
154 window
= wxTopLevelWindows
.GetFirst()->GetData();
158 wxVideoMode
wxAppBase::GetDisplayMode() const
160 return wxVideoMode();
163 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
166 const wxLocale
*const locale
= wxGetLocale();
169 const wxLanguageInfo
*const
170 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
173 return info
->LayoutDirection
;
178 return wxLayout_Default
;
181 #if wxUSE_CMDLINE_PARSER
183 // ----------------------------------------------------------------------------
184 // GUI-specific command line options handling
185 // ----------------------------------------------------------------------------
187 #define OPTION_THEME "theme"
188 #define OPTION_MODE "mode"
190 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
192 // first add the standard non GUI options
193 wxAppConsole::OnInitCmdLine(parser
);
195 // the standard command line options
196 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
198 #ifdef __WXUNIVERSAL__
203 gettext_noop("specify the theme to use"),
204 wxCMD_LINE_VAL_STRING
,
207 #endif // __WXUNIVERSAL__
209 #if defined(__WXMGL__)
210 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
211 // should provide this option. That's why it is in common/appcmn.cpp
212 // and not mgl/app.cpp
217 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
218 wxCMD_LINE_VAL_STRING
,
227 parser
.SetDesc(cmdLineGUIDesc
);
230 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
232 #ifdef __WXUNIVERSAL__
234 if ( parser
.Found(OPTION_THEME
, &themeName
) )
236 wxTheme
*theme
= wxTheme::Create(themeName
);
239 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
243 // Delete the defaultly created theme and set the new theme.
244 delete wxTheme::Get();
247 #endif // __WXUNIVERSAL__
249 #if defined(__WXMGL__)
251 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
254 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
256 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
260 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
265 return wxAppConsole::OnCmdLineParsed(parser
);
268 #endif // wxUSE_CMDLINE_PARSER
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 bool wxAppBase::OnInitGui()
276 #ifdef __WXUNIVERSAL__
277 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
279 #endif // __WXUNIVERSAL__
284 int wxAppBase::OnRun()
286 // see the comment in ctor: if the initial value hasn't been changed, use
287 // the default Yes from now on
288 if ( m_exitOnFrameDelete
== Later
)
290 m_exitOnFrameDelete
= Yes
;
292 //else: it has been changed, assume the user knows what he is doing
294 return wxAppConsole::OnRun();
297 int wxAppBase::OnExit()
299 #ifdef __WXUNIVERSAL__
300 delete wxTheme::Set(NULL
);
301 #endif // __WXUNIVERSAL__
303 return wxAppConsole::OnExit();
306 wxAppTraits
*wxAppBase::CreateTraits()
308 return new wxGUIAppTraits
;
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
317 if ( active
== m_isActive
)
322 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
323 event
.SetEventObject(this);
325 (void)ProcessEvent(event
);
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 void wxAppBase::DeletePendingObjects()
334 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
337 wxObject
*obj
= node
->GetData();
339 // remove it from the list first so that if we get back here somehow
340 // during the object deletion (e.g. wxYield called from its dtor) we
341 // wouldn't try to delete it the second time
342 if ( wxPendingDelete
.Member(obj
) )
343 wxPendingDelete
.Erase(node
);
347 // Deleting one object may have deleted other pending
348 // objects, so start from beginning of list again.
349 node
= wxPendingDelete
.GetFirst();
353 // Returns true if more time is needed.
354 bool wxAppBase::ProcessIdle()
356 // call the base class version first, it will process the pending events
357 // (which should be done before the idle events generation) and send the
358 // idle event to wxTheApp itself
359 bool needMore
= wxAppConsoleBase::ProcessIdle();
361 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
364 wxWindow
* win
= node
->GetData();
365 if (SendIdleEvents(win
, event
))
367 node
= node
->GetNext();
370 // 'Garbage' collection of windows deleted with Close().
371 DeletePendingObjects();
374 // flush the logged messages if any
375 wxLog::FlushActive();
378 wxUpdateUIEvent::ResetUpdateTime();
383 // Send idle event to window and all subwindows
384 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
386 bool needMore
= false;
388 win
->OnInternalIdle();
390 // should we send idle event to this window?
391 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
392 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
394 event
.SetEventObject(win
);
395 win
->HandleWindowEvent(event
);
397 if (event
.MoreRequested())
400 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
403 wxWindow
*child
= node
->GetData();
404 if (SendIdleEvents(child
, event
))
407 node
= node
->GetNext();
413 // ----------------------------------------------------------------------------
414 // wxGUIAppTraitsBase
415 // ----------------------------------------------------------------------------
419 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
424 // we must have something!
425 return new wxLogStderr
;
431 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
433 // The standard way of printing help on command line arguments (app --help)
434 // is (according to common practice):
435 // - console apps: to stderr (on any platform)
436 // - GUI apps: stderr on Unix platforms (!)
437 // stderr if available and message box otherwise on others
438 // (currently stderr only Windows if app running from console)
440 return new wxMessageOutputStderr
;
442 // wxMessageOutputMessageBox doesn't work under Motif
444 return new wxMessageOutputLog
;
446 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
448 return new wxMessageOutputStderr
;
450 #endif // __UNIX__/!__UNIX__
455 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
457 return new wxFontMapper
;
460 #endif // wxUSE_FONTMAP
462 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
464 // use the default native renderer by default
470 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
472 // under MSW we prefer to use the base class version using ::MessageBox()
473 // even if wxMessageBox() is available because it has less chances to
474 // double fault our app than our wxMessageBox()
476 // under DFB the message dialog is not always functional right now
478 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
480 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
481 return wxAppTraitsBase::ShowAssertDialog(msg
);
482 #else // wxUSE_MSGDLG
483 wxString msgDlg
= msg
;
485 #if wxUSE_STACKWALKER
486 // on Unix stack frame generation may take some time, depending on the
487 // size of the executable mainly... warn the user that we are working
488 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
491 const wxString stackTrace
= GetAssertStackTrace();
492 if ( !stackTrace
.empty() )
493 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
494 #endif // wxUSE_STACKWALKER
496 // this message is intentionally not translated -- it is for
498 msgDlg
+= wxT("\nDo you want to stop the program?\n")
499 wxT("You can also choose [Cancel] to suppress ")
500 wxT("further warnings.");
502 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
503 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
513 //case wxNO: nothing to do
517 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
520 #endif // __WXDEBUG__
522 bool wxGUIAppTraitsBase::HasStderr()
524 // we consider that under Unix stderr always goes somewhere, even if the
525 // user doesn't always see it under GUI desktops
533 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
535 if ( !wxPendingDelete
.Member(object
) )
536 wxPendingDelete
.Append(object
);
539 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
541 wxPendingDelete
.DeleteObject(object
);