DefaultLayout.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // DefaultLayout.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Jonathan Pobst
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. // Stefan Noack ([email protected])
  28. //
  29. using System;
  30. using System.Drawing;
  31. namespace System.Windows.Forms.Layout
  32. {
  33. class DefaultLayout : LayoutEngine
  34. {
  35. void LayoutDockedChildren (Control parent, Control[] controls)
  36. {
  37. Rectangle space = parent.DisplayRectangle;
  38. MdiClient mdi = null;
  39. // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
  40. for (int i = controls.Length - 1; i >= 0; i--) {
  41. Control child = controls[i];
  42. Size child_size = child.Size;
  43. if (child.AutoSize)
  44. child_size = GetPreferredControlSize (child);
  45. if (!child.VisibleInternal
  46. || child.ControlLayoutType == Control.LayoutType.Anchor)
  47. continue;
  48. // MdiClient never fills the whole area like other controls, have to do it later
  49. if (child is MdiClient) {
  50. mdi = (MdiClient)child;
  51. continue;
  52. }
  53. switch (child.Dock) {
  54. case DockStyle.None:
  55. // Do nothing
  56. break;
  57. case DockStyle.Left:
  58. child.SetBoundsInternal (space.Left, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
  59. space.X += child.Width;
  60. space.Width -= child.Width;
  61. break;
  62. case DockStyle.Top:
  63. child.SetBoundsInternal (space.Left, space.Y, space.Width, child_size.Height, BoundsSpecified.None);
  64. space.Y += child.Height;
  65. space.Height -= child.Height;
  66. break;
  67. case DockStyle.Right:
  68. child.SetBoundsInternal (space.Right - child_size.Width, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
  69. space.Width -= child.Width;
  70. break;
  71. case DockStyle.Bottom:
  72. child.SetBoundsInternal (space.Left, space.Bottom - child_size.Height, space.Width, child_size.Height, BoundsSpecified.None);
  73. space.Height -= child.Height;
  74. break;
  75. case DockStyle.Fill:
  76. child.SetBoundsInternal (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
  77. break;
  78. }
  79. }
  80. // MdiClient gets whatever space is left
  81. if (mdi != null)
  82. mdi.SetBoundsInternal (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
  83. }
  84. void LayoutAnchoredChildren (Control parent, Control[] controls)
  85. {
  86. Rectangle space = parent.ClientRectangle;
  87. for (int i = 0; i < controls.Length; i++) {
  88. int left;
  89. int top;
  90. int width;
  91. int height;
  92. Control child = controls[i];
  93. if (!child.VisibleInternal
  94. || child.ControlLayoutType == Control.LayoutType.Dock)
  95. continue;
  96. AnchorStyles anchor = child.Anchor;
  97. left = child.Left;
  98. top = child.Top;
  99. width = child.Width;
  100. height = child.Height;
  101. if ((anchor & AnchorStyles.Right) != 0) {
  102. if ((anchor & AnchorStyles.Left) != 0)
  103. width = space.Width - child.dist_right - left;
  104. else
  105. left = space.Width - child.dist_right - width;
  106. }
  107. else if ((anchor & AnchorStyles.Left) == 0) {
  108. // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
  109. // This calculates from scratch every time:
  110. left = left + (space.Width - (left + width + child.dist_right)) / 2;
  111. child.dist_right = space.Width - (left + width);
  112. }
  113. if ((anchor & AnchorStyles.Bottom) != 0) {
  114. if ((anchor & AnchorStyles.Top) != 0)
  115. height = space.Height - child.dist_bottom - top;
  116. else
  117. top = space.Height - child.dist_bottom - height;
  118. }
  119. else if ((anchor & AnchorStyles.Top) == 0) {
  120. // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
  121. // This calculates from scratch every time:
  122. top = top + (space.Height - (top + height + child.dist_bottom)) / 2;
  123. child.dist_bottom = space.Height - (top + height);
  124. }
  125. // Sanity
  126. if (width < 0)
  127. width = 0;
  128. if (height < 0)
  129. height = 0;
  130. child.SetBoundsInternal (left, top, width, height, BoundsSpecified.None);
  131. }
  132. }
  133. void LayoutAutoSizedChildren (Control parent, Control[] controls)
  134. {
  135. for (int i = 0; i < controls.Length; i++) {
  136. int left;
  137. int top;
  138. Control child = controls[i];
  139. if (!child.VisibleInternal
  140. || child.ControlLayoutType == Control.LayoutType.Dock
  141. || !child.AutoSize)
  142. continue;
  143. AnchorStyles anchor = child.Anchor;
  144. left = child.Left;
  145. top = child.Top;
  146. Size preferredsize = GetPreferredControlSize (child);
  147. if (((anchor & AnchorStyles.Left) != 0) || ((anchor & AnchorStyles.Right) == 0))
  148. child.dist_right += child.Width - preferredsize.Width;
  149. if (((anchor & AnchorStyles.Top) != 0) || ((anchor & AnchorStyles.Bottom) == 0))
  150. child.dist_bottom += child.Height - preferredsize.Height;
  151. child.SetBoundsInternal (left, top, preferredsize.Width, preferredsize.Height, BoundsSpecified.None);
  152. }
  153. }
  154. void LayoutAutoSizeContainer (Control container)
  155. {
  156. int left;
  157. int top;
  158. int width;
  159. int height;
  160. if (!container.VisibleInternal || container.ControlLayoutType == Control.LayoutType.Dock || !container.AutoSize)
  161. return;
  162. left = container.Left;
  163. top = container.Top;
  164. Size preferredsize = container.PreferredSize;
  165. if (container.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink) {
  166. width = preferredsize.Width;
  167. height = preferredsize.Height;
  168. } else {
  169. width = container.ExplicitBounds.Width;
  170. height = container.ExplicitBounds.Height;
  171. if (preferredsize.Width > width)
  172. width = preferredsize.Width;
  173. if (preferredsize.Height > height)
  174. height = preferredsize.Height;
  175. }
  176. // Sanity
  177. if (width < container.MinimumSize.Width)
  178. width = container.MinimumSize.Width;
  179. if (height < container.MinimumSize.Height)
  180. height = container.MinimumSize.Height;
  181. if (container.MaximumSize.Width != 0 && width > container.MaximumSize.Width)
  182. width = container.MaximumSize.Width;
  183. if (container.MaximumSize.Height != 0 && height > container.MaximumSize.Height)
  184. height = container.MaximumSize.Height;
  185. container.SetBoundsInternal (left, top, width, height, BoundsSpecified.None);
  186. }
  187. public override bool Layout (object container, LayoutEventArgs args)
  188. {
  189. Control parent = container as Control;
  190. Control[] controls = parent.Controls.GetAllControls ();
  191. LayoutDockedChildren (parent, controls);
  192. LayoutAnchoredChildren (parent, controls);
  193. LayoutAutoSizedChildren (parent, controls);
  194. if (parent is Form) LayoutAutoSizeContainer (parent);
  195. return false;
  196. }
  197. private Size GetPreferredControlSize (Control child)
  198. {
  199. int width;
  200. int height;
  201. Size preferredsize = child.PreferredSize;
  202. if (child.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink || (child.Dock != DockStyle.None && !(child is Button) && !(child is FlowLayoutPanel))) {
  203. width = preferredsize.Width;
  204. height = preferredsize.Height;
  205. } else {
  206. width = child.ExplicitBounds.Width;
  207. height = child.ExplicitBounds.Height;
  208. if (preferredsize.Width > width)
  209. width = preferredsize.Width;
  210. if (preferredsize.Height > height)
  211. height = preferredsize.Height;
  212. }
  213. if (child.AutoSize && child is FlowLayoutPanel && child.Dock != DockStyle.None) {
  214. switch (child.Dock) {
  215. case DockStyle.Left:
  216. case DockStyle.Right:
  217. if (preferredsize.Width < child.ExplicitBounds.Width && preferredsize.Height < child.Parent.PaddingClientRectangle.Height)
  218. width = preferredsize.Width;
  219. break;
  220. case DockStyle.Top:
  221. case DockStyle.Bottom:
  222. if (preferredsize.Height < child.ExplicitBounds.Height && preferredsize.Width < child.Parent.PaddingClientRectangle.Width)
  223. height = preferredsize.Height;
  224. break;
  225. }
  226. }
  227. // Sanity
  228. if (width < child.MinimumSize.Width)
  229. width = child.MinimumSize.Width;
  230. if (height < child.MinimumSize.Height)
  231. height = child.MinimumSize.Height;
  232. if (child.MaximumSize.Width != 0 && width > child.MaximumSize.Width)
  233. width = child.MaximumSize.Width;
  234. if (child.MaximumSize.Height != 0 && height > child.MaximumSize.Height)
  235. height = child.MaximumSize.Height;
  236. return new Size (width, height);
  237. }
  238. }
  239. }