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()
74 m_topWindow
= (wxWindow
*)NULL
;
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 // process pending wx events before sending idle events
357 ProcessPendingEvents();
360 bool needMore
= false;
361 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
364 wxWindow
* win
= node
->GetData();
365 if (SendIdleEvents(win
, event
))
367 node
= node
->GetNext();
370 if (wxAppConsole::ProcessIdle())
373 // 'Garbage' collection of windows deleted with Close().
374 DeletePendingObjects();
377 // flush the logged messages if any
378 wxLog::FlushActive();
381 wxUpdateUIEvent::ResetUpdateTime();
386 // Send idle event to window and all subwindows
387 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
389 bool needMore
= false;
391 win
->OnInternalIdle();
393 // should we send idle event to this window?
394 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
395 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
397 event
.SetEventObject(win
);
398 win
->HandleWindowEvent(event
);
400 if (event
.MoreRequested())
403 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
406 wxWindow
*child
= node
->GetData();
407 if (SendIdleEvents(child
, event
))
410 node
= node
->GetNext();
416 // ----------------------------------------------------------------------------
417 // wxGUIAppTraitsBase
418 // ----------------------------------------------------------------------------
422 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
427 // we must have something!
428 return new wxLogStderr
;
434 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
436 // The standard way of printing help on command line arguments (app --help)
437 // is (according to common practice):
438 // - console apps: to stderr (on any platform)
439 // - GUI apps: stderr on Unix platforms (!)
440 // stderr if available and message box otherwise on others
441 // (currently stderr only Windows if app running from console)
443 return new wxMessageOutputStderr
;
445 // wxMessageOutputMessageBox doesn't work under Motif
447 return new wxMessageOutputLog
;
449 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
451 return new wxMessageOutputStderr
;
453 #endif // __UNIX__/!__UNIX__
458 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
460 return new wxFontMapper
;
463 #endif // wxUSE_FONTMAP
465 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
467 // use the default native renderer by default
473 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
475 // under MSW we prefer to use the base class version using ::MessageBox()
476 // even if wxMessageBox() is available because it has less chances to
477 // double fault our app than our wxMessageBox()
479 // under DFB the message dialog is not always functional right now
481 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
483 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
484 return wxAppTraitsBase::ShowAssertDialog(msg
);
485 #else // wxUSE_MSGDLG
486 wxString msgDlg
= msg
;
488 #if wxUSE_STACKWALKER
489 // on Unix stack frame generation may take some time, depending on the
490 // size of the executable mainly... warn the user that we are working
491 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
494 const wxString stackTrace
= GetAssertStackTrace();
495 if ( !stackTrace
.empty() )
496 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
497 #endif // wxUSE_STACKWALKER
499 // this message is intentionally not translated -- it is for
501 msgDlg
+= wxT("\nDo you want to stop the program?\n")
502 wxT("You can also choose [Cancel] to suppress ")
503 wxT("further warnings.");
505 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
506 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
516 //case wxNO: nothing to do
520 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
523 #endif // __WXDEBUG__
525 bool wxGUIAppTraitsBase::HasStderr()
527 // we consider that under Unix stderr always goes somewhere, even if the
528 // user doesn't always see it under GUI desktops
536 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
538 if ( !wxPendingDelete
.Member(object
) )
539 wxPendingDelete
.Append(object
);
542 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
544 wxPendingDelete
.DeleteObject(object
);