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;
48 child_size = GetPreferredControlSize (child);
50 if (!child.VisibleInternal
51 || child.ControlLayoutType == Control.LayoutType.Anchor)
54 // MdiClient never fills the whole area like other controls, have to do it later
55 if (child is MdiClient) {
56 mdi = (MdiClient)child;
66 child.SetBoundsInternal (space.Left, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
67 space.X += child.Width;
68 space.Width -= child.Width;
72 child.SetBoundsInternal (space.Left, space.Y, space.Width, child_size.Height, BoundsSpecified.None);
73 space.Y += child.Height;
74 space.Height -= child.Height;
78 child.SetBoundsInternal (space.Right - child_size.Width, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
79 space.Width -= child.Width;
82 case DockStyle.Bottom:
83 child.SetBoundsInternal (space.Left, space.Bottom - child_size.Height, space.Width, child_size.Height, BoundsSpecified.None);
84 space.Height -= child.Height;
88 child.SetBoundsInternal (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
93 // MdiClient gets whatever space is left
95 mdi.SetBoundsInternal (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
98 void LayoutAnchoredChildren (Control parent, Control[] controls)
100 Rectangle space = parent.ClientRectangle;
102 for (int i = 0; i < controls.Length; i++) {
108 Control child = controls[i];
110 if (!child.VisibleInternal
111 || child.ControlLayoutType == Control.LayoutType.Dock)
114 AnchorStyles anchor = child.Anchor;
120 height = child.Height;
122 if ((anchor & AnchorStyles.Right) != 0) {
123 if ((anchor & AnchorStyles.Left) != 0)
124 width = space.Width - child.dist_right - left;
126 left = space.Width - child.dist_right - width;
128 else if ((anchor & AnchorStyles.Left) == 0) {
129 // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
130 // This calculates from scratch every time:
131 left = left + (space.Width - (left + width + child.dist_right)) / 2;
132 child.dist_right = space.Width - (left + width);
135 if ((anchor & AnchorStyles.Bottom) != 0) {
136 if ((anchor & AnchorStyles.Top) != 0)
137 height = space.Height - child.dist_bottom - top;
139 top = space.Height - child.dist_bottom - height;
141 else if ((anchor & AnchorStyles.Top) == 0) {
142 // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
143 // This calculates from scratch every time:
144 top = top + (space.Height - (top + height + child.dist_bottom)) / 2;
145 child.dist_bottom = space.Height - (top + height);
155 child.SetBoundsInternal (left, top, width, height, BoundsSpecified.None);
159 void LayoutAutoSizedChildren (Control parent, Control[] controls)
161 for (int i = 0; i < controls.Length; i++) {
165 Control child = controls[i];
166 if (!child.VisibleInternal
167 || child.ControlLayoutType == Control.LayoutType.Dock
171 AnchorStyles anchor = child.Anchor;
175 Size preferredsize = GetPreferredControlSize (child);
177 if (((anchor & AnchorStyles.Left) != 0) || ((anchor & AnchorStyles.Right) == 0))
178 child.dist_right += child.Width - preferredsize.Width;
179 if (((anchor & AnchorStyles.Top) != 0) || ((anchor & AnchorStyles.Bottom) == 0))
180 child.dist_bottom += child.Height - preferredsize.Height;
182 child.SetBoundsInternal (left, top, preferredsize.Width, preferredsize.Height, BoundsSpecified.None);
186 void LayoutAutoSizeContainer (Control container)
193 if (!container.VisibleInternal || container.ControlLayoutType == Control.LayoutType.Dock || !container.AutoSize)
196 left = container.Left;
199 Size preferredsize = container.PreferredSize;
201 if (container.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink) {
202 width = preferredsize.Width;
203 height = preferredsize.Height;
205 width = container.ExplicitBounds.Width;
206 height = container.ExplicitBounds.Height;
207 if (preferredsize.Width > width)
208 width = preferredsize.Width;
209 if (preferredsize.Height > height)
210 height = preferredsize.Height;
214 if (width < container.MinimumSize.Width)
215 width = container.MinimumSize.Width;
217 if (height < container.MinimumSize.Height)
218 height = container.MinimumSize.Height;
220 if (container.MaximumSize.Width != 0 && width > container.MaximumSize.Width)
221 width = container.MaximumSize.Width;
223 if (container.MaximumSize.Height != 0 && height > container.MaximumSize.Height)
224 height = container.MaximumSize.Height;
226 container.SetBoundsInternal (left, top, width, height, BoundsSpecified.None);
229 public override bool Layout (object container, LayoutEventArgs args)
231 Control parent = container as Control;
233 Control[] controls = parent.Controls.GetAllControls ();
235 LayoutDockedChildren (parent, controls);
236 LayoutAnchoredChildren (parent, controls);
237 LayoutAutoSizedChildren (parent, controls);
238 if (parent is Form) LayoutAutoSizeContainer (parent);
243 private Size GetPreferredControlSize (Control child)
247 Size preferredsize = child.PreferredSize;
249 if (child.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink || (child.Dock != DockStyle.None && !(child is Button) && !(child is FlowLayoutPanel))) {
250 width = preferredsize.Width;
251 height = preferredsize.Height;
253 width = child.ExplicitBounds.Width;
254 height = child.ExplicitBounds.Height;
255 if (preferredsize.Width > width)
256 width = preferredsize.Width;
257 if (preferredsize.Height > height)
258 height = preferredsize.Height;
260 if (child.AutoSize && child is FlowLayoutPanel && child.Dock != DockStyle.None) {
261 switch (child.Dock) {
263 case DockStyle.Right:
264 if (preferredsize.Width < child.ExplicitBounds.Width && preferredsize.Height < child.Parent.PaddingClientRectangle.Height)
265 width = preferredsize.Width;
268 case DockStyle.Bottom:
269 if (preferredsize.Height < child.ExplicitBounds.Height && preferredsize.Width < child.Parent.PaddingClientRectangle.Width)
270 height = preferredsize.Height;
275 if (width < child.MinimumSize.Width)
276 width = child.MinimumSize.Width;
278 if (height < child.MinimumSize.Height)
279 height = child.MinimumSize.Height;
281 if (child.MaximumSize.Width != 0 && width > child.MaximumSize.Width)
282 width = child.MaximumSize.Width;
284 if (child.MaximumSize.Height != 0 && height > child.MaximumSize.Height)
285 height = child.MaximumSize.Height;
287 return new Size (width, height);