1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppConsole and 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 // We don't want to exit the app if the user code shows a dialog from its
82 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
83 // to Yes initially as this dialog would be the last top level window.
84 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
85 // when we enter our OnRun() because we do want the default behaviour from
86 // then on. But this would be a problem if the user code calls
87 // SetExitOnFrameDelete(false) from OnInit().
89 // So we use the special "Later" value which is such that
90 // GetExitOnFrameDelete() returns false for it but which we know we can
91 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
92 // call) overwrite in OnRun()
93 m_exitOnFrameDelete
= Later
;
96 bool wxAppBase::Initialize(int& argcOrig
, wxChar
**argvOrig
)
98 if ( !wxAppConsole::Initialize(argcOrig
, argvOrig
) )
101 wxInitializeStockLists();
103 wxBitmap::InitStandardHandlers();
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 wxAppBase::~wxAppBase()
114 // this destructor is required for Darwin
117 void wxAppBase::CleanUp()
119 // clean up all the pending objects
120 DeletePendingObjects();
122 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
123 // when destroyed, so iterate until none are left)
124 while ( !wxTopLevelWindows
.empty() )
126 // do not use Destroy() here as it only puts the TLW in pending list
127 // but we want to delete them now
128 delete wxTopLevelWindows
.GetFirst()->GetData();
131 // undo everything we did in Initialize() above
132 wxBitmap::CleanUpHandlers();
134 wxStockGDI::DeleteAll();
136 wxDeleteStockLists();
138 delete wxTheColourDatabase
;
139 wxTheColourDatabase
= NULL
;
141 wxAppConsole::CleanUp();
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 wxWindow
* wxAppBase::GetTopWindow() const
150 wxWindow
* window
= m_topWindow
;
151 if (window
== NULL
&& wxTopLevelWindows
.GetCount() > 0)
152 window
= wxTopLevelWindows
.GetFirst()->GetData();
156 wxVideoMode
wxAppBase::GetDisplayMode() const
158 return wxVideoMode();
161 wxLayoutDirection
wxAppBase::GetLayoutDirection() const
164 const wxLocale
*const locale
= wxGetLocale();
167 const wxLanguageInfo
*const
168 info
= wxLocale::GetLanguageInfo(locale
->GetLanguage());
171 return info
->LayoutDirection
;
176 return wxLayout_Default
;
179 #if wxUSE_CMDLINE_PARSER
181 // ----------------------------------------------------------------------------
182 // GUI-specific command line options handling
183 // ----------------------------------------------------------------------------
185 #define OPTION_THEME "theme"
186 #define OPTION_MODE "mode"
188 void wxAppBase::OnInitCmdLine(wxCmdLineParser
& parser
)
190 // first add the standard non GUI options
191 wxAppConsole::OnInitCmdLine(parser
);
193 // the standard command line options
194 static const wxCmdLineEntryDesc cmdLineGUIDesc
[] =
196 #ifdef __WXUNIVERSAL__
201 gettext_noop("specify the theme to use"),
202 wxCMD_LINE_VAL_STRING
,
205 #endif // __WXUNIVERSAL__
207 #if defined(__WXMGL__)
208 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
209 // should provide this option. That's why it is in common/appcmn.cpp
210 // and not mgl/app.cpp
215 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
216 wxCMD_LINE_VAL_STRING
,
225 parser
.SetDesc(cmdLineGUIDesc
);
228 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
230 #ifdef __WXUNIVERSAL__
232 if ( parser
.Found(OPTION_THEME
, &themeName
) )
234 wxTheme
*theme
= wxTheme::Create(themeName
);
237 wxLogError(_("Unsupported theme '%s'."), themeName
.c_str());
241 // Delete the defaultly created theme and set the new theme.
242 delete wxTheme::Get();
245 #endif // __WXUNIVERSAL__
247 #if defined(__WXMGL__)
249 if ( parser
.Found(OPTION_MODE
, &modeDesc
) )
252 if ( wxSscanf(modeDesc
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3 )
254 wxLogError(_("Invalid display mode specification '%s'."), modeDesc
.c_str());
258 if ( !SetDisplayMode(wxVideoMode(w
, h
, bpp
)) )
263 return wxAppConsole::OnCmdLineParsed(parser
);
266 #endif // wxUSE_CMDLINE_PARSER
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 bool wxAppBase::OnInitGui()
274 #ifdef __WXUNIVERSAL__
275 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
277 #endif // __WXUNIVERSAL__
282 int wxAppBase::OnRun()
284 // see the comment in ctor: if the initial value hasn't been changed, use
285 // the default Yes from now on
286 if ( m_exitOnFrameDelete
== Later
)
288 m_exitOnFrameDelete
= Yes
;
290 //else: it has been changed, assume the user knows what he is doing
292 return wxAppConsole::OnRun();
295 int wxAppBase::OnExit()
297 #ifdef __WXUNIVERSAL__
298 delete wxTheme::Set(NULL
);
299 #endif // __WXUNIVERSAL__
301 return wxAppConsole::OnExit();
304 wxAppTraits
*wxAppBase::CreateTraits()
306 return new wxGUIAppTraits
;
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
315 if ( active
== m_isActive
)
320 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
321 event
.SetEventObject(this);
323 (void)ProcessEvent(event
);
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 void wxAppBase::DeletePendingObjects()
332 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
335 wxObject
*obj
= node
->GetData();
337 // remove it from the list first so that if we get back here somehow
338 // during the object deletion (e.g. wxYield called from its dtor) we
339 // wouldn't try to delete it the second time
340 if ( wxPendingDelete
.Member(obj
) )
341 wxPendingDelete
.Erase(node
);
345 // Deleting one object may have deleted other pending
346 // objects, so start from beginning of list again.
347 node
= wxPendingDelete
.GetFirst();
351 // Returns true if more time is needed.
352 bool wxAppBase::ProcessIdle()
354 // process pending wx events before sending idle events
355 ProcessPendingEvents();
358 bool needMore
= false;
359 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
362 wxWindow
* win
= node
->GetData();
363 if (SendIdleEvents(win
, event
))
365 node
= node
->GetNext();
368 if (wxAppConsole::ProcessIdle())
371 // 'Garbage' collection of windows deleted with Close().
372 DeletePendingObjects();
375 // flush the logged messages if any
376 wxLog::FlushActive();
379 wxUpdateUIEvent::ResetUpdateTime();
384 // Send idle event to window and all subwindows
385 bool wxAppBase::SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
)
387 bool needMore
= false;
389 win
->OnInternalIdle();
391 // should we send idle event to this window?
392 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL
||
393 win
->HasExtraStyle(wxWS_EX_PROCESS_IDLE
) )
395 event
.SetEventObject(win
);
396 win
->HandleWindowEvent(event
);
398 if (event
.MoreRequested())
401 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
404 wxWindow
*child
= node
->GetData();
405 if (SendIdleEvents(child
, event
))
408 node
= node
->GetNext();
414 // ----------------------------------------------------------------------------
415 // wxGUIAppTraitsBase
416 // ----------------------------------------------------------------------------
420 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
425 // we must have something!
426 return new wxLogStderr
;
432 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
434 // The standard way of printing help on command line arguments (app --help)
435 // is (according to common practice):
436 // - console apps: to stderr (on any platform)
437 // - GUI apps: stderr on Unix platforms (!)
438 // stderr if available and message box otherwise on others
439 // (currently stderr only Windows if app running from console)
441 return new wxMessageOutputStderr
;
443 // wxMessageOutputMessageBox doesn't work under Motif
445 return new wxMessageOutputLog
;
447 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR
);
449 return new wxMessageOutputStderr
;
451 #endif // __UNIX__/!__UNIX__
456 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
458 return new wxFontMapper
;
461 #endif // wxUSE_FONTMAP
463 wxRendererNative
*wxGUIAppTraitsBase::CreateRenderer()
465 // use the default native renderer by default
471 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
473 // under MSW we prefer to use the base class version using ::MessageBox()
474 // even if wxMessageBox() is available because it has less chances to
475 // double fault our app than our wxMessageBox()
477 // under DFB the message dialog is not always functional right now
479 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
481 #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG
482 return wxAppTraitsBase::ShowAssertDialog(msg
);
483 #else // wxUSE_MSGDLG
484 wxString msgDlg
= msg
;
486 #if wxUSE_STACKWALKER
487 // on Unix stack frame generation may take some time, depending on the
488 // size of the executable mainly... warn the user that we are working
489 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
492 const wxString stackTrace
= GetAssertStackTrace();
493 if ( !stackTrace
.empty() )
494 msgDlg
<< _T("\n\nCall stack:\n") << stackTrace
;
495 #endif // wxUSE_STACKWALKER
497 // this message is intentionally not translated -- it is for
499 msgDlg
+= wxT("\nDo you want to stop the program?\n")
500 wxT("You can also choose [Cancel] to suppress ")
501 wxT("further warnings.");
503 switch ( wxMessageBox(msgDlg
, wxT("wxWidgets Debug Alert"),
504 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
514 //case wxNO: nothing to do
518 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
521 #endif // __WXDEBUG__
523 bool wxGUIAppTraitsBase::HasStderr()
525 // we consider that under Unix stderr always goes somewhere, even if the
526 // user doesn't always see it under GUI desktops
534 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
536 if ( !wxPendingDelete
.Member(object
) )
537 wxPendingDelete
.Append(object
);
540 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
542 wxPendingDelete
.DeleteObject(object
);