]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dcclient.cpp | |
3 | // Purpose: wxClientDC class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c6eba8f8 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
c6eba8f8 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
c6eba8f8 | 21 | #pragma implementation "dcclient.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
2bda0e17 KB |
24 | // For compilers that support precompilation, includes "wx.h". |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
c6eba8f8 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
0c589ad0 | 31 | #include "wx/string.h" |
83626bfa | 32 | #include "wx/log.h" |
0c589ad0 | 33 | #include "wx/window.h" |
2bda0e17 | 34 | |
c6eba8f8 VZ |
35 | #include "wx/msw/private.h" |
36 | ||
0c589ad0 BM |
37 | #include "wx/dcclient.h" |
38 | ||
3a5ffa81 VZ |
39 | // ---------------------------------------------------------------------------- |
40 | // array/list types | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | struct WXDLLEXPORT wxPaintDCInfo | |
44 | { | |
45 | wxPaintDCInfo(wxWindow *win, wxDC *dc) | |
46 | { | |
47 | hwnd = win->GetHWND(); | |
48 | hdc = dc->GetHDC(); | |
49 | count = 1; | |
50 | } | |
51 | ||
52 | WXHWND hwnd; // window for this DC | |
53 | WXHDC hdc; // the DC handle | |
54 | size_t count; // usage count | |
55 | }; | |
56 | ||
57 | #include "wx/arrimpl.cpp" | |
58 | ||
59 | WX_DEFINE_OBJARRAY(wxArrayDCInfo); | |
60 | ||
c6eba8f8 VZ |
61 | // ---------------------------------------------------------------------------- |
62 | // macros | |
63 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
64 | |
65 | #if !USE_SHARED_LIBRARY | |
c6eba8f8 VZ |
66 | IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC) |
67 | IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC) | |
68 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC) | |
2bda0e17 KB |
69 | #endif |
70 | ||
c6eba8f8 VZ |
71 | // ---------------------------------------------------------------------------- |
72 | // global variables | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | static PAINTSTRUCT g_paintStruct; | |
76 | ||
77 | #ifdef __WXDEBUG__ | |
78 | // a global variable which we check to verify that wxPaintDC are only | |
79 | // created in resopnse to WM_PAINT message - doing this from elsewhere is a | |
80 | // common programming error among wxWindows programmers and might lead to | |
81 | // very subtle and difficult to debug refresh/repaint bugs. | |
25889d3c | 82 | bool g_isPainting = FALSE; |
c6eba8f8 VZ |
83 | #endif // __WXDEBUG__ |
84 | ||
85 | // =========================================================================== | |
86 | // implementation | |
87 | // =========================================================================== | |
e6460682 | 88 | |
c6eba8f8 VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // wxWindowDC | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | wxWindowDC::wxWindowDC() | |
2bda0e17 KB |
94 | { |
95 | m_canvas = NULL; | |
96 | } | |
97 | ||
e6460682 | 98 | wxWindowDC::wxWindowDC(wxWindow *the_canvas) |
2bda0e17 KB |
99 | { |
100 | m_canvas = the_canvas; | |
c6eba8f8 VZ |
101 | m_hDC = (WXHDC) ::GetWindowDC(GetWinHwnd(the_canvas) ); |
102 | m_hDCCount++; | |
a91b47e8 JS |
103 | |
104 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); | |
2bda0e17 KB |
105 | } |
106 | ||
c6eba8f8 | 107 | wxWindowDC::~wxWindowDC() |
2bda0e17 | 108 | { |
e6460682 | 109 | if (m_canvas && m_hDC) |
2bda0e17 KB |
110 | { |
111 | SelectOldObjects(m_hDC); | |
112 | ||
c6eba8f8 VZ |
113 | ::ReleaseDC(GetWinHwnd(m_canvas), GetHdc()); |
114 | m_hDC = 0; | |
2bda0e17 | 115 | } |
c6eba8f8 VZ |
116 | |
117 | m_hDCCount--; | |
2bda0e17 KB |
118 | } |
119 | ||
c6eba8f8 VZ |
120 | // ---------------------------------------------------------------------------- |
121 | // wxClientDC | |
122 | // ---------------------------------------------------------------------------- | |
e6460682 | 123 | |
c6eba8f8 | 124 | wxClientDC::wxClientDC() |
2bda0e17 KB |
125 | { |
126 | m_canvas = NULL; | |
127 | } | |
128 | ||
e6460682 | 129 | wxClientDC::wxClientDC(wxWindow *the_canvas) |
2bda0e17 KB |
130 | { |
131 | m_canvas = the_canvas; | |
c6eba8f8 | 132 | m_hDC = (WXHDC) ::GetDC(GetWinHwnd(the_canvas)); |
a91b47e8 JS |
133 | |
134 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); | |
2bda0e17 KB |
135 | } |
136 | ||
c6eba8f8 | 137 | wxClientDC::~wxClientDC() |
2bda0e17 | 138 | { |
c6eba8f8 | 139 | if ( m_canvas && GetHdc() ) |
2bda0e17 KB |
140 | { |
141 | SelectOldObjects(m_hDC); | |
142 | ||
c6eba8f8 VZ |
143 | ::ReleaseDC(GetWinHwnd(m_canvas), GetHdc()); |
144 | m_hDC = 0; | |
2bda0e17 | 145 | } |
2bda0e17 KB |
146 | } |
147 | ||
c6eba8f8 VZ |
148 | // ---------------------------------------------------------------------------- |
149 | // wxPaintDC | |
150 | // ---------------------------------------------------------------------------- | |
e6460682 | 151 | |
3a5ffa81 VZ |
152 | // VZ: initial implementation (by JACS) only remembered the last wxPaintDC |
153 | // created and tried to reuse - this was supposed to take care of a | |
154 | // situation when a derived class OnPaint() calls base class OnPaint() | |
155 | // because in this case ::BeginPaint() shouldn't be called second time. | |
156 | // | |
157 | // I'm not sure how useful this is, however we must remember the HWND | |
158 | // associated with the last HDC as well - otherwise we may (and will!) try | |
159 | // to reuse the HDC for another HWND which is a nice recipe for disaster. | |
160 | // | |
161 | // So we store a list of windows for which we already have the DC and not | |
162 | // just one single hDC. This seems to work, but I'm really not sure about | |
163 | // the usefullness of the whole idea - IMHO it's much better to not call | |
85833f5c | 164 | // base class OnPaint() at all, or, if we really want to allow it, add a |
3a5ffa81 VZ |
165 | // "wxPaintDC *" parameter to wxPaintEvent which should be used if it's |
166 | // !NULL instead of creating a new DC. | |
167 | ||
168 | wxArrayDCInfo wxPaintDC::ms_cache; | |
2bda0e17 | 169 | |
c6eba8f8 VZ |
170 | wxPaintDC::wxPaintDC() |
171 | { | |
3a5ffa81 VZ |
172 | m_canvas = NULL; |
173 | m_hDC = 0; | |
c6eba8f8 VZ |
174 | } |
175 | ||
83626bfa | 176 | wxPaintDC::wxPaintDC(wxWindow *canvas) |
2bda0e17 | 177 | { |
3a5ffa81 VZ |
178 | wxCHECK_RET( canvas, "NULL canvas in wxPaintDC ctor" ); |
179 | ||
c455ab93 | 180 | #ifdef __WXDEBUG__ |
3a5ffa81 VZ |
181 | if ( !g_isPainting ) |
182 | { | |
183 | wxFAIL_MSG( _T("wxPaintDC may be created only in EVT_PAINT handler!") ); | |
2bda0e17 | 184 | |
3a5ffa81 VZ |
185 | return; |
186 | } | |
187 | #endif // __WXDEBUG__ | |
c6eba8f8 | 188 | |
3a5ffa81 | 189 | m_canvas = canvas; |
83626bfa | 190 | |
3a5ffa81 VZ |
191 | // do we have a DC for this window in the cache? |
192 | wxPaintDCInfo *info = FindInCache(); | |
193 | if ( info ) | |
194 | { | |
195 | m_hDC = info->hdc; | |
196 | info->count++; | |
197 | } | |
198 | else // not in cache, create a new one | |
199 | { | |
200 | m_hDC = (WXHDC)::BeginPaint(GetWinHwnd(m_canvas), &g_paintStruct); | |
201 | ms_cache.Add(new wxPaintDCInfo(m_canvas, this)); | |
202 | } | |
a91b47e8 | 203 | |
3a5ffa81 | 204 | SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID)); |
2bda0e17 KB |
205 | } |
206 | ||
83626bfa | 207 | wxPaintDC::~wxPaintDC() |
2bda0e17 | 208 | { |
3a5ffa81 VZ |
209 | if ( m_hDC ) |
210 | { | |
211 | size_t index; | |
212 | wxPaintDCInfo *info = FindInCache(&index); | |
213 | ||
214 | wxCHECK_RET( info, _T("existing DC should have a cache entry") ); | |
215 | ||
216 | if ( !--info->count ) | |
217 | { | |
218 | ::EndPaint(GetWinHwnd(m_canvas), &g_paintStruct); | |
219 | ||
220 | ms_cache.Remove(index); | |
221 | } | |
222 | //else: cached DC entry is still in use | |
1c089c47 | 223 | } |
2bda0e17 | 224 | } |
81d66cf3 | 225 | |
3a5ffa81 VZ |
226 | wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const |
227 | { | |
228 | wxPaintDCInfo *info = NULL; | |
229 | size_t nCache = ms_cache.GetCount(); | |
230 | for ( size_t n = 0; n < nCache; n++ ) | |
231 | { | |
232 | info = &ms_cache[n]; | |
233 | if ( info->hwnd == m_canvas->GetHWND() ) | |
234 | { | |
235 | if ( index ) | |
236 | *index = n; | |
237 | break; | |
238 | } | |
239 | } | |
240 | ||
241 | return info; | |
242 | } |