DocumentBottomStrip.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.CustomControl
  8. {
  9. public enum DocumentBottomClickAction
  10. {
  11. Select,
  12. Close
  13. }
  14. public partial class DocumentBottomStrip : IBottomNameStrip
  15. {
  16. private List<string> documents = new List<string>();
  17. private List<Color> documentColors = new List<Color>();
  18. private List<IBottomNameStrip.Item> documentButtons = new List<IBottomNameStrip.Item>();
  19. private Dictionary<string, IBottomNameStrip.Item> dw2button = new Dictionary<string, IBottomNameStrip.Item>();
  20. private Dictionary<string, RenderArgs> thumbs = new Dictionary<string, RenderArgs>();
  21. public void ClearTextBox()
  22. {
  23. this.documents.Clear();
  24. this.documentColors.Clear();
  25. this.items.Clear();
  26. this.documentButtons.Clear();
  27. this.dw2button.Clear();
  28. QueueThumbnailUpdate(this.documents.Count + "");
  29. OnDocumentListChanged();
  30. }
  31. public int DocumentCount
  32. {
  33. get
  34. {
  35. return this.documents.Count;
  36. }
  37. }
  38. public event EventHandler DocumentListChanged;
  39. protected virtual void OnDocumentListChanged()
  40. {
  41. if (DocumentListChanged != null)
  42. {
  43. DocumentListChanged(this, EventArgs.Empty);
  44. }
  45. }
  46. public override Size GetPreferredSize(Size proposedSize)
  47. {
  48. Size itemSize = ItemSize;
  49. int preferredWidth;
  50. if (this.ItemCount == 0)
  51. {
  52. preferredWidth = 0;
  53. }
  54. else
  55. {
  56. preferredWidth = itemSize.Width * DocumentCount;
  57. }
  58. Size preferredSize = new Size(preferredWidth, itemSize.Height);
  59. return preferredSize;
  60. }
  61. public DocumentBottomStrip()
  62. {
  63. InitializeComponent();
  64. this.ShowCloseButtons = false;
  65. this.ShowCheckedColor = false;
  66. }
  67. private void InitializeComponent()
  68. {
  69. this.Name = "DocumentBottomStrip";
  70. }
  71. protected override void Dispose(bool disposing)
  72. {
  73. base.Dispose(disposing);
  74. }
  75. protected override void OnScrollArrowClicked(ArrowDirection arrowDirection)
  76. {
  77. int sign = 0;
  78. switch (arrowDirection)
  79. {
  80. case ArrowDirection.Left:
  81. sign = -1;
  82. break;
  83. case ArrowDirection.Right:
  84. sign = +1;
  85. break;
  86. }
  87. int delta = ItemSize.Width;
  88. ScrollOffset += sign * delta;
  89. base.OnScrollArrowClicked(arrowDirection);
  90. }
  91. protected override void OnItemClicked(Item item, ItemPart itemPart, MouseButtons mouseButtons)
  92. {
  93. string dw = item.Tag as string;
  94. if (dw != null)
  95. {
  96. switch (itemPart)
  97. {
  98. case ItemPart.None:
  99. // do nothing
  100. break;
  101. case ItemPart.CloseButton:
  102. if (mouseButtons == MouseButtons.Left)
  103. {
  104. OnDocumentClicked(dw, DocumentBottomClickAction.Close);
  105. }
  106. break;
  107. case ItemPart.Image:
  108. if (mouseButtons == MouseButtons.Left)
  109. {
  110. SelectTextBox(dw);
  111. //SelectedDocument = dw;
  112. }
  113. else if (mouseButtons == MouseButtons.Right)
  114. {
  115. // TODO ZYH 预留右键菜单
  116. }
  117. break;
  118. default:
  119. throw new InvalidEnumArgumentException();
  120. }
  121. }
  122. base.OnItemClicked(item, itemPart, mouseButtons);
  123. }
  124. public event EventHandler<EventArgs<Pair<string, DocumentBottomClickAction>>> DocumentClicked;
  125. protected virtual void OnDocumentClicked(string dw, DocumentBottomClickAction action)
  126. {
  127. if (DocumentClicked != null)
  128. {
  129. DocumentClicked(this, new EventArgs<Pair<string, DocumentBottomClickAction>>(
  130. Pair.Create(dw, action)));
  131. }
  132. }
  133. public void AddTextBox(string addMe, Color color, bool check)
  134. {
  135. this.documents.Add(addMe);
  136. this.documentColors.Add(color);
  137. IBottomNameStrip.Item docButton = new IBottomNameStrip.Item();
  138. docButton.Image = null;
  139. docButton.Tag = this.documents.Count + "";
  140. docButton.Checked = check;
  141. docButton.NColor = color;
  142. AddItem(docButton);
  143. this.documentButtons.Add(docButton);
  144. if (!this.dw2button.ContainsKey(this.documents.Count + ""))
  145. {
  146. this.dw2button.Add(this.documents.Count + "", docButton);
  147. }
  148. QueueThumbnailUpdate(this.documents.Count + "");
  149. OnDocumentListChanged();
  150. }
  151. public void RemoveTextBox(string removeMe)
  152. {
  153. OnDocumentListChanged();
  154. }
  155. protected override void OnLayout(LayoutEventArgs levent)
  156. {
  157. base.OnLayout(levent);
  158. }
  159. public void SelectTextBox(string selectMe)
  160. {
  161. UI.SuspendControlPainting(this);
  162. foreach (IBottomNameStrip.Item docItem in this.documentButtons)
  163. {
  164. if ((docItem.Tag as string) == selectMe)
  165. {
  166. EnsureItemFullyVisible(docItem);
  167. docItem.Checked = !docItem.Checked;
  168. if (this.AllowMultiChoise)
  169. {
  170. break;
  171. }
  172. }
  173. else if (!this.AllowMultiChoise)
  174. {
  175. docItem.Checked = false;
  176. }
  177. }
  178. UI.ResumeControlPainting(this);
  179. Invalidate(true);
  180. }
  181. private void OnThumbnailUpdated(string dw)
  182. {
  183. if (this.dw2button.ContainsKey(dw))
  184. {
  185. IBottomNameStrip.Item docButton = this.dw2button[dw];
  186. //RenderArgs docRA = this.thumbs[dw];
  187. docButton.Name = this.documents[int.Parse(dw) - 1]/*dw*/;//.GetFriendlyName();
  188. docButton.NColor = this.documentColors[int.Parse(dw) - 1];
  189. //docButton.Image = docRA.Bitmap;
  190. docButton.Update();
  191. }
  192. }
  193. public void QueueThumbnailUpdate(string dw)
  194. {
  195. OnThumbnailUpdated(dw);
  196. }
  197. }
  198. }