|
|
|
|
@ -42,6 +42,8 @@ namespace MultiTerm.Wpf.CustomControl; |
|
|
|
|
/// </summary> |
|
|
|
|
internal class Format |
|
|
|
|
{ |
|
|
|
|
private readonly string? backgroundColorResourceName; |
|
|
|
|
|
|
|
|
|
public string Name { get; set; } |
|
|
|
|
public Brush BackgroundBrush { get; set; } |
|
|
|
|
public Predicate<Key> IsKeyValid { get; set; } |
|
|
|
|
@ -51,18 +53,34 @@ internal class Format |
|
|
|
|
this.BackgroundBrush = backgroundBrush; |
|
|
|
|
this.IsKeyValid = keyValidator; |
|
|
|
|
} |
|
|
|
|
public Format(string name, string backgroundBrushColorHexCode, Predicate<Key> keyValidator) |
|
|
|
|
public Format(string name, string backgroundColorResourceName, Predicate<Key> 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<string> GetListOfNames(IEnumerable<Format> formats) |
|
|
|
|
{ |
|
|
|
|
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 |
|
|
|
|
@ -71,19 +89,19 @@ public class MultiFormatTextBox : Control |
|
|
|
|
private static readonly List<Format> 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,13 +207,22 @@ public class MultiFormatTextBox : Control |
|
|
|
|
|
|
|
|
|
private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
if(e.Changes.Count > 1) { return; } |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
|