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"
40 #include "wx/apptrait.h"
42 #include "wx/fontmap.h"
43 #endif // wxUSE_FONTMAP
44 #include "wx/msgout.h"
45 #include "wx/thread.h"
48 // ============================================================================
49 // wxAppBase implementation
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 wxAppBase::wxAppBase()
58 m_topWindow
= (wxWindow
*)NULL
;
59 m_useBestVisual
= FALSE
;
62 // We don't want to exit the app if the user code shows a dialog from its
63 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
64 // to Yes initially as this dialog would be the last top level window.
65 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
66 // when we enter our OnRun() because we do want the default behaviour from
67 // then on. But this would be a problem if the user code calls
68 // SetExitOnFrameDelete(FALSE) from OnInit().
70 // So we use the special "Later" value which is such that
71 // GetExitOnFrameDelete() returns FALSE for it but which we know we can
72 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
73 // call) overwrite in OnRun()
74 m_exitOnFrameDelete
= Later
;
77 bool wxAppBase::Initialize(int& argc
, wxChar
**argv
)
79 if ( !wxAppConsole::Initialize(argc
, argv
) )
83 wxPendingEventsLocker
= new wxCriticalSection
;
86 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
87 wxTheColourDatabase
->Initialize();
89 wxInitializeStockLists();
90 wxInitializeStockObjects();
92 wxBitmap::InitStandardHandlers();
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 wxAppBase::~wxAppBase()
103 // this destructor is required for Darwin
106 void wxAppBase::CleanUp()
108 // one last chance for pending objects to be cleaned up
109 DeletePendingObjects();
111 wxBitmap::CleanUpHandlers();
113 wxDeleteStockObjects();
115 wxDeleteStockLists();
117 delete wxTheColourDatabase
;
118 wxTheColourDatabase
= NULL
;
121 delete wxPendingEvents
;
122 wxPendingEvents
= NULL
;
124 delete wxPendingEventsLocker
;
125 wxPendingEventsLocker
= NULL
;
128 // If we don't do the following, we get an apparent memory leak.
129 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
130 #endif // wxUSE_VALIDATORS
131 #endif // wxUSE_THREADS
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 bool wxAppBase::OnInitGui()
140 #ifdef __WXUNIVERSAL__
141 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
143 #endif // __WXUNIVERSAL__
148 int wxAppBase::OnRun()
150 // see the comment in ctor: if the initial value hasn't been changed, use
151 // the default Yes from now on
152 if ( m_exitOnFrameDelete
== Later
)
154 m_exitOnFrameDelete
= Yes
;
156 //else: it has been changed, assume the user knows what he is doing
161 void wxAppBase::Exit()
166 wxAppTraits
*wxAppBase::CreateTraits()
168 return wxAppTraits::CreateGUI();
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 void wxAppBase::SetActive(bool active
, wxWindow
* WXUNUSED(lastFocus
))
177 if ( active
== m_isActive
)
182 wxActivateEvent
event(wxEVT_ACTIVATE_APP
, active
);
183 event
.SetEventObject(this);
185 (void)ProcessEvent(event
);
188 void wxAppBase::DeletePendingObjects()
190 wxNode
*node
= wxPendingDelete
.GetFirst();
193 wxObject
*obj
= node
->GetData();
197 if (wxPendingDelete
.Member(obj
))
200 // Deleting one object may have deleted other pending
201 // objects, so start from beginning of list again.
202 node
= wxPendingDelete
.GetFirst();
206 // ----------------------------------------------------------------------------
207 // wxGUIAppTraitsBase
208 // ----------------------------------------------------------------------------
212 wxLog
*wxGUIAppTraitsBase::CreateLogTarget()
219 wxMessageOutput
*wxGUIAppTraitsBase::CreateMessageOutput()
221 // The standard way of printing help on command line arguments (app --help)
222 // is (according to common practice):
223 // - console apps: to stderr (on any platform)
224 // - GUI apps: stderr on Unix platforms (!)
225 // message box under Windows and others
227 return new wxMessageOutputStderr
;
229 // wxMessageOutputMessageBox doesn't work under Motif
231 return new wxMessageOutputLog
;
233 return new wxMessageOutputMessageBox
;
235 #endif // __UNIX__/!__UNIX__
240 wxFontMapper
*wxGUIAppTraitsBase::CreateFontMapper()
242 return new wxFontMapper
;
245 #endif // wxUSE_FONTMAP
249 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
251 // under MSW we prefer to use the base class version using ::MessageBox()
252 // even if wxMessageBox() is available because it has less chances to
253 // double fault our app than our wxMessageBox()
254 #if defined(__WXMSW__) || !wxUSE_MSGDLG
255 return wxAppTraitsBase::ShowAssertDialog(msg
);
256 #else // wxUSE_MSGDLG
257 // this message is intentionally not translated -- it is for
259 wxString
msgDlg(msg
);
260 msgDlg
+= wxT("\nDo you want to stop the program?\n")
261 wxT("You can also choose [Cancel] to suppress ")
262 wxT("further warnings.");
264 switch ( wxMessageBox(msgDlg
, wxT("wxWindows Debug Alert"),
265 wxYES_NO
| wxCANCEL
| wxICON_STOP
) )
275 //case wxNO: nothing to do
279 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
282 #endif // __WXDEBUG__
284 bool wxGUIAppTraitsBase::HasStderr()
286 // we consider that under Unix stderr always goes somewhere, even if the
287 // user doesn't always see it under GUI desktops
295 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
297 if ( !wxPendingDelete
.Member(object
) )
298 wxPendingDelete
.Append(object
);
301 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject
*object
)
303 wxPendingDelete
.DeleteObject(object
);
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 wxAppTraits
*wxAppTraitsBase::CreateGUI()
312 return new wxGUIAppTraits
;