PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/FD3/PluginCore/DockPanelSuite/Docking/FloatWindow.cs

https://bitbucket.org/kkszysiu/flashdevelop
C# | 435 lines | 364 code | 70 blank | 1 comment | 121 complexity | 0c0f33b8210082a84ec2d067a1aa3878 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Runtime.InteropServices;
  6. using System.Security.Permissions;
  7. using System.Diagnostics.CodeAnalysis;
  8. using PluginCore.DockPanelSuite;
  9. namespace WeifenLuo.WinFormsUI.Docking
  10. {
  11. public class FloatWindow : Form, INestedPanesContainer, IDockDragSource
  12. {
  13. private NestedPaneCollection m_nestedPanes;
  14. internal const int WM_CHECKDISPOSE = (int)(Win32.Msgs.WM_USER + 1);
  15. internal protected FloatWindow(DockPanel dockPanel, DockPane pane)
  16. {
  17. InternalConstruct(dockPanel, pane, false, Rectangle.Empty);
  18. }
  19. internal protected FloatWindow(DockPanel dockPanel, DockPane pane, Rectangle bounds)
  20. {
  21. InternalConstruct(dockPanel, pane, true, bounds);
  22. }
  23. private void InternalConstruct(DockPanel dockPanel, DockPane pane, bool boundsSpecified, Rectangle bounds)
  24. {
  25. if (dockPanel == null)
  26. throw(new ArgumentNullException(Strings.FloatWindow_Constructor_NullDockPanel));
  27. m_nestedPanes = new NestedPaneCollection(this);
  28. FormBorderStyle = FormBorderStyle.SizableToolWindow;
  29. ShowInTaskbar = false;
  30. if (dockPanel.RightToLeft != RightToLeft)
  31. RightToLeft = dockPanel.RightToLeft;
  32. if (RightToLeftLayout != dockPanel.RightToLeftLayout)
  33. RightToLeftLayout = dockPanel.RightToLeftLayout;
  34. SuspendLayout();
  35. if (boundsSpecified)
  36. {
  37. Bounds = bounds;
  38. StartPosition = FormStartPosition.Manual;
  39. }
  40. else
  41. {
  42. StartPosition = FormStartPosition.CenterParent;
  43. Size = dockPanel.DefaultFloatWindowSize;
  44. }
  45. m_dockPanel = dockPanel;
  46. Owner = DockPanel.FindForm();
  47. DockPanel.AddFloatWindow(this);
  48. if (pane != null)
  49. pane.FloatWindow = this;
  50. ResumeLayout();
  51. }
  52. protected override void Dispose(bool disposing)
  53. {
  54. if (disposing)
  55. {
  56. if (DockPanel != null)
  57. DockPanel.RemoveFloatWindow(this);
  58. m_dockPanel = null;
  59. }
  60. base.Dispose(disposing);
  61. }
  62. private bool m_allowEndUserDocking = true;
  63. public bool AllowEndUserDocking
  64. {
  65. get { return m_allowEndUserDocking; }
  66. set { m_allowEndUserDocking = value; }
  67. }
  68. public NestedPaneCollection NestedPanes
  69. {
  70. get { return m_nestedPanes; }
  71. }
  72. public VisibleNestedPaneCollection VisibleNestedPanes
  73. {
  74. get { return NestedPanes.VisibleNestedPanes; }
  75. }
  76. private DockPanel m_dockPanel;
  77. public DockPanel DockPanel
  78. {
  79. get { return m_dockPanel; }
  80. }
  81. public DockState DockState
  82. {
  83. get { return DockState.Float; }
  84. }
  85. public bool IsFloat
  86. {
  87. get { return DockState == DockState.Float; }
  88. }
  89. internal bool IsDockStateValid(DockState dockState)
  90. {
  91. foreach (DockPane pane in NestedPanes)
  92. foreach (IDockContent content in pane.Contents)
  93. if (!DockHelper.IsDockStateValid(dockState, content.DockHandler.DockAreas))
  94. return false;
  95. return true;
  96. }
  97. protected override void OnActivated(EventArgs e)
  98. {
  99. DockPanel.FloatWindows.BringWindowToFront(this);
  100. base.OnActivated (e);
  101. }
  102. protected override void OnLayout(LayoutEventArgs levent)
  103. {
  104. VisibleNestedPanes.Refresh();
  105. RefreshChanges();
  106. Visible = (VisibleNestedPanes.Count > 0);
  107. SetText();
  108. base.OnLayout(levent);
  109. }
  110. [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]
  111. internal void SetText()
  112. {
  113. DockPane theOnlyPane = (VisibleNestedPanes.Count == 1) ? VisibleNestedPanes[0] : null;
  114. if (theOnlyPane == null)
  115. Text = " "; // use " " instead of string.Empty because the whole title bar will disappear when ControlBox is set to false.
  116. else if (theOnlyPane.ActiveContent == null)
  117. Text = " ";
  118. else
  119. Text = theOnlyPane.ActiveContent.DockHandler.TabText;
  120. }
  121. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
  122. {
  123. Rectangle rectWorkArea = SystemInformation.VirtualScreen;
  124. if (y + height > rectWorkArea.Bottom)
  125. y -= (y + height) - rectWorkArea.Bottom;
  126. if (y < rectWorkArea.Top)
  127. y += rectWorkArea.Top - y;
  128. base.SetBoundsCore (x, y, width, height, specified);
  129. }
  130. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  131. protected override void WndProc(ref Message m)
  132. {
  133. if (m.Msg == (int)Win32.Msgs.WM_NCLBUTTONDOWN)
  134. {
  135. if (IsDisposed)
  136. return;
  137. uint result = NativeMethods.SendMessage(this.Handle, (int)Win32.Msgs.WM_NCHITTEST, 0, (uint)m.LParam);
  138. if (result == 2 && DockPanel.AllowEndUserDocking && this.AllowEndUserDocking) // HITTEST_CAPTION
  139. {
  140. Activate();
  141. m_dockPanel.BeginDrag(this);
  142. }
  143. else
  144. base.WndProc(ref m);
  145. return;
  146. }
  147. else if (m.Msg == (int)Win32.Msgs.WM_NCRBUTTONDOWN)
  148. {
  149. uint result = NativeMethods.SendMessage(this.Handle, (int)Win32.Msgs.WM_NCHITTEST, 0, (uint)m.LParam);
  150. if (result == 2) // HITTEST_CAPTION
  151. {
  152. DockPane theOnlyPane = (VisibleNestedPanes.Count == 1) ? VisibleNestedPanes[0] : null;
  153. if (theOnlyPane != null && theOnlyPane.ActiveContent != null)
  154. {
  155. theOnlyPane.ShowTabPageContextMenu(this, PointToClient(Control.MousePosition));
  156. return;
  157. }
  158. }
  159. base.WndProc(ref m);
  160. return;
  161. }
  162. else if (m.Msg == (int)Win32.Msgs.WM_CLOSE)
  163. {
  164. if (NestedPanes.Count == 0)
  165. {
  166. base.WndProc(ref m);
  167. return;
  168. }
  169. for (int i = NestedPanes.Count - 1; i >= 0; i--)
  170. {
  171. DockContentCollection contents = NestedPanes[i].Contents;
  172. for (int j = contents.Count - 1; j >= 0; j--)
  173. {
  174. IDockContent content = contents[j];
  175. if (content.DockHandler.DockState != DockState.Float)
  176. continue;
  177. if (!content.DockHandler.CloseButton)
  178. continue;
  179. if (content.DockHandler.HideOnClose)
  180. content.DockHandler.Hide();
  181. else
  182. content.DockHandler.Close();
  183. }
  184. }
  185. return;
  186. }
  187. else if (m.Msg == (int)Win32.Msgs.WM_NCLBUTTONDBLCLK)
  188. {
  189. uint result = NativeMethods.SendMessage(this.Handle, (int)Win32.Msgs.WM_NCHITTEST, 0, (uint)m.LParam);
  190. if (result != 2) // HITTEST_CAPTION
  191. {
  192. base.WndProc(ref m);
  193. return;
  194. }
  195. DockPanel.SuspendLayout(true);
  196. // Restore to panel
  197. foreach (DockPane pane in NestedPanes)
  198. {
  199. if (pane.DockState != DockState.Float)
  200. continue;
  201. pane.RestoreToPanel();
  202. }
  203. DockPanel.ResumeLayout(true, true);
  204. return;
  205. }
  206. else if (m.Msg == WM_CHECKDISPOSE)
  207. {
  208. if (NestedPanes.Count == 0)
  209. Dispose();
  210. return;
  211. }
  212. base.WndProc(ref m);
  213. }
  214. internal void RefreshChanges()
  215. {
  216. if (IsDisposed) return;
  217. if (VisibleNestedPanes.Count == 0)
  218. {
  219. ControlBox = true;
  220. return;
  221. }
  222. for (int i=VisibleNestedPanes.Count - 1; i>=0; i--)
  223. {
  224. DockContentCollection contents = VisibleNestedPanes[i].Contents;
  225. for (int j=contents.Count - 1; j>=0; j--)
  226. {
  227. IDockContent content = contents[j];
  228. if (content.DockHandler.DockState != DockState.Float)
  229. continue;
  230. if (content.DockHandler.CloseButton)
  231. {
  232. ControlBox = true;
  233. return;
  234. }
  235. }
  236. }
  237. ControlBox = false;
  238. }
  239. public virtual Rectangle DisplayingRectangle
  240. {
  241. get { return ClientRectangle; }
  242. }
  243. internal void TestDrop(IDockDragSource dragSource, DockOutlineBase dockOutline)
  244. {
  245. if (VisibleNestedPanes.Count == 1)
  246. {
  247. DockPane pane = VisibleNestedPanes[0];
  248. if (!dragSource.CanDockTo(pane))
  249. return;
  250. Point ptMouse = Control.MousePosition;
  251. uint lParam = Win32Helper.MakeLong(ptMouse.X, ptMouse.Y);
  252. if (NativeMethods.SendMessage(Handle, (int)Win32.Msgs.WM_NCHITTEST, 0, lParam) == (uint)Win32.HitTest.HTCAPTION)
  253. dockOutline.Show(VisibleNestedPanes[0], -1);
  254. }
  255. }
  256. #region IDockDragSource Members
  257. #region IDragSource Members
  258. Control IDragSource.DragControl
  259. {
  260. get { return this; }
  261. }
  262. #endregion
  263. bool IDockDragSource.IsDockStateValid(DockState dockState)
  264. {
  265. return IsDockStateValid(dockState);
  266. }
  267. bool IDockDragSource.CanDockTo(DockPane pane)
  268. {
  269. if (!IsDockStateValid(pane.DockState))
  270. return false;
  271. if (pane.FloatWindow == this)
  272. return false;
  273. return true;
  274. }
  275. Rectangle IDockDragSource.BeginDrag(Point ptMouse)
  276. {
  277. return Bounds;
  278. }
  279. public void FloatAt(Rectangle floatWindowBounds)
  280. {
  281. Bounds = floatWindowBounds;
  282. }
  283. public void DockTo(DockPane pane, DockStyle dockStyle, int contentIndex)
  284. {
  285. if (dockStyle == DockStyle.Fill)
  286. {
  287. for (int i = NestedPanes.Count - 1; i >= 0; i--)
  288. {
  289. DockPane paneFrom = NestedPanes[i];
  290. for (int j = paneFrom.Contents.Count - 1; j >= 0; j--)
  291. {
  292. IDockContent c = paneFrom.Contents[j];
  293. c.DockHandler.Pane = pane;
  294. if (contentIndex != -1)
  295. pane.SetContentIndex(c, contentIndex);
  296. }
  297. }
  298. }
  299. else
  300. {
  301. DockAlignment alignment = DockAlignment.Left;
  302. if (dockStyle == DockStyle.Left)
  303. alignment = DockAlignment.Left;
  304. else if (dockStyle == DockStyle.Right)
  305. alignment = DockAlignment.Right;
  306. else if (dockStyle == DockStyle.Top)
  307. alignment = DockAlignment.Top;
  308. else if (dockStyle == DockStyle.Bottom)
  309. alignment = DockAlignment.Bottom;
  310. MergeNestedPanes(VisibleNestedPanes, pane.NestedPanesContainer.NestedPanes, pane, alignment, 0.5);
  311. }
  312. }
  313. public void DockTo(DockPanel panel, DockStyle dockStyle)
  314. {
  315. if (panel != DockPanel)
  316. throw new ArgumentException(Strings.IDockDragSource_DockTo_InvalidPanel, "panel");
  317. NestedPaneCollection nestedPanesTo = null;
  318. if (dockStyle == DockStyle.Top)
  319. nestedPanesTo = DockPanel.DockWindows[DockState.DockTop].NestedPanes;
  320. else if (dockStyle == DockStyle.Bottom)
  321. nestedPanesTo = DockPanel.DockWindows[DockState.DockBottom].NestedPanes;
  322. else if (dockStyle == DockStyle.Left)
  323. nestedPanesTo = DockPanel.DockWindows[DockState.DockLeft].NestedPanes;
  324. else if (dockStyle == DockStyle.Right)
  325. nestedPanesTo = DockPanel.DockWindows[DockState.DockRight].NestedPanes;
  326. else if (dockStyle == DockStyle.Fill)
  327. nestedPanesTo = DockPanel.DockWindows[DockState.Document].NestedPanes;
  328. DockPane prevPane = null;
  329. for (int i = nestedPanesTo.Count - 1; i >= 0; i--)
  330. if (nestedPanesTo[i] != VisibleNestedPanes[0])
  331. prevPane = nestedPanesTo[i];
  332. MergeNestedPanes(VisibleNestedPanes, nestedPanesTo, prevPane, DockAlignment.Left, 0.5);
  333. }
  334. private static void MergeNestedPanes(VisibleNestedPaneCollection nestedPanesFrom, NestedPaneCollection nestedPanesTo, DockPane prevPane, DockAlignment alignment, double proportion)
  335. {
  336. if (nestedPanesFrom.Count == 0)
  337. return;
  338. int count = nestedPanesFrom.Count;
  339. DockPane[] panes = new DockPane[count];
  340. DockPane[] prevPanes = new DockPane[count];
  341. DockAlignment[] alignments = new DockAlignment[count];
  342. double[] proportions = new double[count];
  343. for (int i = 0; i < count; i++)
  344. {
  345. panes[i] = nestedPanesFrom[i];
  346. prevPanes[i] = nestedPanesFrom[i].NestedDockingStatus.PreviousPane;
  347. alignments[i] = nestedPanesFrom[i].NestedDockingStatus.Alignment;
  348. proportions[i] = nestedPanesFrom[i].NestedDockingStatus.Proportion;
  349. }
  350. DockPane pane = panes[0].DockTo(nestedPanesTo.Container, prevPane, alignment, proportion);
  351. panes[0].DockState = nestedPanesTo.DockState;
  352. for (int i = 1; i < count; i++)
  353. {
  354. for (int j = i; j < count; j++)
  355. {
  356. if (prevPanes[j] == panes[i - 1])
  357. prevPanes[j] = pane;
  358. }
  359. pane = panes[i].DockTo(nestedPanesTo.Container, prevPanes[i], alignments[i], proportions[i]);
  360. panes[i].DockState = nestedPanesTo.DockState;
  361. }
  362. }
  363. #endregion
  364. }
  365. }