PageRenderTime 24ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/SODA/src/Libraries/DockPanel_Src/WinFormsUI/Docking/AutoHideStripBase.cs

https://github.com/jspraul/TickZoomPublic
C# | 508 lines | 427 code | 80 blank | 1 comment | 79 complexity | cf62f3ade61f82f166a92ae0c389abac MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Collections.Generic;
  7. using System.Diagnostics.CodeAnalysis;
  8. namespace WeifenLuo.WinFormsUI.Docking
  9. {
  10. public abstract partial class AutoHideStripBase : Control
  11. {
  12. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  13. protected class Tab : IDisposable
  14. {
  15. private IDockContent m_content;
  16. protected internal Tab(IDockContent content)
  17. {
  18. m_content = content;
  19. }
  20. public IDockContent Content
  21. {
  22. get { return m_content; }
  23. }
  24. public virtual void Dispose()
  25. {
  26. }
  27. }
  28. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  29. protected sealed class TabCollection : IEnumerable<Tab>
  30. {
  31. #region IEnumerable Members
  32. IEnumerator<Tab> IEnumerable<Tab>.GetEnumerator()
  33. {
  34. for (int i = 0; i < Count; i++)
  35. yield return this[i];
  36. }
  37. IEnumerator IEnumerable.GetEnumerator()
  38. {
  39. for (int i = 0; i < Count; i++)
  40. yield return this[i];
  41. }
  42. #endregion
  43. internal TabCollection(DockPane pane)
  44. {
  45. m_dockPane = pane;
  46. }
  47. private DockPane m_dockPane = null;
  48. public DockPane DockPane
  49. {
  50. get { return m_dockPane; }
  51. }
  52. public DockPanel DockPanel
  53. {
  54. get { return DockPane.DockPanel; }
  55. }
  56. public int Count
  57. {
  58. get { return DockPane.DisplayingContents.Count; }
  59. }
  60. public Tab this[int index]
  61. {
  62. get
  63. {
  64. IDockContent content = DockPane.DisplayingContents[index];
  65. if (content == null)
  66. throw (new ArgumentOutOfRangeException("index"));
  67. if (content.DockHandler.AutoHideTab == null)
  68. content.DockHandler.AutoHideTab = (DockPanel.AutoHideStripControl.CreateTab(content));
  69. return content.DockHandler.AutoHideTab as Tab;
  70. }
  71. }
  72. public bool Contains(Tab tab)
  73. {
  74. return (IndexOf(tab) != -1);
  75. }
  76. public bool Contains(IDockContent content)
  77. {
  78. return (IndexOf(content) != -1);
  79. }
  80. public int IndexOf(Tab tab)
  81. {
  82. if (tab == null)
  83. return -1;
  84. return IndexOf(tab.Content);
  85. }
  86. public int IndexOf(IDockContent content)
  87. {
  88. return DockPane.DisplayingContents.IndexOf(content);
  89. }
  90. }
  91. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  92. protected class Pane : IDisposable
  93. {
  94. private DockPane m_dockPane;
  95. protected internal Pane(DockPane dockPane)
  96. {
  97. m_dockPane = dockPane;
  98. }
  99. public DockPane DockPane
  100. {
  101. get { return m_dockPane; }
  102. }
  103. public TabCollection AutoHideTabs
  104. {
  105. get
  106. {
  107. if (DockPane.AutoHideTabs == null)
  108. DockPane.AutoHideTabs = new TabCollection(DockPane);
  109. return DockPane.AutoHideTabs as TabCollection;
  110. }
  111. }
  112. public virtual void Dispose()
  113. {
  114. }
  115. }
  116. [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  117. protected sealed class PaneCollection : IEnumerable<Pane>
  118. {
  119. private class AutoHideState
  120. {
  121. public DockState m_dockState;
  122. public bool m_selected = false;
  123. public AutoHideState(DockState dockState)
  124. {
  125. m_dockState = dockState;
  126. }
  127. public DockState DockState
  128. {
  129. get { return m_dockState; }
  130. }
  131. public bool Selected
  132. {
  133. get { return m_selected; }
  134. set { m_selected = value; }
  135. }
  136. }
  137. private class AutoHideStateCollection
  138. {
  139. private AutoHideState[] m_states;
  140. public AutoHideStateCollection()
  141. {
  142. m_states = new AutoHideState[] {
  143. new AutoHideState(DockState.DockTopAutoHide),
  144. new AutoHideState(DockState.DockBottomAutoHide),
  145. new AutoHideState(DockState.DockLeftAutoHide),
  146. new AutoHideState(DockState.DockRightAutoHide)
  147. };
  148. }
  149. public AutoHideState this[DockState dockState]
  150. {
  151. get
  152. {
  153. for (int i = 0; i < m_states.Length; i++)
  154. {
  155. if (m_states[i].DockState == dockState)
  156. return m_states[i];
  157. }
  158. throw new ArgumentOutOfRangeException("dockState");
  159. }
  160. }
  161. public bool ContainsPane(DockPane pane)
  162. {
  163. if (pane.IsHidden)
  164. return false;
  165. for (int i = 0; i < m_states.Length; i++)
  166. {
  167. if (m_states[i].DockState == pane.DockState && m_states[i].Selected)
  168. return true;
  169. }
  170. return false;
  171. }
  172. }
  173. internal PaneCollection(DockPanel panel, DockState dockState)
  174. {
  175. m_dockPanel = panel;
  176. m_states = new AutoHideStateCollection();
  177. States[DockState.DockTopAutoHide].Selected = (dockState == DockState.DockTopAutoHide);
  178. States[DockState.DockBottomAutoHide].Selected = (dockState == DockState.DockBottomAutoHide);
  179. States[DockState.DockLeftAutoHide].Selected = (dockState == DockState.DockLeftAutoHide);
  180. States[DockState.DockRightAutoHide].Selected = (dockState == DockState.DockRightAutoHide);
  181. }
  182. private DockPanel m_dockPanel;
  183. public DockPanel DockPanel
  184. {
  185. get { return m_dockPanel; }
  186. }
  187. private AutoHideStateCollection m_states;
  188. private AutoHideStateCollection States
  189. {
  190. get { return m_states; }
  191. }
  192. public int Count
  193. {
  194. get
  195. {
  196. int count = 0;
  197. foreach (DockPane pane in DockPanel.Panes)
  198. {
  199. if (States.ContainsPane(pane))
  200. count++;
  201. }
  202. return count;
  203. }
  204. }
  205. public Pane this[int index]
  206. {
  207. get
  208. {
  209. int count = 0;
  210. foreach (DockPane pane in DockPanel.Panes)
  211. {
  212. if (!States.ContainsPane(pane))
  213. continue;
  214. if (count == index)
  215. {
  216. if (pane.AutoHidePane == null)
  217. pane.AutoHidePane = DockPanel.AutoHideStripControl.CreatePane(pane);
  218. return pane.AutoHidePane as Pane;
  219. }
  220. count++;
  221. }
  222. throw new ArgumentOutOfRangeException("index");
  223. }
  224. }
  225. public bool Contains(Pane pane)
  226. {
  227. return (IndexOf(pane) != -1);
  228. }
  229. public int IndexOf(Pane pane)
  230. {
  231. if (pane == null)
  232. return -1;
  233. int index = 0;
  234. foreach (DockPane dockPane in DockPanel.Panes)
  235. {
  236. if (!States.ContainsPane(pane.DockPane))
  237. continue;
  238. if (pane == dockPane.AutoHidePane)
  239. return index;
  240. index++;
  241. }
  242. return -1;
  243. }
  244. #region IEnumerable Members
  245. IEnumerator<Pane> IEnumerable<Pane>.GetEnumerator()
  246. {
  247. for (int i = 0; i < Count; i++)
  248. yield return this[i];
  249. }
  250. IEnumerator IEnumerable.GetEnumerator()
  251. {
  252. for (int i = 0; i < Count; i++)
  253. yield return this[i];
  254. }
  255. #endregion
  256. }
  257. protected AutoHideStripBase(DockPanel panel)
  258. {
  259. m_dockPanel = panel;
  260. m_panesTop = new PaneCollection(panel, DockState.DockTopAutoHide);
  261. m_panesBottom = new PaneCollection(panel, DockState.DockBottomAutoHide);
  262. m_panesLeft = new PaneCollection(panel, DockState.DockLeftAutoHide);
  263. m_panesRight = new PaneCollection(panel, DockState.DockRightAutoHide);
  264. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  265. SetStyle(ControlStyles.Selectable, false);
  266. }
  267. private DockPanel m_dockPanel;
  268. protected DockPanel DockPanel
  269. {
  270. get { return m_dockPanel; }
  271. }
  272. private PaneCollection m_panesTop;
  273. protected PaneCollection PanesTop
  274. {
  275. get { return m_panesTop; }
  276. }
  277. private PaneCollection m_panesBottom;
  278. protected PaneCollection PanesBottom
  279. {
  280. get { return m_panesBottom; }
  281. }
  282. private PaneCollection m_panesLeft;
  283. protected PaneCollection PanesLeft
  284. {
  285. get { return m_panesLeft; }
  286. }
  287. private PaneCollection m_panesRight;
  288. protected PaneCollection PanesRight
  289. {
  290. get { return m_panesRight; }
  291. }
  292. protected PaneCollection GetPanes(DockState dockState)
  293. {
  294. if (dockState == DockState.DockTopAutoHide)
  295. return PanesTop;
  296. else if (dockState == DockState.DockBottomAutoHide)
  297. return PanesBottom;
  298. else if (dockState == DockState.DockLeftAutoHide)
  299. return PanesLeft;
  300. else if (dockState == DockState.DockRightAutoHide)
  301. return PanesRight;
  302. else
  303. throw new ArgumentOutOfRangeException("dockState");
  304. }
  305. internal int GetNumberOfPanes(DockState dockState)
  306. {
  307. return GetPanes(dockState).Count;
  308. }
  309. protected Rectangle RectangleTopLeft
  310. {
  311. get
  312. {
  313. int height = MeasureHeight();
  314. return PanesTop.Count > 0 && PanesLeft.Count > 0 ? new Rectangle(0, 0, height, height) : Rectangle.Empty;
  315. }
  316. }
  317. protected Rectangle RectangleTopRight
  318. {
  319. get
  320. {
  321. int height = MeasureHeight();
  322. return PanesTop.Count > 0 && PanesRight.Count > 0 ? new Rectangle(Width - height, 0, height, height) : Rectangle.Empty;
  323. }
  324. }
  325. protected Rectangle RectangleBottomLeft
  326. {
  327. get
  328. {
  329. int height = MeasureHeight();
  330. return PanesBottom.Count > 0 && PanesLeft.Count > 0 ? new Rectangle(0, Height - height, height, height) : Rectangle.Empty;
  331. }
  332. }
  333. protected Rectangle RectangleBottomRight
  334. {
  335. get
  336. {
  337. int height = MeasureHeight();
  338. return PanesBottom.Count > 0 && PanesRight.Count > 0 ? new Rectangle(Width - height, Height - height, height, height) : Rectangle.Empty;
  339. }
  340. }
  341. protected internal Rectangle GetTabStripRectangle(DockState dockState)
  342. {
  343. int height = MeasureHeight();
  344. if (dockState == DockState.DockTopAutoHide && PanesTop.Count > 0)
  345. return new Rectangle(RectangleTopLeft.Width, 0, Width - RectangleTopLeft.Width - RectangleTopRight.Width, height);
  346. else if (dockState == DockState.DockBottomAutoHide && PanesBottom.Count > 0)
  347. return new Rectangle(RectangleBottomLeft.Width, Height - height, Width - RectangleBottomLeft.Width - RectangleBottomRight.Width, height);
  348. else if (dockState == DockState.DockLeftAutoHide && PanesLeft.Count > 0)
  349. return new Rectangle(0, RectangleTopLeft.Width, height, Height - RectangleTopLeft.Height - RectangleBottomLeft.Height);
  350. else if (dockState == DockState.DockRightAutoHide && PanesRight.Count > 0)
  351. return new Rectangle(Width - height, RectangleTopRight.Width, height, Height - RectangleTopRight.Height - RectangleBottomRight.Height);
  352. else
  353. return Rectangle.Empty;
  354. }
  355. private GraphicsPath m_displayingArea = null;
  356. private GraphicsPath DisplayingArea
  357. {
  358. get
  359. {
  360. if (m_displayingArea == null)
  361. m_displayingArea = new GraphicsPath();
  362. return m_displayingArea;
  363. }
  364. }
  365. private void SetRegion()
  366. {
  367. DisplayingArea.Reset();
  368. DisplayingArea.AddRectangle(RectangleTopLeft);
  369. DisplayingArea.AddRectangle(RectangleTopRight);
  370. DisplayingArea.AddRectangle(RectangleBottomLeft);
  371. DisplayingArea.AddRectangle(RectangleBottomRight);
  372. DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockTopAutoHide));
  373. DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockBottomAutoHide));
  374. DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockLeftAutoHide));
  375. DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockRightAutoHide));
  376. Region = new Region(DisplayingArea);
  377. }
  378. protected override void OnMouseDown(MouseEventArgs e)
  379. {
  380. base.OnMouseDown(e);
  381. if (e.Button != MouseButtons.Left)
  382. return;
  383. IDockContent content = HitTest();
  384. if (content == null)
  385. return;
  386. content.DockHandler.Activate();
  387. }
  388. protected override void OnMouseHover(EventArgs e)
  389. {
  390. base.OnMouseHover(e);
  391. IDockContent content = HitTest();
  392. if (content != null && DockPanel.ActiveAutoHideContent != content)
  393. DockPanel.ActiveAutoHideContent = content;
  394. // requires further tracking of mouse hover behavior,
  395. ResetMouseEventArgs();
  396. }
  397. protected override void OnLayout(LayoutEventArgs levent)
  398. {
  399. RefreshChanges();
  400. base.OnLayout (levent);
  401. }
  402. internal void RefreshChanges()
  403. {
  404. if (IsDisposed)
  405. return;
  406. SetRegion();
  407. OnRefreshChanges();
  408. }
  409. protected virtual void OnRefreshChanges()
  410. {
  411. }
  412. protected internal abstract int MeasureHeight();
  413. private IDockContent HitTest()
  414. {
  415. Point ptMouse = PointToClient(Control.MousePosition);
  416. return HitTest(ptMouse);
  417. }
  418. protected virtual Tab CreateTab(IDockContent content)
  419. {
  420. return new Tab(content);
  421. }
  422. protected virtual Pane CreatePane(DockPane dockPane)
  423. {
  424. return new Pane(dockPane);
  425. }
  426. protected abstract IDockContent HitTest(Point point);
  427. }
  428. }