AutoHideStripBase.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:16k
源码类别:

C#编程

开发平台:

C#

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