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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements a combo box that autoselects items from the list as the
// user types in the edit control. It has the additional feature of
// being able to easily set the text color.
//
//=============================================================================//
#include "stdafx.h"
#include "AutoSelCombo.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
BEGIN_MESSAGE_MAP(CAutoSelComboBox, CComboBox)
//{{AFX_MSG_MAP(CAutoSelComboBox)
ON_WM_CTLCOLOR()
ON_CONTROL_REFLECT_EX(CBN_EDITUPDATE, OnEditUpdate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CAutoSelComboBox::CAutoSelComboBox(void)
{
m_szLastText[0] = '\0';
m_dwTextColor = RGB(0, 0, 0);
m_bNotifyParent = true;
m_nLastSel = -1;
}
//-----------------------------------------------------------------------------
// Purpose: Attaches this object to the given dialog item.
//-----------------------------------------------------------------------------
void CAutoSelComboBox::SubclassDlgItem(UINT nID, CWnd *pParent)
{
//
// Disable parent notifications for CControlBar-derived classes. This is
// necessary because these classes result in multiple message reflections
// unless we return TRUE from our message handler.
//
if (pParent->IsKindOf(RUNTIME_CLASS(CControlBar)))
{
m_bNotifyParent = false;
}
else
{
m_bNotifyParent = true;
}
BaseClass::SubclassDlgItem(nID, pParent);
}
//-----------------------------------------------------------------------------
// Purpose: Automatically selects the first matching combo box item when the
// edit control's text changes.
//-----------------------------------------------------------------------------
void CAutoSelComboBox::OnUpdateText(void)
{
int nCurSel = GetCurSel();
int nNewSel = nCurSel;
DWORD dwEditSel = GetEditSel();
int nEditStart = LOWORD(dwEditSel);
int nEditEnd = HIWORD(dwEditSel);
char szTypedText[MAX_PATH];
GetWindowText(szTypedText, sizeof(szTypedText));
//
// Select the first match in the list if what they typed is different from
// the current selection. This won't do anything if they are deleting characters
// from the end of the text.
//
int nTypedLen = strlen(szTypedText);
int nLastLen = strlen(m_szLastText);
bool bSearched = false;
int nIndex = CB_ERR;
if (strnicmp(szTypedText, m_szLastText, nTypedLen))
{
nIndex = FindString(-1, szTypedText);
bSearched = true;
}
else if (nTypedLen < nLastLen)
{
// They deleted characters, try to match the shorter string exactly.
nIndex = FindStringExact(-1, szTypedText);
bSearched = true;
}
if (bSearched)
{
if (nCurSel != nIndex)
{
SetCurSel(nIndex);
nNewSel = nIndex;
}
if (nIndex != CB_ERR)
{
// Found a match.
if ((nEditEnd == -1) || (nEditEnd == (int)strlen(szTypedText)))
{
SetEditSel(strlen(szTypedText), -1);
}
else
{
SetEditSel(nEditStart, nEditEnd);
}
}
else
{
// No match found.
SetWindowText(szTypedText);
SetEditSel(nEditStart, nEditEnd);
}
}
strcpy(m_szLastText, szTypedText);
if (nNewSel != m_nLastSel)
{
GetParent()->SendMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), CBN_SELCHANGE), (LPARAM)m_hWnd);
m_nLastSel = nNewSel;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
BOOL CAutoSelComboBox::OnEditUpdate(void)
{
OnUpdateText();
//
// Despite MSDN's lies, returning FALSE here allows the parent
// window to hook the notification message as well, not TRUE.
//
return m_bNotifyParent ? FALSE : TRUE;
}
//-----------------------------------------------------------------------------
// Purpose: Resets the 'last typed text' buffer every time we gain/lose focus.
//-----------------------------------------------------------------------------
void CAutoSelComboBox::OnSetFocus(CWnd *pOldWnd)
{
m_szLastText[0] = '\0';
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : dwColor -
//-----------------------------------------------------------------------------
void CAutoSelComboBox::SetTextColor(COLORREF dwColor)
{
m_dwTextColor = dwColor;
}
//-----------------------------------------------------------------------------
// Purpose: Called before painting to override default colors.
// Input : pDC - DEvice context being painted into.
// pWnd - Control asking for color.
// nCtlColor - Type of control asking for color.
// Output : Returns the handle of a brush to use as the background color.
//-----------------------------------------------------------------------------
HBRUSH CAutoSelComboBox::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
{
HBRUSH hBrush = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_EDIT)
{
pDC->SetTextColor(m_dwTextColor);
}
return(hBrush);
}
|