]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
Make mouse capture checking asserts stronger and more detailed.
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9 1/////////////////////////////////////////////////////////////////////////////
127eab18 2// Name: src/common/appcmn.cpp
90e15296 3// Purpose: wxAppBase methods common to all platforms
72cdf4c9
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 18.10.99
72cdf4c9 7// Copyright: (c) Vadim Zeitlin
65571936 8// Licence: wxWindows licence
72cdf4c9
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ---------------------------------------------------------------------------
16// headers
17// ---------------------------------------------------------------------------
18
72cdf4c9
VZ
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#if defined(__BORLANDC__)
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27 #include "wx/app.h"
5ff14574 28 #include "wx/window.h"
b2e972ec 29 #include "wx/bitmap.h"
46446cc2 30 #include "wx/log.h"
e2478fde 31 #include "wx/msgdlg.h"
b3dfbbc9 32 #include "wx/confbase.h"
de6185e2 33 #include "wx/utils.h"
193d0c93 34 #include "wx/wxcrtvararg.h"
72cdf4c9
VZ
35#endif
36
e2478fde 37#include "wx/apptrait.h"
b913d3ed 38#include "wx/cmdline.h"
e2478fde 39#include "wx/msgout.h"
72cdf4c9 40#include "wx/thread.h"
5ff14574 41#include "wx/vidmode.h"
dde19c21 42#include "wx/evtloop.h"
a5f1fd3e 43
1c193821
JS
44#if wxUSE_FONTMAP
45 #include "wx/fontmap.h"
46#endif // wxUSE_FONTMAP
47
34fdf762
VS
48// DLL options compatibility check:
49#include "wx/build.h"
50WX_CHECK_BUILD_OPTIONS("wxCore")
51
e2478fde
VZ
52// ============================================================================
53// wxAppBase implementation
54// ============================================================================
d54598dd 55
bf188f1a 56// ----------------------------------------------------------------------------
94826170 57// initialization
bf188f1a
VZ
58// ----------------------------------------------------------------------------
59
090a6d7a 60wxAppBase::wxAppBase()
697c5f51 61{
d3b9f782 62 m_topWindow = NULL;
b46b1d59 63
4629016d 64 m_useBestVisual = false;
515a31bf 65 m_forceTrueColour = false;
1cbee0b4 66
b46b1d59 67 m_isActive = true;
1bf77ee5 68
1cbee0b4
VZ
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
4629016d 75 // SetExitOnFrameDelete(false) from OnInit().
1cbee0b4
VZ
76 //
77 // So we use the special "Later" value which is such that
4629016d 78 // GetExitOnFrameDelete() returns false for it but which we know we can
1cbee0b4
VZ
79 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
80 // call) overwrite in OnRun()
81 m_exitOnFrameDelete = Later;
1e6feb95
VZ
82}
83
a54930e0 84bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
94826170 85{
a54930e0 86 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
94826170
VZ
87 return false;
88
94826170 89 wxInitializeStockLists();
94826170
VZ
90
91 wxBitmap::InitStandardHandlers();
92
1f2f7329
FM
93 // for compatibility call the old initialization function too
94 if ( !OnInitGui() )
95 return false;
96
94826170
VZ
97 return true;
98}
99
100// ----------------------------------------------------------------------------
101// cleanup
102// ----------------------------------------------------------------------------
103
799ea011
GD
104wxAppBase::~wxAppBase()
105{
106 // this destructor is required for Darwin
107}
108
94826170
VZ
109void wxAppBase::CleanUp()
110{
07460370 111 // clean up all the pending objects
94826170
VZ
112 DeletePendingObjects();
113
07460370
VZ
114 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
115 // when destroyed, so iterate until none are left)
116 while ( !wxTopLevelWindows.empty() )
117 {
118 // do not use Destroy() here as it only puts the TLW in pending list
119 // but we want to delete them now
120 delete wxTopLevelWindows.GetFirst()->GetData();
121 }
4055ed82 122
07460370 123 // undo everything we did in Initialize() above
94826170
VZ
124 wxBitmap::CleanUpHandlers();
125
f516d986 126 wxStockGDI::DeleteAll();
94826170
VZ
127
128 wxDeleteStockLists();
129
5276b0a5 130 wxDELETE(wxTheColourDatabase);
94826170 131
68d2c3be 132 wxAppConsole::CleanUp();
94826170
VZ
133}
134
475a93b7
VZ
135// ----------------------------------------------------------------------------
136// various accessors
5ff14574
PC
137// ----------------------------------------------------------------------------
138
139wxWindow* wxAppBase::GetTopWindow() const
140{
141 wxWindow* window = m_topWindow;
142 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
143 window = wxTopLevelWindows.GetFirst()->GetData();
144 return window;
145}
146
147wxVideoMode wxAppBase::GetDisplayMode() const
148{
149 return wxVideoMode();
150}
151
475a93b7
VZ
152wxLayoutDirection wxAppBase::GetLayoutDirection() const
153{
154#if wxUSE_INTL
155 const wxLocale *const locale = wxGetLocale();
156 if ( locale )
157 {
158 const wxLanguageInfo *const
159 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
160
161 if ( info )
162 return info->LayoutDirection;
163 }
164#endif // wxUSE_INTL
165
166 // we don't know
167 return wxLayout_Default;
168}
169
b913d3ed
VZ
170#if wxUSE_CMDLINE_PARSER
171
172// ----------------------------------------------------------------------------
173// GUI-specific command line options handling
174// ----------------------------------------------------------------------------
175
c2e45372
VZ
176#define OPTION_THEME "theme"
177#define OPTION_MODE "mode"
b913d3ed
VZ
178
179void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
180{
9c13e5ef
VZ
181 // first add the standard non GUI options
182 wxAppConsole::OnInitCmdLine(parser);
183
b913d3ed
VZ
184 // the standard command line options
185 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
186 {
187#ifdef __WXUNIVERSAL__
188 {
189 wxCMD_LINE_OPTION,
0d5ab92f 190 NULL,
b913d3ed
VZ
191 OPTION_THEME,
192 gettext_noop("specify the theme to use"),
193 wxCMD_LINE_VAL_STRING,
194 0x0
195 },
196#endif // __WXUNIVERSAL__
197
0e1f8ea4
VZ
198#if defined(__WXDFB__)
199 // VS: this is not specific to wxDFB, all fullscreen (framebuffer) ports
b913d3ed 200 // should provide this option. That's why it is in common/appcmn.cpp
0e1f8ea4 201 // and not dfb/app.cpp
b913d3ed
VZ
202 {
203 wxCMD_LINE_OPTION,
0d5ab92f 204 NULL,
b913d3ed
VZ
205 OPTION_MODE,
206 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
207 wxCMD_LINE_VAL_STRING,
208 0x0
209 },
0e1f8ea4 210#endif // __WXDFB__
b913d3ed
VZ
211
212 // terminator
0d5ab92f 213 wxCMD_LINE_DESC_END
b913d3ed
VZ
214 };
215
216 parser.SetDesc(cmdLineGUIDesc);
217}
218
219bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
220{
221#ifdef __WXUNIVERSAL__
222 wxString themeName;
223 if ( parser.Found(OPTION_THEME, &themeName) )
224 {
225 wxTheme *theme = wxTheme::Create(themeName);
226 if ( !theme )
227 {
228 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
4629016d 229 return false;
b913d3ed
VZ
230 }
231
232 // Delete the defaultly created theme and set the new theme.
233 delete wxTheme::Get();
234 wxTheme::Set(theme);
235 }
236#endif // __WXUNIVERSAL__
237
0e1f8ea4 238#if defined(__WXDFB__)
b913d3ed
VZ
239 wxString modeDesc;
240 if ( parser.Found(OPTION_MODE, &modeDesc) )
241 {
242 unsigned w, h, bpp;
9a83f860 243 if ( wxSscanf(modeDesc.c_str(), wxT("%ux%u-%u"), &w, &h, &bpp) != 3 )
b913d3ed
VZ
244 {
245 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
4629016d 246 return false;
b913d3ed
VZ
247 }
248
1c53456f 249 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
4629016d 250 return false;
b913d3ed 251 }
0e1f8ea4 252#endif // __WXDFB__
b913d3ed
VZ
253
254 return wxAppConsole::OnCmdLineParsed(parser);
255}
256
257#endif // wxUSE_CMDLINE_PARSER
258
94826170
VZ
259// ----------------------------------------------------------------------------
260// OnXXX() hooks
261// ----------------------------------------------------------------------------
262
1e6feb95
VZ
263bool wxAppBase::OnInitGui()
264{
265#ifdef __WXUNIVERSAL__
bf188f1a 266 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
4629016d 267 return false;
1e6feb95
VZ
268#endif // __WXUNIVERSAL__
269
4629016d 270 return true;
1e6feb95 271}
1e6feb95 272
1cbee0b4
VZ
273int wxAppBase::OnRun()
274{
275 // see the comment in ctor: if the initial value hasn't been changed, use
276 // the default Yes from now on
277 if ( m_exitOnFrameDelete == Later )
278 {
279 m_exitOnFrameDelete = Yes;
280 }
281 //else: it has been changed, assume the user knows what he is doing
282
b46b1d59 283 return wxAppConsole::OnRun();
1cbee0b4
VZ
284}
285
b913d3ed
VZ
286int wxAppBase::OnExit()
287{
288#ifdef __WXUNIVERSAL__
289 delete wxTheme::Set(NULL);
290#endif // __WXUNIVERSAL__
291
292 return wxAppConsole::OnExit();
293}
294
e2478fde 295wxAppTraits *wxAppBase::CreateTraits()
a69be60b 296{
7843d11b 297 return new wxGUIAppTraits;
72cdf4c9
VZ
298}
299
1e6feb95
VZ
300// ----------------------------------------------------------------------------
301// misc
302// ----------------------------------------------------------------------------
303
6e169cf3 304void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 305{
66dfed9b
VZ
306 if ( active == m_isActive )
307 return;
308
1e6feb95 309 m_isActive = active;
66dfed9b
VZ
310
311 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
312 event.SetEventObject(this);
313
314 (void)ProcessEvent(event);
7beba2fc 315}
1e6feb95 316
d48b06bd
FM
317bool wxAppBase::SafeYield(wxWindow *win, bool onlyIfNeeded)
318{
319 wxWindowDisabler wd(win);
320
dde19c21
FM
321 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
322
323 return loop && loop->Yield(onlyIfNeeded);
d48b06bd
FM
324}
325
326bool wxAppBase::SafeYieldFor(wxWindow *win, long eventsToProcess)
327{
328 wxWindowDisabler wd(win);
329
dde19c21
FM
330 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
331
332 return loop && loop->YieldFor(eventsToProcess);
d48b06bd
FM
333}
334
335
2dc62891
VZ
336// ----------------------------------------------------------------------------
337// idle handling
338// ----------------------------------------------------------------------------
339
4629016d 340// Returns true if more time is needed.
e39af974
JS
341bool wxAppBase::ProcessIdle()
342{
3185abc2
VZ
343 // call the base class version first to send the idle event to wxTheApp
344 // itself
14eb37a0 345 bool needMore = wxAppConsoleBase::ProcessIdle();
5109ae5d 346 wxIdleEvent event;
222ed1d6 347 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
348 while (node)
349 {
350 wxWindow* win = node->GetData();
9df70d79
VZ
351
352 // Don't send idle events to the windows that are about to be destroyed
353 // anyhow, this is wasteful and unexpected.
354 if ( !wxPendingDelete.Member(win) && win->SendIdleEvents(event) )
4629016d 355 needMore = true;
e39af974
JS
356 node = node->GetNext();
357 }
358
e39af974 359 wxUpdateUIEvent::ResetUpdateTime();
4629016d 360
5109ae5d 361 return needMore;
e39af974
JS
362}
363
bf188f1a 364// ----------------------------------------------------------------------------
e2478fde 365// wxGUIAppTraitsBase
bf188f1a
VZ
366// ----------------------------------------------------------------------------
367
bf188f1a 368#if wxUSE_LOG
bf188f1a 369
e2478fde
VZ
370wxLog *wxGUIAppTraitsBase::CreateLogTarget()
371{
d30ef769 372#if wxUSE_LOGGUI
a3f78ceb 373#ifndef __WXOSX_IPHONE__
e2478fde 374 return new wxLogGui;
a3f78ceb
SC
375#else
376 return new wxLogStderr;
377#endif
461dae94 378#else
fa6416df 379 // we must have something!
461dae94
VZ
380 return new wxLogStderr;
381#endif
bf188f1a
VZ
382}
383
bf188f1a
VZ
384#endif // wxUSE_LOG
385
e2478fde 386wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 387{
e2478fde
VZ
388 // The standard way of printing help on command line arguments (app --help)
389 // is (according to common practice):
390 // - console apps: to stderr (on any platform)
391 // - GUI apps: stderr on Unix platforms (!)
784ee7d5
VZ
392 // stderr if available and message box otherwise on others
393 // (currently stderr only Windows if app running from console)
e2478fde
VZ
394#ifdef __UNIX__
395 return new wxMessageOutputStderr;
396#else // !__UNIX__
397 // wxMessageOutputMessageBox doesn't work under Motif
398 #ifdef __WXMOTIF__
399 return new wxMessageOutputLog;
a8ff046b 400 #elif wxUSE_MSGDLG
784ee7d5 401 return new wxMessageOutputBest(wxMSGOUT_PREFER_STDERR);
a8ff046b
VZ
402 #else
403 return new wxMessageOutputStderr;
e2478fde
VZ
404 #endif
405#endif // __UNIX__/!__UNIX__
bf188f1a
VZ
406}
407
e2478fde 408#if wxUSE_FONTMAP
bf188f1a 409
e2478fde
VZ
410wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
411{
412 return new wxFontMapper;
bf188f1a
VZ
413}
414
e2478fde 415#endif // wxUSE_FONTMAP
bf188f1a 416
f0244295
VZ
417wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
418{
419 // use the default native renderer by default
420 return NULL;
421}
422
e2478fde
VZ
423bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
424{
2d8e0096 425#if wxDEBUG_LEVEL
e2478fde
VZ
426 // under MSW we prefer to use the base class version using ::MessageBox()
427 // even if wxMessageBox() is available because it has less chances to
428 // double fault our app than our wxMessageBox()
19a67f39
VZ
429 //
430 // under DFB the message dialog is not always functional right now
431 //
432 // and finally we can't use wxMessageBox() if it wasn't compiled in, of
433 // course
2d8e0096
VZ
434#if !defined(__WXMSW__) && !defined(__WXDFB__) && wxUSE_MSGDLG
435
436 // we can't (safely) show the GUI dialog from another thread, only do it
437 // for the asserts in the main thread
438 if ( wxIsMainThread() )
439 {
440 wxString msgDlg = msg;
db9febdf
RR
441
442#if wxUSE_STACKWALKER
2d8e0096
VZ
443 const wxString stackTrace = GetAssertStackTrace();
444 if ( !stackTrace.empty() )
445 msgDlg << wxT("\n\nCall stack:\n") << stackTrace;
db9febdf
RR
446#endif // wxUSE_STACKWALKER
447
2d8e0096
VZ
448 // this message is intentionally not translated -- it is for
449 // developpers only
450 msgDlg += wxT("\nDo you want to stop the program?\n")
451 wxT("You can also choose [Cancel] to suppress ")
452 wxT("further warnings.");
e2478fde 453
2d8e0096
VZ
454 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
455 wxYES_NO | wxCANCEL | wxICON_STOP ) )
456 {
457 case wxYES:
458 wxTrap();
459 break;
460
461 case wxCANCEL:
462 // no more asserts
463 return true;
090a6d7a 464
2d8e0096
VZ
465 //case wxNO: nothing to do
466 }
a5f1fd3e 467
2d8e0096 468 return false;
090a6d7a 469 }
2d8e0096
VZ
470#endif // wxUSE_MSGDLG
471#endif // wxDEBUG_LEVEL
090a6d7a 472
2d8e0096 473 return wxAppTraitsBase::ShowAssertDialog(msg);
a5f1fd3e
VZ
474}
475
e2478fde 476bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 477{
e2478fde
VZ
478 // we consider that under Unix stderr always goes somewhere, even if the
479 // user doesn't always see it under GUI desktops
480#ifdef __UNIX__
481 return true;
a5f1fd3e 482#else
e2478fde 483 return false;
a5f1fd3e 484#endif
a5f1fd3e
VZ
485}
486