]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/appcmn.cpp
Check for null pointer.
[wxWidgets.git] / src / common / appcmn.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/appcmn.cpp
3// Purpose: wxAppConsole and wxAppBase methods common to all platforms
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 18.10.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "appbase.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/bitmap.h"
34 #include "wx/intl.h"
35 #include "wx/list.h"
36 #include "wx/log.h"
37 #include "wx/msgdlg.h"
38 #include "wx/bitmap.h"
39 #include "wx/confbase.h"
40#endif
41
42#include "wx/apptrait.h"
43#if wxUSE_FONTMAP
44 #include "wx/fontmap.h"
45#endif // wxUSE_FONTMAP
46#include "wx/msgout.h"
47#include "wx/thread.h"
48#include "wx/utils.h"
49
50// ============================================================================
51// wxAppBase implementation
52// ============================================================================
53
54// ----------------------------------------------------------------------------
55// initialization
56// ----------------------------------------------------------------------------
57
58wxAppBase::wxAppBase()
59{
60 m_topWindow = (wxWindow *)NULL;
61 m_useBestVisual = FALSE;
62 m_isActive = TRUE;
63
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().
71 //
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;
77}
78
79bool wxAppBase::Initialize(int& argc, wxChar **argv)
80{
81 if ( !wxAppConsole::Initialize(argc, argv) )
82 return false;
83
84#if wxUSE_THREADS
85 wxPendingEventsLocker = new wxCriticalSection;
86#endif
87
88 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
89 wxTheColourDatabase->Initialize();
90
91 wxInitializeStockLists();
92 wxInitializeStockObjects();
93
94 wxBitmap::InitStandardHandlers();
95
96 return true;
97}
98
99// ----------------------------------------------------------------------------
100// cleanup
101// ----------------------------------------------------------------------------
102
103wxAppBase::~wxAppBase()
104{
105 // this destructor is required for Darwin
106}
107
108void wxAppBase::CleanUp()
109{
110 // one last chance for pending objects to be cleaned up
111 DeletePendingObjects();
112
113 wxBitmap::CleanUpHandlers();
114
115 wxDeleteStockObjects();
116
117 wxDeleteStockLists();
118
119 delete wxTheColourDatabase;
120 wxTheColourDatabase = NULL;
121
122#if wxUSE_THREADS
123 delete wxPendingEvents;
124 wxPendingEvents = NULL;
125
126 delete wxPendingEventsLocker;
127 wxPendingEventsLocker = NULL;
128
129#if wxUSE_VALIDATORS
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
134}
135
136// ----------------------------------------------------------------------------
137// OnXXX() hooks
138// ----------------------------------------------------------------------------
139
140bool wxAppBase::OnInitGui()
141{
142#ifdef __WXUNIVERSAL__
143 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
144 return FALSE;
145#endif // __WXUNIVERSAL__
146
147 return TRUE;
148}
149
150int wxAppBase::OnRun()
151{
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 )
155 {
156 m_exitOnFrameDelete = Yes;
157 }
158 //else: it has been changed, assume the user knows what he is doing
159
160 return MainLoop();
161}
162
163void wxAppBase::Exit()
164{
165 ExitMainLoop();
166}
167
168wxAppTraits *wxAppBase::CreateTraits()
169{
170 return wxAppTraits::CreateGUI();
171}
172
173// ----------------------------------------------------------------------------
174// misc
175// ----------------------------------------------------------------------------
176
177void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
178{
179 if ( active == m_isActive )
180 return;
181
182 m_isActive = active;
183
184 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
185 event.SetEventObject(this);
186
187 (void)ProcessEvent(event);
188}
189
190void wxAppBase::DeletePendingObjects()
191{
192 wxNode *node = wxPendingDelete.GetFirst();
193 while (node)
194 {
195 wxObject *obj = node->GetData();
196
197 delete obj;
198
199 if (wxPendingDelete.Member(obj))
200 delete node;
201
202 // Deleting one object may have deleted other pending
203 // objects, so start from beginning of list again.
204 node = wxPendingDelete.GetFirst();
205 }
206}
207
208// ----------------------------------------------------------------------------
209// wxGUIAppTraitsBase
210// ----------------------------------------------------------------------------
211
212#if wxUSE_LOG
213
214wxLog *wxGUIAppTraitsBase::CreateLogTarget()
215{
216 return new wxLogGui;
217}
218
219#endif // wxUSE_LOG
220
221wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
222{
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
228#ifdef __UNIX__
229 return new wxMessageOutputStderr;
230#else // !__UNIX__
231 // wxMessageOutputMessageBox doesn't work under Motif
232 #ifdef __WXMOTIF__
233 return new wxMessageOutputLog;
234 #else
235 return new wxMessageOutputMessageBox;
236 #endif
237#endif // __UNIX__/!__UNIX__
238}
239
240#if wxUSE_FONTMAP
241
242wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
243{
244 return new wxFontMapper;
245}
246
247#endif // wxUSE_FONTMAP
248
249#ifdef __WXDEBUG__
250
251bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
252{
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
260 // developpers only
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.");
265
266 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
267 wxYES_NO | wxCANCEL | wxICON_STOP ) )
268 {
269 case wxYES:
270 wxTrap();
271 break;
272
273 case wxCANCEL:
274 // no more asserts
275 return true;
276
277 //case wxNO: nothing to do
278 }
279
280 return false;
281#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
282}
283
284#endif // __WXDEBUG__
285
286bool wxGUIAppTraitsBase::HasStderr()
287{
288 // we consider that under Unix stderr always goes somewhere, even if the
289 // user doesn't always see it under GUI desktops
290#ifdef __UNIX__
291 return true;
292#else
293 return false;
294#endif
295}
296
297void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
298{
299 if ( !wxPendingDelete.Member(object) )
300 wxPendingDelete.Append(object);
301}
302
303void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
304{
305 wxPendingDelete.DeleteObject(object);
306}
307
308// ----------------------------------------------------------------------------
309// wxAppTraits
310// ----------------------------------------------------------------------------
311
312wxAppTraits *wxAppTraitsBase::CreateGUI()
313{
314 return new wxGUIAppTraits;
315}
316