MultiFormatTextBox:

fixed deleting from other format,
implemented getting background brush from resources
master
Jonas Arnold 3 years ago
parent 06f7503586
commit 90ee6d2fa0
  1. 76
      MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs
  2. 8
      MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml

@ -42,6 +42,8 @@ namespace MultiTerm.Wpf.CustomControl;
/// </summary> /// </summary>
internal class Format internal class Format
{ {
private readonly string? backgroundColorResourceName;
public string Name { get; set; } public string Name { get; set; }
public Brush BackgroundBrush { get; set; } public Brush BackgroundBrush { get; set; }
public Predicate<Key> IsKeyValid { get; set; } public Predicate<Key> IsKeyValid { get; set; }
@ -51,18 +53,34 @@ internal class Format
this.BackgroundBrush = backgroundBrush; this.BackgroundBrush = backgroundBrush;
this.IsKeyValid = keyValidator; this.IsKeyValid = keyValidator;
} }
public Format(string name, string backgroundBrushColorHexCode, Predicate<Key> keyValidator) public Format(string name, string backgroundColorResourceName, Predicate<Key> keyValidator)
{ {
this.Name = name; this.Name = name;
var brushObj = new BrushConverter().ConvertFrom(backgroundBrushColorHexCode); this.backgroundColorResourceName = backgroundColorResourceName;
if(brushObj == null) throw new ArgumentException(nameof(backgroundBrushColorHexCode)); this.BackgroundBrush = Brushes.White; // set background brush to white
this.BackgroundBrush = (SolidColorBrush)brushObj;
this.IsKeyValid = keyValidator; this.IsKeyValid = keyValidator;
} }
public static List<string> GetListOfNames(IEnumerable<Format> formats) public static List<string> GetListOfNames(IEnumerable<Format> formats)
{ {
return formats.Select(item => item.Name).ToList(); return formats.Select(item => item.Name).ToList();
} }
public static void UpdateBackgroundBrushesFromResources(FrameworkElement fwElement, IEnumerable<Format> 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 public class MultiFormatTextBox : Control
@ -71,19 +89,19 @@ public class MultiFormatTextBox : Control
private static readonly List<Format> formats = new() private static readonly List<Format> formats = new()
{ {
// character input, accepts all keys except space // character input, accepts all keys except space
new Format("CHAR", Brushes.LightSkyBlue, delegate(Key k) { return (k != Key.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", "MultiFormatTextBox.CHAR.Background", delegate(Key k) { return (k != Key.Space); }),
// hex input, ignores all keys that are not inbetween 0 and F // 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", 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", "MultiFormatTextBox.HEX.Background", delegate(Key k) { return (k >= Key.D0 && k <= Key.F); }),
// binary input, ignores all keys except 0 and 1 // 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", 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", "MultiFormatTextBox.BIN.Background", delegate(Key k) { return (k == Key.D0 || k == Key.D1); })
}; };
private ComboBox comboBox; private ComboBox comboBox;
private RichTextBox richTextBox; private RichTextBox richTextBox;
private Format currentlySelectedFormat = formats.First(); private Format currentlySelectedFormat;
private int offsetContentStartToFormatStart; private int offsetContentStartToFormatStart;
static MultiFormatTextBox() static MultiFormatTextBox()
@ -95,6 +113,12 @@ public class MultiFormatTextBox : Control
{ {
base.OnApplyTemplate(); base.OnApplyTemplate();
// initialize format background brushes
Format.UpdateBackgroundBrushesFromResources(this, formats);
// set initially selected format
this.currentlySelectedFormat = formats!.First();
// get comboBox from template // get comboBox from template
var comboBox = GetTemplateChild("comboBox") as ComboBox; var comboBox = GetTemplateChild("comboBox") as ComboBox;
if (comboBox != null) if (comboBox != null)
@ -134,7 +158,7 @@ public class MultiFormatTextBox : Control
this.InsertSeparation(); this.InsertSeparation();
// set new start position // set new start position
this.offsetContentStartToFormatStart = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.Document.ContentStart); this.StoreCaretPosition();
// focus textbox // focus textbox
this.richTextBox.Focus(); this.richTextBox.Focus();
@ -146,7 +170,7 @@ public class MultiFormatTextBox : Control
this.richTextBox.TextChanged -= RichTextBox_TextChanged; this.richTextBox.TextChanged -= RichTextBox_TextChanged;
// store caret position before // store caret position before
this.offsetContentStartToFormatStart = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.Document.ContentStart); this.StoreCaretPosition();
// insert // insert
this.richTextBox.AppendText(" "); this.richTextBox.AppendText(" ");
@ -158,12 +182,17 @@ public class MultiFormatTextBox : Control
color: defaultBackgroundBrush); color: defaultBackgroundBrush);
// store new caret position after // store new caret position after
this.offsetContentStartToFormatStart = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.Document.ContentStart); this.StoreCaretPosition();
// reenable // reenable
this.richTextBox.TextChanged += RichTextBox_TextChanged; 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) private void RichTextBox_KeyDown(object sender, KeyEventArgs e)
{ {
// guard combobox null // guard combobox null
@ -178,12 +207,21 @@ public class MultiFormatTextBox : Control
private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e) private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
{ {
if(e.Changes.Count > 1) { return; } if (e.Changes.Count > 1 || e.Changes.Count == 0) { return; }
RtbChangeTextBackground(this.richTextBox, // if something was added => change background color
start: this.richTextBox.Document.ContentStart.GetPositionAtOffset(-this.offsetContentStartToFormatStart), if (e.Changes.First().AddedLength > 0)
end: this.richTextBox.Document.ContentEnd, {
color: this.currentlySelectedFormat.BackgroundBrush); 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) private static void RtbChangeTextBackground(RichTextBox rtb, TextPointer start, TextPointer end, Brush color)

@ -2,9 +2,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultiTerm.Wpf.CustomControl"> xmlns:local="clr-namespace:MultiTerm.Wpf.CustomControl">
<!--<SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
<SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
<SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>-->
<Style TargetType="{x:Type local:MultiFormatTextBox}"> <Style TargetType="{x:Type local:MultiFormatTextBox}">
<!--<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <!--<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/> <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
@ -16,6 +13,11 @@
<Setter Property="AllowDrop" Value="true"/> <Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>--> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>-->
<Style.Resources>
<SolidColorBrush x:Key="MultiFormatTextBox.CHAR.Background" Color="#B4EBEB"/>
<SolidColorBrush x:Key="MultiFormatTextBox.HEX.Background" Color="#C8C8FF"/>
<SolidColorBrush x:Key="MultiFormatTextBox.BIN.Background" Color="#C8FFC8"/>
</Style.Resources>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="{x:Type local:MultiFormatTextBox}"> <ControlTemplate TargetType="{x:Type local:MultiFormatTextBox}">

Loading…
Cancel
Save