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