DefaultLayout.cs 6.3 KB

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