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