]>
Commit | Line | Data |
---|---|---|
d8c89c48 | 1 | /////////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: wx/msw/private/dc.h |
d8c89c48 VZ |
3 | // Purpose: private wxMSW helpers for working with HDCs |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2009-06-16 (extracted from src/msw/dc.cpp) | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _MSW_PRIVATE_DC_H_ | |
12 | #define _MSW_PRIVATE_DC_H_ | |
13 | ||
14 | #include "wx/msw/dc.h" | |
15 | #include "wx/msw/wrapwin.h" | |
16 | ||
17 | namespace wxMSWImpl | |
18 | { | |
19 | ||
20 | // various classes to change some DC property temporarily | |
21 | ||
22 | // text background and foreground colours | |
23 | class wxTextColoursChanger | |
24 | { | |
25 | public: | |
26 | wxTextColoursChanger(HDC hdc, const wxMSWDCImpl& dc) | |
27 | : m_hdc(hdc) | |
28 | { | |
29 | Change(dc.GetTextForeground(), dc.GetTextBackground()); | |
30 | } | |
31 | ||
32 | wxTextColoursChanger(HDC hdc, const wxColour& colFg, const wxColour& colBg) | |
33 | : m_hdc(hdc) | |
34 | { | |
35 | Change(colFg, colBg); | |
36 | } | |
37 | ||
38 | wxTextColoursChanger(HDC hdc, COLORREF colFg, COLORREF colBg) | |
39 | : m_hdc(hdc) | |
40 | { | |
41 | Change(colFg, colBg); | |
42 | } | |
43 | ||
44 | ~wxTextColoursChanger() | |
45 | { | |
46 | if ( m_oldColFg != CLR_INVALID ) | |
47 | ::SetTextColor(m_hdc, m_oldColFg); | |
48 | if ( m_oldColBg != CLR_INVALID ) | |
49 | ::SetBkColor(m_hdc, m_oldColBg); | |
50 | } | |
51 | ||
52 | protected: | |
53 | // this ctor doesn't change mode immediately, call Change() later to do it | |
54 | // only if needed | |
55 | wxTextColoursChanger(HDC hdc) | |
56 | : m_hdc(hdc) | |
57 | { | |
58 | m_oldColFg = | |
59 | m_oldColBg = CLR_INVALID; | |
60 | } | |
61 | ||
62 | void Change(const wxColour& colFg, const wxColour& colBg) | |
63 | { | |
64 | Change(colFg.IsOk() ? colFg.GetPixel() : CLR_INVALID, | |
65 | colBg.IsOk() ? colBg.GetPixel() : CLR_INVALID); | |
66 | } | |
67 | ||
68 | void Change(COLORREF colFg, COLORREF colBg) | |
69 | { | |
70 | if ( colFg != CLR_INVALID ) | |
71 | { | |
72 | m_oldColFg = ::SetTextColor(m_hdc, colFg); | |
73 | if ( m_oldColFg == CLR_INVALID ) | |
74 | { | |
9a83f860 | 75 | wxLogLastError(wxT("SetTextColor")); |
d8c89c48 VZ |
76 | } |
77 | } | |
78 | else | |
79 | { | |
80 | m_oldColFg = CLR_INVALID; | |
81 | } | |
82 | ||
83 | if ( colBg != CLR_INVALID ) | |
84 | { | |
85 | m_oldColBg = ::SetBkColor(m_hdc, colBg); | |
86 | if ( m_oldColBg == CLR_INVALID ) | |
87 | { | |
9a83f860 | 88 | wxLogLastError(wxT("SetBkColor")); |
d8c89c48 VZ |
89 | } |
90 | } | |
91 | else | |
92 | { | |
93 | m_oldColBg = CLR_INVALID; | |
94 | } | |
95 | } | |
96 | ||
97 | private: | |
98 | const HDC m_hdc; | |
99 | COLORREF m_oldColFg, | |
100 | m_oldColBg; | |
101 | ||
102 | wxDECLARE_NO_COPY_CLASS(wxTextColoursChanger); | |
103 | }; | |
104 | ||
105 | // background mode | |
106 | class wxBkModeChanger | |
107 | { | |
108 | public: | |
109 | // set background mode to opaque if mode != wxBRUSHSTYLE_TRANSPARENT | |
110 | wxBkModeChanger(HDC hdc, int mode) | |
111 | : m_hdc(hdc) | |
112 | { | |
113 | Change(mode); | |
114 | } | |
115 | ||
116 | ~wxBkModeChanger() | |
117 | { | |
118 | if ( m_oldMode ) | |
119 | ::SetBkMode(m_hdc, m_oldMode); | |
120 | } | |
121 | ||
122 | protected: | |
123 | // this ctor doesn't change mode immediately, call Change() later to do it | |
124 | // only if needed | |
125 | wxBkModeChanger(HDC hdc) : m_hdc(hdc) { m_oldMode = 0; } | |
126 | ||
127 | void Change(int mode) | |
128 | { | |
129 | m_oldMode = ::SetBkMode(m_hdc, mode == wxBRUSHSTYLE_TRANSPARENT | |
130 | ? TRANSPARENT | |
131 | : OPAQUE); | |
132 | if ( !m_oldMode ) | |
133 | { | |
9a83f860 | 134 | wxLogLastError(wxT("SetBkMode")); |
d8c89c48 VZ |
135 | } |
136 | } | |
137 | ||
138 | private: | |
139 | const HDC m_hdc; | |
140 | int m_oldMode; | |
141 | ||
142 | wxDECLARE_NO_COPY_CLASS(wxBkModeChanger); | |
143 | }; | |
144 | ||
145 | } // namespace wxMSWImpl | |
146 | ||
147 | #endif // _MSW_PRIVATE_DC_H_ | |
148 |