DefaultLayout.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. //
  28. using System;
  29. using System.Drawing;
  30. namespace System.Windows.Forms.Layout
  31. {
  32. class DefaultLayout : LayoutEngine
  33. {
  34. void LayoutDockedChildren (Control parent, Control[] controls)
  35. {
  36. Rectangle space = parent.ClientRectangle;
  37. MdiClient mdi = null;
  38. // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
  39. for (int i = controls.Length - 1; i >= 0; i--) {
  40. Control child = controls[i];
  41. if (!child.VisibleInternal
  42. || child.ControlLayoutType == Control.LayoutType.Anchor)
  43. continue;
  44. // MdiClient never fills the whole area like other controls, have to do it later
  45. if (child is MdiClient) {
  46. mdi = (MdiClient)child;
  47. continue;
  48. }
  49. switch (child.Dock) {
  50. case DockStyle.None:
  51. // Do nothing
  52. break;
  53. case DockStyle.Left:
  54. child.SetBounds (space.Left, space.Y, child.Width, space.Height, BoundsSpecified.None);
  55. space.X += child.Width;
  56. space.Width -= child.Width;
  57. break;
  58. case DockStyle.Top:
  59. child.SetBounds (space.Left, space.Y, space.Width, child.Height, BoundsSpecified.None);
  60. space.Y += child.Height;
  61. space.Height -= child.Height;
  62. break;
  63. case DockStyle.Right:
  64. child.SetBounds (space.Right - child.Width, space.Y, child.Width, space.Height, BoundsSpecified.None);
  65. space.Width -= child.Width;
  66. break;
  67. case DockStyle.Bottom:
  68. child.SetBounds (space.Left, space.Bottom - child.Height, space.Width, child.Height, BoundsSpecified.None);
  69. space.Height -= child.Height;
  70. break;
  71. case DockStyle.Fill:
  72. child.SetBounds (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
  73. break;
  74. }
  75. }
  76. // MdiClient gets whatever space is left
  77. if (mdi != null)
  78. mdi.SetBounds (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
  79. }
  80. void LayoutAnchoredChildren (Control parent, Control[] controls)
  81. {
  82. Rectangle space = parent.ClientRectangle;
  83. for (int i = 0; i < controls.Length; i++) {
  84. int left;
  85. int top;
  86. int width;
  87. int height;
  88. Control child = controls[i];
  89. if (!child.VisibleInternal
  90. || child.ControlLayoutType == Control.LayoutType.Dock)
  91. continue;
  92. AnchorStyles anchor = child.Anchor;
  93. left = child.Left;
  94. top = child.Top;
  95. #if NET_2_0
  96. Size preferredsize = child.PreferredSize;
  97. width = preferredsize.Width;
  98. height = preferredsize.Height;
  99. #else
  100. width = child.Width;
  101. height = child.Height;
  102. #endif
  103. if ((anchor & AnchorStyles.Right) != 0) {
  104. if ((anchor & AnchorStyles.Left) != 0)
  105. width = space.Width - child.dist_right - left;
  106. else
  107. left = space.Width - child.dist_right - width;
  108. }
  109. else if ((anchor & AnchorStyles.Left) == 0) {
  110. // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
  111. // This calculates from scratch every time:
  112. left = left + (space.Width - (left + width + child.dist_right)) / 2;
  113. }
  114. if ((anchor & AnchorStyles.Bottom) != 0) {
  115. if ((anchor & AnchorStyles.Top) != 0)
  116. height = space.Height - child.dist_bottom - top;
  117. else
  118. top = space.Height - child.dist_bottom - height;
  119. }
  120. else if ((anchor & AnchorStyles.Top) == 0) {
  121. // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
  122. // This calculates from scratch every time:
  123. top = top + (space.Height - (top + height + child.dist_bottom)) / 2;
  124. }
  125. // Sanity
  126. if (width < 0)
  127. width = 0;
  128. if (height < 0)
  129. height = 0;
  130. child.SetBounds (left, top, width, height, BoundsSpecified.None);
  131. }
  132. }
  133. public override bool Layout (object container, LayoutEventArgs args)
  134. {
  135. Control parent = container as Control;
  136. Control[] controls = parent.Controls.GetAllControls ();
  137. LayoutDockedChildren (parent, controls);
  138. LayoutAnchoredChildren (parent, controls);
  139. return false;
  140. }
  141. }
  142. }