forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilteredComboBox.h
More file actions
206 lines (144 loc) · 6.7 KB
/
FilteredComboBox.h
File metadata and controls
206 lines (144 loc) · 6.7 KB
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#ifndef FILTERED_COMBO_BOX_H
#define FILTERED_COMBO_BOX_H
#ifdef _WIN32
#pragma once
#endif
#include "utlvector.h"
#pragma warning( disable: 4355 )
// Flags for the SetSuggestions call.
#define SETSUGGESTIONS_SELECTFIRST 0x0001 // Select the first item in the list.
#define SETSUGGESTIONS_CALLBACK 0x0002 // Calls OnTextChanged for whatever it winds up selecting.
// CFilteredComboBox is a glorified EDIT control.
// The user can type stuff into the edit control, and it will provide autocomplete suggestions
// in its combo box. The user of this class provides those suggestions.
//
// To use this class:
//
// 1. Implement CFilteredComboBox::ICallbacks
// 2. Call SetSuggestions to set the list of autocomplete suggestions.
// 3. Call SetOnlyProvideSuggestions to tell it how to behave.
//
// NOTE: Use CComboBox functions with caution! You could screw up the CFilteredComboBox's operation.
class CFilteredComboBox : public CComboBox
{
typedef CComboBox BaseClass;
public:
// Implement this to get updates about the state.
class ICallbacks
{
public:
// Called when the text in the box changes.
virtual void OnTextChanged( const char *pText ) = 0;
// This is sort of a backdoor for "only provide suggestions" mode. Normally, it'll only call
// OnTextChanged with entries that are in the suggestions list. But, it will call OnUnknownEntry
// if they type in something that's not in your suggestion list first. If you return TRUE, it
// will add that entry to the suggestions list. If you return FALSE (the default behavior),
// it will find the closest match to what the user typed and use that.
virtual bool OnUnknownEntry( const char *pText ) { return false; }
};
CFilteredComboBox( CFilteredComboBox::ICallbacks *pCallbacks );
// The main functions to operate the filtered combo box are here.
// This is the list of strings that is filtered into the dropdown combo box.
// flags is a combination of the SETSUGGESTIONS_ flags.
void SetSuggestions( CUtlVector<CString> &suggestions, int flags=SETSUGGESTIONS_SELECTFIRST|SETSUGGESTIONS_CALLBACK );
// Add a single suggestion (if it's unique).
void AddSuggestion( const CString &suggestion );
// This clears all items from the combo and its textbox.
void Clear();
// This will force the edit control text. It won't call OnTextChanged.
void ForceEditControlText( const char *pStr );
// This sets the main mode that the box runs in.
//
// If you pass true, then it will only ever call ICallbacks::OnTextChanged with values that are in your suggestions,
// and it does its best to autocomplete to those suggestions (so if the user types a partial string and closes
// the box, it will find the first possible substring match OR it will revert to the last valid suggestion it was on).
//
// If you pass false, then it will call OnTextChanged for anything that gets entered into the textbox. This is used
// for the entity properties targetname box, and the entity name changes right along as you type.
// When the user presses enter, it does NOT automatically select the first suggestion. They have to use the arrow keys for that.
void SetOnlyProvideSuggestions( bool bOnlyProvideSuggestions );
// These provide access to special behavior like font and color.
// Puts this string in the edit control and selects it in the combo box.
void SelectItem( const char *pStr );
// Returns the same value as the last call to OnTextChanged().
CString GetCurrentItem();
// Get/set the font in the edit control.
void SetEditControlFont( HFONT hFont );
HFONT GetEditControlFont() const;
// Get/set the color that the edit text is drawn in.
void SetEditControlTextColor( COLORREF clr );
COLORREF GetEditControlTextColor() const;
// General windows functions.
// Enable/disable the window.
bool IsWindowEnabled() const;
void EnableWindow( bool bEnable );
// Helper functions.
public:
// This takes the string the user has entered (pStringToMatch passed into GetItemsMatchingString)
// and returns true if pTestString matches it. It ignores underscores in both strings.
bool MatchString( const char *pStringToMatch, const char *pTestString );
// Does this string match one of the suggestions?
// Returns the suggestion index or -1.
int FindSuggestion( const char *pTest ) const;
// Returns the closest-matching suggestion (the first one that would appear
// in the autocomplete list) or the last known good suggestion.
CString GetBestSuggestion( const char *pTest );
void SubclassDlgItem(UINT nID, CWnd *pParent);
protected:
// Get the base font it's using.
CFont& GetNormalFont();
// Get/set the text in the edit control.
void SetEditControlText( const char *pText );
CString GetEditControlText() const;
DECLARE_MESSAGE_MAP()
bool m_bNotifyParent; // Whether we allow our parent to hook our notification messages.
// This is necessary because CControlBar-derived classes result in multiple
// message reflections unless we disable parent notification.
protected:
// Put all suggestions into the dropdown list.
void FillDropdownList( const char *pInitialSel, bool bEnableRedraw=true );
// CBN_ notification handlers.
virtual BOOL PreCreateWindow( CREATESTRUCT& cs );
BOOL OnDropDown();
BOOL OnSelEndOK();
BOOL OnCloseUp();
BOOL OnSelChange();
virtual BOOL OnEditChange();
afx_msg HBRUSH OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor);
void OnEnterKeyPressed( const char *pForceText );
void OnEscapeKeyPressed();
void DoTextChangedCallback( const char *pText );
// Gets the items matching the string and sorts the list alphabetically.
virtual void GetItemsMatchingString( const char *pStringToMatch, CUtlVector<CString> &matchingItems );
static int SortFn( const CString *pItem1, const CString *pItem2 );
virtual LRESULT DefWindowProc(
UINT message,
WPARAM wParam,
LPARAM lParam );
// Overrides for owner draw.
virtual void MeasureItem(LPMEASUREITEMSTRUCT pStruct);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
private:
void InternalSetEditControlFont( HFONT hFont, const char *pEditText, DWORD sel );
void CreateFonts();
bool InternalSelectItemByName( const char *pName );
private:
CUtlVector<CString> m_Suggestions;
HFONT m_hEditControlFont;
CFont m_NormalFont;
CFilteredComboBox::ICallbacks *m_pCallbacks;
bool m_bWasEditing;
DWORD m_dwTextColor;
bool m_bOnlyProvideSuggestions;
bool m_bInEnterKeyPressedHandler;
HFONT m_hQueuedFont;
bool m_bInSelChange;
// We go back here if they type text that we can't give a suggestion on and press enter (or lose focus).
CString m_LastTextChangedValue;
};
#endif // FILTERED_COMBO_BOX_H