DocumentEditorControl.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using InABox.Core;
  9. using InABox.Wpf;
  10. using InABox.Wpf.Editors;
  11. using InABox.WPF;
  12. using Microsoft.Win32;
  13. namespace InABox.DynamicGrid
  14. {
  15. public enum DocumentAction
  16. {
  17. Replace,
  18. UseExisting,
  19. MakeCopy
  20. }
  21. public class DocumentEditorControl : DynamicEditorControl<Guid, BaseDocumentEditor>
  22. {
  23. static DocumentEditorControl()
  24. {
  25. //DynamicEditorControlFactory.Register<DocumentEditorControl, BaseDocumentEditor, ImageDocumentEditor>();
  26. //DynamicEditorControlFactory.Register<DocumentEditorControl, BaseDocumentEditor, PDFDocumentEditor>();
  27. //DynamicEditorControlFactory.Register<DocumentEditorControl, BaseDocumentEditor, VectorDocumentEditor>();
  28. //DynamicEditorControlFactory.Register<DocumentEditorControl, BaseDocumentEditor, MiscellaneousDocumentEditor>();
  29. }
  30. private Document _document = new();
  31. private TextBox Editor;
  32. public string Filter { get; set; }
  33. public override void Configure()
  34. {
  35. if (EditorDefinition is not BaseDocumentEditor editor) return;
  36. Filter = editor.FileMask;
  37. }
  38. protected override FrameworkElement CreateEditor()
  39. {
  40. var Grid = new Grid
  41. {
  42. VerticalAlignment = VerticalAlignment.Stretch,
  43. HorizontalAlignment = HorizontalAlignment.Stretch
  44. };
  45. //Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(25, GridUnitType.Pixel) });
  46. Grid.AddColumn(GridUnitType.Star);
  47. Grid.AddColumn(50);
  48. Grid.AddColumn(50);
  49. Grid.AddColumn(50);
  50. Grid.AddColumn(60);
  51. Editor = new TextBox
  52. {
  53. VerticalAlignment = VerticalAlignment.Stretch,
  54. VerticalContentAlignment = VerticalAlignment.Center,
  55. HorizontalAlignment = HorizontalAlignment.Stretch,
  56. Margin = new Thickness(0, 0, 0, 0),
  57. IsEnabled = false
  58. };
  59. //Editor.LostFocus += (o, e) => CheckChanged();
  60. Grid.AddChild(Editor, 0, 0);
  61. var Select = new Button
  62. {
  63. VerticalAlignment = VerticalAlignment.Stretch,
  64. VerticalContentAlignment = VerticalAlignment.Center,
  65. HorizontalAlignment = HorizontalAlignment.Stretch,
  66. Margin = new Thickness(5, 1, 0, 1),
  67. Content = "Select",
  68. Focusable = false
  69. };
  70. Select.Click += Select_Click;
  71. Grid.AddChild(Select, 0, 1);
  72. var Clear = new Button
  73. {
  74. VerticalAlignment = VerticalAlignment.Stretch,
  75. VerticalContentAlignment = VerticalAlignment.Center,
  76. HorizontalAlignment = HorizontalAlignment.Stretch,
  77. Margin = new Thickness(5, 1, 0, 1),
  78. Content = "Clear",
  79. Focusable = false
  80. };
  81. Clear.Click += Clear_Click;
  82. Grid.AddChild(Clear, 0, 2);
  83. var View = new Button
  84. {
  85. VerticalAlignment = VerticalAlignment.Stretch,
  86. VerticalContentAlignment = VerticalAlignment.Center,
  87. HorizontalAlignment = HorizontalAlignment.Stretch,
  88. Margin = new Thickness(5, 1, 0, 1),
  89. Content = "View",
  90. Focusable = false
  91. };
  92. View.Click += View_Click;
  93. Grid.AddChild(View, 0, 3);
  94. var rename = new Button
  95. {
  96. VerticalAlignment = VerticalAlignment.Stretch,
  97. VerticalContentAlignment = VerticalAlignment.Center,
  98. HorizontalAlignment = HorizontalAlignment.Stretch,
  99. Margin = new Thickness(5, 1, 0, 1),
  100. Content = "Rename",
  101. Focusable = false
  102. };
  103. rename.Click += Rename_Click;
  104. Grid.AddChild(rename, 0, 4);
  105. return Grid;
  106. }
  107. private void Rename_Click(object sender, RoutedEventArgs e)
  108. {
  109. var name = _document.FileName;
  110. if(TextEdit.Execute("Enter filename:", ref name))
  111. {
  112. _document.FileName = name;
  113. Editor.Text = _document.FileName;
  114. Host.SaveDocument(_document);
  115. }
  116. }
  117. private void Select_Click(object sender, RoutedEventArgs e)
  118. {
  119. var dlg = new OpenFileDialog();
  120. dlg.Filter = Filter;
  121. if (dlg.ShowDialog() == true)
  122. {
  123. byte[] data = null;
  124. var filename = Path.GetFileName(dlg.FileName).ToLower();
  125. var timestamp = new FileInfo(dlg.FileName).LastWriteTime;
  126. using (new WaitCursor())
  127. data = File.ReadAllBytes(dlg.FileName);
  128. var crc = CoreUtils.CalculateCRC(data);
  129. var newDocument = DocumentConfirm.CheckDocument(new Document
  130. {
  131. FileName = filename,
  132. TimeStamp = timestamp,
  133. Data = data,
  134. CRC = crc
  135. }, Host.FindDocument, out var shouldSave);
  136. if(newDocument != null)
  137. {
  138. _document = newDocument;
  139. if (shouldSave)
  140. {
  141. using (new WaitCursor())
  142. Host.SaveDocument(newDocument);
  143. }
  144. }
  145. Editor.Text = _document.FileName;
  146. OtherValues[nameof(Document.FileName)] = _document.FileName;
  147. CheckChanged();
  148. }
  149. }
  150. private void View_Click(object sender, RoutedEventArgs e)
  151. {
  152. if (_document.ID == Guid.Empty)
  153. {
  154. MessageWindow.ShowMessage("Please select a document first!", "No document selected");
  155. return;
  156. }
  157. var ext = Path.GetExtension(_document.FileName);
  158. var filename = Path.ChangeExtension(Path.GetTempFileName(), ext);
  159. File.WriteAllBytes(filename, _document.Data);
  160. var gsProcessInfo = new ProcessStartInfo();
  161. gsProcessInfo.Verb = "open";
  162. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  163. gsProcessInfo.FileName = filename;
  164. gsProcessInfo.UseShellExecute = true;
  165. Process.Start(gsProcessInfo);
  166. }
  167. private void Clear_Click(object sender, RoutedEventArgs e)
  168. {
  169. _document = new Document();
  170. Editor.Text = "";
  171. CheckChanged();
  172. }
  173. public override int DesiredHeight()
  174. {
  175. return 25;
  176. }
  177. public override int DesiredWidth()
  178. {
  179. return int.MaxValue;
  180. }
  181. protected override Guid RetrieveValue()
  182. {
  183. return _document.ID;
  184. }
  185. protected override void UpdateValue(Guid value)
  186. {
  187. if (value != Guid.Empty)
  188. _document = Host.GetDocument(value) ?? new Document();
  189. Editor.Text = _document.FileName;
  190. }
  191. public override void SetFocus()
  192. {
  193. Editor.Focus();
  194. }
  195. public override void SetColor(Color color)
  196. {
  197. Editor.Background = new SolidColorBrush(color);
  198. }
  199. }
  200. }