diff --git a/src/client/spark/view/Controls/PriceEntryControl.xaml b/src/client/spark/view/Controls/PriceEntryControl.xaml index 1b694bd80..b9a6d39b5 100644 --- a/src/client/spark/view/Controls/PriceEntryControl.xaml +++ b/src/client/spark/view/Controls/PriceEntryControl.xaml @@ -1,5 +1,6 @@  @@ -21,6 +22,7 @@ BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource TickDisplayTwoWayConverter}, Mode=TwoWay}" + u:Behavior.DisablePaste="True" /> diff --git a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs index 775f4430f..6f90ee8ff 100644 --- a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs +++ b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs @@ -1,17 +1,18 @@ using bts.api; using mbts.spark; +using mbts.ui.extensions; using mbts.ui.spark.converters; +using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; +using static mbts.ui.utility.text.TextUtils; -namespace mbts.ui.spark -{ +namespace mbts.ui.spark { /// /// Interaction logic for PriceEntryControl.xaml /// - class PriceEntryControl : Control - { + class PriceEntryControl : Control { static PriceEntryControl() => DefaultStyleKeyProperty.OverrideMetadata(typeof(PriceEntryControl), new FrameworkPropertyMetadata(typeof(PriceEntryControl))); @@ -46,6 +47,7 @@ namespace mbts.ui.spark #endregion TextBox _text; + IDisposable _text_input_hook; public PriceEntryControl() { TickUpCommand = new DelegateCommand(_tick_up); @@ -53,7 +55,9 @@ namespace mbts.ui.spark } public override void OnApplyTemplate() { - _text = GetTemplateChild("_text") as TextBox; + _text = this.assert_template_child("_text"); + Disposable.dispose(ref _text_input_hook); + _text_input_hook = _text.SubscribePreviewTextInput((s, e) => e.Handled = !RegexFilters.PriceInput.IsMatch(_text.PeekText(e))); _update_text_binding(); } diff --git a/src/client/spark/view/Extensions/UIElementExtensions.cs b/src/client/spark/view/Extensions/UIElementExtensions.cs index 3b82f1a5a..f30961cd5 100644 --- a/src/client/spark/view/Extensions/UIElementExtensions.cs +++ b/src/client/spark/view/Extensions/UIElementExtensions.cs @@ -1,4 +1,5 @@ -using System; +using mbts.ui.utility.text; +using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -86,12 +87,22 @@ namespace mbts.ui.extensions { target.PreviewDrop += callback; return Disposable.Create(() => target.PreviewDrop -= callback); } - } + public static IDisposable SubscribePreviewTextInput(this UIElement target, TextCompositionEventHandler callback) { + target.PreviewTextInput += callback; + return Disposable.Create(() => target.PreviewTextInput -= callback); + } + } + public static class ScrollViewerExtensions { public static IDisposable SubscribeScrollChanged(this ScrollViewer target, ScrollChangedEventHandler callback) { target.ScrollChanged += callback; return Disposable.Create(() => target.ScrollChanged -= callback); } } + + public static class TextBoxExtensions { + public static string PeekText(this TextBox self, TextCompositionEventArgs e) => + TextUtils.InsertText(self.Text, e.Text, self.CaretIndex); + } } \ No newline at end of file diff --git a/src/client/spark/view/Extensions/WPFAttachedBehavior.cs b/src/client/spark/view/Extensions/WPFAttachedBehavior.cs index b9c033bda..97d423382 100644 --- a/src/client/spark/view/Extensions/WPFAttachedBehavior.cs +++ b/src/client/spark/view/Extensions/WPFAttachedBehavior.cs @@ -124,5 +124,20 @@ namespace mbts.ui { public static bool GetEatMouseDown(DependencyObject d) => (bool)d.GetValue(EatMouseDownProperty); public static void SetEatMouseDown(DependencyObject d, bool value) => d.SetValue(EatMouseDownProperty, value); #endregion + + #region DisablePaste + public static readonly DependencyProperty DisablePasteProperty = DependencyProperty.RegisterAttached( + "DisablePaste", typeof(bool), typeof(Behavior), new PropertyMetadata(false, OnDisablePasteChanged)); + + static void OnDisablePasteChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if ((bool)e.NewValue) + DataObject.AddPastingHandler(d, _disable_paste_pasting_handler); + else DataObject.RemovePastingHandler(d, _disable_paste_pasting_handler); + } + static void _disable_paste_pasting_handler(object sender, DataObjectPastingEventArgs e) => e.CancelCommand(); + + public static bool GetDisablePaste(DependencyObject d) => (bool)d.GetValue(DisablePasteProperty); + public static void SetDisablePaste(DependencyObject d, bool value) => d.SetValue(DisablePasteProperty, value); + #endregion } } \ No newline at end of file diff --git a/src/client/spark/view/Utility/TextUtils.cs b/src/client/spark/view/Utility/TextUtils.cs index a9a625e72..61b2ab96c 100644 --- a/src/client/spark/view/Utility/TextUtils.cs +++ b/src/client/spark/view/Utility/TextUtils.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text.RegularExpressions; using System.Windows; using System.Windows.Documents; using System.Windows.Media; @@ -25,6 +26,22 @@ namespace mbts.ui.utility.text { #pragma warning restore CS0618 // Type or member is obsolete ft.SetFontWeight(FontWeights.Bold); return ft; - } + } + + public static string InsertText(string current_text, string insertion_text, int insertion_index) { + if (current_text == null) + return insertion_text; + else if (insertion_index == current_text.Length) + return current_text + insertion_text; + else if (insertion_index == 0) + return insertion_text + current_text; + else if (insertion_index >= 0 && insertion_index < current_text.Length) + return current_text.Substring(0, insertion_index) + insertion_text + current_text.Substring(insertion_index, current_text.Length - insertion_index); + else return null; + } + + public static class RegexFilters { + public static readonly Regex PriceInput = new Regex(@"^-?\d*((-|\.)\d*)?$"); + } } } \ No newline at end of file