CodeEditorControl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using InABox.Core;
  8. namespace InABox.DynamicGrid
  9. {
  10. public class CodeEditorControl : DynamicEditorControl<string>
  11. {
  12. private TextBox Editor;
  13. protected override FrameworkElement CreateEditor()
  14. {
  15. var dock = new DockPanel
  16. {
  17. HorizontalAlignment = HorizontalAlignment.Stretch,
  18. VerticalAlignment = VerticalAlignment.Stretch
  19. };
  20. var buttons = CreateButtons(out var DisableEditor);
  21. foreach (var button in buttons)
  22. {
  23. button.SetValue(DockPanel.DockProperty, Dock.Right);
  24. dock.Children.Add(button);
  25. dock.Width += button.Width + 5;
  26. }
  27. Editor = new TextBox
  28. {
  29. CharacterCasing = CharacterCasing.Upper,
  30. VerticalContentAlignment = VerticalAlignment.Center
  31. };
  32. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  33. if (DisableEditor)
  34. {
  35. Editor.Background = new SolidColorBrush(Colors.Silver);
  36. Editor.IsEnabled = false;
  37. }
  38. Editor.TextChanged += (o, e) => { CheckChanged(); };
  39. Editor.PreviewTextInput += Editor_PreviewTextInput;
  40. DataObject.AddPastingHandler(Editor, Editor_TextPaste);
  41. dock.Children.Add(Editor);
  42. return dock;
  43. }
  44. private void Editor_TextPaste(object sender, DataObjectPastingEventArgs e)
  45. {
  46. if (e.DataObject.GetDataPresent(typeof(string)))
  47. {
  48. var allowable = (EditorDefinition as BaseCodeEditor)?.ValidChars;
  49. var text = (string)e.DataObject.GetData(typeof(string));
  50. if (!IsTextAllowed(text, allowable))
  51. e.CancelCommand();
  52. }
  53. else
  54. {
  55. e.CancelCommand();
  56. }
  57. }
  58. private bool IsTextAllowed(string text, string allowable)
  59. {
  60. if (string.IsNullOrWhiteSpace(allowable))
  61. return true;
  62. return Array.TrueForAll(text.ToCharArray(), delegate(char c) { return allowable.Contains(c); });
  63. }
  64. private void Editor_PreviewTextInput(object sender, TextCompositionEventArgs e)
  65. {
  66. var allowable = (EditorDefinition as BaseCodeEditor)?.ValidChars;
  67. e.Handled = !IsTextAllowed(e.Text, allowable);
  68. }
  69. public override int DesiredHeight()
  70. {
  71. return 25;
  72. }
  73. public override int DesiredWidth()
  74. {
  75. var result = 150;
  76. var btnEditor = EditorDefinition as IButtonEditor;
  77. if (btnEditor?.Buttons != null)
  78. foreach (var button in ((IButtonEditor)EditorDefinition).Buttons)
  79. result += button.Width + 5;
  80. return result;
  81. }
  82. protected override string RetrieveValue()
  83. {
  84. return Editor.Text;
  85. }
  86. protected override void UpdateValue(string value)
  87. {
  88. Editor.Text = value;
  89. }
  90. public override void SetFocus()
  91. {
  92. Editor.Focus();
  93. }
  94. public override void SetColor(Color color)
  95. {
  96. Editor.Background = new SolidColorBrush(color);
  97. }
  98. }
  99. }