]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcclient.cpp
1. wxWindow::IsTopLevel() added and documented
[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
c6eba8f8
VZ
39// ----------------------------------------------------------------------------
40// macros
41// ----------------------------------------------------------------------------
2bda0e17
KB
42
43#if !USE_SHARED_LIBRARY
c6eba8f8
VZ
44 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
45 IMPLEMENT_DYNAMIC_CLASS(wxClientDC, wxWindowDC)
46 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC, wxWindowDC)
2bda0e17
KB
47#endif
48
c6eba8f8
VZ
49// ----------------------------------------------------------------------------
50// global variables
51// ----------------------------------------------------------------------------
52
53static PAINTSTRUCT g_paintStruct;
54
55#ifdef __WXDEBUG__
56 // a global variable which we check to verify that wxPaintDC are only
57 // created in resopnse to WM_PAINT message - doing this from elsewhere is a
58 // common programming error among wxWindows programmers and might lead to
59 // very subtle and difficult to debug refresh/repaint bugs.
60 extern bool g_isPainting = FALSE;
61#endif // __WXDEBUG__
62
63// ===========================================================================
64// implementation
65// ===========================================================================
e6460682 66
c6eba8f8
VZ
67// ----------------------------------------------------------------------------
68// wxWindowDC
69// ----------------------------------------------------------------------------
70
71wxWindowDC::wxWindowDC()
2bda0e17
KB
72{
73 m_canvas = NULL;
74}
75
e6460682 76wxWindowDC::wxWindowDC(wxWindow *the_canvas)
2bda0e17
KB
77{
78 m_canvas = the_canvas;
c6eba8f8
VZ
79 m_hDC = (WXHDC) ::GetWindowDC(GetWinHwnd(the_canvas) );
80 m_hDCCount++;
a91b47e8
JS
81
82 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
2bda0e17
KB
83}
84
c6eba8f8 85wxWindowDC::~wxWindowDC()
2bda0e17 86{
e6460682 87 if (m_canvas && m_hDC)
2bda0e17
KB
88 {
89 SelectOldObjects(m_hDC);
90
c6eba8f8
VZ
91 ::ReleaseDC(GetWinHwnd(m_canvas), GetHdc());
92 m_hDC = 0;
2bda0e17 93 }
c6eba8f8
VZ
94
95 m_hDCCount--;
2bda0e17
KB
96}
97
c6eba8f8
VZ
98// ----------------------------------------------------------------------------
99// wxClientDC
100// ----------------------------------------------------------------------------
e6460682 101
c6eba8f8 102wxClientDC::wxClientDC()
2bda0e17
KB
103{
104 m_canvas = NULL;
105}
106
e6460682 107wxClientDC::wxClientDC(wxWindow *the_canvas)
2bda0e17
KB
108{
109 m_canvas = the_canvas;
c6eba8f8 110 m_hDC = (WXHDC) ::GetDC(GetWinHwnd(the_canvas));
a91b47e8
JS
111
112 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
2bda0e17
KB
113}
114
c6eba8f8 115wxClientDC::~wxClientDC()
2bda0e17 116{
c6eba8f8 117 if ( m_canvas && GetHdc() )
2bda0e17
KB
118 {
119 SelectOldObjects(m_hDC);
120
c6eba8f8
VZ
121 ::ReleaseDC(GetWinHwnd(m_canvas), GetHdc());
122 m_hDC = 0;
2bda0e17 123 }
2bda0e17
KB
124}
125
c6eba8f8
VZ
126// ----------------------------------------------------------------------------
127// wxPaintDC
128// ----------------------------------------------------------------------------
e6460682 129
c6eba8f8
VZ
130// TODO (VZ) I have still some doubts about this hack and I still think that we
131// should store pairs of (hwnd, hdc) and not just the DC - what if
132// BeginPaint() was called on other window? It seems to work like
133// this, but to be sure about it we'd need to store hwnd too...
2bda0e17 134
c085e333
VZ
135WXHDC wxPaintDC::ms_PaintHDC = 0;
136size_t wxPaintDC::ms_PaintCount = 0; // count of ms_PaintHDC usage
2bda0e17 137
c6eba8f8
VZ
138wxPaintDC::wxPaintDC()
139{
140 m_canvas = NULL;
141}
142
83626bfa 143wxPaintDC::wxPaintDC(wxWindow *canvas)
2bda0e17 144{
83626bfa 145 wxCHECK_RET( canvas, "NULL canvas in wxPaintDC ctor" );
c6eba8f8
VZ
146 wxCHECK_RET( g_isPainting,
147 _T("wxPaintDC may be created only in EVT_PAINT handler!") );
2bda0e17 148
83626bfa 149 m_canvas = canvas;
c6eba8f8
VZ
150
151 // Don't call Begin/EndPaint if it's already been called: for example, if
152 // calling a base class OnPaint.
83626bfa
VZ
153 if ( ms_PaintCount > 0 ) {
154 // it means that we've already called BeginPaint and so we must just
155 // reuse the same HDC (BeginPaint shouldn't be called more than once)
156 wxASSERT( ms_PaintHDC );
157
158 m_hDC = ms_PaintHDC;
159 ms_PaintCount++;
160 }
161 else {
162 ms_PaintHDC =
163 m_hDC = (WXHDC)::BeginPaint((HWND)m_canvas->GetHWND(), &g_paintStruct);
164 ms_PaintCount = 1;
165 m_hDCCount++;
166 }
a91b47e8
JS
167
168 SetBackground(wxBrush(m_canvas->GetBackgroundColour(), wxSOLID));
2bda0e17
KB
169}
170
83626bfa 171wxPaintDC::~wxPaintDC()
2bda0e17 172{
83626bfa
VZ
173 if ( m_hDC ) {
174 if ( !--ms_PaintCount ) {
175 ::EndPaint((HWND)m_canvas->GetHWND(), &g_paintStruct);
176 m_hDCCount--;
a0a302dc
JS
177 m_hDC = (WXHDC) NULL;
178 ms_PaintHDC = (WXHDC) NULL;
1c089c47 179 }
c6eba8f8 180 //else: ms_PaintHDC still in use
83626bfa 181 }
2bda0e17 182}
81d66cf3 183