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