-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfcx.ComboBox.cpp
422 lines (364 loc) · 16.4 KB
/
mfcx.ComboBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
Copyright (C) 2021 David Maisonave
The mfcx source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
Purpose:
See header file mfcx.ComboBox.h
Usage Details:
See header file mfcx.ComboBox.h
*/
#include "pch.h"
#include "mfcx.ComboBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
namespace mfcx
{
COLORREF GetColor( COLORREF colorref, int DefaultID )
{
return (colorref == DEFAULT_COLORS) ? GetSysColor( DefaultID ) : colorref;
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// ComboBoxItemDetails class and implementation
class ComboBoxItemDetails
{
public:
ComboBoxItemDetails( BOOL isenabled = TRUE, CString tooltipstr = _T( "" ),
COLORREF DisabledColor = DEFAULT_COLORS, COLORREF DisabledBkColor = DEFAULT_COLORS,
COLORREF EnabledColor = DEFAULT_COLORS, COLORREF EnabledBkColor = DEFAULT_COLORS,
COLORREF EnabledSelectColor = DEFAULT_COLORS, COLORREF EnabledSelectBkColor = DEFAULT_COLORS )
:IsEnabled( isenabled ), ToolTipStr( tooltipstr )
, m_DisabledColor( DisabledColor ), m_DisabledBkColor( DisabledBkColor )
, m_EnabledColor( EnabledColor ), m_EnabledBkColor( EnabledBkColor )
, m_EnabledSelectColor( EnabledSelectColor ), m_EnabledSelectBkColor( EnabledSelectBkColor )
{
}
BOOL IsEnabled;
CString ToolTipStr;
COLORREF m_DisabledColor;
COLORREF m_DisabledBkColor;
COLORREF m_EnabledColor;
COLORREF m_EnabledBkColor;
COLORREF m_EnabledSelectColor;
COLORREF m_EnabledSelectBkColor;
};
BOOL IsItemEnabled( ComboBox* combobox, UINT nIndex, ComboBoxItemDetails * comboboxitemdetails = NULL )
{
if ( nIndex >= static_cast<DWORD>(combobox->GetCount()) )
return TRUE;
ComboBoxItemDetails* data = static_cast<ComboBoxItemDetails*>(combobox->GetItemDataPtr( nIndex ));
if ( data == NULL )
return TRUE;
if ( comboboxitemdetails != NULL )
*comboboxitemdetails = *data;
return data->IsEnabled;
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// ComboBoxListBox functions
class ComboBoxListBox : public CWnd
{
public:
ComboBoxListBox() :m_Parent( NULL ) {}
void SetParent( ComboBox * ptr ) { m_Parent = ptr; }
//{{AFX_VIRTUAL(ComboBoxListBox)
//}}AFX_VIRTUAL
protected:
ComboBox *m_Parent;
//{{AFX_MSG(ComboBoxListBox)
afx_msg void OnLButtonUp( UINT nFlags, CPoint point );
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// ComboBox implementation
const UINT nMessage = ::RegisterWindowMessage( _T( "ComboSelEndOK" ) );
const ColorRefSet ComboBox::DefaultColors;
const TextColorRefSet ComboBox::DefaultTextColors( DEFAULT_COLORS );
const TextBackGroundColorRefSet ComboBox::DefaultTextBkColors( DEFAULT_COLORS );
ComboBox::ComboBox( const ColorRefSet &colorrefset )
:m_ToolTipItemRecievedStr( FALSE ), m_bLastCB_Sel( -2 )
, m_DisabledColor( GetColor( colorrefset.DisabledColor, COLOR_WINDOWTEXT ) ), m_DisabledBkColor( GetColor( colorrefset.DisabledBkColor, COLOR_WINDOW ) )
, m_EnabledColor( GetColor( colorrefset.EnabledColor, COLOR_WINDOWTEXT ) ), m_EnabledBkColor( GetColor( colorrefset.EnabledBkColor, COLOR_WINDOW ) )
, m_EnabledSelectColor( GetColor( colorrefset.EnabledSelectColor, COLOR_HIGHLIGHTTEXT ) ), m_EnabledSelectBkColor( GetColor( colorrefset.EnabledSelectBkColor, COLOR_HIGHLIGHT ) )
, m_ListBox( new ComboBoxListBox() ), m_DoToolTipPopupWhenItemStringTooWide( FALSE )
{
Initiate();
}
ComboBox::ComboBox( const CString &ToolTipString, const ColorRefSet &colorrefset )
:m_ToolTipItemRecievedStr( FALSE ), m_bLastCB_Sel( -2 ), m_ToolTipString( ToolTipString )
, m_DisabledColor( GetColor( colorrefset.DisabledColor, COLOR_WINDOWTEXT ) ), m_DisabledBkColor( GetColor( colorrefset.DisabledBkColor, COLOR_WINDOW ) )
, m_EnabledColor( GetColor( colorrefset.EnabledColor, COLOR_WINDOWTEXT ) ), m_EnabledBkColor( GetColor( colorrefset.EnabledBkColor, COLOR_WINDOW ) )
, m_EnabledSelectColor( GetColor( colorrefset.EnabledSelectColor, COLOR_HIGHLIGHTTEXT ) ), m_EnabledSelectBkColor( GetColor( colorrefset.EnabledSelectBkColor, COLOR_HIGHLIGHT ) )
, m_ListBox( new ComboBoxListBox() ), m_DoToolTipPopupWhenItemStringTooWide( FALSE )
{
Initiate();
}
void ComboBox::Initiate()
{
m_ListBox->SetParent( this );
}
ComboBox::~ComboBox()
{
}
BEGIN_MESSAGE_MAP( ComboBox, CComboBox )
//{{AFX_MSG_MAP(ComboBox)
ON_WM_CHARTOITEM()
ON_CONTROL_REFLECT( CBN_SELENDOK, OnSelendok )
ON_WM_MOUSEMOVE()
ON_WM_MOUSEWHEEL()
ON_MESSAGE( WM_CTLCOLORLISTBOX, OnCtlColor )
ON_REGISTERED_MESSAGE( nMessage, OnRealSelEndOK )
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void ComboBox::PreSubclassWindow()
{
_ASSERT_EXPR( ((GetStyle()&(CBS_OWNERDRAWFIXED | CBS_HASSTRINGS)) == (CBS_OWNERDRAWFIXED | CBS_HASSTRINGS)),
_T( "This ComboBox control requires CBS_OWNERDRAWFIXED and CBS_HASSTRINGS. Look at this ComboBox's properties settings under [Behavior], and set [Has String] to 'true' and set [Owner Draw] to 'Fixed'" ) );
UpdateMainTooltip( m_ToolTipString );
m_hwndItemTip.CreateEx( WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
m_ListBox->GetSafeHwnd(), NULL );
CComboBox::PreSubclassWindow();
}
COLORREF ComboBox::GetDisabledItemTextColor( BOOL IsDisabled, BOOL IsItemSelected, const TextColorRefSet &Superseded )
{
const COLORREF SelectedColor = (Superseded.EnabledSelectColor == DEFAULT_COLORS) ? m_EnabledSelectColor : Superseded.EnabledSelectColor;
if ( !IsDisabled && IsItemSelected )
return SelectedColor;
const COLORREF DisabledColor = (Superseded.DisabledColor == DEFAULT_COLORS) ? m_DisabledColor : Superseded.DisabledColor;
const COLORREF EnabledColor = (Superseded.EnabledColor == DEFAULT_COLORS) ? m_EnabledColor : Superseded.EnabledColor;
const COLORREF TextColor = IsDisabled ? DisabledColor : EnabledColor;
return TextColor;
}
COLORREF ComboBox::GetDisabledItemTextBkColor( BOOL IsDisabled, BOOL IsItemSelected, const TextBackGroundColorRefSet &Superseded )
{
const COLORREF SelectedBkColor = (Superseded.EnabledSelectBkColor == DEFAULT_COLORS) ? m_EnabledSelectBkColor : Superseded.EnabledSelectBkColor;
if ( !IsDisabled && IsItemSelected )
return SelectedBkColor;
const COLORREF DisabledBkColor = (Superseded.DisabledBkColor == DEFAULT_COLORS) ? m_DisabledBkColor : Superseded.DisabledBkColor;
const COLORREF EnabledBkColor = (Superseded.EnabledBkColor == DEFAULT_COLORS) ? m_EnabledBkColor : Superseded.EnabledBkColor;
const COLORREF TextBkColor = IsDisabled ? DisabledBkColor : EnabledBkColor;
return TextBkColor;
}
void ComboBox::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
RECT &rc = lpDrawItemStruct->rcItem;
BOOL IsItemSelected = (lpDrawItemStruct->itemState & ODS_SELECTED) ? TRUE : FALSE;
COLORREF oldTextColor = DEFAULT_COLORS;
COLORREF oldBkColor = DEFAULT_COLORS;
CFont* pOldFont = NULL;
CString strText;
if ( ((LONG)(lpDrawItemStruct->itemID) >= 0) &&
(lpDrawItemStruct->itemAction & (ODA_DRAWENTIRE | ODA_SELECT)) )
{
ComboBoxItemDetails comboboxitemdetails;
BOOL IsItemDisabled = !IsWindowEnabled() || !IsItemEnabled( this, lpDrawItemStruct->itemID, &comboboxitemdetails );
CString ToolTipItem = comboboxitemdetails.ToolTipStr;
COLORREF newTextColor = GetDisabledItemTextColor( IsItemDisabled, IsItemSelected, TextColorRefSet(comboboxitemdetails.m_DisabledColor, comboboxitemdetails.m_EnabledColor, comboboxitemdetails.m_EnabledSelectColor) );
COLORREF newBkColor = GetDisabledItemTextBkColor( IsItemDisabled, IsItemSelected, TextBackGroundColorRefSet(comboboxitemdetails.m_DisabledBkColor, comboboxitemdetails.m_EnabledBkColor, comboboxitemdetails.m_EnabledSelectBkColor) );
oldTextColor = pDC->SetTextColor( newTextColor );
oldBkColor = pDC->SetBkColor( newBkColor );
pOldFont = pDC->SelectObject( pDC->GetCurrentFont() );
GetLBText( lpDrawItemStruct->itemID, strText );
CRect rectClient( rc );
pDC->ExtTextOut( rc.left,
rc.top,
ETO_OPAQUE, &rc,
strText, NULL );
CSize TextSize = pDC->GetTextExtent( strText );
if ( m_DoToolTipPopupWhenItemStringTooWide && (TextSize.cx > (lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left)) )
{
if ( ToolTipItem.IsEmpty() )
ToolTipItem = strText;
else
ToolTipItem = _T( "[" ) + strText + _T( "] -- " ) + ToolTipItem;
}
m_ToolTipItemRecievedStr = (ToolTipItem.IsEmpty()) ? m_ToolTipItemRecievedStr : TRUE;
if ( m_hwndItemTip.m_hWnd != NULL && m_ToolTipItemRecievedStr )
{
int nComboButtonWidth = ::GetSystemMetrics( SM_CXHTHUMB ) + 2;
rectClient.right = rectClient.right - nComboButtonWidth;
ClientToScreen( &rectClient );
TOOLINFO toolInfo = {sizeof( toolInfo ), TTF_IDISHWND | TTF_SUBCLASS, m_ListBox->GetSafeHwnd(), reinterpret_cast<UINT_PTR>(m_ListBox->GetSafeHwnd())};
if ( static_cast<int>(lpDrawItemStruct->itemID) != m_bLastCB_Sel || ToolTipItem.IsEmpty() )
{
m_bLastCB_Sel = static_cast<int>(lpDrawItemStruct->itemID);
TOOLINFO toolInfo_toDelete = {sizeof( toolInfo_toDelete )};
// Not sure why, but in order to remove the old tooltip from another item,
// both the fetched tooltip and the about to be added tooltip need to get a deletion request.
if ( m_hwndItemTip.SendMessage( TTM_GETCURRENTTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo_toDelete) ) )
m_hwndItemTip.SendMessage( TTM_DELTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo_toDelete) );
m_hwndItemTip.SendMessage( TTM_DELTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo) );
}
if ( !ToolTipItem.IsEmpty() )
{
toolInfo.lpszText = const_cast<WCHAR*>(ToolTipItem.operator LPCWSTR());
rectClient.left += 1;
rectClient.top += 3;
m_hwndItemTip.SendMessage( TTM_TRACKPOSITION, 0, static_cast<LPARAM>(MAKELONG( rectClient.left, rectClient.top )) );
m_hwndItemTip.SendMessage( TTM_UPDATETIPTEXT, 0, reinterpret_cast<LPARAM>(&toolInfo) );
m_hwndItemTip.SendMessage( TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo) );
}
}
} else if ( (LONG)(lpDrawItemStruct->itemID) < 0 ) // drawing edit text
{
oldTextColor = pDC->SetTextColor( GetDisabledItemTextColor( FALSE, IsItemSelected ) );
oldBkColor = pDC->SetBkColor( GetDisabledItemTextBkColor( FALSE, IsItemSelected ) );
GetWindowText( strText );
pDC->ExtTextOut( rc.left + 2,
rc.top + 2,
ETO_OPAQUE, &rc,
strText, strText.GetLength(), NULL );
}
if ( oldTextColor != DEFAULT_COLORS && oldBkColor != DEFAULT_COLORS )
{
pDC->SetTextColor( oldTextColor );
pDC->SetBkColor( oldBkColor );
if ( pOldFont != NULL )
pDC->SelectObject( pOldFont );
}
if ( (lpDrawItemStruct->itemAction & ODA_FOCUS) != 0 )
pDC->DrawFocusRect( &lpDrawItemStruct->rcItem );
}
void ComboBox::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{
UNREFERENCED_PARAMETER( lpMeasureItemStruct );
}
int ComboBox::OnCharToItem( UINT nChar, CListBox* pListBox, UINT nIndex )
{
int ret = CComboBox::OnCharToItem( nChar, pListBox, nIndex );
if ( ret >= 0 && !IsItemEnabled( this, ret ) )
return -2;
else
return ret;
}
void ComboBox::OnSelendok()
{
GetWindowText( m_strSavedText );
PostMessage( nMessage );
}
LRESULT ComboBox::OnRealSelEndOK( WPARAM, LPARAM )
{
CString currentText;
GetWindowText( currentText );
int index = FindStringExact( -1, currentText );
if ( index >= 0 && !IsItemEnabled( this, index ) )
{
SetWindowText( m_strSavedText );
GetParent()->SendMessage( WM_COMMAND, MAKELONG( GetWindowLong( m_hWnd, GWL_ID ), CBN_SELCHANGE ), (LPARAM)m_hWnd );
}
return 0;
}
LRESULT ComboBox::OnCtlColor( WPARAM, LPARAM lParam )
{
if ( m_ListBox->m_hWnd == NULL && lParam != 0 && lParam != (LPARAM)m_hWnd )
m_ListBox->SubclassWindow( (HWND)lParam );
return Default();
}
void ComboBox::PostNcDestroy()
{
m_ListBox->Detach();
CComboBox::PostNcDestroy();
}
void ComboBox::SetItemDetails( UINT nIndex, BOOL IsEnabled, CString ToolTipStr, const ColorRefSet &colorrefset )
{
if ( nIndex >= static_cast<DWORD>(GetCount()) )
return;
ComboBoxItemDetails* data = new ComboBoxItemDetails( IsEnabled, ToolTipStr, colorrefset.DisabledColor, colorrefset.DisabledBkColor, colorrefset.EnabledColor, colorrefset.EnabledBkColor, colorrefset.EnabledSelectColor, colorrefset.EnabledSelectBkColor );
m_vComboBoxItemDetails.push_back( std::shared_ptr<ComboBoxItemDetails>( data ) );
SetItemDataPtr( nIndex, static_cast<void*>(data) );
}
int ComboBox::AddString( const CString &str, BOOL IsEnabled, const CString &ToolTipStr, const ColorRefSet &colorrefset )
{
int nIndex = CComboBox::AddString( str );
if ( nIndex != CB_ERR )
SetItemDetails( nIndex, IsEnabled, ToolTipStr, colorrefset );
return nIndex;
}
BOOL ComboBox::UpdateMainTooltip( const CString &value )
{
m_ToolTipString = value;
if ( m_hwndTip.m_hWnd == NULL && !m_ToolTipString.IsEmpty() )
{
RECT rect;
GetWindowRect( &rect );
m_hwndTip.CreateEx( WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
GetParent()->m_hWnd, NULL );
}
if ( m_hwndTip.m_hWnd != NULL )
{
TOOLINFO toolInfo_toDelete = {sizeof( toolInfo_toDelete )};
if ( m_hwndTip.SendMessage( TTM_GETCURRENTTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo_toDelete) ) )
m_hwndTip.SendMessage( TTM_DELTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo_toDelete) );
TOOLINFO toolInfo = {sizeof( toolInfo ), TTF_IDISHWND | TTF_SUBCLASS, GetParent()->m_hWnd, (UINT_PTR)m_hWnd};
m_hwndTip.SendMessage( TTM_DELTOOL, 0, reinterpret_cast<LPARAM>(&toolInfo) );
toolInfo.lpszText = m_ToolTipString.GetBuffer();
return static_cast<BOOL>(m_hwndTip.SendMessage( TTM_ADDTOOL, 0, (LPARAM)&toolInfo ));
}
return FALSE;
}
void ComboBox::EnableWideStrPopup( BOOL Enabled )
{
m_DoToolTipPopupWhenItemStringTooWide = Enabled;
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// ComboBoxListBox functions
BEGIN_MESSAGE_MAP( ComboBoxListBox, CWnd )
//{{AFX_MSG_MAP(ComboBoxListBox)
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void ComboBoxListBox::OnLButtonUp( UINT nFlags, CPoint point )
{
CRect rect; GetClientRect( rect );
if ( rect.PtInRect( point ) )
{
BOOL outside = FALSE;
int index = ((CListBox *)this)->ItemFromPoint( point, outside );
if ( !outside && !IsItemEnabled( m_Parent, index ) )
return; // don't click there
}
CWnd::OnLButtonUp( nFlags, point );
}
ColorRefSet::ColorRefSet( COLORREF enabledcolor, COLORREF enabledbkcolor )
:DisabledColor( DEFAULT_COLORS ), DisabledBkColor( DEFAULT_COLORS )
, EnabledColor( enabledcolor ), EnabledBkColor( enabledbkcolor )
, EnabledSelectColor( DEFAULT_COLORS ), EnabledSelectBkColor( DEFAULT_COLORS )
{
}
ColorRefSet::ColorRefSet(
COLORREF disabledcolor, COLORREF disabledbkcolor,
COLORREF enabledcolor, COLORREF enabledbkcolor,
COLORREF enabledselectcolor, COLORREF enabledselectbkcolor )
:DisabledColor( disabledcolor ), DisabledBkColor( disabledbkcolor )
, EnabledColor( enabledcolor ), EnabledBkColor( enabledbkcolor )
, EnabledSelectColor( enabledselectcolor ), EnabledSelectBkColor( enabledselectbkcolor )
{
}
DisableColorRefSet::DisableColorRefSet(
COLORREF disabledcolor, COLORREF disabledbkcolor,
COLORREF enabledcolor, COLORREF enabledbkcolor,
COLORREF enabledselectcolor, COLORREF enabledselectbkcolor )
:ColorRefSet( disabledcolor, disabledbkcolor, enabledcolor, enabledbkcolor, enabledselectcolor, enabledselectbkcolor )
{
}
TextColorRefSet::TextColorRefSet( COLORREF disabledcolor, COLORREF enabledcolor, COLORREF enabledselectcolor )
: ColorRefSet( disabledcolor, DEFAULT_COLORS, enabledcolor, DEFAULT_COLORS, enabledselectcolor, DEFAULT_COLORS )
{
}
TextBackGroundColorRefSet::TextBackGroundColorRefSet( COLORREF disabledbkcolor, COLORREF enabledbkcolor, COLORREF enabledselectbkcolor )
: ColorRefSet( DEFAULT_COLORS, disabledbkcolor, DEFAULT_COLORS, enabledbkcolor, DEFAULT_COLORS, enabledselectbkcolor )
{
}
}