]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcclient.cpp
end label edit patch from Ricky Gonzales <gonzales@pyramid3.net>
[wxWidgets.git] / src / msw / dcclient.cpp
CommitLineData
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
43struct 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
59WX_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
75static 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.
edccf428 82 int g_isPainting = 0;
c6eba8f8
VZ
83#endif // __WXDEBUG__
84
85// ===========================================================================
86// implementation
87// ===========================================================================
e6460682 88
c6eba8f8
VZ
89// ----------------------------------------------------------------------------
90// wxWindowDC
91// ----------------------------------------------------------------------------
92
93wxWindowDC::wxWindowDC()
2bda0e17
KB
94{
95 m_canvas = NULL;
96}
97
e6460682 98wxWindowDC::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 107wxWindowDC::~wxWindowDC()
2bda0e17 108{
e6460682 109 if (m_canvas && m_hDC)
2bda0e17
KB
110 {
111 SelectOldObjects(m_hDC);
112
edccf428
VZ
113 if ( !::ReleaseDC(GetWinHwnd(m_canvas), GetHdc()) )
114 {
115 wxLogLastError("ReleaseDC");
116 }
117
c6eba8f8 118 m_hDC = 0;
2bda0e17 119 }
c6eba8f8
VZ
120
121 m_hDCCount--;
2bda0e17
KB
122}
123
c6eba8f8
VZ
124// ----------------------------------------------------------------------------
125// wxClientDC
126// ----------------------------------------------------------------------------
e6460682 127
c6eba8f8 128wxClientDC::wxClientDC()
2bda0e17
KB
129{
130 m_canvas = NULL;
131}
132
e6460682 133wxClientDC::wxClientDC(wxWindow *the_canvas)
2bda0e17
KB
134{
135 m_canvas = the_canvas;
c6eba8f8 136 m_hDC = (WXHDC) ::GetDC(GetWinHwnd(the_canvas));
a91b47e8 137
c45a644e
RR
138 // the background mode is only used for text background
139 // and is set in DrawText() to OPAQUE as required, other-
140 // wise always TRANSPARENT, RR
141 ::SetBkMode( GetHdc(), TRANSPARENT );
142
a91b47e8 143 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
2bda0e17
KB
144}
145
c6eba8f8 146wxClientDC::~wxClientDC()
2bda0e17 147{
c6eba8f8 148 if ( m_canvas && GetHdc() )
2bda0e17
KB
149 {
150 SelectOldObjects(m_hDC);
151
c6eba8f8
VZ
152 ::ReleaseDC(GetWinHwnd(m_canvas), GetHdc());
153 m_hDC = 0;
2bda0e17 154 }
2bda0e17
KB
155}
156
c6eba8f8
VZ
157// ----------------------------------------------------------------------------
158// wxPaintDC
159// ----------------------------------------------------------------------------
e6460682 160
3a5ffa81
VZ
161// VZ: initial implementation (by JACS) only remembered the last wxPaintDC
162// created and tried to reuse - this was supposed to take care of a
163// situation when a derived class OnPaint() calls base class OnPaint()
164// because in this case ::BeginPaint() shouldn't be called second time.
165//
166// I'm not sure how useful this is, however we must remember the HWND
167// associated with the last HDC as well - otherwise we may (and will!) try
168// to reuse the HDC for another HWND which is a nice recipe for disaster.
169//
170// So we store a list of windows for which we already have the DC and not
171// just one single hDC. This seems to work, but I'm really not sure about
172// the usefullness of the whole idea - IMHO it's much better to not call
85833f5c 173// base class OnPaint() at all, or, if we really want to allow it, add a
3a5ffa81
VZ
174// "wxPaintDC *" parameter to wxPaintEvent which should be used if it's
175// !NULL instead of creating a new DC.
176
177wxArrayDCInfo wxPaintDC::ms_cache;
2bda0e17 178
c6eba8f8
VZ
179wxPaintDC::wxPaintDC()
180{
3a5ffa81
VZ
181 m_canvas = NULL;
182 m_hDC = 0;
c6eba8f8
VZ
183}
184
83626bfa 185wxPaintDC::wxPaintDC(wxWindow *canvas)
2bda0e17 186{
223d09f6 187 wxCHECK_RET( canvas, wxT("NULL canvas in wxPaintDC ctor") );
3a5ffa81 188
c455ab93 189#ifdef __WXDEBUG__
edccf428 190 if ( g_isPainting <= 0 )
3a5ffa81 191 {
223d09f6 192 wxFAIL_MSG( wxT("wxPaintDC may be created only in EVT_PAINT handler!") );
2bda0e17 193
3a5ffa81
VZ
194 return;
195 }
196#endif // __WXDEBUG__
c6eba8f8 197
3a5ffa81 198 m_canvas = canvas;
83626bfa 199
3a5ffa81
VZ
200 // do we have a DC for this window in the cache?
201 wxPaintDCInfo *info = FindInCache();
202 if ( info )
203 {
204 m_hDC = info->hdc;
205 info->count++;
206 }
207 else // not in cache, create a new one
208 {
209 m_hDC = (WXHDC)::BeginPaint(GetWinHwnd(m_canvas), &g_paintStruct);
210 ms_cache.Add(new wxPaintDCInfo(m_canvas, this));
211 }
a91b47e8 212
c45a644e
RR
213 // the background mode is only used for text background
214 // and is set in DrawText() to OPAQUE as required, other-
215 // wise always TRANSPARENT, RR
216 ::SetBkMode( GetHdc(), TRANSPARENT );
217
3a5ffa81 218 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
2bda0e17
KB
219}
220
83626bfa 221wxPaintDC::~wxPaintDC()
2bda0e17 222{
3a5ffa81
VZ
223 if ( m_hDC )
224 {
edccf428
VZ
225 SelectOldObjects(m_hDC);
226
3a5ffa81
VZ
227 size_t index;
228 wxPaintDCInfo *info = FindInCache(&index);
229
223d09f6 230 wxCHECK_RET( info, wxT("existing DC should have a cache entry") );
3a5ffa81
VZ
231
232 if ( !--info->count )
233 {
234 ::EndPaint(GetWinHwnd(m_canvas), &g_paintStruct);
235
236 ms_cache.Remove(index);
237 }
238 //else: cached DC entry is still in use
edccf428
VZ
239
240 // prevent the base class dtor from ReleaseDC()ing it again
241 m_hDC = 0;
1c089c47 242 }
2bda0e17 243}
81d66cf3 244
3a5ffa81
VZ
245wxPaintDCInfo *wxPaintDC::FindInCache(size_t *index) const
246{
247 wxPaintDCInfo *info = NULL;
248 size_t nCache = ms_cache.GetCount();
249 for ( size_t n = 0; n < nCache; n++ )
250 {
251 info = &ms_cache[n];
252 if ( info->hwnd == m_canvas->GetHWND() )
253 {
254 if ( index )
255 *index = n;
256 break;
257 }
258 }
259
260 return info;
261}