From 90ee6d2fa01edc8573afbd1ee1fbd46f3112b0cc Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 14 Apr 2023 21:11:47 +0200 Subject: [PATCH] MultiFormatTextBox: fixed deleting from other format, implemented getting background brush from resources --- .../MultiFormatTextBox/MultiFormatTextBox.cs | 78 ++++++++++++++----- .../MultiFormatTextBox.xaml | 8 +- 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs index c81bde5..04de1bb 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs +++ b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs @@ -42,6 +42,8 @@ namespace MultiTerm.Wpf.CustomControl; /// internal class Format { + private readonly string? backgroundColorResourceName; + public string Name { get; set; } public Brush BackgroundBrush { get; set; } public Predicate IsKeyValid { get; set; } @@ -51,18 +53,34 @@ internal class Format this.BackgroundBrush = backgroundBrush; this.IsKeyValid = keyValidator; } - public Format(string name, string backgroundBrushColorHexCode, Predicate keyValidator) + public Format(string name, string backgroundColorResourceName, Predicate keyValidator) { this.Name = name; - var brushObj = new BrushConverter().ConvertFrom(backgroundBrushColorHexCode); - if(brushObj == null) throw new ArgumentException(nameof(backgroundBrushColorHexCode)); - this.BackgroundBrush = (SolidColorBrush)brushObj; + this.backgroundColorResourceName = backgroundColorResourceName; + this.BackgroundBrush = Brushes.White; // set background brush to white this.IsKeyValid = keyValidator; } public static List GetListOfNames(IEnumerable formats) { return formats.Select(item => item.Name).ToList(); } + public static void UpdateBackgroundBrushesFromResources(FrameworkElement fwElement, IEnumerable formats) + { + if (fwElement == null) throw new ArgumentNullException(nameof(fwElement)); + + foreach (var format in formats) + { + // if resource name not set => skip + if (format.backgroundColorResourceName == null) continue; + + // get background brush color from resources + try + { + format.BackgroundBrush = (SolidColorBrush)fwElement.FindResource(format.backgroundColorResourceName); + } + catch (Exception) { continue; } // ignore + } + } } public class MultiFormatTextBox : Control @@ -71,19 +89,19 @@ public class MultiFormatTextBox : Control private static readonly List formats = new() { // character input, accepts all keys except space - new Format("CHAR", Brushes.LightSkyBlue, delegate(Key k) { return (k != Key.Space); }), - //new Format("CHAR", "B4EBEB", delegate(Key k) { return (k != Key.Space); }), + //new Format("CHAR", Brushes.LightSkyBlue, delegate(Key k) { return (k != Key.Space); }), + new Format("CHAR", "MultiFormatTextBox.CHAR.Background", delegate(Key k) { return (k != Key.Space); }), // hex input, ignores all keys that are not inbetween 0 and F - new Format("HEX", Brushes.LightGreen, delegate(Key k) { return (k >= Key.D0 && k <= Key.F); }), - //new Format("HEX", "C8C8FF", delegate(Key k) { return (k >= Key.D0 && k <= Key.F); }), + //new Format("HEX", Brushes.LightGreen, delegate(Key k) { return (k >= Key.D0 && k <= Key.F); }), + new Format("HEX", "MultiFormatTextBox.HEX.Background", delegate(Key k) { return (k >= Key.D0 && k <= Key.F); }), // binary input, ignores all keys except 0 and 1 - new Format("BIN", Brushes.LightPink, delegate(Key k) { return (k == Key.D0 || k == Key.D1); }) - //new Format("BIN", "C8FFC8", delegate(Key k) { return (k == Key.D0 || k == Key.D1); }) + //new Format("BIN", Brushes.LightPink, delegate(Key k) { return (k == Key.D0 || k == Key.D1); }) + new Format("BIN", "MultiFormatTextBox.BIN.Background", delegate(Key k) { return (k == Key.D0 || k == Key.D1); }) }; private ComboBox comboBox; private RichTextBox richTextBox; - private Format currentlySelectedFormat = formats.First(); + private Format currentlySelectedFormat; private int offsetContentStartToFormatStart; static MultiFormatTextBox() @@ -95,6 +113,12 @@ public class MultiFormatTextBox : Control { base.OnApplyTemplate(); + // initialize format background brushes + Format.UpdateBackgroundBrushesFromResources(this, formats); + // set initially selected format + this.currentlySelectedFormat = formats!.First(); + + // get comboBox from template var comboBox = GetTemplateChild("comboBox") as ComboBox; if (comboBox != null) @@ -134,7 +158,7 @@ public class MultiFormatTextBox : Control this.InsertSeparation(); // set new start position - this.offsetContentStartToFormatStart = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.Document.ContentStart); + this.StoreCaretPosition(); // focus textbox this.richTextBox.Focus(); @@ -146,7 +170,7 @@ public class MultiFormatTextBox : Control this.richTextBox.TextChanged -= RichTextBox_TextChanged; // store caret position before - this.offsetContentStartToFormatStart = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.Document.ContentStart); + this.StoreCaretPosition(); // insert this.richTextBox.AppendText(" "); @@ -158,12 +182,17 @@ public class MultiFormatTextBox : Control color: defaultBackgroundBrush); // store new caret position after - this.offsetContentStartToFormatStart = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.Document.ContentStart); + this.StoreCaretPosition(); // reenable this.richTextBox.TextChanged += RichTextBox_TextChanged; } + private void StoreCaretPosition() + { + this.offsetContentStartToFormatStart = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.Document.ContentStart); + } + private void RichTextBox_KeyDown(object sender, KeyEventArgs e) { // guard combobox null @@ -178,12 +207,21 @@ public class MultiFormatTextBox : Control private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e) { - if(e.Changes.Count > 1) { return; } - - RtbChangeTextBackground(this.richTextBox, - start: this.richTextBox.Document.ContentStart.GetPositionAtOffset(-this.offsetContentStartToFormatStart), - end: this.richTextBox.Document.ContentEnd, - color: this.currentlySelectedFormat.BackgroundBrush); + if (e.Changes.Count > 1 || e.Changes.Count == 0) { return; } + + // if something was added => change background color + if (e.Changes.First().AddedLength > 0) + { + RtbChangeTextBackground(this.richTextBox, + start: this.richTextBox.Document.ContentStart.GetPositionAtOffset(-this.offsetContentStartToFormatStart), + end: this.richTextBox.Document.ContentEnd, + color: this.currentlySelectedFormat.BackgroundBrush); + } + // if something was removed => update start of content to current location + else if(e.Changes.First().RemovedLength > 0) + { + this.StoreCaretPosition(); + } } private static void RtbChangeTextBackground(RichTextBox rtb, TextPointer start, TextPointer end, Brush color) diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml index 5e85855..315f222 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml +++ b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml @@ -2,9 +2,6 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MultiTerm.Wpf.CustomControl"> -