DefaultLayout.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. public override bool Layout (object container, LayoutEventArgs args)
  35. {
  36. Control parent = container as Control;
  37. Control child;
  38. AnchorStyles anchor;
  39. Rectangle space;
  40. space = parent.DisplayRectangle;
  41. // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
  42. Control[] controls = parent.Controls.GetAllControls ();
  43. for (int i = controls.Length - 1; i >= 0; i--) {
  44. child = controls[i];
  45. if (!child.Visible)
  46. continue;
  47. switch (child.Dock) {
  48. case DockStyle.None:
  49. // Do nothing
  50. break;
  51. case DockStyle.Left:
  52. child.SetImplicitBounds (space.Left, space.Y, child.Width, space.Height);
  53. space.X += child.Width;
  54. space.Width -= child.Width;
  55. break;
  56. case DockStyle.Top:
  57. child.SetImplicitBounds (space.Left, space.Y, space.Width, child.Height);
  58. space.Y += child.Height;
  59. space.Height -= child.Height;
  60. break;
  61. case DockStyle.Right:
  62. child.SetImplicitBounds (space.Right - child.Width, space.Y, child.Width, space.Height);
  63. space.Width -= child.Width;
  64. break;
  65. case DockStyle.Bottom:
  66. child.SetImplicitBounds (space.Left, space.Bottom - child.Height, space.Width, child.Height);
  67. space.Height -= child.Height;
  68. break;
  69. }
  70. }
  71. for (int i = controls.Length - 1; i >= 0; i--) {
  72. child = controls[i];
  73. if (child.Visible && (child.Dock == DockStyle.Fill)) {
  74. child.SetBounds (space.Left, space.Top, space.Width, space.Height);
  75. }
  76. }
  77. 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. child = controls[i];
  84. // If the control is docked we don't need to do anything
  85. if (child.Dock != DockStyle.None) {
  86. continue;
  87. }
  88. anchor = child.Anchor;
  89. left = child.Left;
  90. top = child.Top;
  91. width = child.Width;
  92. height = child.Height;
  93. if ((anchor & AnchorStyles.Left) != 0) {
  94. if ((anchor & AnchorStyles.Right) != 0) {
  95. width = space.Width - child.dist_right - left;
  96. }
  97. else {
  98. ; // Left anchored only, nothing to be done
  99. }
  100. }
  101. else if ((anchor & AnchorStyles.Right) != 0) {
  102. left = space.Width - child.dist_right - width;
  103. }
  104. else {
  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.Top) != 0) {
  110. if ((anchor & AnchorStyles.Bottom) != 0) {
  111. height = space.Height - child.dist_bottom - top;
  112. }
  113. else {
  114. ; // Top anchored only, nothing to be done
  115. }
  116. }
  117. else if ((anchor & AnchorStyles.Bottom) != 0) {
  118. top = space.Height - child.dist_bottom - height;
  119. }
  120. else {
  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. }
  129. if (height < 0) {
  130. height = 0;
  131. }
  132. child.SetBounds (left, top, width, height);
  133. }
  134. return false;
  135. }
  136. }
  137. }