]> git.saurik.com Git - wxWidgets.git/blob - src/common/dpycmn.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / dpycmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dpycmn.cpp
3 // Purpose: wxDisplay and wxDisplayImplSingle implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.03.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003-2006 Vadim Zeitlin <vadim@wxwindows.org>
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 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/gdicmn.h"
29 #include "wx/window.h"
30 #include "wx/module.h"
31 #endif //WX_PRECOMP
32
33 #include "wx/display.h"
34 #include "wx/display_impl.h"
35
36 #if wxUSE_DISPLAY
37
38 #include "wx/arrimpl.cpp"
39 WX_DEFINE_OBJARRAY(wxArrayVideoModes)
40
41 const wxVideoMode wxDefaultVideoMode;
42
43 #endif // wxUSE_DISPLAY
44
45 // ----------------------------------------------------------------------------
46 // globals
47 // ----------------------------------------------------------------------------
48
49 // the factory object used by wxDisplay
50 //
51 // created on demand and destroyed by wxDisplayModule
52 static wxDisplayFactory *gs_factory = NULL;
53
54 // ----------------------------------------------------------------------------
55 // wxDisplayImplSingle: trivial implementation working for main display only
56 // ----------------------------------------------------------------------------
57
58 class WXDLLEXPORT wxDisplayImplSingle : public wxDisplayImpl
59 {
60 public:
61 wxDisplayImplSingle() : wxDisplayImpl(0) { }
62
63 virtual wxRect GetGeometry() const
64 {
65 wxRect r;
66 wxDisplaySize(&r.width, &r.height);
67 return r;
68 }
69
70 virtual wxRect GetClientArea() const { return wxGetClientDisplayRect(); }
71
72 virtual wxString GetName() const { return wxString(); }
73
74 #if wxUSE_DISPLAY
75 // no video modes support for us, provide just the stubs
76
77 virtual wxArrayVideoModes GetModes(const wxVideoMode& WXUNUSED(mode)) const
78 {
79 return wxArrayVideoModes();
80 }
81
82 virtual wxVideoMode GetCurrentMode() const { return wxVideoMode(); }
83
84 virtual bool ChangeMode(const wxVideoMode& WXUNUSED(mode)) { return false; }
85 #endif // wxUSE_DISPLAY
86
87
88 wxDECLARE_NO_COPY_CLASS(wxDisplayImplSingle);
89 };
90
91 // ----------------------------------------------------------------------------
92 // wxDisplayModule is used to cleanup gs_factory
93 // ----------------------------------------------------------------------------
94
95 class wxDisplayModule : public wxModule
96 {
97 public:
98 virtual bool OnInit() { return true; }
99 virtual void OnExit()
100 {
101 wxDELETE(gs_factory);
102 }
103
104 DECLARE_DYNAMIC_CLASS(wxDisplayModule)
105 };
106
107 IMPLEMENT_DYNAMIC_CLASS(wxDisplayModule, wxModule)
108
109 // ============================================================================
110 // wxDisplay implementation
111 // ============================================================================
112
113 // ----------------------------------------------------------------------------
114 // ctor/dtor
115 // ----------------------------------------------------------------------------
116
117 wxDisplay::wxDisplay(unsigned n)
118 {
119 wxASSERT_MSG( n < GetCount(),
120 wxT("An invalid index was passed to wxDisplay") );
121
122 m_impl = Factory().CreateDisplay(n);
123 }
124
125 wxDisplay::~wxDisplay()
126 {
127 delete m_impl;
128 }
129
130 // ----------------------------------------------------------------------------
131 // static functions forwarded to wxDisplayFactory
132 // ----------------------------------------------------------------------------
133
134 /* static */ unsigned wxDisplay::GetCount()
135 {
136 return Factory().GetCount();
137 }
138
139 /* static */ int wxDisplay::GetFromPoint(const wxPoint& pt)
140 {
141 return Factory().GetFromPoint(pt);
142 }
143
144 /* static */ int wxDisplay::GetFromWindow(const wxWindow *window)
145 {
146 wxCHECK_MSG( window, wxNOT_FOUND, wxT("invalid window") );
147
148 return Factory().GetFromWindow(window);
149 }
150
151 // ----------------------------------------------------------------------------
152 // functions forwarded to wxDisplayImpl
153 // ----------------------------------------------------------------------------
154
155 wxRect wxDisplay::GetGeometry() const
156 {
157 wxCHECK_MSG( IsOk(), wxRect(), wxT("invalid wxDisplay object") );
158
159 return m_impl->GetGeometry();
160 }
161
162 wxRect wxDisplay::GetClientArea() const
163 {
164 wxCHECK_MSG( IsOk(), wxRect(), wxT("invalid wxDisplay object") );
165
166 return m_impl->GetClientArea();
167 }
168
169 wxString wxDisplay::GetName() const
170 {
171 wxCHECK_MSG( IsOk(), wxString(), wxT("invalid wxDisplay object") );
172
173 return m_impl->GetName();
174 }
175
176 bool wxDisplay::IsPrimary() const
177 {
178 return m_impl && m_impl->GetIndex() == 0;
179 }
180
181 #if wxUSE_DISPLAY
182
183 wxArrayVideoModes wxDisplay::GetModes(const wxVideoMode& mode) const
184 {
185 wxCHECK_MSG( IsOk(), wxArrayVideoModes(), wxT("invalid wxDisplay object") );
186
187 return m_impl->GetModes(mode);
188 }
189
190 wxVideoMode wxDisplay::GetCurrentMode() const
191 {
192 wxCHECK_MSG( IsOk(), wxVideoMode(), wxT("invalid wxDisplay object") );
193
194 return m_impl->GetCurrentMode();
195 }
196
197 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
198 {
199 wxCHECK_MSG( IsOk(), false, wxT("invalid wxDisplay object") );
200
201 return m_impl->ChangeMode(mode);
202 }
203
204 #endif // wxUSE_DISPLAY
205
206 // ----------------------------------------------------------------------------
207 // static functions implementation
208 // ----------------------------------------------------------------------------
209
210 // if wxUSE_DISPLAY == 1 this is implemented in port-specific code
211 #if !wxUSE_DISPLAY
212
213 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
214 {
215 return new wxDisplayFactorySingle;
216 }
217
218 #endif // !wxUSE_DISPLAY
219
220 /* static */ wxDisplayFactory& wxDisplay::Factory()
221 {
222 if ( !gs_factory )
223 {
224 gs_factory = CreateFactory();
225 }
226
227 return *gs_factory;
228 }
229
230 // ============================================================================
231 // wxDisplayFactory implementation
232 // ============================================================================
233
234 int wxDisplayFactory::GetFromWindow(const wxWindow *window)
235 {
236 // consider that the window belongs to the display containing its centre
237 const wxRect r(window->GetScreenRect());
238 return GetFromPoint(wxPoint(r.x + r.width/2, r.y + r.height/2));
239 }
240
241 // ============================================================================
242 // wxDisplayFactorySingle implementation
243 // ============================================================================
244
245 /* static */
246 wxDisplayImpl *wxDisplayFactorySingle::CreateDisplay(unsigned n)
247 {
248 // we recognize the main display only
249 return n != 0 ? NULL : new wxDisplayImplSingle;
250 }
251
252 int wxDisplayFactorySingle::GetFromPoint(const wxPoint& pt)
253 {
254 if ( pt.x >= 0 && pt.y >= 0 )
255 {
256 int w, h;
257 wxDisplaySize(&w, &h);
258
259 if ( pt.x < w && pt.y < h )
260 return 0;
261 }
262
263 // the point is outside of the screen
264 return wxNOT_FOUND;
265 }