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