DockPanel.cs 38 KB

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