DocumentStrip.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. namespace PaintDotNet
  8. {
  9. /// <summary>
  10. /// Zyh
  11. /// 原本是继承ImageStrip
  12. /// copy了一个ImageNameStrip
  13. /// </summary>
  14. internal class DocumentStrip : ImageNameStrip, IDocumentList
  15. {
  16. private List<DocumentWorkspace> documents = new List<DocumentWorkspace>();
  17. private List<ImageNameStrip.Item> documentButtons = new List<ImageNameStrip.Item>();
  18. private Dictionary<DocumentWorkspace, ImageNameStrip.Item> dw2button = new Dictionary<DocumentWorkspace, ImageNameStrip.Item>();
  19. private Dictionary<DocumentWorkspace, RenderArgs> thumbs = new Dictionary<DocumentWorkspace, RenderArgs>();
  20. private DocumentWorkspace selectedDocument = null;
  21. private bool ensureSelectedIsVisible = true;
  22. private AppWorkspace appWorkspace;
  23. public DocumentWorkspace[] DocumentList
  24. {
  25. get
  26. {
  27. return this.documents.ToArray();
  28. }
  29. }
  30. public int DocumentCount
  31. {
  32. get
  33. {
  34. return this.documents.Count;
  35. }
  36. }
  37. public event EventHandler DocumentListChanged;
  38. protected virtual void OnDocumentListChanged()
  39. {
  40. if (DocumentListChanged != null)
  41. {
  42. DocumentListChanged(this, EventArgs.Empty);
  43. }
  44. }
  45. public DocumentWorkspace SelectedDocument
  46. {
  47. get
  48. {
  49. return this.selectedDocument;
  50. }
  51. set
  52. {
  53. if (!this.documents.Contains(value))
  54. {
  55. throw new ArgumentException("DocumentWorkspace isn't being tracked by this instance of DocumentStrip");
  56. }
  57. if (this.selectedDocument != value)
  58. {
  59. SelectDocumentWorkspace(value);
  60. OnDocumentClicked(value, DocumentClickAction.Select);
  61. Refresh();
  62. }
  63. }
  64. }
  65. public int SelectedDocumentIndex
  66. {
  67. get
  68. {
  69. return this.documents.IndexOf(this.selectedDocument);
  70. }
  71. }
  72. private bool OnDigitHotKeyPressed(Keys keys)
  73. {
  74. keys &= ~Keys.Alt;
  75. keys &= ~Keys.Control;
  76. if (keys < Keys.D0 || keys > Keys.D9)
  77. {
  78. return false;
  79. }
  80. int digit = (int)keys - (int)Keys.D0;
  81. int index;
  82. if (digit == 0)
  83. {
  84. index = 9;
  85. }
  86. else
  87. {
  88. index = digit - 1;
  89. }
  90. if (index < this.documents.Count)
  91. {
  92. PerformItemClick(index, ItemPart.Image, MouseButtons.Left);
  93. return true;
  94. }
  95. else
  96. {
  97. return false;
  98. }
  99. }
  100. public DocumentStrip(AppWorkspace appWorkspace)
  101. {
  102. this.appWorkspace = appWorkspace;
  103. PdnBaseForm.RegisterFormHotKey(Keys.Delete, OnDeleteHotKeyPressed);
  104. PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.Tab, OnNextTabHotKeyPressed);
  105. PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.PageDown, OnNextTabHotKeyPressed);
  106. PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.Shift | Keys.Tab, OnPreviousTabHotKeyPressed);
  107. PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.PageUp, OnPreviousTabHotKeyPressed);
  108. //thumbnailManager = new ThumbnailManager(this); // allow for a 1px black border
  109. InitializeComponent();
  110. //#18291
  111. //for (int i = 0; i <= 9; ++i)
  112. //{
  113. // Keys digit = Utility.LetterOrDigitCharToKeys((char)(i + '0'));
  114. // PdnBaseForm.RegisterFormHotKey(Keys.Control | digit, OnDigitHotKeyPressed);
  115. // PdnBaseForm.RegisterFormHotKey(Keys.Alt | digit, OnDigitHotKeyPressed);
  116. //}
  117. this.ShowCloseButtons = true;
  118. }
  119. /// <summary>
  120. /// 删除按键
  121. /// </summary>
  122. /// <param name="keys"></param>
  123. /// <returns></returns>
  124. private bool OnDeleteHotKeyPressed(Keys keys)
  125. {
  126. if (this.selectedDocument != null)
  127. {
  128. this.selectedDocument.MouseEvent_Del(null, null);
  129. }
  130. if (this.appWorkspace!=null)
  131. {
  132. if(this.appWorkspace.cameraPreviewDialog!=null && !this.appWorkspace.cameraPreviewDialog.IsDisposed)
  133. {
  134. if(this.appWorkspace.cameraPreviewDialog.documentWorkspace!=null)
  135. this.appWorkspace.cameraPreviewDialog.documentWorkspace.MouseEvent_Del(null, null);
  136. }
  137. }
  138. return true;
  139. }
  140. private bool OnNextTabHotKeyPressed(Keys keys)
  141. {
  142. bool processed = NextTab();
  143. return processed;
  144. }
  145. private bool OnPreviousTabHotKeyPressed(Keys keys)
  146. {
  147. bool processed = PreviousTab();
  148. return processed;
  149. }
  150. public bool NextTab()
  151. {
  152. bool changed = false;
  153. if (this.selectedDocument != null)
  154. {
  155. int currentIndex = this.documents.IndexOf(this.selectedDocument);
  156. int newIndex = (currentIndex + 1) % this.documents.Count;
  157. SelectedDocument = this.documents[newIndex];
  158. changed = true;
  159. }
  160. return changed;
  161. }
  162. public bool PreviousTab()
  163. {
  164. bool changed = false;
  165. if (this.selectedDocument != null)
  166. {
  167. int currentIndex = this.documents.IndexOf(this.selectedDocument);
  168. int newIndex = (currentIndex + (this.documents.Count - 1)) % this.documents.Count;
  169. SelectedDocument = this.documents[newIndex];
  170. changed = true;
  171. }
  172. return changed;
  173. }
  174. private void InitializeComponent()
  175. {
  176. this.Name = "DocumentStrip";
  177. }
  178. protected override void Dispose(bool disposing)
  179. {
  180. if (disposing)
  181. {
  182. while (this.documents.Count > 0)
  183. {
  184. RemoveDocumentWorkspace(this.documents[this.documents.Count - 1]);
  185. }
  186. /*if (this.thumbnailManager != null)
  187. {
  188. this.thumbnailManager.Dispose();
  189. this.thumbnailManager = null;
  190. }*/
  191. foreach (DocumentWorkspace dw in this.thumbs.Keys)
  192. {
  193. RenderArgs ra = this.thumbs[dw];
  194. ra.Dispose();
  195. }
  196. this.thumbs.Clear();
  197. this.thumbs = null;
  198. }
  199. base.Dispose(disposing);
  200. }
  201. protected override void OnScrollArrowClicked(ArrowDirection arrowDirection)
  202. {
  203. int sign = 0;
  204. switch (arrowDirection)
  205. {
  206. case ArrowDirection.Left:
  207. sign = -1;
  208. break;
  209. case ArrowDirection.Right:
  210. sign = +1;
  211. break;
  212. }
  213. int delta = ItemSize.Width;
  214. ScrollOffset += sign * delta;
  215. base.OnScrollArrowClicked(arrowDirection);
  216. }
  217. protected override void OnItemClicked(Item item, ItemPart itemPart, MouseButtons mouseButtons)
  218. {
  219. DocumentWorkspace dw = item.Tag as DocumentWorkspace;
  220. if (dw != null)
  221. {
  222. switch (itemPart)
  223. {
  224. case ItemPart.None:
  225. // do nothing
  226. break;
  227. case ItemPart.CloseButton:
  228. if (mouseButtons == MouseButtons.Left)
  229. {
  230. OnDocumentClicked(dw, DocumentClickAction.Close);
  231. }
  232. break;
  233. case ItemPart.Image:
  234. if (mouseButtons == MouseButtons.Left)
  235. {
  236. SelectedDocument = dw;
  237. }
  238. else if (mouseButtons == MouseButtons.Right)
  239. {
  240. // TODO ZYH 预留右键菜单
  241. }
  242. break;
  243. default:
  244. throw new InvalidEnumArgumentException();
  245. }
  246. }
  247. base.OnItemClicked(item, itemPart, mouseButtons);
  248. }
  249. public event EventHandler<EventArgs<Pair<DocumentWorkspace, DocumentClickAction>>> DocumentClicked;
  250. protected virtual void OnDocumentClicked(DocumentWorkspace dw, DocumentClickAction action)
  251. {
  252. if (DocumentClicked != null)
  253. {
  254. DocumentClicked(this, new EventArgs<Pair<DocumentWorkspace, DocumentClickAction>>(
  255. Pair.Create(dw, action)));
  256. }
  257. }
  258. public void UnlockDocumentWorkspaceDirtyValue(DocumentWorkspace unlockMe)
  259. {
  260. Item docItem = this.dw2button[unlockMe];
  261. docItem.UnlockDirtyValue();
  262. }
  263. public void LockDocumentWorkspaceDirtyValue(DocumentWorkspace lockMe, bool forceDirtyValue)
  264. {
  265. Item docItem = this.dw2button[lockMe];
  266. docItem.LockDirtyValue(forceDirtyValue);
  267. }
  268. public void AddDocumentWorkspace(DocumentWorkspace addMe)
  269. {
  270. this.documents.Add(addMe);
  271. ImageNameStrip.Item docButton = new ImageNameStrip.Item();
  272. docButton.Image = null;
  273. docButton.Tag = addMe;
  274. AddItem(docButton);
  275. this.documentButtons.Add(docButton);
  276. addMe.CompositionUpdated += Workspace_CompositionUpdated;
  277. this.dw2button.Add(addMe, docButton);
  278. if (addMe.Document != null)
  279. {
  280. QueueThumbnailUpdate(addMe);
  281. docButton.Dirty = addMe.Document.Dirty;
  282. addMe.Document.DirtyChanged += Document_DirtyChanged;
  283. }
  284. addMe.DocumentChanging += Workspace_DocumentChanging;
  285. addMe.DocumentChanged += Workspace_DocumentChanged;
  286. OnDocumentListChanged();
  287. }
  288. private void Workspace_DocumentChanging(object sender, EventArgs<Document> e)
  289. {
  290. if (e.Data != null)
  291. {
  292. e.Data.DirtyChanged -= Document_DirtyChanged;
  293. }
  294. }
  295. private void Workspace_DocumentChanged(object sender, EventArgs e)
  296. {
  297. DocumentWorkspace dw = (DocumentWorkspace)sender;
  298. ImageNameStrip.Item docButton = this.dw2button[dw];
  299. if (dw.Document != null)
  300. {
  301. docButton.Dirty = dw.Document.Dirty;
  302. dw.Document.DirtyChanged += Document_DirtyChanged;
  303. }
  304. else
  305. {
  306. docButton.Dirty = false;
  307. }
  308. }
  309. private void Document_DirtyChanged(object sender, EventArgs e)
  310. {
  311. for (int i = 0; i < this.documents.Count; ++i)
  312. {
  313. if (object.ReferenceEquals(sender, this.documents[i].Document))
  314. {
  315. ImageNameStrip.Item docButton = this.dw2button[this.documents[i]];
  316. docButton.Dirty = ((Document)sender).Dirty;
  317. }
  318. }
  319. }
  320. private void Workspace_CompositionUpdated(object sender, EventArgs e)
  321. {
  322. DocumentWorkspace dw = (DocumentWorkspace)sender;
  323. QueueThumbnailUpdate(dw);
  324. }
  325. public void RemoveDocumentWorkspace(DocumentWorkspace removeMe)
  326. {
  327. removeMe.CompositionUpdated -= Workspace_CompositionUpdated;
  328. if (this.selectedDocument == removeMe)
  329. {
  330. this.selectedDocument = null;
  331. }
  332. removeMe.DocumentChanging -= Workspace_DocumentChanging;
  333. removeMe.DocumentChanged -= Workspace_DocumentChanged;
  334. if (removeMe.Document != null)
  335. {
  336. removeMe.Document.DirtyChanged -= Document_DirtyChanged;
  337. }
  338. this.documents.Remove(removeMe);
  339. ImageNameStrip.Item docButton = this.dw2button[removeMe];
  340. this.RemoveItem(docButton);
  341. this.dw2button.Remove(removeMe);
  342. this.documentButtons.Remove(docButton);
  343. if (this.thumbs.ContainsKey(removeMe))
  344. {
  345. RenderArgs thumbRA = this.thumbs[removeMe];
  346. Surface surface = thumbRA.Surface;
  347. thumbRA.Dispose();
  348. this.thumbs.Remove(removeMe);
  349. surface.Dispose();
  350. }
  351. OnDocumentListChanged();
  352. }
  353. protected override void OnLayout(LayoutEventArgs levent)
  354. {
  355. base.OnLayout(levent);
  356. if (this.ensureSelectedIsVisible &&
  357. (!Focused && !LeftScrollButton.Focused && !RightScrollButton.Focused))
  358. {
  359. int index = this.documents.IndexOf(this.selectedDocument);
  360. EnsureItemFullyVisible(index);
  361. }
  362. }
  363. public void SelectDocumentWorkspace(DocumentWorkspace selectMe)
  364. {
  365. UI.SuspendControlPainting(this);
  366. this.selectedDocument = selectMe;
  367. if (this.thumbs.ContainsKey(selectMe))
  368. {
  369. //RenderArgs thumb = this.thumbs[selectMe];
  370. //Bitmap bitmap = thumb.Bitmap;
  371. }
  372. else
  373. {
  374. QueueThumbnailUpdate(selectMe);
  375. }
  376. foreach (ImageNameStrip.Item docItem in this.documentButtons)
  377. {
  378. if ((docItem.Tag as DocumentWorkspace) == selectMe)
  379. {
  380. EnsureItemFullyVisible(docItem);
  381. docItem.Checked = true;
  382. }
  383. else
  384. {
  385. docItem.Checked = false;
  386. }
  387. }
  388. UI.ResumeControlPainting(this);
  389. Invalidate(true);
  390. }
  391. public void RefreshAllThumbnails()
  392. {
  393. foreach (DocumentWorkspace dw in this.documents)
  394. {
  395. QueueThumbnailUpdate(dw);
  396. }
  397. }
  398. private void OnThumbnailUpdated(DocumentWorkspace dw)
  399. {
  400. if (this.dw2button.ContainsKey(dw))
  401. {
  402. ImageNameStrip.Item docButton = this.dw2button[dw];
  403. docButton.Name = dw.GetFriendlyName();
  404. docButton.Update();
  405. }
  406. }
  407. public void SetDw2buttonName(DocumentWorkspace dw, string buttonName)
  408. {
  409. if (this.dw2button.ContainsKey(dw))
  410. {
  411. ImageNameStrip.Item docButton = this.dw2button[dw];
  412. docButton.Name = buttonName;
  413. docButton.Update();
  414. }
  415. }
  416. public void QueueThumbnailUpdate(DocumentWorkspace dw)
  417. {
  418. OnThumbnailUpdated(dw);
  419. }
  420. }
  421. }