DockPaneStripBase.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DockPaneStripBase.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Diagnostics.CodeAnalysis;
  14. using System.Drawing;
  15. using System.Drawing.Drawing2D;
  16. using System.Security.Permissions;
  17. using System.Windows.Forms;
  18. namespace Dongke.IBOSS.PRD.Basics.DockPanel
  19. {
  20. public abstract class DockPaneStripBase : Control
  21. {
  22. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  23. protected internal class Tab : IDisposable
  24. {
  25. private IDockContent m_content;
  26. public Tab(IDockContent content)
  27. {
  28. m_content = content;
  29. }
  30. ~Tab()
  31. {
  32. Dispose(false);
  33. }
  34. public IDockContent Content
  35. {
  36. get
  37. {
  38. return m_content;
  39. }
  40. }
  41. public Form ContentForm
  42. {
  43. get
  44. {
  45. return m_content as Form;
  46. }
  47. }
  48. public void Dispose()
  49. {
  50. Dispose(true);
  51. GC.SuppressFinalize(this);
  52. }
  53. protected virtual void Dispose(bool disposing)
  54. {
  55. }
  56. }
  57. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  58. protected sealed class TabCollection : IEnumerable<Tab>
  59. {
  60. #region IEnumerable Members
  61. IEnumerator<Tab> IEnumerable<Tab>.GetEnumerator()
  62. {
  63. for (int i = 0; i < Count; i++)
  64. yield return this[i];
  65. }
  66. IEnumerator IEnumerable.GetEnumerator()
  67. {
  68. for (int i = 0; i < Count; i++)
  69. yield return this[i];
  70. }
  71. #endregion
  72. internal TabCollection(DockPane pane)
  73. {
  74. m_dockPane = pane;
  75. }
  76. private DockPane m_dockPane;
  77. public DockPane DockPane
  78. {
  79. get
  80. {
  81. return m_dockPane;
  82. }
  83. }
  84. public int Count
  85. {
  86. get
  87. {
  88. return DockPane.DisplayingContents.Count;
  89. }
  90. }
  91. public Tab this[int index]
  92. {
  93. get
  94. {
  95. IDockContent content = DockPane.DisplayingContents[index];
  96. if (content == null)
  97. throw (new ArgumentOutOfRangeException("index"));
  98. return content.DockHandler.GetTab(DockPane.TabStripControl);
  99. }
  100. }
  101. public bool Contains(Tab tab)
  102. {
  103. return (IndexOf(tab) != -1);
  104. }
  105. public bool Contains(IDockContent content)
  106. {
  107. return (IndexOf(content) != -1);
  108. }
  109. public int IndexOf(Tab tab)
  110. {
  111. if (tab == null)
  112. return -1;
  113. return DockPane.DisplayingContents.IndexOf(tab.Content);
  114. }
  115. public int IndexOf(IDockContent content)
  116. {
  117. return DockPane.DisplayingContents.IndexOf(content);
  118. }
  119. }
  120. protected DockPaneStripBase(DockPane pane)
  121. {
  122. m_dockPane = pane;
  123. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  124. SetStyle(ControlStyles.Selectable, false);
  125. AllowDrop = true;
  126. }
  127. private DockPane m_dockPane;
  128. protected DockPane DockPane
  129. {
  130. get
  131. {
  132. return m_dockPane;
  133. }
  134. }
  135. protected DockPane.AppearanceStyle Appearance
  136. {
  137. get
  138. {
  139. return DockPane.Appearance;
  140. }
  141. }
  142. private TabCollection m_tabs = null;
  143. protected TabCollection Tabs
  144. {
  145. get
  146. {
  147. if (m_tabs == null)
  148. m_tabs = new TabCollection(DockPane);
  149. return m_tabs;
  150. }
  151. }
  152. internal void RefreshChanges()
  153. {
  154. if (IsDisposed)
  155. return;
  156. OnRefreshChanges();
  157. }
  158. protected virtual void OnRefreshChanges()
  159. {
  160. }
  161. protected internal abstract int MeasureHeight();
  162. protected internal abstract void EnsureTabVisible(IDockContent content);
  163. protected int HitTest()
  164. {
  165. return HitTest(PointToClient(Control.MousePosition));
  166. }
  167. protected internal abstract int HitTest(Point point);
  168. protected internal abstract GraphicsPath GetOutline(int index);
  169. protected internal virtual Tab CreateTab(IDockContent content)
  170. {
  171. return new Tab(content);
  172. }
  173. protected override void OnMouseDown(MouseEventArgs e)
  174. {
  175. base.OnMouseDown(e);
  176. int index = HitTest(e.Location);
  177. if (index != -1)
  178. {
  179. if (e.Button == MouseButtons.Middle)
  180. {
  181. // Close the specified content.
  182. IDockContent content = Tabs[index].Content;
  183. DockPane.CloseContent(content);
  184. }
  185. else
  186. {
  187. IDockContent content = Tabs[index].Content;
  188. if (DockPane.ActiveContent != content)
  189. DockPane.ActiveContent = content;
  190. }
  191. }
  192. if (e.Button == MouseButtons.Left)
  193. {
  194. if (DockPane.DockPanel.AllowEndUserDocking && DockPane.AllowDockDragAndDrop && DockPane.ActiveContent.DockHandler.AllowEndUserDocking)
  195. DockPane.DockPanel.BeginDrag(DockPane.ActiveContent.DockHandler);
  196. }
  197. }
  198. protected bool HasTabPageContextMenu
  199. {
  200. get
  201. {
  202. return DockPane.HasTabPageContextMenu;
  203. }
  204. }
  205. protected void ShowTabPageContextMenu(Point position)
  206. {
  207. DockPane.ShowTabPageContextMenu(this, position);
  208. }
  209. protected override void OnMouseUp(MouseEventArgs e)
  210. {
  211. base.OnMouseUp(e);
  212. if (e.Button == MouseButtons.Right)
  213. ShowTabPageContextMenu(new Point(e.X, e.Y));
  214. }
  215. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  216. protected override void WndProc(ref Message m)
  217. {
  218. if (m.Msg == (int)Win32.Msgs.WM_LBUTTONDBLCLK)
  219. {
  220. base.WndProc(ref m);
  221. int index = HitTest();
  222. if (DockPane.DockPanel.AllowEndUserDocking && index != -1)
  223. {
  224. IDockContent content = Tabs[index].Content;
  225. if (content.DockHandler.CheckDockState(!content.DockHandler.IsFloat) != DockState.Unknown)
  226. //content.DockHandler.IsFloat = !content.DockHandler.IsFloat;
  227. content.DockHandler.Close();
  228. }
  229. return;
  230. }
  231. base.WndProc(ref m);
  232. return;
  233. }
  234. protected override void OnDragOver(DragEventArgs drgevent)
  235. {
  236. base.OnDragOver(drgevent);
  237. int index = HitTest();
  238. if (index != -1)
  239. {
  240. IDockContent content = Tabs[index].Content;
  241. if (DockPane.ActiveContent != content)
  242. DockPane.ActiveContent = content;
  243. }
  244. }
  245. }
  246. }