using System;
using HashtableSystem.Collections.Hashtable;
using RectangleSystem.Drawing.Rectangle;
using ControlSystem.Windows.Forms.Control;
namespace LayoutManager{
public class RubberLayout : ILayoutManager {
Hashtable boundTable;
Control con;
#region private class RelBounds
/// <summary>
/// This class contains the relative Bound
/// for a control.
/// </summary>
private class RelBounds {
private double x, y, width, height;
public RelBounds(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public RelBounds(Rectangle controlBound, Rectangle panelBound) {
x = (double)controlBound.X / (double)panelBound.Width;
y = (double)controlBound.Y / (double)panelBound.Height;
width = (double)controlBound.Width / (double)panelBound.Width;
height = (double)controlBound.Height / (double)panelBound.Height;
}
public Rectangle GetAbsBounds(Rectangle panelBounds) {
return new Rectangle((int)(x * panelBounds.Width), (int)(y * panelBounds.Height),
(int)(width * panelBounds.Width), (int)(height * panelBounds.Height));
}
public override string ToString() {
return string.Format("x = {0}, y = {1}, width = {2}, height = {3}", x, y, width, height);
}
}
#endregion
public Control Control {
get {
return con;
}
set {
if (value != null && value != con) {
con = value;
ReInit();
}
}
}
public RubberLayout() {
boundTable = new Hashtable();
con = null;
}
/// <summary>
/// Call this method to take over the actual layout.
/// </summary>
public void ReInit() {
boundTable = new Hashtable();
foreach (Control c in Control.Controls) {
boundTable.Add(c, new RelBounds(c.Bounds, Control.Bounds));
}
}
public void DoLayout() {
RelBounds rb;
if (Control != null) {
foreach (Control c in Control.Controls) {
rb = (RelBounds)boundTable[c];
if (rb != null) {
c.Bounds = rb.GetAbsBounds(Control.Bounds);
}
}
}
}
public void ControlAdded(Control c) {
if (c != null) {
if (boundTable.Contains(c)) {
boundTable[c] = new RelBounds(c.Bounds, Control.Bounds);
} else {
boundTable.Add(c, new RelBounds(c.Bounds, Control.Bounds));
}
}
}
public void ControlRemoved(Control c) {
if (c != null) {
boundTable.Remove(c);
}
}
}
}
|