!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Tools / LuaRemoteDebugger / Aga.Controls / NumericTextBox.cs
blobf1a415b32714a286f48e77eb9120500db2336c42
1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
3 using System;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using System.Globalization;
9 namespace Aga.Controls
11 /// <summary>
12 /// Restricts the entry of characters to digits, the negative sign,
13 /// the decimal point, and editing keystrokes (backspace).
14 /// It does not handle the AltGr key so any keys that can be created in any
15 /// combination with AltGr these are not filtered
16 /// </summary>
17 public class NumericTextBox : TextBox
19 private const int WM_PASTE = 0x302;
20 private NumberStyles numberStyle = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign;
22 /// <summary>
23 /// Restricts the entry of characters to digits, the negative sign,
24 /// the decimal point, and editing keystrokes (backspace).
25 /// It does not handle the AltGr key
26 /// </summary>
27 /// <param name="e"></param>
28 protected override void OnKeyPress(KeyPressEventArgs e)
30 base.OnKeyPress(e);
32 e.Handled = invalidNumeric(e.KeyChar);
36 /// <summary>
37 /// Main method for verifying allowed keypresses.
38 /// This does not catch cut paste copy ... operations.
39 /// </summary>
40 /// <param name="key"></param>
41 /// <returns></returns>
42 private bool invalidNumeric(char key)
44 bool handled = false;
46 NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
47 string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
48 string negativeSign = numberFormatInfo.NegativeSign;
50 string keyString = key.ToString();
52 if (Char.IsDigit(key))
54 // Digits are OK
56 else if (AllowDecimalSeparator && keyString.Equals(decimalSeparator))
58 if (Text.IndexOf(decimalSeparator) >= 0)
60 handled = true;
63 else if (AllowNegativeSign && keyString.Equals(negativeSign))
65 if (Text.IndexOf(negativeSign) >= 0)
67 handled = true;
70 else if (key == '\b')
72 // Backspace key is OK
74 else if ((ModifierKeys & (Keys.Control)) != 0)
76 // Let the edit control handle control and alt key combinations
78 else
80 // Swallow this invalid key and beep
81 handled = true;
83 return handled;
87 /// <summary>
88 /// Method invoked when Windows sends a message.
89 /// </summary>
90 /// <param name="m">Message from Windows.</param>
91 /// <remarks>
92 /// This is over-ridden so that the user can not use
93 /// cut or paste operations to bypass the TextChanging event.
94 /// This catches ContextMenu Paste, Shift+Insert, Ctrl+V,
95 /// While it is generally frowned upon to override WndProc, no
96 /// other simple mechanism was apparent to simultaneously and
97 /// transparently intercept so many different operations.
98 /// </remarks>
99 protected override void WndProc(ref Message m)
101 // Switch to handle message...
102 switch (m.Msg)
104 case WM_PASTE:
106 // Get clipboard object to paste
107 IDataObject clipboardData = Clipboard.GetDataObject();
109 // Get text from clipboard data
110 string pasteText = (string)clipboardData.GetData(
111 DataFormats.UnicodeText);
113 // Get the number of characters to replace
114 int selectionLength = SelectionLength;
116 // If no replacement or insertion, we are done
117 if (pasteText.Length == 0)
119 break;
121 else if (selectionLength != 0)
123 base.Text = base.Text.Remove(SelectionStart, selectionLength);
126 bool containsInvalidChars = false;
127 foreach (char c in pasteText)
129 if (containsInvalidChars)
131 break;
133 else if (invalidNumeric(c))
135 containsInvalidChars = true;
139 if (!containsInvalidChars)
141 base.Text = base.Text.Insert(SelectionStart, pasteText);
144 return;
148 base.WndProc(ref m);
152 public int IntValue
156 int intValue;
157 Int32.TryParse(this.Text, numberStyle, CultureInfo.CurrentCulture.NumberFormat, out intValue);
158 return intValue;
162 public decimal DecimalValue
166 decimal decimalValue;
167 Decimal.TryParse(this.Text, numberStyle, CultureInfo.CurrentCulture.NumberFormat, out decimalValue);
168 return decimalValue;
173 private bool allowNegativeSign;
174 [DefaultValue(true)]
175 public bool AllowNegativeSign
177 get { return allowNegativeSign; }
178 set { allowNegativeSign = value; }
181 private bool allowDecimalSeparator;
182 [DefaultValue(true)]
183 public bool AllowDecimalSeparator
185 get { return allowDecimalSeparator; }
186 set { allowDecimalSeparator = value; }