DefaultLayout.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #if NET_2_0
  40. space.X += parent.Padding.Left;
  41. space.Y += parent.Padding.Top;
  42. space.Width -= parent.Padding.Horizontal;
  43. space.Height -= parent.Padding.Vertical;
  44. #endif
  45. // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
  46. for (int i = controls.Length - 1; i >= 0; i--) {
  47. Control child = controls[i];
  48. if (!child.VisibleInternal
  49. || child.ControlLayoutType == Control.LayoutType.Anchor)
  50. continue;
  51. // MdiClient never fills the whole area like other controls, have to do it later
  52. if (child is MdiClient) {
  53. mdi = (MdiClient)child;
  54. continue;
  55. }
  56. switch (child.Dock) {
  57. case DockStyle.None:
  58. // Do nothing
  59. break;
  60. case DockStyle.Left:
  61. child.SetBounds (space.Left, space.Y, child.Width, space.Height, BoundsSpecified.None);
  62. space.X += child.Width;
  63. space.Width -= child.Width;
  64. break;
  65. case DockStyle.Top:
  66. child.SetBounds (space.Left, space.Y, space.Width, child.Height, BoundsSpecified.None);
  67. space.Y += child.Height;
  68. space.Height -= child.Height;
  69. break;
  70. case DockStyle.Right:
  71. child.SetBounds (space.Right - child.Width, space.Y, child.Width, space.Height, BoundsSpecified.None);
  72. space.Width -= child.Width;
  73. break;
  74. case DockStyle.Bottom:
  75. child.SetBounds (space.Left, space.Bottom - child.Height, space.Width, child.Height, BoundsSpecified.None);
  76. space.Height -= child.Height;
  77. break;
  78. case DockStyle.Fill:
  79. child.SetBounds (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
  80. break;
  81. }
  82. }
  83. // MdiClient gets whatever space is left
  84. if (mdi != null)
  85. mdi.SetBounds (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
  86. }
  87. void LayoutAnchoredChildren (Control parent, Control[] controls)
  88. {
  89. Rectangle space = parent.ClientRectangle;
  90. for (int i = 0; i < controls.Length; i++) {
  91. int left;
  92. int top;
  93. int width;
  94. int height;
  95. Control child = controls[i];
  96. if (!child.VisibleInternal
  97. || child.ControlLayoutType == Control.LayoutType.Dock)
  98. continue;
  99. AnchorStyles anchor = child.Anchor;
  100. left = child.Left;
  101. top = child.Top;
  102. width = child.Width;
  103. height = child.Height;
  104. if ((anchor & AnchorStyles.Right) != 0) {
  105. if ((anchor & AnchorStyles.Left) != 0)
  106. width = space.Width - child.dist_right - left;
  107. else
  108. left = space.Width - child.dist_right - width;
  109. }
  110. else if ((anchor & AnchorStyles.Left) == 0) {
  111. // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
  112. // This calculates from scratch every time:
  113. left = left + (space.Width - (left + width + child.dist_right)) / 2;
  114. }
  115. if ((anchor & AnchorStyles.Bottom) != 0) {
  116. if ((anchor & AnchorStyles.Top) != 0)
  117. height = space.Height - child.dist_bottom - top;
  118. else
  119. top = space.Height - child.dist_bottom - height;
  120. }
  121. else if ((anchor & AnchorStyles.Top) == 0) {
  122. // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
  123. // This calculates from scratch every time:
  124. top = top + (space.Height - (top + height + child.dist_bottom)) / 2;
  125. }
  126. // Sanity
  127. if (width < 0)
  128. width = 0;
  129. if (height < 0)
  130. height = 0;
  131. child.SetBounds (left, top, width, height, BoundsSpecified.None);
  132. }
  133. }
  134. #if NET_2_0
  135. void LayoutAutoSizedChildren (Control parent, Control[] controls)
  136. {
  137. for (int i = 0; i < controls.Length; i++) {
  138. int left;
  139. int top;
  140. int width;
  141. int height;
  142. Control child = controls[i];
  143. if (!child.VisibleInternal
  144. || child.ControlLayoutType == Control.LayoutType.Dock
  145. || !child.AutoSize)
  146. continue;
  147. left = child.Left;
  148. top = child.Top;
  149. Size preferredsize = child.PreferredSize;
  150. if (child.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink) {
  151. width = preferredsize.Width;
  152. height = preferredsize.Height;
  153. } else {
  154. width = child.Width;
  155. height = child.Height;
  156. if (preferredsize.Width > width)
  157. width = preferredsize.Width;
  158. if (preferredsize.Height > height)
  159. height = preferredsize.Height;
  160. }
  161. // Sanity
  162. if (width < child.MinimumSize.Width)
  163. width = child.MinimumSize.Width;
  164. if (height < child.MinimumSize.Height)
  165. height = child.MinimumSize.Height;
  166. if (child.MaximumSize.Width != 0 && width > child.MaximumSize.Width)
  167. width = child.MaximumSize.Width;
  168. if (child.MaximumSize.Height != 0 && height > child.MaximumSize.Height)
  169. height = child.MaximumSize.Height;
  170. child.SetBounds (left, top, width, height, BoundsSpecified.None);
  171. }
  172. }
  173. #endif
  174. public override bool Layout (object container, LayoutEventArgs args)
  175. {
  176. Control parent = container as Control;
  177. Control[] controls = parent.Controls.GetAllControls ();
  178. LayoutDockedChildren (parent, controls);
  179. LayoutAnchoredChildren (parent, controls);
  180. #if NET_2_0
  181. LayoutAutoSizedChildren (parent, controls);
  182. #endif
  183. return false;
  184. }
  185. }
  186. }