-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastTextBox.cs
195 lines (178 loc) · 8.17 KB
/
FastTextBox.cs
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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TFRandomizerGUI
{
// https://stackoverflow.com/a/46125277
// credit: Davide Cannizzo
public partial class FastTextBox : TextBox
{
public FastTextBox()
{
InitializeComponent();
}
[DebuggerHidden, DebuggerNonUserCode, DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string hint = string.Empty;
/// <summary>
/// Is called in preview the system handle the changes, and determines whether the text will be accepted and handled by the system.
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The current text (included the newer changes).</param>
/// <param name="input">The newer changes.</param>
/// <param name="offset">Start position of changes.</param>
/// <param name="length">Length of <paramref name="input"/>.</param>
/// <returns>Whether the system can handle the text.</returns>
public delegate bool TextAcceptorEventHandler(string oldText, string newText, string input, int offset, int length);
/// <summary>
/// Is called in preview of or after the system handle the changes.
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The current text (included the newer changes).</param>
/// <param name="input">The newer changes.</param>
/// <param name="offset">Start position of changes.</param>
/// <param name="length">Length of <paramref name="input"/>.</param>
public delegate void TextEventHandler(string oldText, string newText, string input, int offset, int length);
/// <summary>
/// Is called in preview of the system handle the changes.
/// </summary>
public event TextEventHandler PreviewTextChange = null;
/// <summary>
/// Is called after the system handled the changes.
/// </summary>
public event TextEventHandler AfterTextChange = null;
public string Hint
{
[DebuggerHidden]
get
{
return hint;
}
[DebuggerHidden]
set
{
if (value == hint)
return;
hint = value;
SendMessage(Handle, 0x1501, 1, value);
}
}
public TextAcceptorEventHandler TextAcceptor
{
[DebuggerHidden]
get;
[DebuggerHidden]
set;
}
[DebuggerHidden]
protected override void CreateHandle()
{
base.CreateHandle();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.FixedWidth | ControlStyles.FixedHeight | ControlStyles.Opaque, DoubleBuffered = true);
}
/// <summary>
/// Is called in preview of a text change, before the system handles it, and determines whether the text will be accepted and handled by system.
/// </summary>
/// <param name="oldText">The last written text.</param>
/// <param name="newText">The current text (included the newer changes).</param>
/// <param name="input">The newer changes.</param>
/// <param name="offset">Start position of changes.</param>
/// <param name="length">Length of <paramref name="input"/>.</param>
/// <returns></returns>
[DebuggerHidden]
protected virtual bool OnAcceptTextInput(string oldText, string newText, string input, int offset, int length)
{
return TextAcceptor == null ? true : TextAcceptor.Invoke(oldText, newText, input, offset, length);
}
/// <summary>
/// Is called in preview of a text change, before the system handles it.
/// </summary>
/// <param name="oldText">The last written text.</param>
/// <param name="newText">The current text (included the newer changes).</param>
/// <param name="input">The newer changes.</param>
/// <param name="offset">Start position of changes.</param>
/// <param name="length">Length of <paramref name="input"/>.</param>
[DebuggerHidden]
protected virtual void OnPreviewTextChange(string oldText, string newText, string input, int offset, int length)
{
if (PreviewTextChange != null)
PreviewTextChange.Invoke(oldText, newText, input, offset, length);
}
/// <summary>
/// Is called after the system handled the changes.
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The current text (included the newer changes).</param>
/// <param name="input">The newer changes.</param>
/// <param name="offset">Start position of changes.</param>
/// <param name="length">Length of <paramref name="input"/>.</param>
[DebuggerHidden]
protected virtual void OnAfterTextChange(string oldText, string newText, string input, int offset, int length)
{
if (AfterTextChange != null)
AfterTextChange.Invoke(oldText, newText, input, offset, length);
}
[DebuggerHidden]
private bool CallbackPreview(string oldText, string newText, string input, int offset, int length)
{
if (string.IsNullOrEmpty(newText) ? true : OnAcceptTextInput(oldText, newText, input, offset, length))
{
OnPreviewTextChange(oldText, newText, input, offset, length);
return true;
}
return false;
}
[DebuggerHidden]
private bool CallbackPreview(ref string newText, string oldText, string input, int offset, int length)
{
newText = GetNewText(oldText, input, offset, length);
return CallbackPreview(oldText, newText, input, offset, length);
}
[DebuggerHidden]
private string GetNewText(string oldText, string input, int offset, int length)
{
if (input.Length == 1 ? input.ToCharArray()[0] == (int)Keys.Back : false)
return Text.Length > 0 ? $"{oldText.Substring(0, offset - (length == 0 ? 1 : 0))}{oldText.Substring(offset + length)}" : string.Empty;
else
return $"{(offset > 0 ? oldText.Substring(0, offset) : string.Empty)}{input}{(offset == oldText.Length - 1 ? string.Empty : oldText.Substring(offset + length))}";
}
[DebuggerHidden]
protected override void WndProc(ref Message m)
{
string text = "";
switch (m.Msg)
{
case 0x102:
int code = m.WParam.ToInt32();
if (code == 22 && ModifierKeys.HasFlag(Keys.Control))
goto case 0x302;
text = ((char)code).ToString();
goto case 0xC;
case 0x302:
text = Clipboard.GetText();
goto case 0xC;
case 0xC:
string input = string.Empty, newText = string.Empty;
if (string.IsNullOrEmpty(text))
{
newText = Marshal.PtrToStringUni(m.LParam);
if (CallbackPreview(Text, newText, string.Empty, SelectionStart, SelectionLength))
base.WndProc(ref m);
}
else
{
input = text;
if (CallbackPreview(ref newText, Text, input, SelectionStart, SelectionLength))
base.WndProc(ref m);
}
OnAfterTextChange(Text, newText, input, SelectionStart, SelectionLength);
break;
default:
base.WndProc(ref m);
break;
}
}
}
}