NotesEditorControl.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Data;
  6. using System.Windows.Media;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.WPF;
  10. using java.util;
  11. namespace InABox.DynamicGrid
  12. {
  13. public class TextBoxDecorator : Decorator
  14. {
  15. // properties
  16. public override UIElement Child
  17. {
  18. get => base.Child;
  19. set
  20. {
  21. var oldValue = base.Child;
  22. if (oldValue != null)
  23. {
  24. //var wbind = BindingOperations.GetBinding(oldValue, FrameworkElement.WidthProperty);
  25. //if ((wbind != null) && (wbind.Source == this))
  26. // BindingOperations.ClearBinding(oldValue, FrameworkElement.WidthProperty);
  27. var hbind = BindingOperations.GetBinding(oldValue, HeightProperty);
  28. if (hbind != null && hbind.Source == this)
  29. BindingOperations.ClearBinding(oldValue, HeightProperty);
  30. }
  31. base.Child = value;
  32. if (value != null &&
  33. BindingOperations.GetBinding(value, HeightProperty) == null)
  34. BindingOperations.SetBinding(
  35. value,
  36. HeightProperty,
  37. new Binding
  38. {
  39. Source = this,
  40. Path = new PropertyPath(ActualHeightProperty),
  41. Mode = BindingMode.OneWay
  42. });
  43. }
  44. }
  45. // methods
  46. protected override Size MeasureOverride(Size constraint)
  47. {
  48. var result = base.MeasureOverride(constraint);
  49. if (double.IsInfinity(constraint.Height))
  50. result.Height = (Child as FrameworkElement)?.MinHeight ?? 0.0;
  51. return result;
  52. }
  53. }
  54. public class NotesEditorControl : DynamicEditorControl<string[], NotesEditor>
  55. {
  56. static NotesEditorControl()
  57. {
  58. //DynamicEditorControlFactory.Register<NotesEditorControl, NotesEditor>();
  59. }
  60. private Color BGColor = Colors.Gainsboro;
  61. //private TextBox Editor = null;
  62. private Button Button;
  63. private Grid Grid;
  64. private TextBox History;
  65. private TextBoxDecorator Scroll;
  66. private string[]? _value = [];
  67. public override int DesiredHeight()
  68. {
  69. return int.MaxValue;
  70. }
  71. public override int DesiredWidth()
  72. {
  73. return int.MaxValue;
  74. }
  75. public override void SetColor(Color color)
  76. {
  77. BGColor = color;
  78. if (!History.IsReadOnly)
  79. History.Background = new SolidColorBrush(color);
  80. }
  81. public override void SetFocus()
  82. {
  83. Button.Focus();
  84. }
  85. public override void Configure()
  86. {
  87. }
  88. protected override FrameworkElement CreateEditor()
  89. {
  90. VerticalAlignment = VerticalAlignment.Stretch;
  91. MinHeight = 250;
  92. Grid = new Grid();
  93. Grid.VerticalAlignment = VerticalAlignment.Stretch;
  94. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  95. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
  96. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  97. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  98. Scroll = new TextBoxDecorator
  99. {
  100. VerticalAlignment = VerticalAlignment.Stretch,
  101. HorizontalAlignment = HorizontalAlignment.Stretch
  102. //HorizontalContentAlignment = HorizontalAlignment.Stretch,
  103. //VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
  104. //HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,
  105. //Background = new SolidColorBrush(Colors.Yellow)
  106. };
  107. Scroll.SetValue(Grid.RowProperty, 0);
  108. Scroll.SetValue(Grid.RowSpanProperty, 2);
  109. Scroll.SetValue(Grid.ColumnProperty, 0);
  110. Scroll.SetValue(Grid.ColumnSpanProperty, 2);
  111. Grid.Children.Add(Scroll);
  112. History = new TextBox
  113. {
  114. VerticalAlignment = VerticalAlignment.Top,
  115. VerticalContentAlignment = VerticalAlignment.Stretch,
  116. HorizontalAlignment = HorizontalAlignment.Stretch,
  117. TextWrapping = TextWrapping.Wrap,
  118. AcceptsReturn = true,
  119. IsReadOnly = true,
  120. VerticalScrollBarVisibility = ScrollBarVisibility.Auto
  121. };
  122. History.IsVisibleChanged += (o, e) =>
  123. {
  124. History.Focus();
  125. History.CaretIndex = History.Text.Length;
  126. History.ScrollToEnd();
  127. };
  128. History.TextChanged += (o, e) =>
  129. {
  130. if (History.IsReadOnly)
  131. {
  132. History.Focus();
  133. History.CaretIndex = History.Text.Length;
  134. History.ScrollToEnd();
  135. }
  136. else
  137. {
  138. CheckChanged();
  139. }
  140. };
  141. Scroll.Child = History;
  142. Button = new Button
  143. {
  144. VerticalAlignment = VerticalAlignment.Stretch,
  145. VerticalContentAlignment = VerticalAlignment.Center,
  146. HorizontalAlignment = HorizontalAlignment.Stretch,
  147. HorizontalContentAlignment = HorizontalAlignment.Center,
  148. BorderThickness = new Thickness(0F),
  149. Background = Brushes.Transparent,
  150. Content = new Image() { Source = Wpf.Resources.add.AsBitmapImage() },
  151. Width = 48,
  152. Height=48,
  153. Margin=new Thickness(0,0,5,5)
  154. };
  155. Button.Click += Button_Click;
  156. Button.SetValue(Grid.RowProperty, 1);
  157. Button.SetValue(Grid.ColumnProperty, 1);
  158. Grid.Children.Add(Button);
  159. return Grid;
  160. }
  161. private void Button_Click(object sender, RoutedEventArgs e)
  162. {
  163. var popup = new NotePopup { Caption = "Add Note", Text = "" };
  164. if (popup.ShowDialog() == true)
  165. {
  166. if (string.IsNullOrEmpty(History.Text))
  167. {
  168. _value = [popup.Text];
  169. }
  170. else
  171. {
  172. _value = (_value ?? []).Concatenate([string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, ClientFactory.UserID, popup.Text)]);
  173. }
  174. History.Text = NotesEditor.FormatNotes(_value);
  175. History.ScrollToEnd();
  176. CheckChanged();
  177. }
  178. }
  179. protected override string[] RetrieveValue()
  180. {
  181. if(_value is null || _value.Length == 0 && !History.Text.IsNullOrWhiteSpace())
  182. {
  183. return [$"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {ClientFactory.UserID}: {History.Text}"];
  184. }
  185. return _value;
  186. }
  187. protected override void UpdateValue(string[] value)
  188. {
  189. if(value is not null)
  190. {
  191. _value = value;
  192. History.Text = NotesEditor.FormatNotes(_value);
  193. }
  194. else
  195. {
  196. _value = null;
  197. History.Text = "";
  198. }
  199. var AlwaysEnabled = EditorDefinition is NotesEditor && ((NotesEditor)EditorDefinition).AlwaysEnabled;
  200. History.IsReadOnly = !AlwaysEnabled && !string.IsNullOrWhiteSpace(History.Text);
  201. History.Background = History.IsReadOnly ? new SolidColorBrush(Colors.Gainsboro) : new SolidColorBrush(BGColor);
  202. Button.Visibility = History.IsReadOnly ? Visibility.Visible : Visibility.Collapsed;
  203. History.Focus();
  204. History.CaretIndex = History.Text.Length;
  205. History.ScrollToEnd();
  206. }
  207. }
  208. }