]> git.saurik.com Git - wxWidgets.git/blob - src/dfb/dcclient.cpp
added links to Dir/FileName in construction section (part of patch 1617156)
[wxWidgets.git] / src / dfb / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/dcclient.cpp
3 // Purpose: wxWindowDC, wxClientDC and wxPaintDC
4 // Author: Vaclav Slavik
5 // Created: 2006-08-10
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ===========================================================================
12 // declarations
13 // ===========================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/dcclient.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/window.h"
30 #endif
31
32 #include "wx/dfb/private.h"
33
34 #define TRACE_PAINT _T("paint")
35
36 // ===========================================================================
37 // implementation
38 // ===========================================================================
39
40 //-----------------------------------------------------------------------------
41 // helpers
42 //-----------------------------------------------------------------------------
43
44 // Returns subrect of the window that is not outside of its parent's
45 // boundaries ("hidden behind its borders"), recursively:
46 static wxRect GetUncoveredWindowArea(wxWindow *win)
47 {
48 wxRect r(win->GetSize());
49
50 if ( win->IsTopLevel() )
51 return r;
52
53 wxWindow *parent = win->GetParent();
54 if ( !parent )
55 return r;
56
57 // intersect with parent's uncovered area, after offsetting it into win's
58 // coordinates; this will remove parts of 'r' that are outside of the
59 // parent's area:
60 wxRect rp(GetUncoveredWindowArea(parent));
61 rp.Offset(-win->GetPosition());
62 rp.Offset(-parent->GetClientAreaOrigin());
63 r.Intersect(rp);
64
65 return r;
66 }
67
68 // creates a dummy surface that has the same format as the real window's
69 // surface, but is not visible and so can be painted on even if the window
70 // is hidden
71 static
72 wxIDirectFBSurfacePtr CreateDummySurface(wxWindow *win, const wxRect *rect)
73 {
74 wxLogTrace(TRACE_PAINT, _T("%p ('%s'): creating dummy DC surface"),
75 win, win->GetName().c_str());
76 wxSize size(rect ? rect->GetSize() : win->GetSize());
77 return win->GetDfbSurface()->CreateCompatible
78 (
79 size,
80 wxIDirectFBSurface::CreateCompatible_NoBackBuffer
81 );
82 }
83
84 //-----------------------------------------------------------------------------
85 // wxWindowDC
86 //-----------------------------------------------------------------------------
87
88 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
89
90 wxWindowDC::wxWindowDC(wxWindow *win)
91 {
92 InitForWin(win, NULL);
93 }
94
95 void wxWindowDC::InitForWin(wxWindow *win, const wxRect *rect)
96 {
97 wxCHECK_RET( win, _T("invalid window") );
98
99 m_win = win;
100
101 // obtain the surface used for painting:
102 wxPoint origin;
103 wxIDirectFBSurfacePtr surface;
104
105 wxRect rectOrig(rect ? *rect : wxRect(win->GetSize()));
106 wxRect r;
107
108 if ( !win->IsShownOnScreen() )
109 {
110 // leave 'r' rectangle empty to indicate the window is not visible,
111 // see below (below "create the surface:") for how is this case handled
112 }
113 else
114 {
115 // compute painting rectangle after clipping if we're in PaintWindow
116 // code, otherwise paint on the entire window:
117 r = rectOrig;
118
119 const wxRegion& updateRegion = win->GetUpdateRegion();
120 if ( win->GetTLW()->IsPainting() && !updateRegion.IsEmpty() )
121 {
122 r.Intersect(updateRegion.AsRect());
123 wxCHECK_RET( !r.IsEmpty(), _T("invalid painting rectangle") );
124
125 // parent TLW will flip the entire surface when painting is done
126 m_shouldFlip = false;
127 }
128 else
129 {
130 // One of two things happened:
131 // (1) the TLW is not being painted by PaintWindow() now; or
132 // (2) we're drawing on some window other than the one that is
133 // currently painted on by PaintWindow()
134 // In either case, we need to flip the surface when we're done
135 // painting and we don't have to use updateRegion for clipping.
136 // OTOH, if the window is (partially) hidden by being
137 // out of its parent's area, we must clip the surface accordingly.
138 r.Intersect(GetUncoveredWindowArea(win));
139 m_shouldFlip = true; // paint the results immediately
140 }
141 }
142
143 // create the surface:
144 if ( r.IsEmpty() )
145 {
146 // we're painting on invisible window: the changes won't have any
147 // effect, as the window will be repainted anyhow when it is shown,
148 // but we still need a valid DC so that e.g. text extents can be
149 // measured, so let's create a dummy surface that has the same
150 // format as the real one would have and let the code paint on it:
151 surface = CreateDummySurface(win, rect);
152
153 // painting on hidden window has no effect on TLW's surface, don't
154 // waste time flipping the dummy surface:
155 m_shouldFlip = false;
156 }
157 else
158 {
159 m_winRect = r;
160 DFBRectangle dfbrect = { r.x, r.y, r.width, r.height };
161 surface = win->GetDfbSurface()->GetSubSurface(&dfbrect);
162
163 // if the DC was clipped thanks to rectPaint, we must adjust the
164 // origin accordingly; but we do *not* adjust for 'rect', because
165 // rect.GetPosition() has coordinates (0,0) in the DC:
166 origin.x = rectOrig.x - r.x;
167 origin.y = rectOrig.y - r.y;
168
169 // m_shouldFlip was set in the "if" block above this one
170 }
171
172 if ( !surface )
173 return;
174
175 wxLogTrace(TRACE_PAINT,
176 _T("%p ('%s'): creating DC for area [%i,%i,%i,%i], clipped to [%i,%i,%i,%i], origin [%i,%i]"),
177 win, win->GetName().c_str(),
178 rectOrig.x, rectOrig.y, rectOrig.GetRight(), rectOrig.GetBottom(),
179 r.x, r.y, r.GetRight(), r.GetBottom(),
180 origin.x, origin.y);
181
182 DFBInit(surface);
183 SetFont(win->GetFont());
184
185 // offset coordinates to account for subsurface's origin coordinates:
186 SetDeviceOrigin(origin.x, origin.y);
187 }
188
189 wxWindowDC::~wxWindowDC()
190 {
191 wxIDirectFBSurfacePtr surface(GetDirectFBSurface());
192 if ( !surface )
193 return;
194
195 // if no painting was done on the DC, we don't have to flip the surface:
196 if ( !m_isBBoxValid )
197 return;
198
199 if ( m_shouldFlip )
200 {
201 // paint overlays on top of the surface being drawn to by this DC
202 // before showing anything on the screen:
203 m_win->PaintOverlays(m_winRect);
204
205 DFBSurfaceCapabilities caps = DSCAPS_NONE;
206 surface->GetCapabilities(&caps);
207 if ( caps & DSCAPS_DOUBLE )
208 {
209 // FIXME: flip only modified parts of the surface
210 surface->FlipToFront();
211 }
212 // else: the surface is not double-buffered and so cannot be flipped
213 }
214 // else: don't flip the surface, wxTLW will do it when it finishes
215 // painting of its invalidated areas
216 }
217
218 //-----------------------------------------------------------------------------
219 // wxClientDC
220 //-----------------------------------------------------------------------------
221
222 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
223
224 wxClientDC::wxClientDC(wxWindow *win)
225 {
226 wxCHECK_RET( win, _T("invalid window") );
227
228 wxRect rect = win->GetClientRect();
229 InitForWin(win, &rect);
230 }
231
232 //-----------------------------------------------------------------------------
233 // wxPaintDC
234 //-----------------------------------------------------------------------------
235
236 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)