Mark tests as not working under TARGET_JVM
[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 //
28
29 using System;
30 using System.Drawing;
31
32 namespace System.Windows.Forms.Layout
33 {
34         class DefaultLayout : LayoutEngine
35         {
36                 void LayoutDockedChildren (Control parent, Control[] controls)
37                 {
38                         Rectangle space = parent.ClientRectangle;
39                         MdiClient mdi = null;
40                         
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];
44
45                                 if (!child.VisibleInternal
46                                     || child.ControlLayoutType == Control.LayoutType.Anchor)
47                                         continue;
48
49                                 // MdiClient never fills the whole area like other controls, have to do it later
50                                 if (child is MdiClient) {
51                                         mdi = (MdiClient)child;
52                                         continue;
53                                 }
54                                 
55                                 switch (child.Dock) {
56                                 case DockStyle.None:
57                                         // Do nothing
58                                         break;
59
60                                 case DockStyle.Left:
61                                         child.SetImplicitBounds (space.Left, space.Y, child.Width, space.Height);
62                                         space.X += child.Width;
63                                         space.Width -= child.Width;
64                                         break;
65
66                                 case DockStyle.Top:
67                                         child.SetImplicitBounds (space.Left, space.Y, space.Width, child.Height);
68                                         space.Y += child.Height;
69                                         space.Height -= child.Height;
70                                         break;
71
72                                 case DockStyle.Right:
73                                         child.SetImplicitBounds (space.Right - child.Width, space.Y, child.Width, space.Height);
74                                         space.Width -= child.Width;
75                                         break;
76
77                                 case DockStyle.Bottom:
78                                         child.SetImplicitBounds (space.Left, space.Bottom - child.Height, space.Width, child.Height);
79                                         space.Height -= child.Height;
80                                         break;
81                                         
82                                 case DockStyle.Fill:
83                                         child.SetImplicitBounds (space.Left, space.Top, space.Width, space.Height);
84                                         break;
85                                 }
86                         }
87
88                         // MdiClient gets whatever space is left
89                         if (mdi != null)
90                                 mdi.SetImplicitBounds (space.Left, space.Top, space.Width, space.Height);
91                 }
92
93                 void LayoutAnchoredChildren (Control parent, Control[] controls)
94                 {
95                         Rectangle space = parent.ClientRectangle;
96
97                         for (int i = 0; i < controls.Length; i++) {
98                                 int left;
99                                 int top;
100                                 int width;
101                                 int height;
102
103                                 Control child = controls[i];
104
105                                 if (!child.VisibleInternal
106                                     || child.ControlLayoutType == Control.LayoutType.Dock)
107                                         continue;
108
109                                 AnchorStyles anchor = child.Anchor;
110
111                                 left = child.Left;
112                                 top = child.Top;
113                                 
114 #if NET_2_0
115                                 Size preferredsize = child.PreferredSize;
116                                 width = preferredsize.Width;
117                                 height = preferredsize.Height;
118 #else
119                                 width = child.Width;
120                                 height = child.Height;
121 #endif
122
123                                 if ((anchor & AnchorStyles.Right) != 0) {
124                                         if ((anchor & AnchorStyles.Left) != 0)
125                                                 width = space.Width - child.dist_right - left;
126                                         else
127                                                 left = space.Width - child.dist_right - width;
128                                 }
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;
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                                 }
146
147                                 // Sanity
148                                 if (width < 0)
149                                         width = 0;
150
151                                 if (height < 0)
152                                         height = 0;
153
154                                 child.SetBounds (left, top, width, height);
155                         }
156                 }
157
158                 public override bool Layout (object container, LayoutEventArgs args)
159                 {
160                         Control parent = container as Control;
161
162                         Control[] controls = parent.Controls.GetAllControls ();
163
164                         LayoutDockedChildren (parent, controls);
165                         LayoutAnchoredChildren (parent, controls);
166
167                         return false;
168                 }
169         }
170 }