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