forked from microsoft/perfview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistoryCombBox.cs
More file actions
205 lines (183 loc) · 5.91 KB
/
HistoryCombBox.cs
File metadata and controls
205 lines (183 loc) · 5.91 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
205
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Controls
{
/// <summary>
/// A trivial variation on a ComboBox that always remembers the last several entries entered into it.
/// It is intended to be a drop-in replacement for TextBox.
/// </summary>
public partial class HistoryComboBox : ComboBox
{
public HistoryComboBox()
{
IsEditable = true;
HistoryLength = 10;
KeyDown += DoKeyDown;
GotFocus += DoGotFocus;
LostFocus += DoLostFocus;
SelectionChanged += DoComboSelectionChanged;
DropDownClosed += DoDropDownClosed;
Items.Add("");
// TODO REMOVE var dpd = DependencyPropertyDescriptor.FromProperty(TextProperty, typeof(ComboBox));
// dpd.AddValueChanged(this, DoTextChanged);
}
public int HistoryLength { get; set; }
public void SetHistory(IEnumerable values)
{
Items.Clear();
int count = 0;
foreach (var value in values)
{
count++;
if (count >= HistoryLength)
{
break;
}
Items.Add(value);
}
}
public void RemoveFromHistory(string value)
{
var text = Text;
for (int i = 0; i < Items.Count; i++)
{
if ((string)Items[i] == value)
{
Items.RemoveAt(i);
Text = text;
break;
}
}
}
public bool AddToHistory(string value)
{
if (Items.Count > 0 && ((string)Items[0]) == value) // Common special case that does nothing.
{
return false;
}
RemoveFromHistory(value);
Items.Insert(0, value);
Text = value;
// Keep the number of entries under control
while (Items.Count > HistoryLength)
{
Items.RemoveAt(HistoryLength);
}
return true;
}
/// <summary>
/// This event fires when focus is lost or and Enter is typed in the box.
/// </summary>
public event RoutedEventHandler TextEntered;
/// <summary>
/// This fires only when the enter character is typed or a combo box item is selected.
/// </summary>
public event RoutedEventHandler Enter;
public void CopyFrom(HistoryComboBox other)
{
foreach (var item in other.Items)
{
Items.Add(item);
}
}
#region private
private void DoComboSelectionChanged(object sender, SelectionChangedEventArgs e)
{
m_selectedItem = null;
if (e.AddedItems != null && e.AddedItems.Count > 0)
{
m_selectedItem = e.AddedItems[0].ToString();
}
}
private void DoGotFocus(object sender, RoutedEventArgs e)
{
m_hasFocus = true;
m_origBackground = Background;
Background = new SolidColorBrush(Color.FromRgb(0xDC, 0xE5, 0xEB));
}
private void DoLostFocus(object sender, RoutedEventArgs e)
{
bool prevFocus = m_hasFocus;
m_hasFocus = false;
if (m_origBackground != null)
{
Background = m_origBackground;
}
if (prevFocus)
{
TextEntered?.Invoke(sender, e);
ValueUpdate();
}
}
private void DoKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
FireEnter(sender, e);
}
}
private void DoDropDownClosed(object sender, EventArgs e)
{
if (m_selectedItem != null)
{
Text = m_selectedItem;
}
FireEnter(sender, e as RoutedEventArgs);
}
/// <summary>
/// Force an callback as if you hit the Enter Key.
/// </summary>
private void FireEnter(object sender, RoutedEventArgs e)
{
// Remove selected text (completion guess), so we only get what the user typed
var selectionLength = GetTextBox().SelectionLength;
var text = GetTextBox().Text;
// the selection is the whole textbox, go ahead and leave it.
if (selectionLength != 0 && selectionLength != GetTextBox().Text.Length)
{
GetTextBox().Text = text.Substring(0, GetTextBox().SelectionStart);
}
if (text.Length > 0)
{
AddToHistory(Text);
}
// Logically we have lost focus. (Any way of giving it up for real?)
m_hasFocus = false;
var enter = Enter;
if (enter != null)
{
Enter(sender, e);
}
TextEntered?.Invoke(sender, e);
ValueUpdate();
}
/// <summary>
/// If someone is data-bound to me, update them.
/// </summary>
private void ValueUpdate()
{
var binding = GetBindingExpression(ComboBox.TextProperty);
if (binding != null)
{
binding.UpdateSource();
}
}
internal TextBox GetTextBox()
{
if (m_textBox == null)
{
m_textBox = (TextBox)GetTemplateChild("PART_EditableTextBox");
}
return m_textBox;
}
private Brush m_origBackground;
private bool m_hasFocus;
private string m_selectedItem;
private TextBox m_textBox;
#endregion
}
}