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__)
36 #include "wx/msgdlg.h"
39 #include "wx/apptrait.h"
41 #include "wx/fontmap.h"
42 #endif // wxUSE_FONTMAP
43 #include "wx/msgout.h"
44 #include "wx/thread.h"
47 // ============================================================================
48 // wxAppBase implementation
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 wxAppBase::wxAppBase()
57 m_topWindow
= (wxWindow
*)NULL
;
58 m_useBestVisual
= FALSE
;
61 // We don't want to exit the app if the user code shows a dialog from its
62 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
63 // to Yes initially as this dialog would be the last top level window.
64 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
65 // when we enter our OnRun() because we do want the default behaviour from
66 // then on. But this would be a problem if the user code calls
67 // SetExitOnFrameDelete(FALSE) from OnInit().
69 // So we use the special "Later" value which is such that
70 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
71 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
72 // call) overwrite in OnRun()
73 m_exitOnFrameDelete
= Later
;
76 bool wxAppBase::Initialize(int argc
, wxChar
**argv
)
78 if ( !wxAppConsole::Initialize(argc
, argv
) )
81 // for compatibility call the old initialization function too
84 wxAppConsole::CleanUp();
90 wxPendingEventsLocker
= new wxCriticalSection
;
93 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
94 wxTheColourDatabase
->Initialize();
96 wxInitializeStockLists();
97 wxInitializeStockObjects();
99 wxBitmap::InitStandardHandlers();
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 wxAppBase::~wxAppBase()
110 // this destructor is required for Darwin
113 void wxAppBase::CleanUp()
115 // one last chance for pending objects to be cleaned up
116 DeletePendingObjects();
118 wxBitmap::CleanUpHandlers();
120 wxDeleteStockObjects();
122 wxDeleteStockLists();
124 delete wxTheColourDatabase
;
125 wxTheColourDatabase
= NULL
;
128 delete wxPendingEvents
;
129 wxPendingEvents
= NULL
;
131 delete wxPendingEventsLocker
;
132 wxPendingEventsLocker
= NULL
;
135 // If we don't do the following, we get an apparent memory leak.
136 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
137 #endif // wxUSE_VALIDATORS
138 #endif // wxUSE_THREADS
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 bool wxAppBase::OnInitGui()
147 #ifdef __WXUNIVERSAL__
148 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
150 #endif // __WXUNIVERSAL__
155 int wxAppBase::OnRun()
157 // see the comment in ctor: if the initial value hasn't been changed, use
158 // the default Yes from now on
159 if ( m_exitOnFrameDelete
== Later
)
161 m_exitOnFrameDelete
= Yes
;
163 //else: it has been changed, assume the user knows what he is doing
168 void wxAppBase::Exit()
173 wxAppTraits
*wxAppBase::CreateTraits()
175 return wxAppTraits::CreateGUI();
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
184 if ( active
== m_isActive
)
189 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
190 event
.SetEventObject(this);
192 (void)ProcessEvent(event
);
195 void wxAppBase::DeletePendingObjects()
197 wxNode
*node
= wxPendingDelete
.GetFirst();
200 wxObject
*obj
= node
->GetData();
204 if (wxPendingDelete
.Member(obj
))
207 // Deleting one object may have deleted other pending
208 // objects, so start from beginning of list again.
209 node
= wxPendingDelete
.GetFirst();
213 // ----------------------------------------------------------------------------
214 // wxGUIAppTraitsBase
215 // ----------------------------------------------------------------------------
219 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
226 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
228 // The standard way of printing help on command line arguments (app --help)
229 // is (according to common practice):
230 // - console apps: to stderr (on any platform)
231 // - GUI apps: stderr on Unix platforms (!)
232 // message box under Windows and others
234 return new wxMessageOutputStderr
;
236 // wxMessageOutputMessageBox doesn't work under Motif
238 return new wxMessageOutputLog
;
240 return new wxMessageOutputMessageBox
;
242 #endif // __UNIX__/!__UNIX__
247 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
249 return new wxFontMapper
;
252 #endif // wxUSE_FONTMAP
256 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
258 // under MSW we prefer to use the base class version using ::MessageBox()
259 // even if wxMessageBox() is available because it has less chances to
260 // double fault our app than our wxMessageBox()
261 #if defined(__WXMSW__) || !wxUSE_MSGDLG
262 return wxAppTraitsBase::ShowAssertDialog(msg
);
263 #else // wxUSE_MSGDLG
264 // this message is intentionally not translated -- it is for
266 wxString
msgDlg(msg
);
267 msgDlg
+= wxT("\nDo you want to stop the program?\n")
268 wxT("You can also choose [Cancel] to suppress ")
269 wxT("further warnings.");
271 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
272 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
282 //case wxNO: nothing to do
286 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
289 #endif // __WXDEBUG__
291 bool wxGUIAppTraitsBase::HasStderr()
293 // we consider that under Unix stderr always goes somewhere, even if the
294 // user doesn't always see it under GUI desktops
302 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
304 if ( !wxPendingDelete
.Member(object
) )
305 wxPendingDelete
.Append(object
);
308 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
310 wxPendingDelete
.DeleteObject(object
);
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 wxAppTraits
*wxAppTraitsBase::CreateGUI()
319 return new wxGUIAppTraits
;