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)
27 // Stefan Noack (noackstefan@googlemail.com)
33 namespace System.Windows.Forms.Layout
35 class DefaultLayout : LayoutEngine
37 void LayoutDockedChildren (Control parent, Control[] controls)
39 Rectangle space = parent.DisplayRectangle;
42 // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
43 for (int i = controls.Length - 1; i >= 0; i--) {
44 Control child = controls[i];
45 Size child_size = child.Size;
49 child_size = GetPreferredControlSize (child);
52 if (!child.VisibleInternal
53 || child.ControlLayoutType == Control.LayoutType.Anchor)
56 // MdiClient never fills the whole area like other controls, have to do it later
57 if (child is MdiClient) {
58 mdi = (MdiClient)child;
68 child.SetBounds (space.Left, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
69 space.X += child.Width;
70 space.Width -= child.Width;
74 child.SetBounds (space.Left, space.Y, space.Width, child_size.Height, BoundsSpecified.None);
75 space.Y += child.Height;
76 space.Height -= child.Height;
80 child.SetBounds (space.Right - child_size.Width, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
81 space.Width -= child.Width;
84 case DockStyle.Bottom:
85 child.SetBounds (space.Left, space.Bottom - child_size.Height, space.Width, child_size.Height, BoundsSpecified.None);
86 space.Height -= child.Height;
90 child.SetBounds (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
95 // MdiClient gets whatever space is left
97 mdi.SetBounds (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
100 void LayoutAnchoredChildren (Control parent, Control[] controls)
102 Rectangle space = parent.ClientRectangle;
104 for (int i = 0; i < controls.Length; i++) {
110 Control child = controls[i];
112 if (!child.VisibleInternal
113 || child.ControlLayoutType == Control.LayoutType.Dock)
116 AnchorStyles anchor = child.Anchor;
122 height = child.Height;
124 if ((anchor & AnchorStyles.Right) != 0) {
125 if ((anchor & AnchorStyles.Left) != 0)
126 width = space.Width - child.dist_right - left;
128 left = space.Width - child.dist_right - width;
130 else if ((anchor & AnchorStyles.Left) == 0) {
131 // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
132 // This calculates from scratch every time:
133 left = left + (space.Width - (left + width + child.dist_right)) / 2;
134 child.dist_right = space.Width - (left + width);
137 if ((anchor & AnchorStyles.Bottom) != 0) {
138 if ((anchor & AnchorStyles.Top) != 0)
139 height = space.Height - child.dist_bottom - top;
141 top = space.Height - child.dist_bottom - height;
143 else if ((anchor & AnchorStyles.Top) == 0) {
144 // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
145 // This calculates from scratch every time:
146 top = top + (space.Height - (top + height + child.dist_bottom)) / 2;
147 child.dist_bottom = space.Height - (top + height);
157 child.SetBounds (left, top, width, height, BoundsSpecified.None);
162 void LayoutAutoSizedChildren (Control parent, Control[] controls)
164 for (int i = 0; i < controls.Length; i++) {
168 Control child = controls[i];
169 if (!child.VisibleInternal
170 || child.ControlLayoutType == Control.LayoutType.Dock
177 Size preferredsize = GetPreferredControlSize (child);
179 child.SetBounds (left, top, preferredsize.Width, preferredsize.Height, BoundsSpecified.None);
183 void LayoutAutoSizeContainer (Control container)
190 if (!container.VisibleInternal || container.ControlLayoutType == Control.LayoutType.Dock || !container.AutoSize)
193 left = container.Left;
196 Size preferredsize = container.PreferredSize;
198 if (container.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink) {
199 width = preferredsize.Width;
200 height = preferredsize.Height;
202 width = container.ExplicitBounds.Width;
203 height = container.ExplicitBounds.Height;
204 if (preferredsize.Width > width)
205 width = preferredsize.Width;
206 if (preferredsize.Height > height)
207 height = preferredsize.Height;
211 if (width < container.MinimumSize.Width)
212 width = container.MinimumSize.Width;
214 if (height < container.MinimumSize.Height)
215 height = container.MinimumSize.Height;
217 if (container.MaximumSize.Width != 0 && width > container.MaximumSize.Width)
218 width = container.MaximumSize.Width;
220 if (container.MaximumSize.Height != 0 && height > container.MaximumSize.Height)
221 height = container.MaximumSize.Height;
223 container.SetBounds (left, top, width, height, BoundsSpecified.None);
227 public override bool Layout (object container, LayoutEventArgs args)
229 Control parent = container as Control;
231 Control[] controls = parent.Controls.GetAllControls ();
233 LayoutDockedChildren (parent, controls);
234 LayoutAnchoredChildren (parent, controls);
236 LayoutAutoSizedChildren (parent, controls);
237 if (parent is Form) LayoutAutoSizeContainer (parent);
244 private Size GetPreferredControlSize (Control child)
248 Size preferredsize = child.PreferredSize;
250 if (child.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink) {
251 width = preferredsize.Width;
252 height = preferredsize.Height;
254 width = child.ExplicitBounds.Width;
255 height = child.ExplicitBounds.Height;
256 if (preferredsize.Width > width)
257 width = preferredsize.Width;
258 if (preferredsize.Height > height)
259 height = preferredsize.Height;
263 if (width < child.MinimumSize.Width)
264 width = child.MinimumSize.Width;
266 if (height < child.MinimumSize.Height)
267 height = child.MinimumSize.Height;
269 if (child.MaximumSize.Width != 0 && width > child.MaximumSize.Width)
270 width = child.MaximumSize.Width;
272 if (child.MaximumSize.Height != 0 && height > child.MaximumSize.Height)
273 height = child.MaximumSize.Height;
275 return new Size (width, height);