]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/appcmn.cpp | |
3 | // Purpose: 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 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #if defined(__BORLANDC__) | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/app.h" | |
29 | #include "wx/window.h" | |
30 | #include "wx/bitmap.h" | |
31 | #include "wx/log.h" | |
32 | #include "wx/msgdlg.h" | |
33 | #include "wx/confbase.h" | |
34 | #include "wx/utils.h" | |
35 | #include "wx/wxcrtvararg.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/apptrait.h" | |
39 | #include "wx/cmdline.h" | |
40 | #include "wx/msgout.h" | |
41 | #include "wx/thread.h" | |
42 | #include "wx/vidmode.h" | |
43 | #include "wx/evtloop.h" | |
44 | ||
45 | #ifdef __WXDEBUG__ | |
46 | #if wxUSE_STACKWALKER | |
47 | #include "wx/stackwalk.h" | |
48 | #endif // wxUSE_STACKWALKER | |
49 | #endif // __WXDEBUG__ | |
50 | ||
51 | #if defined(__WXMSW__) | |
52 | #include "wx/msw/private.h" // includes windows.h for LOGFONT | |
53 | #endif | |
54 | ||
55 | #if wxUSE_FONTMAP | |
56 | #include "wx/fontmap.h" | |
57 | #endif // wxUSE_FONTMAP | |
58 | ||
59 | // DLL options compatibility check: | |
60 | #include "wx/build.h" | |
61 | WX_CHECK_BUILD_OPTIONS("wxCore") | |
62 | ||
63 | WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete; | |
64 | ||
65 | // ============================================================================ | |
66 | // wxAppBase implementation | |
67 | // ============================================================================ | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // initialization | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | wxAppBase::wxAppBase() | |
74 | { | |
75 | m_topWindow = NULL; | |
76 | ||
77 | m_useBestVisual = false; | |
78 | m_forceTrueColour = false; | |
79 | ||
80 | m_isActive = true; | |
81 | ||
82 | // We don't want to exit the app if the user code shows a dialog from its | |
83 | // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete | |
84 | // to Yes initially as this dialog would be the last top level window. | |
85 | // OTOH, if we set it to No initially we'll have to overwrite it with Yes | |
86 | // when we enter our OnRun() because we do want the default behaviour from | |
87 | // then on. But this would be a problem if the user code calls | |
88 | // SetExitOnFrameDelete(false) from OnInit(). | |
89 | // | |
90 | // So we use the special "Later" value which is such that | |
91 | // GetExitOnFrameDelete() returns false for it but which we know we can | |
92 | // safely (i.e. without losing the effect of the users SetExitOnFrameDelete | |
93 | // call) overwrite in OnRun() | |
94 | m_exitOnFrameDelete = Later; | |
95 | } | |
96 | ||
97 | bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig) | |
98 | { | |
99 | if ( !wxAppConsole::Initialize(argcOrig, argvOrig) ) | |
100 | return false; | |
101 | ||
102 | wxInitializeStockLists(); | |
103 | ||
104 | wxBitmap::InitStandardHandlers(); | |
105 | ||
106 | // for compatibility call the old initialization function too | |
107 | if ( !OnInitGui() ) | |
108 | return false; | |
109 | ||
110 | return true; | |
111 | } | |
112 | ||
113 | // ---------------------------------------------------------------------------- | |
114 | // cleanup | |
115 | // ---------------------------------------------------------------------------- | |
116 | ||
117 | wxAppBase::~wxAppBase() | |
118 | { | |
119 | // this destructor is required for Darwin | |
120 | } | |
121 | ||
122 | void wxAppBase::CleanUp() | |
123 | { | |
124 | // clean up all the pending objects | |
125 | DeletePendingObjects(); | |
126 | ||
127 | // and any remaining TLWs (they remove themselves from wxTopLevelWindows | |
128 | // when destroyed, so iterate until none are left) | |
129 | while ( !wxTopLevelWindows.empty() ) | |
130 | { | |
131 | // do not use Destroy() here as it only puts the TLW in pending list | |
132 | // but we want to delete them now | |
133 | delete wxTopLevelWindows.GetFirst()->GetData(); | |
134 | } | |
135 | ||
136 | // undo everything we did in Initialize() above | |
137 | wxBitmap::CleanUpHandlers(); | |
138 | ||
139 | wxStockGDI::DeleteAll(); | |
140 | ||
141 | wxDeleteStockLists(); | |
142 | ||
143 | delete wxTheColourDatabase; | |
144 | wxTheColourDatabase = NULL; | |
145 | ||
146 | wxAppConsole::CleanUp(); | |
147 | } | |
148 | ||
149 | // ---------------------------------------------------------------------------- | |
150 | // various accessors | |
151 | // ---------------------------------------------------------------------------- | |
152 | ||
153 | wxWindow* wxAppBase::GetTopWindow() const | |
154 | { | |
155 | wxWindow* window = m_topWindow; | |
156 | if (window == NULL && wxTopLevelWindows.GetCount() > 0) | |
157 | window = wxTopLevelWindows.GetFirst()->GetData(); | |
158 | return window; | |
159 | } | |
160 | ||
161 | wxVideoMode wxAppBase::GetDisplayMode() const | |
162 | { | |
163 | return wxVideoMode(); | |
164 | } | |
165 | ||
166 | wxLayoutDirection wxAppBase::GetLayoutDirection() const | |
167 | { | |
168 | #if wxUSE_INTL | |
169 | const wxLocale *const locale = wxGetLocale(); | |
170 | if ( locale ) | |
171 | { | |
172 | const wxLanguageInfo *const | |
173 | info = wxLocale::GetLanguageInfo(locale->GetLanguage()); | |
174 | ||
175 | if ( info ) | |
176 | return info->LayoutDirection; | |
177 | } | |
178 | #endif // wxUSE_INTL | |
179 | ||
180 | // we don't know | |
181 | return wxLayout_Default; | |
182 | } | |
183 | ||
184 | #if wxUSE_CMDLINE_PARSER | |
185 | ||
186 | // ---------------------------------------------------------------------------- | |
187 | // GUI-specific command line options handling | |
188 | // ---------------------------------------------------------------------------- | |
189 | ||
190 | #define OPTION_THEME "theme" | |
191 | #define OPTION_MODE "mode" | |
192 | ||
193 | void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser) | |
194 | { | |
195 | // first add the standard non GUI options | |
196 | wxAppConsole::OnInitCmdLine(parser); | |
197 | ||
198 | // the standard command line options | |
199 | static const wxCmdLineEntryDesc cmdLineGUIDesc[] = | |
200 | { | |
201 | #ifdef __WXUNIVERSAL__ | |
202 | { | |
203 | wxCMD_LINE_OPTION, | |
204 | NULL, | |
205 | OPTION_THEME, | |
206 | gettext_noop("specify the theme to use"), | |
207 | wxCMD_LINE_VAL_STRING, | |
208 | 0x0 | |
209 | }, | |
210 | #endif // __WXUNIVERSAL__ | |
211 | ||
212 | #if defined(__WXMGL__) | |
213 | // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports | |
214 | // should provide this option. That's why it is in common/appcmn.cpp | |
215 | // and not mgl/app.cpp | |
216 | { | |
217 | wxCMD_LINE_OPTION, | |
218 | NULL, | |
219 | OPTION_MODE, | |
220 | gettext_noop("specify display mode to use (e.g. 640x480-16)"), | |
221 | wxCMD_LINE_VAL_STRING, | |
222 | 0x0 | |
223 | }, | |
224 | #endif // __WXMGL__ | |
225 | ||
226 | // terminator | |
227 | wxCMD_LINE_DESC_END | |
228 | }; | |
229 | ||
230 | parser.SetDesc(cmdLineGUIDesc); | |
231 | } | |
232 | ||
233 | bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser) | |
234 | { | |
235 | #ifdef __WXUNIVERSAL__ | |
236 | wxString themeName; | |
237 | if ( parser.Found(OPTION_THEME, &themeName) ) | |
238 | { | |
239 | wxTheme *theme = wxTheme::Create(themeName); | |
240 | if ( !theme ) | |
241 | { | |
242 | wxLogError(_("Unsupported theme '%s'."), themeName.c_str()); | |
243 | return false; | |
244 | } | |
245 | ||
246 | // Delete the defaultly created theme and set the new theme. | |
247 | delete wxTheme::Get(); | |
248 | wxTheme::Set(theme); | |
249 | } | |
250 | #endif // __WXUNIVERSAL__ | |
251 | ||
252 | #if defined(__WXMGL__) | |
253 | wxString modeDesc; | |
254 | if ( parser.Found(OPTION_MODE, &modeDesc) ) | |
255 | { | |
256 | unsigned w, h, bpp; | |
257 | if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 ) | |
258 | { | |
259 | wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str()); | |
260 | return false; | |
261 | } | |
262 | ||
263 | if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) ) | |
264 | return false; | |
265 | } | |
266 | #endif // __WXMGL__ | |
267 | ||
268 | return wxAppConsole::OnCmdLineParsed(parser); | |
269 | } | |
270 | ||
271 | #endif // wxUSE_CMDLINE_PARSER | |
272 | ||
273 | // ---------------------------------------------------------------------------- | |
274 | // OnXXX() hooks | |
275 | // ---------------------------------------------------------------------------- | |
276 | ||
277 | bool wxAppBase::OnInitGui() | |
278 | { | |
279 | #ifdef __WXUNIVERSAL__ | |
280 | if ( !wxTheme::Get() && !wxTheme::CreateDefault() ) | |
281 | return false; | |
282 | #endif // __WXUNIVERSAL__ | |
283 | ||
284 | return true; | |
285 | } | |
286 | ||
287 | int wxAppBase::OnRun() | |
288 | { | |
289 | // see the comment in ctor: if the initial value hasn't been changed, use | |
290 | // the default Yes from now on | |
291 | if ( m_exitOnFrameDelete == Later ) | |
292 | { | |
293 | m_exitOnFrameDelete = Yes; | |
294 | } | |
295 | //else: it has been changed, assume the user knows what he is doing | |
296 | ||
297 | return wxAppConsole::OnRun(); | |
298 | } | |
299 | ||
300 | int wxAppBase::OnExit() | |
301 | { | |
302 | #ifdef __WXUNIVERSAL__ | |
303 | delete wxTheme::Set(NULL); | |
304 | #endif // __WXUNIVERSAL__ | |
305 | ||
306 | return wxAppConsole::OnExit(); | |
307 | } | |
308 | ||
309 | wxAppTraits *wxAppBase::CreateTraits() | |
310 | { | |
311 | return new wxGUIAppTraits; | |
312 | } | |
313 | ||
314 | // ---------------------------------------------------------------------------- | |
315 | // misc | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
318 | void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus)) | |
319 | { | |
320 | if ( active == m_isActive ) | |
321 | return; | |
322 | ||
323 | m_isActive = active; | |
324 | ||
325 | wxActivateEvent event(wxEVT_ACTIVATE_APP, active); | |
326 | event.SetEventObject(this); | |
327 | ||
328 | (void)ProcessEvent(event); | |
329 | } | |
330 | ||
331 | bool wxAppBase::SafeYield(wxWindow *win, bool onlyIfNeeded) | |
332 | { | |
333 | wxWindowDisabler wd(win); | |
334 | ||
335 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
336 | ||
337 | return loop && loop->Yield(onlyIfNeeded); | |
338 | } | |
339 | ||
340 | bool wxAppBase::SafeYieldFor(wxWindow *win, long eventsToProcess) | |
341 | { | |
342 | wxWindowDisabler wd(win); | |
343 | ||
344 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
345 | ||
346 | return loop && loop->YieldFor(eventsToProcess); | |
347 | } | |
348 | ||
349 | ||
350 | // ---------------------------------------------------------------------------- | |
351 | // idle handling | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | void wxAppBase::DeletePendingObjects() | |
355 | { | |
356 | wxList::compatibility_iterator node = wxPendingDelete.GetFirst(); | |
357 | while (node) | |
358 | { | |
359 | wxObject *obj = node->GetData(); | |
360 | ||
361 | // remove it from the list first so that if we get back here somehow | |
362 | // during the object deletion (e.g. wxYield called from its dtor) we | |
363 | // wouldn't try to delete it the second time | |
364 | if ( wxPendingDelete.Member(obj) ) | |
365 | wxPendingDelete.Erase(node); | |
366 | ||
367 | delete obj; | |
368 | ||
369 | // Deleting one object may have deleted other pending | |
370 | // objects, so start from beginning of list again. | |
371 | node = wxPendingDelete.GetFirst(); | |
372 | } | |
373 | } | |
374 | ||
375 | // Returns true if more time is needed. | |
376 | bool wxAppBase::ProcessIdle() | |
377 | { | |
378 | // call the base class version first, it will process the pending events | |
379 | // (which should be done before the idle events generation) and send the | |
380 | // idle event to wxTheApp itself | |
381 | bool needMore = wxAppConsoleBase::ProcessIdle(); | |
382 | wxIdleEvent event; | |
383 | wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst(); | |
384 | while (node) | |
385 | { | |
386 | wxWindow* win = node->GetData(); | |
387 | if (SendIdleEvents(win, event)) | |
388 | needMore = true; | |
389 | node = node->GetNext(); | |
390 | } | |
391 | ||
392 | // 'Garbage' collection of windows deleted with Close(). | |
393 | DeletePendingObjects(); | |
394 | ||
395 | #if wxUSE_LOG | |
396 | // flush the logged messages if any | |
397 | wxLog::FlushActive(); | |
398 | #endif | |
399 | ||
400 | wxUpdateUIEvent::ResetUpdateTime(); | |
401 | ||
402 | return needMore; | |
403 | } | |
404 | ||
405 | // Send idle event to window and all subwindows | |
406 | bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event) | |
407 | { | |
408 | bool needMore = false; | |
409 | ||
410 | win->OnInternalIdle(); | |
411 | ||
412 | // should we send idle event to this window? | |
413 | if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL || | |
414 | win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) ) | |
415 | { | |
416 | event.SetEventObject(win); | |
417 | win->HandleWindowEvent(event); | |
418 | ||
419 | if (event.MoreRequested()) | |
420 | needMore = true; | |
421 | } | |
422 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); | |
423 | while ( node ) | |
424 | { | |
425 | wxWindow *child = node->GetData(); | |
426 | if (SendIdleEvents(child, event)) | |
427 | needMore = true; | |
428 | ||
429 | node = node->GetNext(); | |
430 | } | |
431 | ||
432 | return needMore; | |
433 | } | |
434 | ||
435 | // ---------------------------------------------------------------------------- | |
436 | // wxGUIAppTraitsBase | |
437 | // ---------------------------------------------------------------------------- | |
438 | ||
439 | #if wxUSE_LOG | |
440 | ||
441 | wxLog *wxGUIAppTraitsBase::CreateLogTarget() | |
442 | { | |
443 | #if wxUSE_LOGGUI | |
444 | return new wxLogGui; | |
445 | #else | |
446 | // we must have something! | |
447 | return new wxLogStderr; | |
448 | #endif | |
449 | } | |
450 | ||
451 | #endif // wxUSE_LOG | |
452 | ||
453 | wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput() | |
454 | { | |
455 | // The standard way of printing help on command line arguments (app --help) | |
456 | // is (according to common practice): | |
457 | // - console apps: to stderr (on any platform) | |
458 | // - GUI apps: stderr on Unix platforms (!) | |
459 | // stderr if available and message box otherwise on others | |
460 | // (currently stderr only Windows if app running from console) | |
461 | #ifdef __UNIX__ | |
462 | return new wxMessageOutputStderr; | |
463 | #else // !__UNIX__ | |
464 | // wxMessageOutputMessageBox doesn't work under Motif | |
465 | #ifdef __WXMOTIF__ | |
466 | return new wxMessageOutputLog; | |
467 | #elif wxUSE_MSGDLG | |
468 | return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR); | |
469 | #else | |
470 | return new wxMessageOutputStderr; | |
471 | #endif | |
472 | #endif // __UNIX__/!__UNIX__ | |
473 | } | |
474 | ||
475 | #if wxUSE_FONTMAP | |
476 | ||
477 | wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper() | |
478 | { | |
479 | return new wxFontMapper; | |
480 | } | |
481 | ||
482 | #endif // wxUSE_FONTMAP | |
483 | ||
484 | wxRendererNative *wxGUIAppTraitsBase::CreateRenderer() | |
485 | { | |
486 | // use the default native renderer by default | |
487 | return NULL; | |
488 | } | |
489 | ||
490 | #ifdef __WXDEBUG__ | |
491 | ||
492 | bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
493 | { | |
494 | // under MSW we prefer to use the base class version using ::MessageBox() | |
495 | // even if wxMessageBox() is available because it has less chances to | |
496 | // double fault our app than our wxMessageBox() | |
497 | // | |
498 | // under DFB the message dialog is not always functional right now | |
499 | // | |
500 | // and finally we can't use wxMessageBox() if it wasn't compiled in, of | |
501 | // course | |
502 | #if defined(__WXMSW__) || defined(__WXDFB__) || !wxUSE_MSGDLG | |
503 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
504 | #else // wxUSE_MSGDLG | |
505 | wxString msgDlg = msg; | |
506 | ||
507 | #if wxUSE_STACKWALKER | |
508 | // on Unix stack frame generation may take some time, depending on the | |
509 | // size of the executable mainly... warn the user that we are working | |
510 | wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait")); | |
511 | fflush(stderr); | |
512 | ||
513 | const wxString stackTrace = GetAssertStackTrace(); | |
514 | if ( !stackTrace.empty() ) | |
515 | msgDlg << _T("\n\nCall stack:\n") << stackTrace; | |
516 | #endif // wxUSE_STACKWALKER | |
517 | ||
518 | // this message is intentionally not translated -- it is for | |
519 | // developpers only | |
520 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
521 | wxT("You can also choose [Cancel] to suppress ") | |
522 | wxT("further warnings."); | |
523 | ||
524 | switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"), | |
525 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) | |
526 | { | |
527 | case wxYES: | |
528 | wxTrap(); | |
529 | break; | |
530 | ||
531 | case wxCANCEL: | |
532 | // no more asserts | |
533 | return true; | |
534 | ||
535 | //case wxNO: nothing to do | |
536 | } | |
537 | ||
538 | return false; | |
539 | #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG | |
540 | } | |
541 | ||
542 | #endif // __WXDEBUG__ | |
543 | ||
544 | bool wxGUIAppTraitsBase::HasStderr() | |
545 | { | |
546 | // we consider that under Unix stderr always goes somewhere, even if the | |
547 | // user doesn't always see it under GUI desktops | |
548 | #ifdef __UNIX__ | |
549 | return true; | |
550 | #else | |
551 | return false; | |
552 | #endif | |
553 | } | |
554 | ||
555 | void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
556 | { | |
557 | if ( !wxPendingDelete.Member(object) ) | |
558 | wxPendingDelete.Append(object); | |
559 | } | |
560 | ||
561 | void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object) | |
562 | { | |
563 | wxPendingDelete.DeleteObject(object); | |
564 | } | |
565 |