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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
32 namespace System.Windows.Forms.Layout
34 class DefaultLayout : LayoutEngine
36 void LayoutDockedChildren (Control parent, Control[] controls)
38 Rectangle space = parent.DisplayRectangle;
40 // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
41 for (int i = controls.Length - 1; i >= 0; i--) {
42 Control child = controls[i];
44 if (!child.VisibleInternal
45 || child.ControlLayoutType == Control.LayoutType.Anchor)
54 child.SetImplicitBounds (space.Left, space.Y, child.Width, space.Height);
55 space.X += child.Width;
56 space.Width -= child.Width;
60 child.SetImplicitBounds (space.Left, space.Y, space.Width, child.Height);
61 space.Y += child.Height;
62 space.Height -= child.Height;
66 child.SetImplicitBounds (space.Right - child.Width, space.Y, child.Width, space.Height);
67 space.Width -= child.Width;
70 case DockStyle.Bottom:
71 child.SetImplicitBounds (space.Left, space.Bottom - child.Height, space.Width, child.Height);
72 space.Height -= child.Height;
77 for (int i = controls.Length - 1; i >= 0; i--) {
78 Control child = controls[i];
80 if (child.VisibleInternal
81 && (child.ControlLayoutType == Control.LayoutType.Dock)
82 && (child.Dock == DockStyle.Fill))
83 child.SetBounds (space.Left, space.Top, space.Width, space.Height);
87 void LayoutAnchoredChildren (Control parent, Control[] controls)
89 Rectangle space = parent.DisplayRectangle;
91 for (int i = 0; i < controls.Length; i++) {
97 Control child = controls[i];
99 if (!child.VisibleInternal
100 || child.ControlLayoutType == Control.LayoutType.Dock)
103 AnchorStyles anchor = child.Anchor;
108 height = child.Height;
110 if ((anchor & AnchorStyles.Right) != 0) {
111 if ((anchor & AnchorStyles.Left) != 0)
112 width = space.Width - child.dist_right - left;
114 left = space.Width - child.dist_right - width;
116 else if ((anchor & AnchorStyles.Left) == 0) {
117 // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
118 // This calculates from scratch every time:
119 left = left + (space.Width - (left + width + child.dist_right)) / 2;
122 if ((anchor & AnchorStyles.Bottom) != 0) {
123 if ((anchor & AnchorStyles.Top) != 0)
124 height = space.Height - child.dist_bottom - top;
126 top = space.Height - child.dist_bottom - height;
128 else if ((anchor & AnchorStyles.Top) == 0) {
129 // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
130 // This calculates from scratch every time:
131 top = top + (space.Height - (top + height + child.dist_bottom)) / 2;
141 child.SetBounds (left, top, width, height);
145 public override bool Layout (object container, LayoutEventArgs args)
147 Control parent = container as Control;
149 Control[] controls = parent.Controls.GetAllControls ();
151 LayoutDockedChildren (parent, controls);
152 LayoutAnchoredChildren (parent, controls);