DefaultLayout.cs 4.6 KB

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