DefaultLayout.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.DisplayRectangle;
  37. // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
  38. for (int i = controls.Length - 1; i >= 0; i--) {
  39. Control child = controls[i];
  40. if (!child.VisibleInternal
  41. || child.ControlLayoutType == Control.LayoutType.Anchor)
  42. continue;
  43. switch (child.Dock) {
  44. case DockStyle.None:
  45. // Do nothing
  46. break;
  47. case DockStyle.Left:
  48. child.SetImplicitBounds (space.Left, space.Y, child.Width, space.Height);
  49. space.X += child.Width;
  50. space.Width -= child.Width;
  51. break;
  52. case DockStyle.Top:
  53. child.SetImplicitBounds (space.Left, space.Y, space.Width, child.Height);
  54. space.Y += child.Height;
  55. space.Height -= child.Height;
  56. break;
  57. case DockStyle.Right:
  58. child.SetImplicitBounds (space.Right - child.Width, space.Y, child.Width, space.Height);
  59. space.Width -= child.Width;
  60. break;
  61. case DockStyle.Bottom:
  62. child.SetImplicitBounds (space.Left, space.Bottom - child.Height, space.Width, child.Height);
  63. space.Height -= child.Height;
  64. break;
  65. }
  66. }
  67. for (int i = controls.Length - 1; i >= 0; i--) {
  68. Control child = controls[i];
  69. if (child.VisibleInternal
  70. && (child.ControlLayoutType == Control.LayoutType.Dock)
  71. && (child.Dock == DockStyle.Fill))
  72. child.SetBounds (space.Left, space.Top, space.Width, space.Height);
  73. }
  74. }
  75. void LayoutAnchoredChildren (Control parent, Control[] controls)
  76. {
  77. Rectangle space = parent.DisplayRectangle;
  78. for (int i = 0; i < controls.Length; i++) {
  79. int left;
  80. int top;
  81. int width;
  82. int height;
  83. Control child = controls[i];
  84. if (!child.VisibleInternal
  85. || child.ControlLayoutType == Control.LayoutType.Dock)
  86. continue;
  87. AnchorStyles anchor = child.Anchor;
  88. left = child.Left;
  89. top = child.Top;
  90. width = child.Width;
  91. height = child.Height;
  92. if ((anchor & AnchorStyles.Right) != 0) {
  93. if ((anchor & AnchorStyles.Left) != 0)
  94. width = space.Width - child.dist_right - left;
  95. else
  96. left = space.Width - child.dist_right - width;
  97. }
  98. else if ((anchor & AnchorStyles.Left) == 0) {
  99. // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
  100. // This calculates from scratch every time:
  101. left = left + (space.Width - (left + width + child.dist_right)) / 2;
  102. }
  103. if ((anchor & AnchorStyles.Bottom) != 0) {
  104. if ((anchor & AnchorStyles.Top) != 0)
  105. height = space.Height - child.dist_bottom - top;
  106. else
  107. top = space.Height - child.dist_bottom - height;
  108. }
  109. else if ((anchor & AnchorStyles.Top) == 0) {
  110. // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
  111. // This calculates from scratch every time:
  112. top = top + (space.Height - (top + height + child.dist_bottom)) / 2;
  113. }
  114. // Sanity
  115. if (width < 0)
  116. width = 0;
  117. if (height < 0)
  118. height = 0;
  119. child.SetBounds (left, top, width, height);
  120. }
  121. }
  122. public override bool Layout (object container, LayoutEventArgs args)
  123. {
  124. Control parent = container as Control;
  125. Control[] controls = parent.Controls.GetAllControls ();
  126. LayoutDockedChildren (parent, controls);
  127. LayoutAnchoredChildren (parent, controls);
  128. return false;
  129. }
  130. }
  131. }