NotesEditorControl.cs 7.9 KB

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