NotesEditorControl.cs 7.5 KB

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