123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace OpenSEMI.Ctrlib.Controls
- {
- public class TextBoxEx : TextBox
- {
- private string m_strOldValue = string.Empty;
- public static readonly DependencyProperty EditBoxModeProperty;
- public static readonly DependencyProperty NormalColorProperty;
- public static readonly DependencyProperty ChangedColorProperty;
- public static readonly DependencyProperty WarningColorProperty;
- private bool m_AllowEmpty = true;
- private bool m_AllowBackgroundChange = true;
- public static readonly DependencyProperty MaxValueProperty;
- public static readonly DependencyProperty MinValueProperty;
- public static readonly DependencyProperty AccuracyProperty;
- public static readonly DependencyProperty TextSavedProperty;
- public static readonly DependencyProperty ScrollToEndProperty;
- public static readonly DependencyProperty IsTextboxFocusedProperty;
- public EditBoxMode EditBoxMode
- {
- get
- {
- return (EditBoxMode)GetValue(EditBoxModeProperty);
- }
- set
- {
- SetValue(EditBoxModeProperty, value);
- }
- }
- public Brush NormalColor
- {
- get
- {
- return (Brush)GetValue(NormalColorProperty);
- }
- set
- {
- SetValue(NormalColorProperty, value);
- }
- }
- public Brush ChangedColor
- {
- get
- {
- return (Brush)GetValue(ChangedColorProperty);
- }
- set
- {
- SetValue(ChangedColorProperty, value);
- }
- }
- public Brush WarningColor
- {
- get
- {
- return (Brush)GetValue(WarningColorProperty);
- }
- set
- {
- SetValue(WarningColorProperty, value);
- }
- }
- public bool AllowEmpty
- {
- get
- {
- return m_AllowEmpty;
- }
- set
- {
- m_AllowEmpty = value;
- }
- }
- public bool AllowBackgroundChange
- {
- get
- {
- return m_AllowBackgroundChange;
- }
- set
- {
- m_AllowBackgroundChange = value;
- }
- }
- public double MaxValue
- {
- get
- {
- return (double)GetValue(MaxValueProperty);
- }
- set
- {
- SetValue(MaxValueProperty, value);
- }
- }
- public double MinValue
- {
- get
- {
- return (double)GetValue(MinValueProperty);
- }
- set
- {
- SetValue(MinValueProperty, value);
- }
- }
- public int Accuracy
- {
- get
- {
- return (int)GetValue(AccuracyProperty);
- }
- set
- {
- SetValue(AccuracyProperty, value);
- }
- }
- public bool TextSaved
- {
- get
- {
- return (bool)GetValue(TextSavedProperty);
- }
- set
- {
- SetValue(TextSavedProperty, value);
- }
- }
- public bool IsScrollToEnd
- {
- get
- {
- return (bool)GetValue(ScrollToEndProperty);
- }
- set
- {
- SetValue(ScrollToEndProperty, value);
- }
- }
- public bool IsTextboxFocused
- {
- get
- {
- return (bool)GetValue(IsTextboxFocusedProperty);
- }
- set
- {
- SetValue(IsTextboxFocusedProperty, value);
- }
- }
- public event KeyEventHandler TextBoxExEnterKeyDown;
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- }
- static TextBoxEx()
- {
- EditBoxModeProperty = DependencyProperty.Register("EditBoxMode", typeof(EditBoxMode), typeof(TextBoxEx), new UIPropertyMetadata(EditBoxMode.Default));
- NormalColorProperty = DependencyProperty.Register("NormalColor", typeof(Brush), typeof(TextBoxEx));
- ChangedColorProperty = DependencyProperty.Register("ChangedColor", typeof(Brush), typeof(TextBoxEx));
- WarningColorProperty = DependencyProperty.Register("WarningColor", typeof(Brush), typeof(TextBoxEx));
- MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(TextBoxEx), new UIPropertyMetadata(double.NaN));
- MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(TextBoxEx), new UIPropertyMetadata(double.NaN));
- AccuracyProperty = DependencyProperty.Register("Accuracy", typeof(int), typeof(TextBoxEx), new UIPropertyMetadata(4));
- TextSavedProperty = DependencyProperty.Register("TextSaved", typeof(bool), typeof(TextBoxEx), new UIPropertyMetadata(true, TextSavedChangedCallBack));
- ScrollToEndProperty = DependencyProperty.Register("IsScrollToEnd", typeof(bool), typeof(TextBoxEx), new UIPropertyMetadata(false));
- IsTextboxFocusedProperty = DependencyProperty.Register("IsTextboxFocused", typeof(bool), typeof(TextBoxEx), new UIPropertyMetadata(false));
- FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx)));
- }
- protected override void OnPreviewKeyDown(KeyEventArgs e)
- {
- switch (EditBoxMode)
- {
- case EditBoxMode.Email:
- break;
- case EditBoxMode.SignInteger:
- AllowInteger(e);
- break;
- case EditBoxMode.UnSignInteger:
- AllowUnsignInteger(e);
- break;
- case EditBoxMode.Decimal:
- AllowDecimal(e);
- break;
- case EditBoxMode.UnSignDecimal:
- AllowUnSignDecimal(e);
- break;
- case EditBoxMode.Time:
- AllowTime(e);
- break;
- case EditBoxMode.FileName:
- AllowFileName(e);
- break;
- }
- }
- private void AllowInteger(KeyEventArgs e)
- {
- int num;
- if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
- {
- num = ((e.Key == Key.End) ? 1 : 0);
- goto IL_00ae;
- }
- num = 1;
- goto IL_00ae;
- IL_00ae:
- bool flag = (byte)num != 0;
- bool flag2 = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
- bool flag3 = e.Key >= Key.D0 && e.Key <= Key.D9;
- if ((flag3 | flag2) && Keyboard.Modifiers != 0)
- {
- e.Handled = true;
- return;
- }
- if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
- {
- if (Keyboard.Modifiers != ModifierKeys.Shift)
- {
- if (base.Text.IndexOfAny(new char[1]
- {
- '-'
- }, 0) == -1 && base.CaretIndex == 0)
- {
- flag = true;
- goto IL_017c;
- }
- e.Handled = true;
- return;
- }
- flag = false;
- }
- goto IL_017c;
- IL_017c:
- e.Handled = (!flag && !flag3 && !flag2);
- }
- private void AllowUnsignInteger(KeyEventArgs e)
- {
- bool flag = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
- bool flag2 = e.Key >= Key.D0 && e.Key <= Key.D9;
- if ((flag2 | flag) && Keyboard.Modifiers != 0)
- {
- e.Handled = true;
- return;
- }
- int num;
- if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
- {
- num = ((e.Key == Key.End) ? 1 : 0);
- goto IL_0106;
- }
- num = 1;
- goto IL_0106;
- IL_0106:
- bool flag3 = (byte)num != 0;
- e.Handled = (!flag3 && !flag2 && !flag);
- }
- private void AllowDecimal(KeyEventArgs e)
- {
- int num;
- if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
- {
- num = ((e.Key == Key.End) ? 1 : 0);
- goto IL_00ae;
- }
- num = 1;
- goto IL_00ae;
- IL_00ae:
- bool flag = (byte)num != 0;
- bool flag2 = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
- bool flag3 = e.Key >= Key.D0 && e.Key <= Key.D9;
- if ((flag3 | flag2) && Keyboard.Modifiers != 0)
- {
- e.Handled = true;
- return;
- }
- if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
- {
- if (Keyboard.Modifiers != ModifierKeys.Shift)
- {
- if (base.Text.IndexOfAny(new char[1]
- {
- '-'
- }, 0) == -1 && base.CaretIndex == 0)
- {
- flag = true;
- goto IL_0181;
- }
- e.Handled = true;
- return;
- }
- flag = false;
- }
- goto IL_0181;
- IL_0181:
- if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
- {
- if (Keyboard.Modifiers != ModifierKeys.Shift)
- {
- if (base.Text.IndexOfAny(new char[1]
- {
- '.'
- }, 0) == -1 && base.CaretIndex > 0 && base.CaretIndex + Accuracy >= base.Text.Length)
- {
- flag = true;
- goto IL_0213;
- }
- e.Handled = true;
- return;
- }
- flag = false;
- }
- goto IL_0213;
- IL_0213:
- int num2 = base.Text.IndexOf('.');
- if (num2 > -1 && base.CaretIndex > num2 && base.Text.Length - num2 > Accuracy)
- {
- flag3 = (flag2 = false);
- }
- e.Handled = (!flag && !flag3 && !flag2);
- }
- private void AllowUnSignDecimal(KeyEventArgs e)
- {
- bool flag = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
- bool flag2 = e.Key >= Key.D0 && e.Key <= Key.D9;
- if ((flag2 | flag) && Keyboard.Modifiers != 0)
- {
- e.Handled = true;
- return;
- }
- int num;
- if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
- {
- num = ((e.Key == Key.End) ? 1 : 0);
- goto IL_0108;
- }
- num = 1;
- goto IL_0108;
- IL_0108:
- bool flag3 = (byte)num != 0;
- if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
- {
- if (Keyboard.Modifiers != ModifierKeys.Shift)
- {
- if (base.Text.IndexOfAny(new char[1]
- {
- '.'
- }, 0) == -1 && base.CaretIndex > 0 && base.CaretIndex + Accuracy >= base.Text.Length)
- {
- flag3 = true;
- goto IL_019b;
- }
- e.Handled = true;
- return;
- }
- flag3 = false;
- }
- goto IL_019b;
- IL_019b:
- int num2 = base.Text.IndexOf('.');
- if (num2 > -1 && base.CaretIndex > num2 && base.Text.Length - num2 > Accuracy)
- {
- flag2 = (flag = false);
- }
- e.Handled = (!flag3 && !flag2 && !flag);
- }
- private void AllowTime(KeyEventArgs e)
- {
- AllowDecimal(e);
- if (e.Key == Key.Oem1 && Keyboard.Modifiers == ModifierKeys.Shift)
- {
- e.Handled = false;
- }
- if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
- {
- e.Handled = true;
- }
- }
- private void AllowFileName(KeyEventArgs e)
- {
- if (base.Text.Length >= 128)
- {
- e.Handled = true;
- }
- else if (Keyboard.Modifiers == ModifierKeys.Shift && (e.Key == Key.OemPeriod || e.Key == Key.OemComma || e.Key == Key.D8 || e.Key == Key.Oem1 || e.Key == Key.Oem7))
- {
- e.Handled = true;
- }
- else if (e.Key == Key.Oem2 || e.Key == Key.Oem5)
- {
- e.Handled = true;
- }
- }
- public bool IsOutOfRange()
- {
- if (!double.IsNaN(MinValue) && !double.IsNaN(MaxValue))
- {
- double result;
- bool flag = double.TryParse(base.Text, out result);
- if (result > MaxValue)
- {
- return true;
- }
- if (result < MinValue)
- {
- return true;
- }
- return false;
- }
- return false;
- }
- protected override void OnLostFocus(RoutedEventArgs e)
- {
- base.OnLostFocus(e);
- IsTextboxFocused = false;
- if (base.Text.Length == 0)
- {
- if (!AllowEmpty)
- {
- base.Text = m_strOldValue;
- }
- }
- else if (EditBoxMode == EditBoxMode.UnSignDecimal || EditBoxMode == EditBoxMode.Decimal || EditBoxMode == EditBoxMode.SignInteger || EditBoxMode == EditBoxMode.UnSignInteger)
- {
- if (IsOutOfRange())
- {
- base.Background = Brushes.Red;
- base.ToolTip = MinValue + "-" + MaxValue;
- }
- else
- {
- SetBGColor(this);
- }
- }
- }
- protected override void OnKeyDown(KeyEventArgs e)
- {
- if (e.Key == Key.Tab)
- {
- SelectAll();
- }
- KeyEventHandler textBoxExEnterKeyDown = this.TextBoxExEnterKeyDown;
- if (textBoxExEnterKeyDown != null && e.Key == Key.Return)
- {
- if (IsOutOfRange())
- {
- base.Background = Brushes.Red;
- base.ToolTip = MinValue + "-" + MaxValue;
- }
- else
- {
- textBoxExEnterKeyDown(this, e);
- }
- }
- if (e.Key == Key.Return || e.Key == Key.Return)
- {
- if (IsOutOfRange())
- {
- base.Background = Brushes.Red;
- base.ToolTip = MinValue + "-" + MaxValue;
- }
- else
- {
- SetBGColor(this);
- }
- }
- }
- protected override void OnTextChanged(TextChangedEventArgs e)
- {
- base.OnTextChanged(e);
- if (base.IsInitialized)
- {
- if (m_strOldValue != base.Text && base.IsFocused && AllowBackgroundChange)
- {
- TextSaved = false;
- }
- else
- {
- m_strOldValue = base.Text;
- TextSaved = true;
- }
- if (EditBoxMode == EditBoxMode.FileName)
- {
- Regex regex = new Regex("\\*|\\\\|\\/|\\?|\"|:|\\<|\\>|\\|");
- base.Text = regex.Replace(base.Text, string.Empty);
- }
- }
- if (IsScrollToEnd)
- {
- ScrollToEnd();
- }
- }
- protected override void OnMouseDown(MouseButtonEventArgs e)
- {
- base.OnMouseDown(e);
- if (e.ClickCount == 1)
- {
- SelectAll();
- }
- }
- protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
- {
- base.OnGotKeyboardFocus(e);
- IsTextboxFocused = true;
- }
- private static void TextSavedChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
- {
- TextBoxEx textBoxEx = d as TextBoxEx;
- if (textBoxEx != null)
- {
- if (textBoxEx.IsOutOfRange())
- {
- textBoxEx.Background = Brushes.Red;
- textBoxEx.ToolTip = textBoxEx.MinValue + "-" + textBoxEx.MaxValue;
- }
- else
- {
- SetBGColor(textBoxEx);
- }
- }
- }
- private static void SetBGColor(TextBoxEx tb)
- {
- if (tb.TextSaved)
- {
- tb.m_strOldValue = tb.Text;
- if (tb.NormalColor != null)
- {
- tb.Background = tb.NormalColor;
- }
- }
- else if (tb.ChangedColor != null)
- {
- tb.Background = tb.ChangedColor;
- }
- }
- public void SaveText()
- {
- TextSaved = true;
- }
- }
- }
|