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

C#编程

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. using System.ComponentModel;
  6. using System.Runtime.InteropServices;
  7. using System.IO;
  8. using System.Text;
  9. using System.Diagnostics.CodeAnalysis;
  10. using System.Collections.Generic;
  11. using System.Windows.Forms.Design;
  12. // To simplify the process of finding the toolbox bitmap resource:
  13. // #1 Create an internal class called "resfinder" outside of the root namespace.
  14. // #2 Use "resfinder" in the toolbox bitmap attribute instead of the control name.
  15. // #3 use the "<default namespace>.<resourcename>" string to locate the resource.
  16. // See: http://www.bobpowell.net/toolboxbitmap.htm
  17. internal class resfinder
  18. {
  19. }
  20. namespace WeifenLuo.WinFormsUI.Docking
  21. {
  22.     [SuppressMessage("Microsoft.Naming", "CA1720:AvoidTypeNamesInParameters", MessageId = "0#")]
  23. public delegate IDockContent DeserializeDockContent(string persistString);
  24.     [LocalizedDescription("DockPanel_Description")]
  25.     [Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
  26.     [ToolboxBitmap(typeof(resfinder), "WeifenLuo.WinFormsUI.Docking.DockPanel.bmp")]
  27.     [DefaultProperty("DocumentStyle")]
  28.     [DefaultEvent("ActiveContentChanged")]
  29. public partial class DockPanel : Panel
  30. {
  31.         private FocusManagerImpl m_focusManager;
  32.         private DockPanelExtender m_extender;
  33.         private DockPaneCollection m_panes;
  34.         private FloatWindowCollection m_floatWindows;
  35.         private AutoHideWindowControl m_autoHideWindow;
  36.         private DockWindowCollection m_dockWindows;
  37.         private DockContent m_dummyContent; 
  38.         private Control m_dummyControl;
  39.         
  40. public DockPanel()
  41. {
  42.             m_focusManager = new FocusManagerImpl(this);
  43. m_extender = new DockPanelExtender(this);
  44. m_panes = new DockPaneCollection();
  45. m_floatWindows = new FloatWindowCollection();
  46.             SuspendLayout();
  47. m_autoHideWindow = new AutoHideWindowControl(this);
  48. m_autoHideWindow.Visible = false;
  49.             SetAutoHideWindowParent();
  50. m_dummyControl = new DummyControl();
  51. m_dummyControl.Bounds = new Rectangle(0, 0, 1, 1);
  52. Controls.Add(m_dummyControl);
  53. m_dockWindows = new DockWindowCollection(this);
  54. Controls.AddRange(new Control[] {
  55. DockWindows[DockState.Document],
  56. DockWindows[DockState.DockLeft],
  57. DockWindows[DockState.DockRight],
  58. DockWindows[DockState.DockTop],
  59. DockWindows[DockState.DockBottom]
  60. });
  61. m_dummyContent = new DockContent();
  62.             ResumeLayout();
  63.         }
  64.         
  65.         private Color m_BackColor;
  66.         /// <summary>
  67.         /// Determines the color with which the client rectangle will be drawn.
  68.         /// If you take this property instead of the BackColor it will not have any influence on the borders to the surrounding controls (DockPane).
  69.         /// If you use BackColor the borders to the surrounding controls (DockPane) will also change there colors.
  70.         /// Alternatively you can use both of them (BackColor to draw the define the color of the borders and DockBackColor to define the color of the client rectangle). 
  71.         /// For Backgroundimages: Set your prefered Image, then set the DockBackColor and the BackColor to the same Color (Control)
  72.         /// </summary>
  73.         public Color DockBackColor
  74.         {
  75.             get
  76.             {
  77.                 return !m_BackColor.IsEmpty ? m_BackColor : base.BackColor;
  78.             }
  79.             set
  80.             {
  81.                 m_BackColor = value;
  82.             }
  83.         }
  84. private AutoHideStripBase m_autoHideStripControl = null;
  85. internal AutoHideStripBase AutoHideStripControl
  86. {
  87. get
  88. {
  89. if (m_autoHideStripControl == null)
  90. {
  91. m_autoHideStripControl = AutoHideStripFactory.CreateAutoHideStrip(this);
  92. Controls.Add(m_autoHideStripControl);
  93. }
  94. return m_autoHideStripControl;
  95. }
  96. }
  97.         internal void ResetAutoHideStripControl()
  98.         {
  99.             if (m_autoHideStripControl != null)
  100.                 m_autoHideStripControl.Dispose();
  101.             m_autoHideStripControl = null;
  102.         }
  103. private void MdiClientHandleAssigned(object sender, EventArgs e)
  104. {
  105. SetMdiClient();
  106. PerformLayout();
  107. }
  108. private void MdiClient_Layout(object sender, LayoutEventArgs e)
  109. {
  110. if (DocumentStyle != DocumentStyle.DockingMdi)
  111. return;
  112. foreach (DockPane pane in Panes)
  113. if (pane.DockState == DockState.Document)
  114. pane.SetContentBounds();
  115. InvalidateWindowRegion();
  116. }
  117. private bool m_disposed = false;
  118. protected override void Dispose(bool disposing)
  119. {
  120. lock (this)
  121. {
  122. if (!m_disposed && disposing)
  123. {
  124.                     m_focusManager.Dispose();
  125. if (m_mdiClientController != null)
  126. {
  127. m_mdiClientController.HandleAssigned -= new EventHandler(MdiClientHandleAssigned);
  128. m_mdiClientController.MdiChildActivate -= new EventHandler(ParentFormMdiChildActivate);
  129. m_mdiClientController.Layout -= new LayoutEventHandler(MdiClient_Layout);
  130. m_mdiClientController.Dispose();
  131. }
  132. FloatWindows.Dispose();
  133. Panes.Dispose();
  134. DummyContent.Dispose();
  135. m_disposed = true;
  136. }
  137. base.Dispose(disposing);
  138. }
  139. }
  140. [Browsable(false)]
  141. public IDockContent ActiveAutoHideContent
  142. {
  143. get { return AutoHideWindow.ActiveContent; }
  144. set { AutoHideWindow.ActiveContent = value; }
  145. }
  146.         private bool m_allowEndUserDocking = true;
  147. [LocalizedCategory("Category_Docking")]
  148. [LocalizedDescription("DockPanel_AllowEndUserDocking_Description")]
  149. [DefaultValue(true)]
  150. public bool AllowEndUserDocking
  151. {
  152. get { return m_allowEndUserDocking; }
  153. set { m_allowEndUserDocking = value; }
  154. }
  155.         private bool m_allowEndUserNestedDocking = true;
  156.         [LocalizedCategory("Category_Docking")]
  157.         [LocalizedDescription("DockPanel_AllowEndUserNestedDocking_Description")]
  158.         [DefaultValue(true)]
  159.         public bool AllowEndUserNestedDocking
  160.         {
  161.             get { return m_allowEndUserNestedDocking; }
  162.             set { m_allowEndUserNestedDocking = value; }
  163.         }
  164.         private DockContentCollection m_contents = new DockContentCollection();
  165. [Browsable(false)]
  166. public DockContentCollection Contents
  167. {
  168. get { return m_contents; }
  169. }
  170. internal DockContent DummyContent
  171. {
  172. get { return m_dummyContent; }
  173. }
  174.         private bool m_rightToLeftLayout = false;
  175.         [DefaultValue(false)]
  176.         [LocalizedCategory("Appearance")]
  177.         [LocalizedDescription("DockPanel_RightToLeftLayout_Description")]
  178.         public bool RightToLeftLayout
  179.         {
  180.             get { return m_rightToLeftLayout; }
  181.             set
  182.             {
  183.                 if (m_rightToLeftLayout == value)
  184.                     return;
  185.                 m_rightToLeftLayout = value;
  186.                 foreach (FloatWindow floatWindow in FloatWindows)
  187.                     floatWindow.RightToLeftLayout = value;
  188.             }
  189.         }
  190.         protected override void OnRightToLeftChanged(EventArgs e)
  191.         {
  192.             base.OnRightToLeftChanged(e);
  193.             foreach (FloatWindow floatWindow in FloatWindows)
  194.             {
  195.                 if (floatWindow.RightToLeft != RightToLeft)
  196.                     floatWindow.RightToLeft = RightToLeft;
  197.             }
  198.         }
  199. private bool m_showDocumentIcon = false;
  200. [DefaultValue(false)]
  201. [LocalizedCategory("Category_Docking")]
  202. [LocalizedDescription("DockPanel_ShowDocumentIcon_Description")]
  203. public bool ShowDocumentIcon
  204. {
  205. get { return m_showDocumentIcon; }
  206. set
  207. {
  208. if (m_showDocumentIcon == value)
  209. return;
  210. m_showDocumentIcon = value;
  211. Refresh();
  212. }
  213. }
  214.         private DockPanelSkin m_dockPanelSkin = new DockPanelSkin();
  215.         [LocalizedCategory("Category_Docking")]
  216.         [LocalizedDescription("DockPanel_DockPanelSkin")]
  217.         public DockPanelSkin Skin
  218.         {
  219.             get { return m_dockPanelSkin; }
  220.             set { m_dockPanelSkin = value; }
  221.         }
  222.         private DocumentTabStripLocation m_documentTabStripLocation = DocumentTabStripLocation.Top;
  223.         [DefaultValue(DocumentTabStripLocation.Top)]
  224.         [LocalizedCategory("Category_Docking")]
  225.         [LocalizedDescription("DockPanel_DocumentTabStripLocation")]
  226.         public DocumentTabStripLocation DocumentTabStripLocation
  227.         {
  228.             get { return m_documentTabStripLocation; }
  229.             set { m_documentTabStripLocation = value; }
  230.         }
  231. [Browsable(false)]
  232. public DockPanelExtender Extender
  233. {
  234. get { return m_extender; }
  235. }
  236. public DockPanelExtender.IDockPaneFactory DockPaneFactory
  237. {
  238. get { return Extender.DockPaneFactory; }
  239. }
  240. public DockPanelExtender.IFloatWindowFactory FloatWindowFactory
  241. {
  242. get { return Extender.FloatWindowFactory; }
  243. }
  244. internal DockPanelExtender.IDockPaneCaptionFactory DockPaneCaptionFactory
  245. {
  246. get { return Extender.DockPaneCaptionFactory; }
  247. }
  248. internal DockPanelExtender.IDockPaneStripFactory DockPaneStripFactory
  249. {
  250. get { return Extender.DockPaneStripFactory; }
  251. }
  252. internal DockPanelExtender.IAutoHideStripFactory AutoHideStripFactory
  253. {
  254. get { return Extender.AutoHideStripFactory; }
  255. }
  256. [Browsable(false)]
  257. public DockPaneCollection Panes
  258. {
  259. get { return m_panes; }
  260. }
  261. internal Rectangle DockArea
  262. {
  263. get
  264. {
  265. return new Rectangle(DockPadding.Left, DockPadding.Top,
  266. ClientRectangle.Width - DockPadding.Left - DockPadding.Right,
  267. ClientRectangle.Height - DockPadding.Top - DockPadding.Bottom);
  268. }
  269. }
  270. private double m_dockBottomPortion = 0.25;
  271. [LocalizedCategory("Category_Docking")]
  272. [LocalizedDescription("DockPanel_DockBottomPortion_Description")]
  273. [DefaultValue(0.25)]
  274. public double DockBottomPortion
  275. {
  276. get { return m_dockBottomPortion; }
  277. set
  278. {
  279. if (value <= 0)
  280. throw new ArgumentOutOfRangeException("value");
  281. if (value == m_dockBottomPortion)
  282. return;
  283. m_dockBottomPortion = value;
  284.                 if (m_dockBottomPortion < 1 && m_dockTopPortion < 1)
  285.                 {
  286.                     if (m_dockTopPortion + m_dockBottomPortion > 1)
  287.                         m_dockTopPortion = 1 - m_dockBottomPortion;
  288.                 }
  289. PerformLayout();
  290. }
  291. }
  292. private double m_dockLeftPortion = 0.25;
  293. [LocalizedCategory("Category_Docking")]
  294. [LocalizedDescription("DockPanel_DockLeftPortion_Description")]
  295. [DefaultValue(0.25)]
  296. public double DockLeftPortion
  297. {
  298. get { return m_dockLeftPortion; }
  299. set
  300. {
  301. if (value <= 0)
  302. throw new ArgumentOutOfRangeException("value");
  303. if (value == m_dockLeftPortion)
  304. return;
  305. m_dockLeftPortion = value;
  306.                 if (m_dockLeftPortion < 1 && m_dockRightPortion < 1)
  307.                 {
  308.                     if (m_dockLeftPortion + m_dockRightPortion > 1)
  309.                         m_dockRightPortion = 1 - m_dockLeftPortion;
  310.                 }
  311. PerformLayout();
  312. }
  313. }
  314. private double m_dockRightPortion = 0.25;
  315. [LocalizedCategory("Category_Docking")]
  316. [LocalizedDescription("DockPanel_DockRightPortion_Description")]
  317. [DefaultValue(0.25)]
  318. public double DockRightPortion
  319. {
  320. get { return m_dockRightPortion; }
  321. set
  322. {
  323. if (value <= 0)
  324. throw new ArgumentOutOfRangeException("value");
  325. if (value == m_dockRightPortion)
  326. return;
  327. m_dockRightPortion = value;
  328.                 if (m_dockLeftPortion < 1 && m_dockRightPortion < 1)
  329.                 {
  330.                     if (m_dockLeftPortion + m_dockRightPortion > 1)
  331.                         m_dockLeftPortion = 1 - m_dockRightPortion;
  332.                 }
  333. PerformLayout();
  334. }
  335. }
  336. private double m_dockTopPortion = 0.25;
  337. [LocalizedCategory("Category_Docking")]
  338. [LocalizedDescription("DockPanel_DockTopPortion_Description")]
  339. [DefaultValue(0.25)]
  340. public double DockTopPortion
  341. {
  342. get { return m_dockTopPortion; }
  343. set
  344. {
  345. if (value <= 0)
  346. throw new ArgumentOutOfRangeException("value");
  347. if (value == m_dockTopPortion)
  348. return;
  349. m_dockTopPortion = value;
  350.                 if (m_dockTopPortion < 1 && m_dockBottomPortion < 1)
  351.                 {
  352.                     if (m_dockTopPortion + m_dockBottomPortion > 1)
  353.                         m_dockBottomPortion = 1 - m_dockTopPortion;
  354.                 }
  355. PerformLayout();
  356. }
  357. }
  358. [Browsable(false)]
  359. public DockWindowCollection DockWindows
  360. {
  361. get { return m_dockWindows; }
  362. }
  363.         public void UpdateDockWindowZOrder(DockStyle dockStyle, bool fullPanelEdge)
  364.         {
  365.             if (dockStyle == DockStyle.Left)
  366.             {
  367.                 if (fullPanelEdge)
  368.                     DockWindows[DockState.DockLeft].SendToBack();
  369.                 else
  370.                     DockWindows[DockState.DockLeft].BringToFront();
  371.             }
  372.             else if (dockStyle == DockStyle.Right)
  373.             {
  374.                 if (fullPanelEdge)
  375.                     DockWindows[DockState.DockRight].SendToBack();
  376.                 else
  377.                     DockWindows[DockState.DockRight].BringToFront();
  378.             }
  379.             else if (dockStyle == DockStyle.Top)
  380.             {
  381.                 if (fullPanelEdge)
  382.                     DockWindows[DockState.DockTop].SendToBack();
  383.                 else
  384.                     DockWindows[DockState.DockTop].BringToFront();
  385.             }
  386.             else if (dockStyle == DockStyle.Bottom)
  387.             {
  388.                 if (fullPanelEdge)
  389.                     DockWindows[DockState.DockBottom].SendToBack();
  390.                 else
  391.                     DockWindows[DockState.DockBottom].BringToFront();
  392.             }
  393.         }
  394.         public int DocumentsCount
  395.         {
  396.             get
  397.             {
  398.                 int count = 0;
  399.                 foreach (IDockContent content in Documents)
  400.                     count++;
  401.                 return count;
  402.             }
  403.         }
  404.         public IDockContent[] DocumentsToArray()
  405.         {
  406.             int count = DocumentsCount;
  407.             IDockContent[] documents = new IDockContent[count];
  408.             int i = 0;
  409.             foreach (IDockContent content in Documents)
  410.             {
  411.                 documents[i] = content;
  412.                 i++;
  413.             }
  414.             return documents;
  415.         }
  416. public IEnumerable<IDockContent> Documents
  417. {
  418.             get
  419.             {
  420.                 foreach (IDockContent content in Contents)
  421.                 {
  422.                     if (content.DockHandler.DockState == DockState.Document)
  423.                         yield return content;
  424.                 }
  425.             }
  426. }
  427. private Rectangle DocumentRectangle
  428. {
  429. get
  430. {
  431. Rectangle rect = DockArea;
  432. if (DockWindows[DockState.DockLeft].VisibleNestedPanes.Count != 0)
  433. {
  434. rect.X += (int)(DockArea.Width * DockLeftPortion);
  435. rect.Width -= (int)(DockArea.Width * DockLeftPortion);
  436. }
  437. if (DockWindows[DockState.DockRight].VisibleNestedPanes.Count != 0)
  438. rect.Width -= (int)(DockArea.Width * DockRightPortion);
  439. if (DockWindows[DockState.DockTop].VisibleNestedPanes.Count != 0)
  440. {
  441. rect.Y += (int)(DockArea.Height * DockTopPortion);
  442. rect.Height -= (int)(DockArea.Height * DockTopPortion);
  443. }
  444. if (DockWindows[DockState.DockBottom].VisibleNestedPanes.Count != 0)
  445. rect.Height -= (int)(DockArea.Height * DockBottomPortion);
  446. return rect;
  447. }
  448. }
  449. private Control DummyControl
  450. {
  451. get { return m_dummyControl; }
  452. }
  453. [Browsable(false)]
  454. public FloatWindowCollection FloatWindows
  455. {
  456. get { return m_floatWindows; }
  457. }
  458.         private Size m_defaultFloatWindowSize = new Size(300, 300);
  459.         [Category("Layout")]
  460.         [LocalizedDescription("DockPanel_DefaultFloatWindowSize_Description")]
  461.         public Size DefaultFloatWindowSize
  462.         {
  463.             get { return m_defaultFloatWindowSize; }
  464.             set { m_defaultFloatWindowSize = value; }
  465.         }
  466.         private bool ShouldSerializeDefaultFloatWindowSize()
  467.         {
  468.             return DefaultFloatWindowSize != new Size(300, 300);
  469.         }
  470. private DocumentStyle m_documentStyle = DocumentStyle.DockingMdi;
  471. [LocalizedCategory("Category_Docking")]
  472. [LocalizedDescription("DockPanel_DocumentStyle_Description")]
  473. [DefaultValue(DocumentStyle.DockingMdi)]
  474. public DocumentStyle DocumentStyle
  475. {
  476. get { return m_documentStyle; }
  477. set
  478. {
  479. if (value == m_documentStyle)
  480. return;
  481. if (!Enum.IsDefined(typeof(DocumentStyle), value))
  482. throw new InvalidEnumArgumentException();
  483. if (value == DocumentStyle.SystemMdi && DockWindows[DockState.Document].VisibleNestedPanes.Count > 0)
  484. throw new InvalidEnumArgumentException();
  485. m_documentStyle = value;
  486. SuspendLayout(true);
  487.                 SetAutoHideWindowParent();
  488. SetMdiClient();
  489. InvalidateWindowRegion();
  490. foreach (IDockContent content in Contents)
  491. {
  492. if (content.DockHandler.DockState == DockState.Document)
  493. content.DockHandler.SetPaneAndVisible(content.DockHandler.Pane);
  494. }
  495.                 PerformMdiClientLayout();
  496. ResumeLayout(true, true);
  497. }
  498. }
  499.         private int GetDockWindowSize(DockState dockState)
  500.         {
  501.             if (dockState == DockState.DockLeft || dockState == DockState.DockRight)
  502.             {
  503.                 int width = ClientRectangle.Width - DockPadding.Left - DockPadding.Right;
  504.                 int dockLeftSize = m_dockLeftPortion >= 1 ? (int)m_dockLeftPortion : (int)(width * m_dockLeftPortion);
  505.                 int dockRightSize = m_dockRightPortion >= 1 ? (int)m_dockRightPortion : (int)(width * m_dockRightPortion);
  506.                 if (dockLeftSize < MeasurePane.MinSize)
  507.                     dockLeftSize = MeasurePane.MinSize;
  508.                 if (dockRightSize < MeasurePane.MinSize)
  509.                     dockRightSize = MeasurePane.MinSize;
  510.                 if (dockLeftSize + dockRightSize > width - MeasurePane.MinSize)
  511.                 {
  512.                     int adjust = (dockLeftSize + dockRightSize) - (width - MeasurePane.MinSize);
  513.                     dockLeftSize -= adjust / 2;
  514.                     dockRightSize -= adjust / 2;
  515.                 }
  516.                 return dockState == DockState.DockLeft ? dockLeftSize : dockRightSize;
  517.             }
  518.             else if (dockState == DockState.DockTop || dockState == DockState.DockBottom)
  519.             {
  520.                 int height = ClientRectangle.Height - DockPadding.Top - DockPadding.Bottom;
  521.                 int dockTopSize = m_dockTopPortion >= 1 ? (int)m_dockTopPortion : (int)(height * m_dockTopPortion);
  522.                 int dockBottomSize = m_dockBottomPortion >= 1 ? (int)m_dockBottomPortion : (int)(height * m_dockBottomPortion);
  523.                 if (dockTopSize < MeasurePane.MinSize)
  524.                     dockTopSize = MeasurePane.MinSize;
  525.                 if (dockBottomSize < MeasurePane.MinSize)
  526.                     dockBottomSize = MeasurePane.MinSize;
  527.                 if (dockTopSize + dockBottomSize > height - MeasurePane.MinSize)
  528.                 {
  529.                     int adjust = (dockTopSize + dockBottomSize) - (height - MeasurePane.MinSize);
  530.                     dockTopSize -= adjust / 2;
  531.                     dockBottomSize -= adjust / 2;
  532.                 }
  533.                 return dockState == DockState.DockTop ? dockTopSize : dockBottomSize;
  534.             }
  535.             else
  536.                 return 0;
  537.         }
  538.         protected override void OnLayout(LayoutEventArgs levent)
  539. {
  540. SuspendLayout(true);
  541. AutoHideStripControl.Bounds = ClientRectangle;
  542. CalculateDockPadding();
  543.             DockWindows[DockState.DockLeft].Width = GetDockWindowSize(DockState.DockLeft);
  544. DockWindows[DockState.DockRight].Width = GetDockWindowSize(DockState.DockRight);
  545. DockWindows[DockState.DockTop].Height = GetDockWindowSize(DockState.DockTop);
  546. DockWindows[DockState.DockBottom].Height = GetDockWindowSize(DockState.DockBottom);
  547. AutoHideWindow.Bounds = GetAutoHideWindowBounds(AutoHideWindowRectangle);
  548. DockWindows[DockState.Document].BringToFront();
  549. AutoHideWindow.BringToFront();
  550. base.OnLayout(levent);
  551.             if (DocumentStyle == DocumentStyle.SystemMdi && MdiClientExists)
  552.             {
  553.                 SetMdiClientBounds(SystemMdiClientBounds);
  554.                 InvalidateWindowRegion();
  555.             }
  556.             else if (DocumentStyle == DocumentStyle.DockingMdi)
  557.                 InvalidateWindowRegion();
  558. ResumeLayout(true, true);
  559. }
  560. internal Rectangle GetTabStripRectangle(DockState dockState)
  561. {
  562. return AutoHideStripControl.GetTabStripRectangle(dockState);
  563. }
  564. protected override void OnPaint(PaintEventArgs e)
  565. {
  566. base.OnPaint(e);
  567.     if (DockBackColor == BackColor) return;
  568.     Graphics g = e.Graphics;
  569.     SolidBrush bgBrush = new SolidBrush(DockBackColor);
  570.     g.FillRectangle(bgBrush, ClientRectangle);
  571. }
  572. internal void AddContent(IDockContent content)
  573. {
  574. if (content == null)
  575. throw(new ArgumentNullException());
  576. if (!Contents.Contains(content))
  577. {
  578. Contents.Add(content);
  579. OnContentAdded(new DockContentEventArgs(content));
  580. }
  581. }
  582. internal void AddPane(DockPane pane)
  583. {
  584. if (Panes.Contains(pane))
  585. return;
  586. Panes.Add(pane);
  587. }
  588. internal void AddFloatWindow(FloatWindow floatWindow)
  589. {
  590. if (FloatWindows.Contains(floatWindow))
  591. return;
  592. FloatWindows.Add(floatWindow);
  593. }
  594. private void CalculateDockPadding()
  595. {
  596. DockPadding.All = 0;
  597. int height = AutoHideStripControl.MeasureHeight();
  598. if (AutoHideStripControl.GetNumberOfPanes(DockState.DockLeftAutoHide) > 0)
  599. DockPadding.Left = height;
  600. if (AutoHideStripControl.GetNumberOfPanes(DockState.DockRightAutoHide) > 0)
  601. DockPadding.Right = height;
  602. if (AutoHideStripControl.GetNumberOfPanes(DockState.DockTopAutoHide) > 0)
  603. DockPadding.Top = height;
  604. if (AutoHideStripControl.GetNumberOfPanes(DockState.DockBottomAutoHide) > 0)
  605. DockPadding.Bottom = height;
  606. }
  607. internal void RemoveContent(IDockContent content)
  608. {
  609. if (content == null)
  610. throw(new ArgumentNullException());
  611. if (Contents.Contains(content))
  612. {
  613. Contents.Remove(content);
  614. OnContentRemoved(new DockContentEventArgs(content));
  615. }
  616. }
  617. internal void RemovePane(DockPane pane)
  618. {
  619. if (!Panes.Contains(pane))
  620. return;
  621. Panes.Remove(pane);
  622. }
  623. internal void RemoveFloatWindow(FloatWindow floatWindow)
  624. {
  625. if (!FloatWindows.Contains(floatWindow))
  626. return;
  627. FloatWindows.Remove(floatWindow);
  628. }
  629. public void SetPaneIndex(DockPane pane, int index)
  630. {
  631. int oldIndex = Panes.IndexOf(pane);
  632. if (oldIndex == -1)
  633. throw(new ArgumentException(Strings.DockPanel_SetPaneIndex_InvalidPane));
  634. if (index < 0 || index > Panes.Count - 1)
  635. if (index != -1)
  636. throw(new ArgumentOutOfRangeException(Strings.DockPanel_SetPaneIndex_InvalidIndex));
  637. if (oldIndex == index)
  638. return;
  639. if (oldIndex == Panes.Count - 1 && index == -1)
  640. return;
  641. Panes.Remove(pane);
  642. if (index == -1)
  643. Panes.Add(pane);
  644. else if (oldIndex < index)
  645. Panes.AddAt(pane, index - 1);
  646. else
  647. Panes.AddAt(pane, index);
  648. }
  649. public void SuspendLayout(bool allWindows)
  650. {
  651.             FocusManager.SuspendFocusTracking();
  652. SuspendLayout();
  653. if (allWindows)
  654. SuspendMdiClientLayout();
  655. }
  656. public void ResumeLayout(bool performLayout, bool allWindows)
  657. {
  658.             FocusManager.ResumeFocusTracking();
  659.             ResumeLayout(performLayout);
  660.             if (allWindows)
  661.                 ResumeMdiClientLayout(performLayout);
  662. }
  663.     internal Form ParentForm
  664. {
  665. get
  666. {
  667. if (!IsParentFormValid())
  668. throw new InvalidOperationException(Strings.DockPanel_ParentForm_Invalid);
  669. return GetMdiClientController().ParentForm;
  670. }
  671. }
  672. private bool IsParentFormValid()
  673. {
  674. if (DocumentStyle == DocumentStyle.DockingSdi || DocumentStyle == DocumentStyle.DockingWindow)
  675. return true;
  676.             if (!MdiClientExists)
  677.                 GetMdiClientController().RenewMdiClient();
  678.             return (MdiClientExists);
  679. }
  680. protected override void OnParentChanged(EventArgs e)
  681. {
  682.             SetAutoHideWindowParent();
  683.             GetMdiClientController().ParentForm = (this.Parent as Form);
  684. base.OnParentChanged (e);
  685. }
  686.         private void SetAutoHideWindowParent()
  687.         {
  688.             Control parent;
  689.             if (DocumentStyle == DocumentStyle.DockingMdi ||
  690.                 DocumentStyle == DocumentStyle.SystemMdi)
  691.                 parent = this.Parent;
  692.             else
  693.                 parent = this;
  694.             if (AutoHideWindow.Parent != parent)
  695.             {
  696.                 AutoHideWindow.Parent = parent;
  697.                 AutoHideWindow.BringToFront();
  698.             }
  699.         }
  700. protected override void OnVisibleChanged(EventArgs e)
  701. {
  702. base.OnVisibleChanged (e);
  703. if (Visible)
  704. SetMdiClient();
  705. }
  706. private Rectangle SystemMdiClientBounds
  707. {
  708. get
  709. {
  710. if (!IsParentFormValid() || !Visible)
  711. return Rectangle.Empty;
  712. Rectangle rect = ParentForm.RectangleToClient(RectangleToScreen(DocumentWindowBounds));
  713. return rect;
  714. }
  715. }
  716. internal Rectangle DocumentWindowBounds
  717. {
  718. get
  719. {
  720. Rectangle rectDocumentBounds = DisplayRectangle;
  721. if (DockWindows[DockState.DockLeft].Visible)
  722. {
  723. rectDocumentBounds.X += DockWindows[DockState.DockLeft].Width;
  724. rectDocumentBounds.Width -= DockWindows[DockState.DockLeft].Width;
  725. }
  726. if (DockWindows[DockState.DockRight].Visible)
  727. rectDocumentBounds.Width -= DockWindows[DockState.DockRight].Width;
  728. if (DockWindows[DockState.DockTop].Visible)
  729. {
  730. rectDocumentBounds.Y += DockWindows[DockState.DockTop].Height;
  731. rectDocumentBounds.Height -= DockWindows[DockState.DockTop].Height;
  732. }
  733. if (DockWindows[DockState.DockBottom].Visible)
  734. rectDocumentBounds.Height -= DockWindows[DockState.DockBottom].Height;
  735. return rectDocumentBounds;
  736. }
  737. }
  738.         private PaintEventHandler m_dummyControlPaintEventHandler = null;
  739.         private void InvalidateWindowRegion()
  740.         {
  741.             if (DesignMode)
  742.                 return;
  743.             if (m_dummyControlPaintEventHandler == null)
  744.                 m_dummyControlPaintEventHandler = new PaintEventHandler(DummyControl_Paint);
  745.             DummyControl.Paint += m_dummyControlPaintEventHandler;
  746.             DummyControl.Invalidate();
  747.         }
  748.         void DummyControl_Paint(object sender, PaintEventArgs e)
  749.         {
  750.             DummyControl.Paint -= m_dummyControlPaintEventHandler;
  751.             UpdateWindowRegion();
  752.         }
  753. private void UpdateWindowRegion()
  754. {
  755. if (this.DocumentStyle == DocumentStyle.DockingMdi)
  756. UpdateWindowRegion_ClipContent();
  757. else if (this.DocumentStyle == DocumentStyle.DockingSdi ||
  758. this.DocumentStyle == DocumentStyle.DockingWindow)
  759. UpdateWindowRegion_FullDocumentArea();
  760. else if (this.DocumentStyle == DocumentStyle.SystemMdi)
  761. UpdateWindowRegion_EmptyDocumentArea();
  762. }
  763. private void UpdateWindowRegion_FullDocumentArea()
  764. {
  765. SetRegion(null);
  766. }
  767. private void UpdateWindowRegion_EmptyDocumentArea()
  768. {
  769. Rectangle rect = DocumentWindowBounds;
  770. SetRegion(new Rectangle[] { rect });
  771. }
  772. private void UpdateWindowRegion_ClipContent()
  773. {
  774. int count = 0;
  775. foreach (DockPane pane in this.Panes)
  776. {
  777. if (!pane.Visible || pane.DockState != DockState.Document)
  778. continue;
  779. count ++;
  780. }
  781.             if (count == 0)
  782.             {
  783.                 SetRegion(null);
  784.                 return;
  785.             }
  786. Rectangle[] rects = new Rectangle[count];
  787. int i = 0;
  788. foreach (DockPane pane in this.Panes)
  789. {
  790. if (!pane.Visible || pane.DockState != DockState.Document)
  791. continue;
  792.                 rects[i] = RectangleToClient(pane.RectangleToScreen(pane.ContentRectangle));
  793. i++;
  794. }
  795. SetRegion(rects);
  796. }
  797. private Rectangle[] m_clipRects = null;
  798. private void SetRegion(Rectangle[] clipRects)
  799. {
  800. if (!IsClipRectsChanged(clipRects))
  801. return;
  802. m_clipRects = clipRects;
  803. if (m_clipRects == null || m_clipRects.GetLength(0) == 0)
  804. Region = null;
  805. else
  806. {
  807. Region region = new Region(new Rectangle(0, 0, this.Width, this.Height));
  808. foreach (Rectangle rect in m_clipRects)
  809. region.Exclude(rect);
  810. Region = region;
  811. }
  812. }
  813. private bool IsClipRectsChanged(Rectangle[] clipRects)
  814. {
  815. if (clipRects == null && m_clipRects == null)
  816. return false;
  817. else if ((clipRects == null) != (m_clipRects == null))
  818. return true;
  819. foreach (Rectangle rect in clipRects)
  820. {
  821. bool matched = false;
  822. foreach (Rectangle rect2 in m_clipRects)
  823. {
  824. if (rect == rect2)
  825. {
  826. matched = true;
  827. break;
  828. }
  829. }
  830. if (!matched)
  831. return true;
  832. }
  833. foreach (Rectangle rect2 in m_clipRects)
  834. {
  835. bool matched = false;
  836. foreach (Rectangle rect in clipRects)
  837. {
  838. if (rect == rect2)
  839. {
  840. matched = true;
  841. break;
  842. }
  843. }
  844. if (!matched)
  845. return true;
  846. }
  847. return false;
  848. }
  849. private static readonly object ContentAddedEvent = new object();
  850. [LocalizedCategory("Category_DockingNotification")]
  851. [LocalizedDescription("DockPanel_ContentAdded_Description")]
  852. public event EventHandler<DockContentEventArgs> ContentAdded
  853. {
  854. add { Events.AddHandler(ContentAddedEvent, value); }
  855. remove { Events.RemoveHandler(ContentAddedEvent, value); }
  856. }
  857. protected virtual void OnContentAdded(DockContentEventArgs e)
  858. {
  859. EventHandler<DockContentEventArgs> handler = (EventHandler<DockContentEventArgs>)Events[ContentAddedEvent];
  860. if (handler != null)
  861. handler(this, e);
  862. }
  863. private static readonly object ContentRemovedEvent = new object();
  864. [LocalizedCategory("Category_DockingNotification")]
  865. [LocalizedDescription("DockPanel_ContentRemoved_Description")]
  866. public event EventHandler<DockContentEventArgs> ContentRemoved
  867. {
  868. add { Events.AddHandler(ContentRemovedEvent, value); }
  869. remove { Events.RemoveHandler(ContentRemovedEvent, value); }
  870. }
  871. protected virtual void OnContentRemoved(DockContentEventArgs e)
  872. {
  873. EventHandler<DockContentEventArgs> handler = (EventHandler<DockContentEventArgs>)Events[ContentRemovedEvent];
  874. if (handler != null)
  875. handler(this, e);
  876. }
  877.     }
  878. }