DocumentEditor.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.ComponentModel;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using InABox.Core;
  6. using InABox.Wpf;
  7. namespace InABox.DynamicGrid
  8. {
  9. /// <summary>
  10. /// Interaction logic for PDFViewer.xaml
  11. /// </summary>
  12. public partial class DocumentEditor : ThemableWindow
  13. {
  14. private IEntityDocument[] _documents;
  15. private readonly bool bReady;
  16. public DocumentEditor(params IEntityDocument[] documents)
  17. {
  18. PrintAllowed = false;
  19. SaveAllowed = false;
  20. InitializeComponent();
  21. _documents = documents;
  22. foreach (var doc in documents)
  23. {
  24. var tab = new DynamicTabItem();
  25. tab.Header = Path.GetFileName(doc.DocumentLink.FileName);
  26. tab.Tag = doc;
  27. Documents.Items.Add(tab);
  28. }
  29. bReady = true;
  30. Documents.SelectedIndex = -1;
  31. }
  32. public bool PrintAllowed { get; set; }
  33. public bool SaveAllowed { get; set; }
  34. public string Watermark { get; set; }
  35. private void Documents_SelectionChanged(object sender, SelectionChangedEventArgs e)
  36. {
  37. if (!bReady)
  38. return;
  39. for (var i = 0; i < e.RemovedItems.Count; i++)
  40. {
  41. var item = e.RemovedItems[i] as DynamicTabItem;
  42. if (item != null)
  43. {
  44. var viewer = item.Content as IDocumentEditor;
  45. if (viewer != null)
  46. viewer.Document = null;
  47. item.Content = null;
  48. }
  49. }
  50. for (var i = 0; i < e.AddedItems.Count; i++)
  51. {
  52. var item = e.AddedItems[i] as DynamicTabItem;
  53. if (item != null)
  54. {
  55. var doc = item.Tag as IEntityDocument;
  56. if (doc != null)
  57. {
  58. IDocumentEditor editor = null;
  59. var extension = Path.GetExtension(doc.DocumentLink.FileName).ToLower();
  60. if (extension.Equals(".pdf"))
  61. {
  62. var pdf = new PDFEditorControl();
  63. pdf.PrintAllowed = PrintAllowed;
  64. pdf.SaveAllowed = SaveAllowed;
  65. pdf.Watermark = Watermark;
  66. editor = pdf;
  67. }
  68. else if (extension.Equals(".png") || extension.Equals(".bmp") || extension.Equals(".jpg") || extension.Equals(".jpeg"))
  69. {
  70. editor = new ImageEditorControl();
  71. }
  72. if (editor != null)
  73. {
  74. item.Content = editor;
  75. editor.Document = doc;
  76. }
  77. }
  78. }
  79. }
  80. e.Handled = true;
  81. }
  82. protected override void OnClosing(CancelEventArgs e)
  83. {
  84. base.OnClosing(e);
  85. Documents.SelectedIndex = -1;
  86. }
  87. private void Window_Loaded(object sender, RoutedEventArgs e)
  88. {
  89. //Editor.Document = _documents.FirstOrDefault();
  90. }
  91. private void OKButton_Click(object sender, RoutedEventArgs e)
  92. {
  93. //Editor.Document = null;
  94. DialogResult = true;
  95. Close();
  96. }
  97. private void CancelButton_Click(object sender, RoutedEventArgs e)
  98. {
  99. DialogResult = false;
  100. Close();
  101. }
  102. }
  103. }