[jit] Enable partial generic sharing when not using AOT as an experiment.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.Layout / DefaultLayout.cs
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 (monkey@jpobst.com)
27 //      Stefan Noack (noackstefan@googlemail.com)
28 //
29
30 using System;
31 using System.Drawing;
32
33 namespace System.Windows.Forms.Layout
34 {
35         class DefaultLayout : LayoutEngine
36         {
37                 void LayoutDockedChildren (Control parent, Control[] controls)
38                 {
39                         Rectangle space = parent.DisplayRectangle;
40                         MdiClient mdi = null;
41                         
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;
46
47                                 if (child.AutoSize)
48                                         child_size = GetPreferredControlSize (child);
49
50                                 if (!child.VisibleInternal
51                                     || child.ControlLayoutType == Control.LayoutType.Anchor)
52                                         continue;
53
54                                 // MdiClient never fills the whole area like other controls, have to do it later
55                                 if (child is MdiClient) {
56                                         mdi = (MdiClient)child;
57                                         continue;
58                                 }
59                                 
60                                 switch (child.Dock) {
61                                 case DockStyle.None:
62                                         // Do nothing
63                                         break;
64
65                                 case DockStyle.Left:
66                                         child.SetBoundsInternal (space.Left, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
67                                         space.X += child.Width;
68                                         space.Width -= child.Width;
69                                         break;
70
71                                 case DockStyle.Top:
72                                         child.SetBoundsInternal (space.Left, space.Y, space.Width, child_size.Height, BoundsSpecified.None);
73                                         space.Y += child.Height;
74                                         space.Height -= child.Height;
75                                         break;
76
77                                 case DockStyle.Right:
78                                         child.SetBoundsInternal (space.Right - child_size.Width, space.Y, child_size.Width, space.Height, BoundsSpecified.None);
79                                         space.Width -= child.Width;
80                                         break;
81
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;
85                                         break;
86                                         
87                                 case DockStyle.Fill:
88                                         child.SetBoundsInternal (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
89                                         break;
90                                 }
91                         }
92
93                         // MdiClient gets whatever space is left
94                         if (mdi != null)
95                                 mdi.SetBoundsInternal (space.Left, space.Top, space.Width, space.Height, BoundsSpecified.None);
96                 }
97
98                 void LayoutAnchoredChildren (Control parent, Control[] controls)
99                 {
100                         Rectangle space = parent.ClientRectangle;
101
102                         for (int i = 0; i < controls.Length; i++) {
103                                 int left;
104                                 int top;
105                                 int width;
106                                 int height;
107
108                                 Control child = controls[i];
109
110                                 if (!child.VisibleInternal
111                                     || child.ControlLayoutType == Control.LayoutType.Dock)
112                                         continue;
113
114                                 AnchorStyles anchor = child.Anchor;
115
116                                 left = child.Left;
117                                 top = child.Top;
118                                 
119                                 width = child.Width;
120                                 height = child.Height;
121
122                                 if ((anchor & AnchorStyles.Right) != 0) {
123                                         if ((anchor & AnchorStyles.Left) != 0)
124                                                 width = space.Width - child.dist_right - left;
125                                         else
126                                                 left = space.Width - child.dist_right - width;
127                                 }
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);
133                                 }
134
135                                 if ((anchor & AnchorStyles.Bottom) != 0) {
136                                         if ((anchor & AnchorStyles.Top) != 0)
137                                                 height = space.Height - child.dist_bottom - top;
138                                         else
139                                                 top = space.Height - child.dist_bottom - height;
140                                 }
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);
146                                 }
147
148                                 // Sanity
149                                 if (width < 0)
150                                         width = 0;
151
152                                 if (height < 0)
153                                         height = 0;
154
155                                 child.SetBoundsInternal (left, top, width, height, BoundsSpecified.None);
156                         }
157                 }
158                 
159                 void LayoutAutoSizedChildren (Control parent, Control[] controls)
160                 {
161                         for (int i = 0; i < controls.Length; i++) {
162                                 int left;
163                                 int top;
164
165                                 Control child = controls[i];
166                                 if (!child.VisibleInternal
167                                     || child.ControlLayoutType == Control.LayoutType.Dock
168                                     || !child.AutoSize)
169                                         continue;
170
171                                 AnchorStyles anchor = child.Anchor;
172                                 left = child.Left;
173                                 top = child.Top;
174                                 
175                                 Size preferredsize = GetPreferredControlSize (child);
176
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;
181
182                                 child.SetBoundsInternal (left, top, preferredsize.Width, preferredsize.Height, BoundsSpecified.None);
183                         }
184                 }
185
186                 void LayoutAutoSizeContainer (Control container)
187                 {
188                         int left;
189                         int top;
190                         int width;
191                         int height;
192
193                         if (!container.VisibleInternal || container.ControlLayoutType == Control.LayoutType.Dock || !container.AutoSize)
194                                 return;
195
196                         left = container.Left;
197                         top = container.Top;
198
199                         Size preferredsize = container.PreferredSize;
200
201                         if (container.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink) {
202                                 width = preferredsize.Width;
203                                 height = preferredsize.Height;
204                         } else {
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;
211                         }
212
213                         // Sanity
214                         if (width < container.MinimumSize.Width)
215                                 width = container.MinimumSize.Width;
216
217                         if (height < container.MinimumSize.Height)
218                                 height = container.MinimumSize.Height;
219
220                         if (container.MaximumSize.Width != 0 && width > container.MaximumSize.Width)
221                                 width = container.MaximumSize.Width;
222
223                         if (container.MaximumSize.Height != 0 && height > container.MaximumSize.Height)
224                                 height = container.MaximumSize.Height;
225
226                         container.SetBoundsInternal (left, top, width, height, BoundsSpecified.None);
227                 }
228
229                 public override bool Layout (object container, LayoutEventArgs args)
230                 {
231                         Control parent = container as Control;
232
233                         Control[] controls = parent.Controls.GetAllControls ();
234
235                         LayoutDockedChildren (parent, controls);
236                         LayoutAnchoredChildren (parent, controls);
237                         LayoutAutoSizedChildren (parent, controls);
238                         if (parent is Form) LayoutAutoSizeContainer (parent);
239
240                         return false;
241                 }
242
243                 private Size GetPreferredControlSize (Control child)
244                 {
245                         int width;
246                         int height;
247                         Size preferredsize = child.PreferredSize;
248
249                         if (child.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink || (child.Dock != DockStyle.None && !(child is Button) && !(child is FlowLayoutPanel))) {
250                                 width = preferredsize.Width;
251                                 height = preferredsize.Height;
252                         } else {
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;
259                         }
260                         if (child.AutoSize && child is FlowLayoutPanel && child.Dock != DockStyle.None) {
261                                 switch (child.Dock) {
262                                 case DockStyle.Left:
263                                 case DockStyle.Right:
264                                         if (preferredsize.Width < child.ExplicitBounds.Width && preferredsize.Height < child.Parent.PaddingClientRectangle.Height)
265                                                 width = preferredsize.Width;
266                                         break;
267                                 case DockStyle.Top:
268                                 case DockStyle.Bottom:
269                                         if (preferredsize.Height < child.ExplicitBounds.Height && preferredsize.Width < child.Parent.PaddingClientRectangle.Width)
270                                                 height = preferredsize.Height;
271                                         break;
272                                 }
273                         }
274                         // Sanity
275                         if (width < child.MinimumSize.Width)
276                                 width = child.MinimumSize.Width;
277
278                         if (height < child.MinimumSize.Height)
279                                 height = child.MinimumSize.Height;
280
281                         if (child.MaximumSize.Width != 0 && width > child.MaximumSize.Width)
282                                 width = child.MaximumSize.Width;
283
284                         if (child.MaximumSize.Height != 0 && height > child.MaximumSize.Height)
285                                 height = child.MaximumSize.Height;
286                                 
287                         return new Size (width, height);
288                 }
289         }
290 }