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.ClientRectangle;
41 // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
42 for (int i = controls.Length - 1; i >= 0; i--) {
43 Control child = controls[i];
45 if (!child.VisibleInternal
46 || child.ControlLayoutType == Control.LayoutType.Anchor)
49 // MdiClient never fills the whole area like other controls, have to do it later
50 if (child is MdiClient) {
51 mdi = (MdiClient)child;
61 child.SetImplicitBounds (space.Left, space.Y, child.Width, space.Height);
62 space.X += child.Width;
63 space.Width -= child.Width;
67 child.SetImplicitBounds (space.Left, space.Y, space.Width, child.Height);
68 space.Y += child.Height;
69 space.Height -= child.Height;
73 child.SetImplicitBounds (space.Right - child.Width, space.Y, child.Width, space.Height);
74 space.Width -= child.Width;
77 case DockStyle.Bottom:
78 child.SetImplicitBounds (space.Left, space.Bottom - child.Height, space.Width, child.Height);
79 space.Height -= child.Height;
83 child.SetImplicitBounds (space.Left, space.Top, space.Width, space.Height);
88 // MdiClient gets whatever space is left
90 mdi.SetImplicitBounds (space.Left, space.Top, space.Width, space.Height);
93 void LayoutAnchoredChildren (Control parent, Control[] controls)
95 Rectangle space = parent.ClientRectangle;
97 for (int i = 0; i < controls.Length; i++) {
103 Control child = controls[i];
105 if (!child.VisibleInternal
106 || child.ControlLayoutType == Control.LayoutType.Dock)
109 AnchorStyles anchor = child.Anchor;
115 Size preferredsize = child.PreferredSize;
116 width = preferredsize.Width;
117 height = preferredsize.Height;
120 height = child.Height;
123 if ((anchor & AnchorStyles.Right) != 0) {
124 if ((anchor & AnchorStyles.Left) != 0)
125 width = space.Width - child.dist_right - left;
127 left = space.Width - child.dist_right - width;
129 else if ((anchor & AnchorStyles.Left) == 0) {
130 // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
131 // This calculates from scratch every time:
132 left = left + (space.Width - (left + width + child.dist_right)) / 2;
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;
154 child.SetBounds (left, top, width, height);
158 public override bool Layout (object container, LayoutEventArgs args)
160 Control parent = container as Control;
162 Control[] controls = parent.Controls.GetAllControls ();
164 LayoutDockedChildren (parent, controls);
165 LayoutAnchoredChildren (parent, controls);