DocumentStrip.cs 14 KB

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