]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/msgout.h" | |
44 | #include "wx/thread.h" | |
45 | #include "wx/utils.h" | |
46 | ||
47 | #if defined(__WXMSW__) | |
48 | #include "wx/msw/private.h" // includes windows.h for LOGFONT | |
49 | #endif | |
50 | ||
51 | #if wxUSE_FONTMAP | |
52 | #include "wx/fontmap.h" | |
53 | #endif // wxUSE_FONTMAP | |
54 | ||
55 | // ============================================================================ | |
56 | // wxAppBase implementation | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // initialization | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | wxAppBase::wxAppBase() | |
64 | { | |
65 | m_topWindow = (wxWindow *)NULL; | |
66 | m_useBestVisual = FALSE; | |
67 | m_isActive = TRUE; | |
68 | ||
69 | // We don't want to exit the app if the user code shows a dialog from its | |
70 | // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete | |
71 | // to Yes initially as this dialog would be the last top level window. | |
72 | // OTOH, if we set it to No initially we'll have to overwrite it with Yes | |
73 | // when we enter our OnRun() because we do want the default behaviour from | |
74 | // then on. But this would be a problem if the user code calls | |
75 | // SetExitOnFrameDelete(FALSE) from OnInit(). | |
76 | // | |
77 | // So we use the special "Later" value which is such that | |
78 | // GetExitOnFrameDelete() returns FALSE for it but which we know we can | |
79 | // safely (i.e. without losing the effect of the users SetExitOnFrameDelete | |
80 | // call) overwrite in OnRun() | |
81 | m_exitOnFrameDelete = Later; | |
82 | } | |
83 | ||
84 | bool wxAppBase::Initialize(int& argc, wxChar **argv) | |
85 | { | |
86 | if ( !wxAppConsole::Initialize(argc, argv) ) | |
87 | return false; | |
88 | ||
89 | #if wxUSE_THREADS | |
90 | wxPendingEventsLocker = new wxCriticalSection; | |
91 | #endif | |
92 | ||
93 | wxInitializeStockLists(); | |
94 | wxInitializeStockObjects(); | |
95 | ||
96 | wxBitmap::InitStandardHandlers(); | |
97 | ||
98 | return true; | |
99 | } | |
100 | ||
101 | // ---------------------------------------------------------------------------- | |
102 | // cleanup | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
105 | wxAppBase::~wxAppBase() | |
106 | { | |
107 | // this destructor is required for Darwin | |
108 | } | |
109 | ||
110 | void wxAppBase::CleanUp() | |
111 | { | |
112 | // one last chance for pending objects to be cleaned up | |
113 | DeletePendingObjects(); | |
114 | ||
115 | wxBitmap::CleanUpHandlers(); | |
116 | ||
117 | wxDeleteStockObjects(); | |
118 | ||
119 | wxDeleteStockLists(); | |
120 | ||
121 | delete wxTheColourDatabase; | |
122 | wxTheColourDatabase = NULL; | |
123 | ||
124 | #if wxUSE_THREADS | |
125 | delete wxPendingEvents; | |
126 | wxPendingEvents = NULL; | |
127 | ||
128 | delete wxPendingEventsLocker; | |
129 | wxPendingEventsLocker = NULL; | |
130 | ||
131 | #if wxUSE_VALIDATORS | |
132 | // If we don't do the following, we get an apparent memory leak. | |
133 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
134 | #endif // wxUSE_VALIDATORS | |
135 | #endif // wxUSE_THREADS | |
136 | } | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // OnXXX() hooks | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
142 | bool wxAppBase::OnInitGui() | |
143 | { | |
144 | #ifdef __WXUNIVERSAL__ | |
145 | if ( !wxTheme::Get() && !wxTheme::CreateDefault() ) | |
146 | return FALSE; | |
147 | #endif // __WXUNIVERSAL__ | |
148 | ||
149 | return TRUE; | |
150 | } | |
151 | ||
152 | int wxAppBase::OnRun() | |
153 | { | |
154 | // see the comment in ctor: if the initial value hasn't been changed, use | |
155 | // the default Yes from now on | |
156 | if ( m_exitOnFrameDelete == Later ) | |
157 | { | |
158 | m_exitOnFrameDelete = Yes; | |
159 | } | |
160 | //else: it has been changed, assume the user knows what he is doing | |
161 | ||
162 | return MainLoop(); | |
163 | } | |
164 | ||
165 | void wxAppBase::Exit() | |
166 | { | |
167 | ExitMainLoop(); | |
168 | } | |
169 | ||
170 | wxAppTraits *wxAppBase::CreateTraits() | |
171 | { | |
172 | return new wxGUIAppTraits; | |
173 | } | |
174 | ||
175 | // ---------------------------------------------------------------------------- | |
176 | // misc | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
179 | void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus)) | |
180 | { | |
181 | if ( active == m_isActive ) | |
182 | return; | |
183 | ||
184 | m_isActive = active; | |
185 | ||
186 | wxActivateEvent event(wxEVT_ACTIVATE_APP, active); | |
187 | event.SetEventObject(this); | |
188 | ||
189 | (void)ProcessEvent(event); | |
190 | } | |
191 | ||
192 | void wxAppBase::DeletePendingObjects() | |
193 | { | |
194 | wxList::compatibility_iterator node = wxPendingDelete.GetFirst(); | |
195 | while (node) | |
196 | { | |
197 | wxObject *obj = node->GetData(); | |
198 | ||
199 | delete obj; | |
200 | ||
201 | if (wxPendingDelete.Member(obj)) | |
202 | wxPendingDelete.Erase(node); | |
203 | ||
204 | // Deleting one object may have deleted other pending | |
205 | // objects, so start from beginning of list again. | |
206 | node = wxPendingDelete.GetFirst(); | |
207 | } | |
208 | } | |
209 | ||
210 | // Returns TRUE if more time is needed. | |
211 | bool wxAppBase::ProcessIdle() | |
212 | { | |
213 | wxIdleEvent event; | |
214 | bool needMore = FALSE; | |
215 | wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst(); | |
216 | node = wxTopLevelWindows.GetFirst(); | |
217 | while (node) | |
218 | { | |
219 | wxWindow* win = node->GetData(); | |
220 | if (SendIdleEvents(win, event)) | |
221 | needMore = TRUE; | |
222 | node = node->GetNext(); | |
223 | } | |
224 | ||
225 | event.SetEventObject(this); | |
226 | (void) ProcessEvent(event); | |
227 | if (event.MoreRequested()) | |
228 | needMore = TRUE; | |
229 | ||
230 | wxUpdateUIEvent::ResetUpdateTime(); | |
231 | ||
232 | return needMore; | |
233 | } | |
234 | ||
235 | // Send idle event to window and all subwindows | |
236 | bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event) | |
237 | { | |
238 | bool needMore = FALSE; | |
239 | ||
240 | win->OnInternalIdle(); | |
241 | ||
242 | if (wxIdleEvent::CanSend(win)) | |
243 | { | |
244 | event.SetEventObject(win); | |
245 | win->GetEventHandler()->ProcessEvent(event); | |
246 | ||
247 | if (event.MoreRequested()) | |
248 | needMore = TRUE; | |
249 | } | |
250 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); | |
251 | while ( node ) | |
252 | { | |
253 | wxWindow *child = node->GetData(); | |
254 | if (SendIdleEvents(child, event)) | |
255 | needMore = TRUE; | |
256 | ||
257 | node = node->GetNext(); | |
258 | } | |
259 | ||
260 | return needMore; | |
261 | } | |
262 | ||
263 | void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event)) | |
264 | { | |
265 | // If there are pending events, we must process them: pending events | |
266 | // are either events to the threads other than main or events posted | |
267 | // with wxPostEvent() functions | |
268 | // GRG: I have moved this here so that all pending events are processed | |
269 | // before starting to delete any objects. This behaves better (in | |
270 | // particular, wrt wxPostEvent) and is coherent with wxGTK's current | |
271 | // behaviour. Changed Feb/2000 before 2.1.14 | |
272 | ProcessPendingEvents(); | |
273 | ||
274 | // 'Garbage' collection of windows deleted with Close(). | |
275 | DeletePendingObjects(); | |
276 | ||
277 | #if wxUSE_LOG | |
278 | // flush the logged messages if any | |
279 | wxLog::FlushActive(); | |
280 | #endif // wxUSE_LOG | |
281 | ||
282 | } | |
283 | ||
284 | // ---------------------------------------------------------------------------- | |
285 | // wxGUIAppTraitsBase | |
286 | // ---------------------------------------------------------------------------- | |
287 | ||
288 | #if wxUSE_LOG | |
289 | ||
290 | wxLog *wxGUIAppTraitsBase::CreateLogTarget() | |
291 | { | |
292 | return new wxLogGui; | |
293 | } | |
294 | ||
295 | #endif // wxUSE_LOG | |
296 | ||
297 | wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput() | |
298 | { | |
299 | // The standard way of printing help on command line arguments (app --help) | |
300 | // is (according to common practice): | |
301 | // - console apps: to stderr (on any platform) | |
302 | // - GUI apps: stderr on Unix platforms (!) | |
303 | // message box under Windows and others | |
304 | #ifdef __UNIX__ | |
305 | return new wxMessageOutputStderr; | |
306 | #else // !__UNIX__ | |
307 | // wxMessageOutputMessageBox doesn't work under Motif | |
308 | #ifdef __WXMOTIF__ | |
309 | return new wxMessageOutputLog; | |
310 | #else | |
311 | return new wxMessageOutputMessageBox; | |
312 | #endif | |
313 | #endif // __UNIX__/!__UNIX__ | |
314 | } | |
315 | ||
316 | #if wxUSE_FONTMAP | |
317 | ||
318 | wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper() | |
319 | { | |
320 | return new wxFontMapper; | |
321 | } | |
322 | ||
323 | #endif // wxUSE_FONTMAP | |
324 | ||
325 | wxRendererNative *wxGUIAppTraitsBase::CreateRenderer() | |
326 | { | |
327 | // use the default native renderer by default | |
328 | return NULL; | |
329 | } | |
330 | ||
331 | #ifdef __WXDEBUG__ | |
332 | ||
333 | bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
334 | { | |
335 | // under MSW we prefer to use the base class version using ::MessageBox() | |
336 | // even if wxMessageBox() is available because it has less chances to | |
337 | // double fault our app than our wxMessageBox() | |
338 | #if defined(__WXMSW__) || !wxUSE_MSGDLG | |
339 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
340 | #else // wxUSE_MSGDLG | |
341 | // this message is intentionally not translated -- it is for | |
342 | // developpers only | |
343 | wxString msgDlg(msg); | |
344 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
345 | wxT("You can also choose [Cancel] to suppress ") | |
346 | wxT("further warnings."); | |
347 | ||
348 | switch ( wxMessageBox(msgDlg, wxT("wxWindows Debug Alert"), | |
349 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) | |
350 | { | |
351 | case wxYES: | |
352 | wxTrap(); | |
353 | break; | |
354 | ||
355 | case wxCANCEL: | |
356 | // no more asserts | |
357 | return true; | |
358 | ||
359 | //case wxNO: nothing to do | |
360 | } | |
361 | ||
362 | return false; | |
363 | #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG | |
364 | } | |
365 | ||
366 | #endif // __WXDEBUG__ | |
367 | ||
368 | bool wxGUIAppTraitsBase::HasStderr() | |
369 | { | |
370 | // we consider that under Unix stderr always goes somewhere, even if the | |
371 | // user doesn't always see it under GUI desktops | |
372 | #ifdef __UNIX__ | |
373 | return true; | |
374 | #else | |
375 | return false; | |
376 | #endif | |
377 | } | |
378 | ||
379 | void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
380 | { | |
381 | if ( !wxPendingDelete.Member(object) ) | |
382 | wxPendingDelete.Append(object); | |
383 | } | |
384 | ||
385 | void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object) | |
386 | { | |
387 | wxPendingDelete.DeleteObject(object); | |
388 | } | |
389 |