MainWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using Compdfkit_Tools.Helper;
  2. using Compdfkit_Tools.PDFControl;
  3. using Dragablz;
  4. using System;
  5. using System.ComponentModel;
  6. using System.Runtime.CompilerServices;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using Path = System.IO.Path;
  12. namespace PDFViewer
  13. {
  14. public partial class MainWindow : Window
  15. {
  16. #region Property
  17. private PDFViewControl passwordViewer;
  18. private PDFViewControl pdfViewControl = new PDFViewControl();
  19. private string[] oldAndNewFilePath;
  20. #endregion
  21. public MainWindow()
  22. {
  23. InitializeComponent();
  24. Loaded += MainWindow_Loaded;
  25. }
  26. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  27. {
  28. PasswordUI.Closed -= PasswordUI_Closed;
  29. PasswordUI.Canceled -= PasswordUI_Canceled;
  30. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  31. PasswordUI.Confirmed += PasswordUI_Confirmed;
  32. PasswordUI.Canceled += PasswordUI_Canceled;
  33. PasswordUI.Closed += PasswordUI_Closed;
  34. if (App.DefaultPDFLoaded == false)
  35. {
  36. LoadDefaultDocument();
  37. App.DefaultPDFLoaded = true;
  38. }
  39. }
  40. #region Load document
  41. private void LoadDefaultDocument()
  42. {
  43. string defaultFilePath = "PDF32000_2008.pdf";
  44. TabControlLoadDocument(defaultFilePath);
  45. }
  46. private void TabControlLoadDocument(string filePath)
  47. {
  48. if (App.OpenedFilePathList.Contains(filePath))
  49. {
  50. for (int i = 0; i < App.Current.Windows.Count; i++)
  51. {
  52. MainWindow win = App.Current.Windows[i] as MainWindow;
  53. if (win != null)
  54. {
  55. var items = win.TabControl.Items;
  56. foreach (TabItemExt item in items)
  57. {
  58. if (item.Tag.ToString().ToLower() == filePath.ToLower())
  59. {
  60. win.Activate();
  61. item.IsSelected = true;
  62. return;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. TabItemExt tabItem = new TabItemExt();
  69. MainPage viewPage = new MainPage();
  70. viewPage.CheckExistBeforeOpenFileEvent -= ViewPage_CheckExistBeforeOpenFileEvent;
  71. viewPage.FileChangeEvent -= ViewPage_FileChangeEvent;
  72. viewPage.CheckExistBeforeOpenFileEvent += ViewPage_CheckExistBeforeOpenFileEvent;
  73. viewPage.FileChangeEvent += ViewPage_FileChangeEvent;
  74. passwordViewer = new PDFViewControl();
  75. passwordViewer.PDFView.InitDocument(filePath);
  76. if (passwordViewer.PDFView.Document == null)
  77. {
  78. MessageBox.Show("Open File Failed");
  79. return;
  80. }
  81. if (passwordViewer.PDFView.Document.IsLocked)
  82. {
  83. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  84. PasswordUI.ClearPassword();
  85. PopupBorder.Visibility = Visibility.Visible;
  86. PasswordUI.Visibility = Visibility.Visible;
  87. }
  88. else
  89. {
  90. viewPage.InitWithFilePath(filePath);
  91. tabItem.Content = viewPage;
  92. tabItem.IsSelected = true;
  93. tabItem.FileName = Path.GetFileName(filePath);
  94. tabItem.Tag = filePath;
  95. App.OpenedFilePathList.Add(filePath);
  96. TabControl.Items.Add(tabItem);
  97. }
  98. }
  99. private void ViewPage_FileChangeEvent(object sender, EventArgs e)
  100. {
  101. for (int i = 0; i < App.Current.Windows.Count; i++)
  102. {
  103. MainWindow win = App.Current.Windows[i] as MainWindow;
  104. if (win != null)
  105. {
  106. var items = win.TabControl.Items;
  107. foreach (TabItemExt item in items)
  108. {
  109. if (item.Tag.ToString().ToLower() == oldAndNewFilePath[1].ToLower())
  110. {
  111. item.IsSelected = true;
  112. item.FileName = Path.GetFileName(oldAndNewFilePath[0]);
  113. item.IsSelected = true;
  114. item.Tag = oldAndNewFilePath[0];
  115. }
  116. }
  117. }
  118. }
  119. for (int i = 0; i < App.OpenedFilePathList.Count; i++)
  120. {
  121. if (oldAndNewFilePath[1].ToLower() == App.OpenedFilePathList[i].ToLower())
  122. {
  123. App.OpenedFilePathList[i] = oldAndNewFilePath[0];
  124. break;
  125. }
  126. }
  127. }
  128. private bool ViewPage_CheckExistBeforeOpenFileEvent(string[] arg)
  129. {
  130. if (App.OpenedFilePathList.Contains(arg[0]))
  131. {
  132. for (int i = 0; i < App.Current.Windows.Count; i++)
  133. {
  134. MainWindow win = App.Current.Windows[i] as MainWindow;
  135. if (win != null)
  136. {
  137. var items = win.TabControl.Items;
  138. foreach (TabItemExt item in items)
  139. {
  140. if (item.Tag.ToString().ToLower() == arg[0].ToLower())
  141. {
  142. win.Activate();
  143. item.IsSelected = true;
  144. return true;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. oldAndNewFilePath = arg;
  151. return false;
  152. }
  153. #endregion
  154. #region password
  155. private void PasswordUI_Closed(object sender, EventArgs e)
  156. {
  157. PopupBorder.Visibility = Visibility.Collapsed;
  158. PasswordUI.Visibility = Visibility.Collapsed;
  159. }
  160. private void PasswordUI_Canceled(object sender, EventArgs e)
  161. {
  162. PopupBorder.Visibility = Visibility.Collapsed;
  163. PasswordUI.Visibility = Visibility.Collapsed;
  164. }
  165. private void PasswordUI_Confirmed(object sender, string e)
  166. {
  167. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  168. {
  169. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  170. if (passwordViewer.PDFView.Document.IsLocked == false)
  171. {
  172. PasswordUI.SetShowError("", Visibility.Collapsed);
  173. PasswordUI.ClearPassword();
  174. PasswordUI.Visibility = Visibility.Collapsed;
  175. PopupBorder.Visibility = Visibility.Collapsed;
  176. pdfViewControl = passwordViewer;
  177. string filePath = passwordViewer.PDFView.Document.FilePath;
  178. TabItemExt tabItem = new TabItemExt();
  179. MainPage viewPage = new MainPage();
  180. tabItem.Content = viewPage;
  181. tabItem.IsSelected = true;
  182. tabItem.FileName = Path.GetFileName(filePath);
  183. tabItem.Tag = filePath;
  184. viewPage.SetPDFViewer(pdfViewControl);
  185. App.OpenedFilePathList.Add(filePath);
  186. TabControl.Items.Add(tabItem);
  187. }
  188. else
  189. {
  190. PasswordUI.SetShowError("error", Visibility.Visible);
  191. }
  192. }
  193. }
  194. #endregion
  195. #region UI
  196. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  197. {
  198. DragMove();
  199. }
  200. private void CloseFileButton_Click(object sender, RoutedEventArgs e)
  201. {
  202. var button = sender as Button;
  203. var dragablzItem = FindParentDragablzItem(button);
  204. var tabControl = FindParentTabControl(dragablzItem);
  205. MainPage mainPage = (dragablzItem.Content as TabItemExt).Content as MainPage;
  206. if (mainPage.CanSave)
  207. {
  208. string fileName = (dragablzItem.Content as TabItemExt).FileName;
  209. var message = string.Format("Do you want to save the changes in {0}?", fileName);
  210. var result = MessageBox.Show(message, "Alert", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  211. if (result == MessageBoxResult.Yes)
  212. {
  213. mainPage.SaveFile();
  214. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  215. tabControl.Items.Remove(dragablzItem.Content);
  216. }
  217. else if (result == MessageBoxResult.No)
  218. {
  219. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  220. tabControl.Items.Remove(dragablzItem.Content);
  221. }
  222. else
  223. {
  224. }
  225. }
  226. else
  227. {
  228. App.OpenedFilePathList.Remove((dragablzItem.Content as TabItemExt).Tag.ToString());
  229. tabControl.Items.Remove(dragablzItem.Content);
  230. }
  231. if (tabControl.Items.Count == 0)
  232. {
  233. SystemCommands.CloseWindow(this);
  234. }
  235. }
  236. private DragablzItem FindParentDragablzItem(DependencyObject element)
  237. {
  238. while (element != null && !(element is DragablzItem))
  239. {
  240. element = VisualTreeHelper.GetParent(element);
  241. }
  242. return element as DragablzItem;
  243. }
  244. private TabControl FindParentTabControl(DependencyObject element)
  245. {
  246. while (element != null && !(element is TabControl))
  247. {
  248. element = VisualTreeHelper.GetParent(element);
  249. }
  250. return element as TabControl;
  251. }
  252. private void MinimizeButton_Click(object sender, RoutedEventArgs e)
  253. {
  254. SystemCommands.MinimizeWindow(this);
  255. }
  256. private void MaximizeRestoreButton_Click(object sender, RoutedEventArgs e)
  257. {
  258. if (this.WindowState == WindowState.Maximized)
  259. {
  260. SystemCommands.RestoreWindow(this);
  261. }
  262. else
  263. {
  264. SystemCommands.MaximizeWindow(this);
  265. }
  266. }
  267. private void CloseButton_Click(object sender, RoutedEventArgs e)
  268. {
  269. int count = TabControl.Items.Count;
  270. while (count > 0)
  271. {
  272. TabItemExt item = TabControl.Items[0] as TabItemExt;
  273. MainPage mainPage = item.Content as MainPage;
  274. if (mainPage.CanSave)
  275. {
  276. string fileName = item.FileName;
  277. var message = string.Format("Do you want to save the changes in {0}?", fileName);
  278. var result = MessageBox.Show(message, "Alert", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  279. if (result == MessageBoxResult.Yes)
  280. {
  281. mainPage.SaveFile();
  282. App.OpenedFilePathList.Remove(item.Tag.ToString());
  283. TabControl.Items.Remove(item);
  284. count--;
  285. }
  286. else if (result == MessageBoxResult.No)
  287. {
  288. App.OpenedFilePathList.Remove(item.Tag.ToString());
  289. TabControl.Items.Remove(item);
  290. count--;
  291. }
  292. else
  293. {
  294. break;
  295. }
  296. }
  297. else
  298. {
  299. App.OpenedFilePathList.Remove(item.Tag.ToString());
  300. TabControl.Items.Remove(item);
  301. count--;
  302. }
  303. }
  304. if (count == 0)
  305. {
  306. SystemCommands.CloseWindow(this);
  307. }
  308. }
  309. private void DefaultAddButton_Click(object sender, RoutedEventArgs e)
  310. {
  311. string filePath = CommonHelper.GetFilePathOrEmpty();
  312. if (filePath != string.Empty)
  313. {
  314. TabControlLoadDocument(filePath);
  315. }
  316. }
  317. #endregion
  318. public class TabItemExt : TabItem, INotifyPropertyChanged
  319. {
  320. public event PropertyChangedEventHandler PropertyChanged;
  321. private string _fileName;
  322. public string FileName
  323. {
  324. get
  325. {
  326. return _fileName;
  327. }
  328. set
  329. {
  330. if (_fileName != value)
  331. {
  332. _fileName = value;
  333. OnPropertyChanged();
  334. }
  335. }
  336. }
  337. private Visibility _fileChanged = Visibility.Hidden;
  338. public Visibility FileChanged
  339. {
  340. get
  341. {
  342. return _fileChanged;
  343. }
  344. set
  345. {
  346. if (_fileChanged != value)
  347. {
  348. _fileChanged = value;
  349. OnPropertyChanged();
  350. }
  351. }
  352. }
  353. public void OnPropertyChanged([CallerMemberName] string name = null)
  354. {
  355. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  356. }
  357. }
  358. }
  359. }