]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
added init.cpp
[wxWidgets.git] / src / common / appcmn.cpp
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/intl.h"
34 #include "wx/list.h"
35 #include "wx/log.h"
36 #include "wx/msgdlg.h"
37 #endif
38
39 #include "wx/apptrait.h"
40 #if wxUSE_FONTMAP
41 #include "wx/fontmap.h"
42 #endif // wxUSE_FONTMAP
43 #include "wx/msgout.h"
44 #include "wx/thread.h"
45 #include "wx/utils.h"
46
47 // ============================================================================
48 // wxAppBase implementation
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // initialization
53 // ----------------------------------------------------------------------------
54
55 wxAppBase::wxAppBase()
56 {
57 m_topWindow = (wxWindow *)NULL;
58 m_useBestVisual = FALSE;
59 m_isActive = TRUE;
60
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().
68 //
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;
74 }
75
76 bool wxAppBase::Initialize(int& argc, wxChar **argv)
77 {
78 if ( !wxAppConsole::Initialize(argc, argv) )
79 return false;
80
81 #if wxUSE_THREADS
82 wxPendingEventsLocker = new wxCriticalSection;
83 #endif
84
85 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
86 wxTheColourDatabase->Initialize();
87
88 wxInitializeStockLists();
89 wxInitializeStockObjects();
90
91 wxBitmap::InitStandardHandlers();
92
93 return true;
94 }
95
96 // ----------------------------------------------------------------------------
97 // cleanup
98 // ----------------------------------------------------------------------------
99
100 wxAppBase::~wxAppBase()
101 {
102 // this destructor is required for Darwin
103 }
104
105 void wxAppBase::CleanUp()
106 {
107 // one last chance for pending objects to be cleaned up
108 DeletePendingObjects();
109
110 wxBitmap::CleanUpHandlers();
111
112 wxDeleteStockObjects();
113
114 wxDeleteStockLists();
115
116 delete wxTheColourDatabase;
117 wxTheColourDatabase = NULL;
118
119 #if wxUSE_THREADS
120 delete wxPendingEvents;
121 wxPendingEvents = NULL;
122
123 delete wxPendingEventsLocker;
124 wxPendingEventsLocker = NULL;
125
126 #if wxUSE_VALIDATORS
127 // If we don't do the following, we get an apparent memory leak.
128 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
129 #endif // wxUSE_VALIDATORS
130 #endif // wxUSE_THREADS
131 }
132
133 // ----------------------------------------------------------------------------
134 // OnXXX() hooks
135 // ----------------------------------------------------------------------------
136
137 bool wxAppBase::OnInitGui()
138 {
139 #ifdef __WXUNIVERSAL__
140 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
141 return FALSE;
142 #endif // __WXUNIVERSAL__
143
144 return TRUE;
145 }
146
147 int wxAppBase::OnRun()
148 {
149 // see the comment in ctor: if the initial value hasn't been changed, use
150 // the default Yes from now on
151 if ( m_exitOnFrameDelete == Later )
152 {
153 m_exitOnFrameDelete = Yes;
154 }
155 //else: it has been changed, assume the user knows what he is doing
156
157 return MainLoop();
158 }
159
160 void wxAppBase::Exit()
161 {
162 ExitMainLoop();
163 }
164
165 wxAppTraits *wxAppBase::CreateTraits()
166 {
167 return wxAppTraits::CreateGUI();
168 }
169
170 // ----------------------------------------------------------------------------
171 // misc
172 // ----------------------------------------------------------------------------
173
174 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
175 {
176 if ( active == m_isActive )
177 return;
178
179 m_isActive = active;
180
181 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
182 event.SetEventObject(this);
183
184 (void)ProcessEvent(event);
185 }
186
187 void wxAppBase::DeletePendingObjects()
188 {
189 wxNode *node = wxPendingDelete.GetFirst();
190 while (node)
191 {
192 wxObject *obj = node->GetData();
193
194 delete obj;
195
196 if (wxPendingDelete.Member(obj))
197 delete node;
198
199 // Deleting one object may have deleted other pending
200 // objects, so start from beginning of list again.
201 node = wxPendingDelete.GetFirst();
202 }
203 }
204
205 // ----------------------------------------------------------------------------
206 // wxGUIAppTraitsBase
207 // ----------------------------------------------------------------------------
208
209 #if wxUSE_LOG
210
211 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
212 {
213 return new wxLogGui;
214 }
215
216 #endif // wxUSE_LOG
217
218 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
219 {
220 // The standard way of printing help on command line arguments (app --help)
221 // is (according to common practice):
222 // - console apps: to stderr (on any platform)
223 // - GUI apps: stderr on Unix platforms (!)
224 // message box under Windows and others
225 #ifdef __UNIX__
226 return new wxMessageOutputStderr;
227 #else // !__UNIX__
228 // wxMessageOutputMessageBox doesn't work under Motif
229 #ifdef __WXMOTIF__
230 return new wxMessageOutputLog;
231 #else
232 return new wxMessageOutputMessageBox;
233 #endif
234 #endif // __UNIX__/!__UNIX__
235 }
236
237 #if wxUSE_FONTMAP
238
239 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
240 {
241 return new wxFontMapper;
242 }
243
244 #endif // wxUSE_FONTMAP
245
246 #ifdef __WXDEBUG__
247
248 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
249 {
250 // under MSW we prefer to use the base class version using ::MessageBox()
251 // even if wxMessageBox() is available because it has less chances to
252 // double fault our app than our wxMessageBox()
253 #if defined(__WXMSW__) || !wxUSE_MSGDLG
254 return wxAppTraitsBase::ShowAssertDialog(msg);
255 #else // wxUSE_MSGDLG
256 // this message is intentionally not translated -- it is for
257 // developpers only
258 wxString msgDlg(msg);
259 msgDlg += wxT("\nDo you want to stop the program?\n")
260 wxT("You can also choose [Cancel] to suppress ")
261 wxT("further warnings.");
262
263 switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"),
264 wxYES_NO | wxCANCEL | wxICON_STOP ) )
265 {
266 case wxYES:
267 wxTrap();
268 break;
269
270 case wxCANCEL:
271 // no more asserts
272 return true;
273
274 //case wxNO: nothing to do
275 }
276
277 return false;
278 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
279 }
280
281 #endif // __WXDEBUG__
282
283 bool wxGUIAppTraitsBase::HasStderr()
284 {
285 // we consider that under Unix stderr always goes somewhere, even if the
286 // user doesn't always see it under GUI desktops
287 #ifdef __UNIX__
288 return true;
289 #else
290 return false;
291 #endif
292 }
293
294 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
295 {
296 if ( !wxPendingDelete.Member(object) )
297 wxPendingDelete.Append(object);
298 }
299
300 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
301 {
302 wxPendingDelete.DeleteObject(object);
303 }
304
305 // ----------------------------------------------------------------------------
306 // wxAppTraits
307 // ----------------------------------------------------------------------------
308
309 wxAppTraits *wxAppTraitsBase::CreateGUI()
310 {
311 return new wxGUIAppTraits;
312 }
313