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;
82 m_eventsToProcessInsideYield
= wxEVT_CATEGORY_ALL
;
84 // We don't want to exit the app if the user code shows a dialog from its
85 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
86 // to Yes initially as this dialog would be the last top level window.
87 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
88 // when we enter our OnRun() because we do want the default behaviour from
89 // then on. But this would be a problem if the user code calls
90 // SetExitOnFrameDelete(false) from OnInit().
92 // So we use the special "Later" value which is such that
93 // GetExitOnFrameDelete() returns false for it but which we know we can
94 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
95 // call) overwrite in OnRun()
96 m_exitOnFrameDelete
= Later
;
99 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
101 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
104 wxInitializeStockLists();
106 wxBitmap::InitStandardHandlers();
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 wxAppBase::~wxAppBase()
117 // this destructor is required for Darwin
120 void wxAppBase::CleanUp()
122 // clean up all the pending objects
123 DeletePendingObjects();
125 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
126 // when destroyed, so iterate until none are left)
127 while ( !wxTopLevelWindows
.empty() )
129 // do not use Destroy() here as it only puts the TLW in pending list
130 // but we want to delete them now
131 delete wxTopLevelWindows
.GetFirst()->GetData();
134 // undo everything we did in Initialize() above
135 wxBitmap::CleanUpHandlers();
137 wxStockGDI::DeleteAll();
139 wxDeleteStockLists();
141 delete wxTheColourDatabase
;
142 wxTheColourDatabase
= NULL
;
144 wxAppConsole::CleanUp();
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 wxWindow
* wxAppBase::GetTopWindow() const
153 wxWindow
* window
= m_topWindow
;
154 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
155 window
= wxTopLevelWindows
.GetFirst()->GetData();
159 wxVideoMode
wxAppBase::GetDisplayMode() const
161 return wxVideoMode();
164 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
167 const wxLocale
*const locale
= wxGetLocale();
170 const wxLanguageInfo
*const
171 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
174 return info
->LayoutDirection
;
179 return wxLayout_Default
;
182 #if wxUSE_CMDLINE_PARSER
184 // ----------------------------------------------------------------------------
185 // GUI-specific command line options handling
186 // ----------------------------------------------------------------------------
188 #define OPTION_THEME "theme"
189 #define OPTION_MODE "mode"
191 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
193 // first add the standard non GUI options
194 wxAppConsole::OnInitCmdLine(parser
);
196 // the standard command line options
197 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
199 #ifdef __WXUNIVERSAL__
204 gettext_noop("specify the theme to use"),
205 wxCMD_LINE_VAL_STRING
,
208 #endif // __WXUNIVERSAL__
210 #if defined(__WXMGL__)
211 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
212 // should provide this option. That's why it is in common/appcmn.cpp
213 // and not mgl/app.cpp
218 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
219 wxCMD_LINE_VAL_STRING
,
228 parser
.SetDesc(cmdLineGUIDesc
);
231 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
233 #ifdef __WXUNIVERSAL__
235 if ( parser
.Found(OPTION_THEME
, &themeName
) )
237 wxTheme
*theme
= wxTheme::Create(themeName
);
240 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
244 // Delete the defaultly created theme and set the new theme.
245 delete wxTheme::Get();
248 #endif // __WXUNIVERSAL__
250 #if defined(__WXMGL__)
252 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
255 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
257 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
261 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
266 return wxAppConsole::OnCmdLineParsed(parser
);
269 #endif // wxUSE_CMDLINE_PARSER
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 bool wxAppBase::OnInitGui()
277 #ifdef __WXUNIVERSAL__
278 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
280 #endif // __WXUNIVERSAL__
285 int wxAppBase::OnRun()
287 // see the comment in ctor: if the initial value hasn't been changed, use
288 // the default Yes from now on
289 if ( m_exitOnFrameDelete
== Later
)
291 m_exitOnFrameDelete
= Yes
;
293 //else: it has been changed, assume the user knows what he is doing
295 return wxAppConsole::OnRun();
298 int wxAppBase::OnExit()
300 #ifdef __WXUNIVERSAL__
301 delete wxTheme::Set(NULL
);
302 #endif // __WXUNIVERSAL__
304 return wxAppConsole::OnExit();
307 wxAppTraits
*wxAppBase::CreateTraits()
309 return new wxGUIAppTraits
;
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
318 if ( active
== m_isActive
)
323 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
324 event
.SetEventObject(this);
326 (void)ProcessEvent(event
);
329 bool wxAppBase::IsEventAllowedInsideYield(wxEventCategory cat
) const
331 return (m_eventsToProcessInsideYield
& cat
) != 0;
334 bool wxAppBase::SafeYield(wxWindow
*win
, bool onlyIfNeeded
)
336 wxWindowDisabler
wd(win
);
338 return Yield(onlyIfNeeded
);
341 bool wxAppBase::SafeYieldFor(wxWindow
*win
, long eventsToProcess
)
343 wxWindowDisabler
wd(win
);
345 return YieldFor(eventsToProcess
);
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 void wxAppBase::DeletePendingObjects()
355 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
358 wxObject
*obj
= node
->GetData();
360 // remove it from the list first so that if we get back here somehow
361 // during the object deletion (e.g. wxYield called from its dtor) we
362 // wouldn't try to delete it the second time
363 if ( wxPendingDelete
.Member(obj
) )
364 wxPendingDelete
.Erase(node
);
368 // Deleting one object may have deleted other pending
369 // objects, so start from beginning of list again.
370 node
= wxPendingDelete
.GetFirst();
374 // Returns true if more time is needed.
375 bool wxAppBase::ProcessIdle()
377 // call the base class version first, it will process the pending events
378 // (which should be done before the idle events generation) and send the
379 // idle event to wxTheApp itself
380 bool needMore
= wxAppConsoleBase::ProcessIdle();
382 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
385 wxWindow
* win
= node
->GetData();
386 if (SendIdleEvents(win
, event
))
388 node
= node
->GetNext();
391 // 'Garbage' collection of windows deleted with Close().
392 DeletePendingObjects();
395 // flush the logged messages if any
396 wxLog::FlushActive();
399 wxUpdateUIEvent::ResetUpdateTime();
404 // Send idle event to window and all subwindows
405 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
407 bool needMore
= false;
409 win
->OnInternalIdle();
411 // should we send idle event to this window?
412 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
413 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
415 event
.SetEventObject(win
);
416 win
->HandleWindowEvent(event
);
418 if (event
.MoreRequested())
421 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
424 wxWindow
*child
= node
->GetData();
425 if (SendIdleEvents(child
, event
))
428 node
= node
->GetNext();
434 // ----------------------------------------------------------------------------
435 // wxGUIAppTraitsBase
436 // ----------------------------------------------------------------------------
440 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
445 // we must have something!
446 return new wxLogStderr
;
452 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
454 // The standard way of printing help on command line arguments (app --help)
455 // is (according to common practice):
456 // - console apps: to stderr (on any platform)
457 // - GUI apps: stderr on Unix platforms (!)
458 // stderr if available and message box otherwise on others
459 // (currently stderr only Windows if app running from console)
461 return new wxMessageOutputStderr
;
463 // wxMessageOutputMessageBox doesn't work under Motif
465 return new wxMessageOutputLog
;
467 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
469 return new wxMessageOutputStderr
;
471 #endif // __UNIX__/!__UNIX__
476 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
478 return new wxFontMapper
;
481 #endif // wxUSE_FONTMAP
483 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
485 // use the default native renderer by default
491 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
493 // under MSW we prefer to use the base class version using ::MessageBox()
494 // even if wxMessageBox() is available because it has less chances to
495 // double fault our app than our wxMessageBox()
497 // under DFB the message dialog is not always functional right now
499 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
501 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
502 return wxAppTraitsBase::ShowAssertDialog(msg
);
503 #else // wxUSE_MSGDLG
504 wxString msgDlg
= msg
;
506 #if wxUSE_STACKWALKER
507 // on Unix stack frame generation may take some time, depending on the
508 // size of the executable mainly... warn the user that we are working
509 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
512 const wxString stackTrace
= GetAssertStackTrace();
513 if ( !stackTrace
.empty() )
514 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
515 #endif // wxUSE_STACKWALKER
517 // this message is intentionally not translated -- it is for
519 msgDlg
+= wxT("\nDo you want to stop the program?\n")
520 wxT("You can also choose [Cancel] to suppress ")
521 wxT("further warnings.");
523 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
524 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
534 //case wxNO: nothing to do
538 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
541 #endif // __WXDEBUG__
543 bool wxGUIAppTraitsBase::HasStderr()
545 // we consider that under Unix stderr always goes somewhere, even if the
546 // user doesn't always see it under GUI desktops
554 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
556 if ( !wxPendingDelete
.Member(object
) )
557 wxPendingDelete
.Append(object
);
560 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
562 wxPendingDelete
.DeleteObject(object
);