FileEditorControl.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. namespace InABox.DynamicGrid;
  2. #if USEIMAGES
  3. public class FileEditorControl<T> : DynamicEditorControl<DataFile> where T : DataFile, new()
  4. {
  5. private TextBox Editor = null;
  6. T _value = null;
  7. protected override FrameworkElement CreateEditor()
  8. {
  9. Grid Grid = new Grid()
  10. {
  11. VerticalAlignment = VerticalAlignment.Stretch,
  12. HorizontalAlignment = HorizontalAlignment.Stretch
  13. };
  14. Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  15. Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50, GridUnitType.Pixel) });
  16. Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50, GridUnitType.Pixel) });
  17. Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50, GridUnitType.Pixel) });
  18. Editor = new TextBox()
  19. {
  20. VerticalAlignment = VerticalAlignment.Stretch,
  21. VerticalContentAlignment = VerticalAlignment.Center,
  22. HorizontalAlignment = HorizontalAlignment.Stretch,
  23. };
  24. Editor.TextChanged += (o, e) => Changed = true;
  25. Editor.SetValue(Grid.ColumnProperty, 0);
  26. Editor.SetValue(Grid.RowProperty, 0);
  27. Grid.Children.Add(Editor);
  28. Button Select = new Button()
  29. {
  30. VerticalAlignment = VerticalAlignment.Stretch,
  31. VerticalContentAlignment = VerticalAlignment.Center,
  32. HorizontalAlignment = HorizontalAlignment.Stretch,
  33. Margin = new Thickness(5,0,0,0),
  34. Content = "Select",
  35. Focusable = false
  36. };
  37. Select.SetValue(Grid.ColumnProperty, 1);
  38. Select.SetValue(Grid.RowProperty, 0);
  39. Select.Click += Select_Click;
  40. Grid.Children.Add(Select);
  41. Button Clear = new Button()
  42. {
  43. VerticalAlignment = VerticalAlignment.Stretch,
  44. VerticalContentAlignment = VerticalAlignment.Center,
  45. HorizontalAlignment = HorizontalAlignment.Stretch,
  46. Margin = new Thickness(5, 0, 0, 0),
  47. Content = "Clear",
  48. Focusable = false
  49. };
  50. Clear.SetValue(Grid.ColumnProperty, 2);
  51. Clear.SetValue(Grid.RowProperty, 0);
  52. Clear.Click += Clear_Click;
  53. Grid.Children.Add(Clear);
  54. Button View = new Button()
  55. {
  56. VerticalAlignment = VerticalAlignment.Stretch,
  57. VerticalContentAlignment = VerticalAlignment.Center,
  58. HorizontalAlignment = HorizontalAlignment.Stretch,
  59. Margin = new Thickness(5, 0, 0, 0),
  60. Content = "View",
  61. Focusable = false
  62. };
  63. View.SetValue(Grid.ColumnProperty, 3);
  64. View.SetValue(Grid.RowProperty, 0);
  65. View.Click += View_Click;
  66. Grid.Children.Add(View);
  67. return Grid;
  68. }
  69. private void View_Click(object sender, RoutedEventArgs e)
  70. {
  71. if (String.IsNullOrEmpty(_value.FileName) || (_value.Data == null) || (_value.Data.Length == 0))
  72. {
  73. System.Windows.MessageBox.Show("No File Selected!");
  74. return;
  75. }
  76. String ext = System.IO.Path.GetExtension(_value.FileName);
  77. String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ext);
  78. File.WriteAllBytes(filename, _value.Data);
  79. ProcessStartInfo gsProcessInfo = new ProcessStartInfo();
  80. gsProcessInfo.Verb = "open";
  81. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  82. gsProcessInfo.FileName = filename;
  83. gsProcessInfo.UseShellExecute = true;
  84. Process.Start(gsProcessInfo);
  85. }
  86. private void Clear_Click(object sender, RoutedEventArgs e)
  87. {
  88. _value.FileName = "";
  89. _value.Data = new byte[] { };
  90. Editor.Text = "";
  91. }
  92. private void Select_Click(object sender, RoutedEventArgs e)
  93. {
  94. VistaOpenFileDialog dlg = new VistaOpenFileDialog();
  95. dlg.Filter = _value.Filter();
  96. if (dlg.ShowDialog() == true)
  97. {
  98. _value.FileName = System.IO.Path.GetFileName(dlg.FileName);
  99. _value.Data = File.ReadAllBytes(dlg.FileName);
  100. Editor.Text = _value.FileName;
  101. }
  102. }
  103. protected override int DesiredHeight()
  104. {
  105. return 25;
  106. }
  107. protected override DataFile GetValue()
  108. {
  109. return _value;
  110. }
  111. protected override void SetValue(DataFile value)
  112. {
  113. T file = (T)value;
  114. T newfile = new T(); //(DataFile)Activator.CreateInstance(file != null ? value.GetType() : typeof(MiscellaneousDataFile));
  115. newfile.FileName = file != null ? file.FileName : "";
  116. newfile.Data = file != null ? file.Data : new byte[] { };
  117. Editor.Text = file != null ? file.FileName : "";
  118. _value = newfile;
  119. }
  120. public override void SetFocus()
  121. {
  122. Editor.Focus();
  123. }
  124. public override void SetColor(Color color)
  125. {
  126. Editor.Background = new SolidColorBrush(color);
  127. }
  128. }
  129. public class ImageFileEditorControl : FileEditorControl<ImageDataFile> { }
  130. public class PDFFileEditorControl : FileEditorControl<PDFDataFile> { }
  131. public class MiscellaneousFileEditorControl : FileEditorControl<MiscellaneousDataFile> { }
  132. #endif