forked from koush/WindowlessControls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DockPanel.cs
executable file
·216 lines (194 loc) · 8.34 KB
/
DockPanel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//////////////////////////////////////////////////////////////
// Koushik Dutta - 9/1/2007
//////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace WindowlessControls
{
public class DockLayout
{
public LayoutMeasurement LayoutMeasurement;
public DockStyle DockStyle;
public double Measurement
{
get
{
return LayoutMeasurement.Measurement;
}
set
{
LayoutMeasurement.Measurement = value;
}
}
public LayoutUnit Unit
{
get
{
return LayoutMeasurement.Unit;
}
set
{
LayoutMeasurement.Unit = value;
}
}
public DockLayout(LayoutMeasurement measurement, DockStyle dockStyle)
{
LayoutMeasurement = measurement;
DockStyle = dockStyle;
}
}
public class DockPanel : WindowlessPanel
{
DependencyPropertyStorage<bool> myCapDimension;
public DockPanel()
{
myCapDimension = new DependencyPropertyStorage<bool>(this, false, new DependencyPropertyChangedEvent(CapDimensionChanged));
}
void CapDimensionChanged(object sender, DependencyPropertyEventArgs e)
{
Remeasure();
}
public bool CapDimension
{
get
{
return myCapDimension;
}
set
{
myCapDimension.Value = value;
}
}
public override bool MeasureUnpadded(Size bounds, bool boundsChange)
{
ClientWidth = 0;
ClientHeight = 0;
if (Controls.Count == 0)
return false;
int newWidth = bounds.Width;
int newHeight = bounds.Height;
Rectangle r = new Rectangle(Margin.Left, Margin.Top, newWidth, newHeight);
bool layoutChange = false;
// lay everything out first
int maxLeft = Margin.Left;
int maxRight = Margin.Left;
int maxTop = Margin.Top;
int maxBottom = Margin.Top;
foreach (IWindowlessControl control in Controls)
{
if (!control.Visible)
continue;
Rectangle oldRect = new Rectangle(control.Left, control.Top, control.Width, control.Height);
DockLayout layout = control.Layout as DockLayout;
if ((layout.Measurement > 1 || layout.Measurement < 0) && layout.Unit == LayoutUnit.Star)
{
throw new Exception("Measurement invalid.");
}
// just do set the value for both width and height, only one value is really used.
int layoutWidth = (int)Math.Round(layout.Measurement, 0);
int layoutHeight = layoutWidth;
int newRectWidth = r.Width;
int newRectHeight = r.Height;
switch (layout.DockStyle)
{
case DockStyle.Top:
case DockStyle.Bottom:
if (layout.Unit == LayoutUnit.Star)
{
if (layout.Measurement == 0)
layoutHeight = r.Height;
else
layoutHeight = (int)Math.Round(r.Height * layout.Measurement, 0);
}
control.Measure(new Size(r.Width, layoutHeight));
if (layout.Unit == LayoutUnit.Star && layout.Measurement == 0)
layoutHeight = control.Height;
if (bounds.Height == Int32.MaxValue)
newRectHeight = Int32.MaxValue;
else
newRectHeight = r.Height - layoutHeight;
break;
case DockStyle.Left:
case DockStyle.Right:
if (layout.Unit == LayoutUnit.Star)
{
if (layout.Measurement == 0)
layoutWidth = r.Width;
else
layoutWidth = (int)Math.Round(r.Width * layout.Measurement, 0);
}
control.Measure(new Size(layoutWidth, r.Height));
if (layout.Unit == LayoutUnit.Star && layout.Measurement == 0)
layoutWidth = control.Width;
if (bounds.Width == Int32.MaxValue)
newRectWidth = Int32.MaxValue;
else
newRectWidth = r.Width - layoutWidth;
break;
default:
throw new ArgumentException("Invalid DockStyle.");
}
if (bounds.Width == Int32.MaxValue && myCapDimension && (layout.DockStyle == DockStyle.Top || layout.DockStyle == DockStyle.Bottom))
r.Width = bounds.Width = control.Width;
if (bounds.Height == Int32.MaxValue && myCapDimension && (layout.DockStyle == DockStyle.Left || layout.DockStyle == DockStyle.Right))
r.Height = bounds.Height = control.Height;
switch (layout.DockStyle)
{
case DockStyle.Left:
LayoutControl(control, new Rectangle(r.Left, r.Top, layoutWidth, r.Height));
r = new Rectangle(r.Left + layoutWidth, r.Top, newRectWidth, r.Height);
break;
case DockStyle.Top:
LayoutControl(control, new Rectangle(r.Left, r.Top, r.Width, layoutHeight));
r = new Rectangle(r.Left, r.Top + layoutHeight, r.Width, newRectHeight);
break;
case DockStyle.Right:
LayoutControl(control, new Rectangle(r.Right - layoutWidth, r.Top, layoutWidth, r.Height));
r = new Rectangle(r.Left, r.Top, newRectWidth, r.Height);
break;
case DockStyle.Bottom:
LayoutControl(control, new Rectangle(r.Left, r.Bottom - layoutHeight, r.Width, layoutHeight));
r = new Rectangle(r.Left, r.Top, r.Width, newRectHeight);
break;
}
Rectangle newRect = new Rectangle(control.Left, control.Top, control.Width, control.Height);
layoutChange |= oldRect != newRect;
maxLeft = Math.Max(maxLeft, newRect.Left);
maxRight = Math.Max(maxRight, newRect.Right);
maxTop = Math.Max(maxTop, newRect.Top);
maxBottom = Math.Max(maxBottom, newRect.Bottom);
if (bounds.Width == Int32.MaxValue)
{
if (newWidth == Int32.MaxValue)
newWidth = control.Left + control.Width;
else
newWidth = Math.Max(newWidth, control.Left + control.Width);
}
if (bounds.Height == Int32.MaxValue)
{
if (newHeight == Int32.MaxValue)
newHeight = control.Top + control.Height;
else
newHeight = Math.Max(newHeight, control.Top + control.Height);
}
}
if (HorizontalAlignment == HorizontalAlignment.Stretch)
ClientWidth = newWidth;
else
{
ClientWidth = maxRight - Margin.Left;
}
if (VerticalAlignment == VerticalAlignment.Stretch)
ClientHeight = newHeight;
else
{
ClientHeight = maxBottom - Margin.Top;
}
return layoutChange;
}
}
}