|
13 | 13 | // REFLECT_NOTIFICATIONS()
|
14 | 14 | //
|
15 | 15 | // This code may be used in compiled form in any way you desire. This
|
16 |
| -// source file may be redistributed by any means PROVIDING it is |
17 |
| -// not sold for profit without the authors written consent, and |
18 |
| -// providing that this notice and the authors name is included. |
| 16 | +// source file may be redistributed by any means PROVIDING it is |
| 17 | +// not sold for profit without the authors written consent, and |
| 18 | +// providing that this notice and the authors name is included. |
19 | 19 | //
|
20 | 20 | // This file is provided "as is" with no expressed or implied warranty.
|
21 | 21 | // The author accepts no liability if it causes any damage to you or your
|
|
25 | 25 | //
|
26 | 26 |
|
27 | 27 | #ifndef __cplusplus
|
28 |
| - #error WTL requires C++ compilation (use a .cpp suffix) |
| 28 | +#error WTL requires C++ compilation (use a .cpp suffix) |
29 | 29 | #endif
|
30 | 30 |
|
31 | 31 | #ifndef __ATLCTRLS_H__
|
32 |
| - #error ColorCombo.h requires atlctrls.h to be included first |
| 32 | +#error ColorCombo.h requires atlctrls.h to be included first |
33 | 33 | #endif
|
34 | 34 |
|
35 | 35 |
|
36 | 36 | /////////////////////////////////////////////////////////////////////////////
|
37 |
| -// |
| 37 | +// |
38 | 38 |
|
39 |
| -template< class T, class TBase = CComboBox, class TWinTraits = CControlWinTraits > |
40 |
| -class ATL_NO_VTABLE CColorPickerImpl : |
41 |
| - public CWindowImpl< T, TBase, TWinTraits >, |
42 |
| - public COwnerDraw< T > |
| 39 | +template <class T, class TBase = CComboBox, class TWinTraits = CControlWinTraits> |
| 40 | +class ATL_NO_VTABLE CColorPickerImpl : public CWindowImpl<T, TBase, TWinTraits>, |
| 41 | + public COwnerDraw<T> |
43 | 42 | {
|
44 | 43 | public:
|
45 |
| - DECLARE_WND_SUPERCLASS(NULL, TBase::GetWndClassName()) |
46 |
| - |
47 |
| - enum |
48 |
| - { |
49 |
| - s_cxIndent = 1, |
50 |
| - s_cyIndent = 1 |
51 |
| - }; |
52 |
| - |
53 |
| - // Operations |
54 |
| - |
55 |
| - BOOL SubclassWindow(HWND hWnd) |
56 |
| - { |
57 |
| - ATLASSERT(m_hWnd==NULL); |
58 |
| - ATLASSERT(::IsWindow(hWnd)); |
59 |
| - BOOL bRet = CWindowImpl< T, TBase, TWinTraits >::SubclassWindow(hWnd); |
60 |
| - if( bRet ) _Init(); |
61 |
| - return bRet; |
62 |
| - } |
63 |
| - |
64 |
| - int AddColor(int iIndex, COLORREF clr) |
65 |
| - { |
66 |
| - ATLASSERT(::IsWindow(m_hWnd)); |
67 |
| - int iItem = InsertString(iIndex, _T("")); |
68 |
| - if( iItem == -1 ) return -1; |
69 |
| - SetItemData(iItem, (DWORD) clr); |
70 |
| - return iItem; |
71 |
| - } |
72 |
| - |
73 |
| - BOOL RemoveColor(COLORREF clr) |
74 |
| - { |
75 |
| - ATLASSERT(::IsWindow(m_hWnd)); |
76 |
| - int iItem = FindColor(clr); |
77 |
| - if( iItem == -1 ) return FALSE; |
78 |
| - return DeleteString(iItem); |
79 |
| - } |
80 |
| - |
81 |
| - BOOL ChangeColor(int iIndex, COLORREF clr) |
82 |
| - { |
83 |
| - ATLASSERT(::IsWindow(m_hWnd)); |
84 |
| - int iItem = FindColor(clr); |
85 |
| - if( iItem == -1 ) return FALSE; |
86 |
| - SetItemData(iItem, (DWORD) clr); |
87 |
| - Invalidate(); |
88 |
| - return TRUE; |
89 |
| - } |
90 |
| - |
91 |
| - BOOL SelectColor(COLORREF clr) |
92 |
| - { |
93 |
| - ATLASSERT(::IsWindow(m_hWnd)); |
94 |
| - int iItem = FindColor(clr); |
95 |
| - if( iItem == -1 ) return FALSE; |
96 |
| - return SetCurSel(iItem); |
97 |
| - } |
98 |
| - |
99 |
| - COLORREF GetSelectedColor() const |
100 |
| - { |
101 |
| - ATLASSERT(::IsWindow(m_hWnd)); |
102 |
| - int iItem = GetCurSel(); |
103 |
| - if( iItem == -1 ) return (COLORREF) -1; // Returns -1 if none if selected |
104 |
| - return (COLORREF) GetItemData(iItem) & 0xFFFFFF; |
105 |
| - } |
106 |
| - |
107 |
| - int FindColor(COLORREF clr) const |
108 |
| - { |
109 |
| - ATLASSERT(::IsWindow(m_hWnd)); |
110 |
| - int nCount = GetCount(); |
111 |
| - for( int i = 0; i < nCount; i++ ) { |
112 |
| - if( (GetItemData(i) & 0xFFFFFF) == (DWORD) clr ) return i; |
113 |
| - } |
114 |
| - return -1; |
115 |
| - } |
116 |
| - |
117 |
| - // Implementation |
118 |
| - |
119 |
| - void _Init() |
120 |
| - { |
121 |
| - ATLASSERT(::IsWindow(m_hWnd)); |
122 |
| - // Need to set these in resource editor |
123 |
| - // If it's a ListBox, use LBS_OWNERDRAWFIXED. |
124 |
| - ATLASSERT(GetStyle() & CBS_OWNERDRAWFIXED); |
125 |
| - } |
126 |
| - |
127 |
| - // Message map and handlers |
128 |
| - |
129 |
| - BEGIN_MSG_MAP(CColorPickerImpl) |
130 |
| - MESSAGE_HANDLER(WM_CREATE, OnCreate) |
131 |
| - CHAIN_MSG_MAP_ALT( COwnerDraw< T >, 1 ) |
132 |
| - END_MSG_MAP() |
133 |
| - |
134 |
| - LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) |
135 |
| - { |
136 |
| - LRESULT lRes = DefWindowProc(); |
137 |
| - _Init(); |
138 |
| - return lRes; |
139 |
| - } |
140 |
| - |
141 |
| - // COwnerDraw |
142 |
| - |
143 |
| - void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) |
144 |
| - { |
145 |
| - if( lpDrawItemStruct->itemID == -1 ) return; |
146 |
| - |
147 |
| - CDCHandle dc = lpDrawItemStruct->hDC; |
148 |
| - RECT rc = lpDrawItemStruct->rcItem; |
149 |
| - COLORREF clr = (COLORREF) lpDrawItemStruct->itemData; |
150 |
| - bool bSelected = lpDrawItemStruct->itemState & ODS_SELECTED; |
151 |
| - |
152 |
| - // Never paint selected color for combobox itself (edit part) |
153 |
| - if( lpDrawItemStruct->itemState & ODS_COMBOBOXEDIT ) bSelected = false; |
154 |
| - |
155 |
| - // Fill background and color item |
156 |
| - CBrushHandle brBack = ::GetSysColorBrush(bSelected ? COLOR_HIGHLIGHT : COLOR_WINDOW); |
157 |
| - dc.FillRect(&rc, brBack); |
158 |
| - ::InflateRect(&rc, -s_cxIndent, -s_cyIndent); |
159 |
| - dc.FillSolidRect(&rc, RGB(0,0,0)); // Hmm, should have been a call to ::Rectangle() |
160 |
| - ::InflateRect(&rc, -1, -1); |
161 |
| - dc.FillSolidRect(&rc, clr & 0xFFFFFF); |
| 44 | + DECLARE_WND_SUPERCLASS(NULL, TBase::GetWndClassName()) |
| 45 | + |
| 46 | + enum |
| 47 | + { |
| 48 | + s_cxIndent = 1, |
| 49 | + s_cyIndent = 1 |
| 50 | + }; |
| 51 | + |
| 52 | + // Operations |
| 53 | + |
| 54 | + BOOL SubclassWindow(HWND hWnd) |
| 55 | + { |
| 56 | + ATLASSERT(m_hWnd == NULL); |
| 57 | + ATLASSERT(::IsWindow(hWnd)); |
| 58 | + BOOL bRet = CWindowImpl<T, TBase, TWinTraits>::SubclassWindow(hWnd); |
| 59 | + if (bRet) |
| 60 | + _Init(); |
| 61 | + return bRet; |
| 62 | + } |
| 63 | + |
| 64 | + int AddColor(int iIndex, COLORREF clr) |
| 65 | + { |
| 66 | + ATLASSERT(::IsWindow(m_hWnd)); |
| 67 | + int iItem = InsertString(iIndex, _T("")); |
| 68 | + if (iItem == -1) |
| 69 | + return -1; |
| 70 | + SetItemData(iItem, (DWORD)clr); |
| 71 | + return iItem; |
| 72 | + } |
| 73 | + |
| 74 | + BOOL RemoveColor(COLORREF clr) |
| 75 | + { |
| 76 | + ATLASSERT(::IsWindow(m_hWnd)); |
| 77 | + int iItem = FindColor(clr); |
| 78 | + if (iItem == -1) |
| 79 | + return FALSE; |
| 80 | + return DeleteString(iItem); |
| 81 | + } |
| 82 | + |
| 83 | + BOOL ChangeColor(int iIndex, COLORREF clr) |
| 84 | + { |
| 85 | + ATLASSERT(::IsWindow(m_hWnd)); |
| 86 | + int iItem = FindColor(clr); |
| 87 | + if (iItem == -1) |
| 88 | + return FALSE; |
| 89 | + SetItemData(iItem, (DWORD)clr); |
| 90 | + Invalidate(); |
| 91 | + return TRUE; |
| 92 | + } |
| 93 | + |
| 94 | + BOOL SelectColor(COLORREF clr) |
| 95 | + { |
| 96 | + ATLASSERT(::IsWindow(m_hWnd)); |
| 97 | + int iItem = FindColor(clr); |
| 98 | + if (iItem == -1) |
| 99 | + return FALSE; |
| 100 | + return SetCurSel(iItem); |
| 101 | + } |
| 102 | + |
| 103 | + COLORREF GetSelectedColor() const |
| 104 | + { |
| 105 | + ATLASSERT(::IsWindow(m_hWnd)); |
| 106 | + int iItem = GetCurSel(); |
| 107 | + if (iItem == -1) |
| 108 | + return (COLORREF)-1; // Returns -1 if none if selected |
| 109 | + return (COLORREF)GetItemData(iItem) & 0xFFFFFF; |
| 110 | + } |
| 111 | + |
| 112 | + int FindColor(COLORREF clr) const |
| 113 | + { |
| 114 | + ATLASSERT(::IsWindow(m_hWnd)); |
| 115 | + int nCount = GetCount(); |
| 116 | + for (int i = 0; i < nCount; i++) |
| 117 | + { |
| 118 | + if ((GetItemData(i) & 0xFFFFFF) == (DWORD)clr) |
| 119 | + return i; |
| 120 | + } |
| 121 | + return -1; |
| 122 | + } |
| 123 | + |
| 124 | + // Implementation |
| 125 | + |
| 126 | + void _Init() |
| 127 | + { |
| 128 | + ATLASSERT(::IsWindow(m_hWnd)); |
| 129 | + // Need to set these in resource editor |
| 130 | + // If it's a ListBox, use LBS_OWNERDRAWFIXED. |
| 131 | + ATLASSERT(GetStyle() & CBS_OWNERDRAWFIXED); |
| 132 | + } |
| 133 | + |
| 134 | + // Message map and handlers |
| 135 | + |
| 136 | + BEGIN_MSG_MAP(CColorPickerImpl) |
| 137 | + MESSAGE_HANDLER(WM_CREATE, OnCreate) |
| 138 | + CHAIN_MSG_MAP_ALT(COwnerDraw<T>, 1) |
| 139 | + END_MSG_MAP() |
| 140 | + |
| 141 | + LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) |
| 142 | + { |
| 143 | + LRESULT lRes = DefWindowProc(); |
| 144 | + _Init(); |
| 145 | + return lRes; |
| 146 | + } |
| 147 | + |
| 148 | + // COwnerDraw |
| 149 | + |
| 150 | + void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) |
| 151 | + { |
| 152 | + if (lpDrawItemStruct->itemID == -1) |
| 153 | + return; |
| 154 | + |
| 155 | + CDCHandle dc = lpDrawItemStruct->hDC; |
| 156 | + RECT rc = lpDrawItemStruct->rcItem; |
| 157 | + COLORREF clr = (COLORREF)lpDrawItemStruct->itemData; |
| 158 | + bool bSelected = lpDrawItemStruct->itemState & ODS_SELECTED; |
| 159 | + |
| 160 | + // Never paint selected color for combobox itself (edit part) |
| 161 | + if (lpDrawItemStruct->itemState & ODS_COMBOBOXEDIT) |
| 162 | + bSelected = false; |
| 163 | + |
| 164 | + // Fill background and color item |
| 165 | + CBrushHandle brBack = ::GetSysColorBrush(bSelected ? COLOR_HIGHLIGHT : COLOR_WINDOW); |
| 166 | + dc.FillRect(&rc, brBack); |
| 167 | + ::InflateRect(&rc, -s_cxIndent, -s_cyIndent); |
| 168 | + dc.FillSolidRect(&rc, RGB(0, 0, 0)); // Hmm, should have been a call to ::Rectangle() |
| 169 | + ::InflateRect(&rc, -1, -1); |
| 170 | + dc.FillSolidRect(&rc, clr & 0xFFFFFF); |
162 | 171 |
|
163 | 172 | #if defined(IDS_CUSTOM) && (_ATL_VER < 0x0700)
|
164 |
| - // NOTE: A little hack to display the "Custom" text on |
165 |
| - // a color item. Simply add 0xFF000000 to the value of |
166 |
| - // the color and remember to include the "resource.h" header |
167 |
| - // before this file... |
168 |
| - if( (clr & 0xFF000000) == 0xFF000000 ) { |
169 |
| - TCHAR szCustom[32] = { 0 }; |
170 |
| - ::LoadString(_Module.GetResourceInstance(), IDS_CUSTOM, szCustom, 31); |
171 |
| - dc.SetBkMode(TRANSPARENT); |
172 |
| - dc.SetTextColor(RGB(255,255,255)); |
173 |
| - dc.DrawText(szCustom, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER); |
174 |
| - } |
| 173 | + // NOTE: A little hack to display the "Custom" text on |
| 174 | + // a color item. Simply add 0xFF000000 to the value of |
| 175 | + // the color and remember to include the "resource.h" header |
| 176 | + // before this file... |
| 177 | + if ((clr & 0xFF000000) == 0xFF000000) |
| 178 | + { |
| 179 | + TCHAR szCustom[32] = {0}; |
| 180 | + ::LoadString(_Module.GetResourceInstance(), IDS_CUSTOM, szCustom, 31); |
| 181 | + dc.SetBkMode(TRANSPARENT); |
| 182 | + dc.SetTextColor(RGB(255, 255, 255)); |
| 183 | + dc.DrawText(szCustom, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER); |
| 184 | + } |
175 | 185 | #endif // IDS_CUSTOM
|
176 |
| - } |
| 186 | + } |
177 | 187 | };
|
178 | 188 |
|
179 |
| -class CColorPickerComboCtrl : public CColorPickerImpl<CColorPickerComboCtrl, CComboBox, CWinTraitsOR<CBS_OWNERDRAWFIXED|CBS_DROPDOWNLIST|CBS_HASSTRINGS> > |
| 189 | +class CColorPickerComboCtrl : public CColorPickerImpl<CColorPickerComboCtrl, CComboBox, CWinTraitsOR<CBS_OWNERDRAWFIXED | CBS_DROPDOWNLIST | CBS_HASSTRINGS>> |
180 | 190 | {
|
181 | 191 | public:
|
182 |
| - DECLARE_WND_SUPERCLASS(_T("WTL_ColorPickerComboBox"), GetWndClassName()) |
| 192 | + DECLARE_WND_SUPERCLASS(_T("WTL_ColorPickerComboBox"), GetWndClassName()) |
183 | 193 | };
|
184 | 194 |
|
185 |
| -class CColorPickerListCtrl : public CColorPickerImpl<CColorPickerListCtrl, CListBox, CWinTraitsOR<LBS_OWNERDRAWFIXED|LBS_HASSTRINGS> > |
| 195 | +class CColorPickerListCtrl : public CColorPickerImpl<CColorPickerListCtrl, CListBox, CWinTraitsOR<LBS_OWNERDRAWFIXED | LBS_HASSTRINGS>> |
186 | 196 | {
|
187 | 197 | public:
|
188 |
| - DECLARE_WND_SUPERCLASS(_T("WTL_ColorPickerListBox"), GetWndClassName()) |
| 198 | + DECLARE_WND_SUPERCLASS(_T("WTL_ColorPickerListBox"), GetWndClassName()) |
189 | 199 | };
|
190 | 200 |
|
191 | 201 |
|
|
0 commit comments