AutoHideStripBase.cs 18 KB

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