|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Windows.Forms; |
| 5 | + |
| 6 | +namespace TFRandomizerGUI |
| 7 | +{ |
| 8 | + // https://stackoverflow.com/a/46125277 |
| 9 | + // credit: Davide Cannizzo |
| 10 | + public partial class FastTextBox : TextBox |
| 11 | + { |
| 12 | + public FastTextBox() |
| 13 | + { |
| 14 | + InitializeComponent(); |
| 15 | + } |
| 16 | + |
| 17 | + [DebuggerHidden, DebuggerNonUserCode, DllImport("user32.dll")] |
| 18 | + private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); |
| 19 | + |
| 20 | + [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| 21 | + private string hint = string.Empty; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Is called in preview the system handle the changes, and determines whether the text will be accepted and handled by the system. |
| 25 | + /// </summary> |
| 26 | + /// <param name="oldText">The old text.</param> |
| 27 | + /// <param name="newText">The current text (included the newer changes).</param> |
| 28 | + /// <param name="input">The newer changes.</param> |
| 29 | + /// <param name="offset">Start position of changes.</param> |
| 30 | + /// <param name="length">Length of <paramref name="input"/>.</param> |
| 31 | + /// <returns>Whether the system can handle the text.</returns> |
| 32 | + public delegate bool TextAcceptorEventHandler(string oldText, string newText, string input, int offset, int length); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Is called in preview of or after the system handle the changes. |
| 36 | + /// </summary> |
| 37 | + /// <param name="oldText">The old text.</param> |
| 38 | + /// <param name="newText">The current text (included the newer changes).</param> |
| 39 | + /// <param name="input">The newer changes.</param> |
| 40 | + /// <param name="offset">Start position of changes.</param> |
| 41 | + /// <param name="length">Length of <paramref name="input"/>.</param> |
| 42 | + public delegate void TextEventHandler(string oldText, string newText, string input, int offset, int length); |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Is called in preview of the system handle the changes. |
| 46 | + /// </summary> |
| 47 | + public event TextEventHandler PreviewTextChange = null; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Is called after the system handled the changes. |
| 51 | + /// </summary> |
| 52 | + public event TextEventHandler AfterTextChange = null; |
| 53 | + |
| 54 | + public string Hint |
| 55 | + { |
| 56 | + [DebuggerHidden] |
| 57 | + get |
| 58 | + { |
| 59 | + return hint; |
| 60 | + } |
| 61 | + [DebuggerHidden] |
| 62 | + set |
| 63 | + { |
| 64 | + if (value == hint) |
| 65 | + return; |
| 66 | + hint = value; |
| 67 | + SendMessage(Handle, 0x1501, 1, value); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public TextAcceptorEventHandler TextAcceptor |
| 72 | + { |
| 73 | + [DebuggerHidden] |
| 74 | + get; |
| 75 | + [DebuggerHidden] |
| 76 | + set; |
| 77 | + } |
| 78 | + |
| 79 | + [DebuggerHidden] |
| 80 | + protected override void CreateHandle() |
| 81 | + { |
| 82 | + base.CreateHandle(); |
| 83 | + SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.FixedWidth | ControlStyles.FixedHeight | ControlStyles.Opaque, DoubleBuffered = true); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// 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. |
| 88 | + /// </summary> |
| 89 | + /// <param name="oldText">The last written text.</param> |
| 90 | + /// <param name="newText">The current text (included the newer changes).</param> |
| 91 | + /// <param name="input">The newer changes.</param> |
| 92 | + /// <param name="offset">Start position of changes.</param> |
| 93 | + /// <param name="length">Length of <paramref name="input"/>.</param> |
| 94 | + /// <returns></returns> |
| 95 | + [DebuggerHidden] |
| 96 | + protected virtual bool OnAcceptTextInput(string oldText, string newText, string input, int offset, int length) |
| 97 | + { |
| 98 | + return TextAcceptor == null ? true : TextAcceptor.Invoke(oldText, newText, input, offset, length); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Is called in preview of a text change, before the system handles it. |
| 103 | + /// </summary> |
| 104 | + /// <param name="oldText">The last written text.</param> |
| 105 | + /// <param name="newText">The current text (included the newer changes).</param> |
| 106 | + /// <param name="input">The newer changes.</param> |
| 107 | + /// <param name="offset">Start position of changes.</param> |
| 108 | + /// <param name="length">Length of <paramref name="input"/>.</param> |
| 109 | + [DebuggerHidden] |
| 110 | + protected virtual void OnPreviewTextChange(string oldText, string newText, string input, int offset, int length) |
| 111 | + { |
| 112 | + if (PreviewTextChange != null) |
| 113 | + PreviewTextChange.Invoke(oldText, newText, input, offset, length); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Is called after the system handled the changes. |
| 118 | + /// </summary> |
| 119 | + /// <param name="oldText">The old text.</param> |
| 120 | + /// <param name="newText">The current text (included the newer changes).</param> |
| 121 | + /// <param name="input">The newer changes.</param> |
| 122 | + /// <param name="offset">Start position of changes.</param> |
| 123 | + /// <param name="length">Length of <paramref name="input"/>.</param> |
| 124 | + [DebuggerHidden] |
| 125 | + protected virtual void OnAfterTextChange(string oldText, string newText, string input, int offset, int length) |
| 126 | + { |
| 127 | + if (AfterTextChange != null) |
| 128 | + AfterTextChange.Invoke(oldText, newText, input, offset, length); |
| 129 | + } |
| 130 | + |
| 131 | + [DebuggerHidden] |
| 132 | + private bool CallbackPreview(string oldText, string newText, string input, int offset, int length) |
| 133 | + { |
| 134 | + if (string.IsNullOrEmpty(newText) ? true : OnAcceptTextInput(oldText, newText, input, offset, length)) |
| 135 | + { |
| 136 | + OnPreviewTextChange(oldText, newText, input, offset, length); |
| 137 | + return true; |
| 138 | + } |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + [DebuggerHidden] |
| 143 | + private bool CallbackPreview(ref string newText, string oldText, string input, int offset, int length) |
| 144 | + { |
| 145 | + newText = GetNewText(oldText, input, offset, length); |
| 146 | + return CallbackPreview(oldText, newText, input, offset, length); |
| 147 | + } |
| 148 | + |
| 149 | + [DebuggerHidden] |
| 150 | + private string GetNewText(string oldText, string input, int offset, int length) |
| 151 | + { |
| 152 | + if (input.Length == 1 ? input.ToCharArray()[0] == (int)Keys.Back : false) |
| 153 | + return Text.Length > 0 ? $"{oldText.Substring(0, offset - (length == 0 ? 1 : 0))}{oldText.Substring(offset + length)}" : string.Empty; |
| 154 | + else |
| 155 | + return $"{(offset > 0 ? oldText.Substring(0, offset) : string.Empty)}{input}{(offset == oldText.Length - 1 ? string.Empty : oldText.Substring(offset + length))}"; |
| 156 | + } |
| 157 | + |
| 158 | + [DebuggerHidden] |
| 159 | + protected override void WndProc(ref Message m) |
| 160 | + { |
| 161 | + string text = ""; |
| 162 | + switch (m.Msg) |
| 163 | + { |
| 164 | + case 0x102: |
| 165 | + int code = m.WParam.ToInt32(); |
| 166 | + if (code == 22 && ModifierKeys.HasFlag(Keys.Control)) |
| 167 | + goto case 0x302; |
| 168 | + text = ((char)code).ToString(); |
| 169 | + goto case 0xC; |
| 170 | + case 0x302: |
| 171 | + text = Clipboard.GetText(); |
| 172 | + goto case 0xC; |
| 173 | + case 0xC: |
| 174 | + string input = string.Empty, newText = string.Empty; |
| 175 | + if (string.IsNullOrEmpty(text)) |
| 176 | + { |
| 177 | + newText = Marshal.PtrToStringUni(m.LParam); |
| 178 | + if (CallbackPreview(Text, newText, string.Empty, SelectionStart, SelectionLength)) |
| 179 | + base.WndProc(ref m); |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + input = text; |
| 184 | + if (CallbackPreview(ref newText, Text, input, SelectionStart, SelectionLength)) |
| 185 | + base.WndProc(ref m); |
| 186 | + } |
| 187 | + OnAfterTextChange(Text, newText, input, SelectionStart, SelectionLength); |
| 188 | + break; |
| 189 | + default: |
| 190 | + base.WndProc(ref m); |
| 191 | + break; |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments