1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // ---------------------------------------------------------------------------
21 #pragma implementation "appbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
33 #include "wx/bitmap.h"
37 #include "wx/msgdlg.h"
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
42 #include "wx/apptrait.h"
44 #include "wx/fontmap.h"
45 #endif // wxUSE_FONTMAP
46 #include "wx/msgout.h"
47 #include "wx/thread.h"
50 // ============================================================================
51 // wxAppBase implementation
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxAppBase::wxAppBase()
60 m_topWindow
= (wxWindow
*)NULL
;
61 m_useBestVisual
= FALSE
;
64 // We don't want to exit the app if the user code shows a dialog from its
65 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
66 // to Yes initially as this dialog would be the last top level window.
67 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
68 // when we enter our OnRun() because we do want the default behaviour from
69 // then on. But this would be a problem if the user code calls
70 // SetExitOnFrameDelete(FALSE) from OnInit().
72 // So we use the special "Later" value which is such that
73 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
74 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
75 // call) overwrite in OnRun()
76 m_exitOnFrameDelete
= Later
;
79 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
81 if ( !wxAppConsole::Initialize(argc
, argv
) )
85 wxPendingEventsLocker
= new wxCriticalSection
;
88 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
89 wxTheColourDatabase
->Initialize();
91 wxInitializeStockLists();
92 wxInitializeStockObjects();
94 wxBitmap::InitStandardHandlers();
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 wxAppBase::~wxAppBase()
105 // this destructor is required for Darwin
108 void wxAppBase::CleanUp()
110 // one last chance for pending objects to be cleaned up
111 DeletePendingObjects();
113 wxBitmap::CleanUpHandlers();
115 wxDeleteStockObjects();
117 wxDeleteStockLists();
119 delete wxTheColourDatabase
;
120 wxTheColourDatabase
= NULL
;
123 delete wxPendingEvents
;
124 wxPendingEvents
= NULL
;
126 delete wxPendingEventsLocker
;
127 wxPendingEventsLocker
= NULL
;
130 // If we don't do the following, we get an apparent memory leak.
131 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
132 #endif // wxUSE_VALIDATORS
133 #endif // wxUSE_THREADS
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 bool wxAppBase::OnInitGui()
142 #ifdef __WXUNIVERSAL__
143 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
145 #endif // __WXUNIVERSAL__
150 int wxAppBase::OnRun()
152 // see the comment in ctor: if the initial value hasn't been changed, use
153 // the default Yes from now on
154 if ( m_exitOnFrameDelete
== Later
)
156 m_exitOnFrameDelete
= Yes
;
158 //else: it has been changed, assume the user knows what he is doing
163 void wxAppBase::Exit()
168 wxAppTraits
*wxAppBase::CreateTraits()
170 return new wxGUIAppTraits
;
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
179 if ( active
== m_isActive
)
184 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
185 event
.SetEventObject(this);
187 (void)ProcessEvent(event
);
190 void wxAppBase::DeletePendingObjects()
192 wxNode
*node
= wxPendingDelete
.GetFirst();
195 wxObject
*obj
= node
->GetData();
199 if (wxPendingDelete
.Member(obj
))
202 // Deleting one object may have deleted other pending
203 // objects, so start from beginning of list again.
204 node
= wxPendingDelete
.GetFirst();
208 // ----------------------------------------------------------------------------
209 // wxGUIAppTraitsBase
210 // ----------------------------------------------------------------------------
214 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
221 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
223 // The standard way of printing help on command line arguments (app --help)
224 // is (according to common practice):
225 // - console apps: to stderr (on any platform)
226 // - GUI apps: stderr on Unix platforms (!)
227 // message box under Windows and others
229 return new wxMessageOutputStderr
;
231 // wxMessageOutputMessageBox doesn't work under Motif
233 return new wxMessageOutputLog
;
235 return new wxMessageOutputMessageBox
;
237 #endif // __UNIX__/!__UNIX__
242 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
244 return new wxFontMapper
;
247 #endif // wxUSE_FONTMAP
251 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
253 // under MSW we prefer to use the base class version using ::MessageBox()
254 // even if wxMessageBox() is available because it has less chances to
255 // double fault our app than our wxMessageBox()
256 #if defined(__WXMSW__) || !wxUSE_MSGDLG
257 return wxAppTraitsBase::ShowAssertDialog(msg
);
258 #else // wxUSE_MSGDLG
259 // this message is intentionally not translated -- it is for
261 wxString
msgDlg(msg
);
262 msgDlg
+= wxT("\nDo you want to stop the program?\n")
263 wxT("You can also choose [Cancel] to suppress ")
264 wxT("further warnings.");
266 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
267 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
277 //case wxNO: nothing to do
281 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
284 #endif // __WXDEBUG__
286 bool wxGUIAppTraitsBase::HasStderr()
288 // we consider that under Unix stderr always goes somewhere, even if the
289 // user doesn't always see it under GUI desktops
297 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
299 if ( !wxPendingDelete
.Member(object
) )
300 wxPendingDelete
.Append(object
);
303 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
305 wxPendingDelete
.DeleteObject(object
);