using HashtableSystem.Collections.Hashtable;
using System.Windows.Forms;
using RectangleSystem.Drawing.Rectangle;
namespace LayoutManager{
public enum HAlign:byte{ Left=0, Center=1, Right=2, Fill=3, Fix=0 };
public enum VAlign:byte{ Top=0, Center=1, Bottom=2, Fill=3, Fix=0 };
public enum Direction:byte {Horizontal, Vertical};
public class QConstrainedLayout : ILayoutManager {
Direction dir;
public int Margin = 10;
public int HBorderMargin = 10;
public int VBorderMargin = 10;
#region private class Constrain
private class Constrain {
public Constrain(HAlign h, VAlign v) {
this.h = h;
this.v = v;
}
public HAlign h;
public VAlign v;
}
#endregion
Hashtable constrains;
Control con;
public QConstrainedLayout(Direction dir) {
constrains = new Hashtable();
this.Dir = dir;
}
public Direction Dir {
get {
return dir;
}
set {
if (value == Direction.Horizontal || value == Direction.Vertical) {
dir = value;
}
}
}
public void SetAlign(object control, HAlign h, VAlign v) {
if (control != null &&
(h >= HAlign.Left && h <= HAlign.Fill) &&
(v >= VAlign.Top && v <= VAlign.Fill)) {
if (constrains.Contains(control)) {
constrains[control] = new Constrain(h, v);
} else {
constrains.Add(control, new Constrain(h, v));
}
}
}
private void HorizontalLayout() {
Constrain constr;
int maxSpace, space, curPos, step;
int varContAmount = con.Controls.Count;
maxSpace = con.ClientSize.Width;
for (int i = 0; i < con.Controls.Count; ++i) {
constr = (Constrain)constrains[con.Controls[i]];
if (constr != null && constr.h != HAlign.Fill) {
maxSpace -= con.Controls[i].Width;
--varContAmount;
}
}
maxSpace -= Margin * (con.Controls.Count ) * 2 + HBorderMargin * 2;//Changed
space = (varContAmount > 1)?(maxSpace / varContAmount):maxSpace;
curPos = HBorderMargin - Margin;//Changed
for (int i = 0; i < con.Controls.Count; ++i) {
curPos += Margin;//Changed
constr = (Constrain)constrains[con.Controls[i]];
if (constr == null) {
con.Controls[i].Bounds = new Rectangle(curPos, VBorderMargin, space, con.ClientSize.Height);
curPos += space;
} else {
step = (constr.h == HAlign.Fill)?space:con.Controls[i].Width;
if (constr.v == VAlign.Top) {
con.Controls[i].Bounds = new Rectangle(curPos, VBorderMargin, step, con.Controls[i].Height);
} else if (constr.v == VAlign.Center) {
con.Controls[i].Bounds = new Rectangle(
curPos,
con.ClientSize.Height/2 - con.Controls[i].Height/2,
step,
con.Controls[i].Height);
} else if (constr.v == VAlign.Bottom) {
con.Controls[i].Bounds = new Rectangle(
curPos,
-VBorderMargin + con.ClientSize.Height - con.Controls[i].Height,
step,
con.Controls[i].Height);
} else { // constr.v == VAlign.Fill
con.Controls[i].Bounds = new Rectangle(curPos, 0, step, con.ClientSize.Height);
}
curPos += step;
}
curPos += Margin;//Changed
}
}
private void VerticalLayout() {
Constrain constr;
int maxSpace, space, curPos, step;
int varContAmount = con.Controls.Count;
maxSpace = con.ClientSize.Height;
for (int i = 0; i < con.Controls.Count; ++i) {
constr = (Constrain)constrains[con.Controls[i]];
if (constr != null && constr.v != VAlign.Fill) {
maxSpace -= con.Controls[i].Height;
--varContAmount;
}
}
space = (varContAmount > 1)?(maxSpace / varContAmount):maxSpace;
curPos = 0;
for (int i = 0; i < con.Controls.Count; ++i) {
constr = (Constrain)constrains[con.Controls[i]];
if (constr == null) {
con.Controls[i].Bounds = new Rectangle(0, curPos, con.ClientSize.Width, space);
curPos += space;
} else {
step = (constr.v == VAlign.Fill)?space:con.Controls[i].Height;
if (constr.h == HAlign.Left) {
con.Controls[i].Bounds = new Rectangle(0, curPos, con.Controls[i].Width, step);
} else if (constr.h == HAlign.Center) {
con.Controls[i].Bounds = new Rectangle(
con.ClientSize.Width/2 - con.Controls[i].Width/2,
curPos,
con.Controls[i].Width,
step);
} else if (constr.h == HAlign.Right) {
con.Controls[i].Bounds = new Rectangle(
con.ClientSize.Width - con.Controls[i].Width,
curPos,
con.Controls[i].Width,
step);
} else { // constr.h == HAlign.Fill
con.Controls[i].Bounds = new Rectangle(0, curPos, con.ClientSize.Width, step);
}
curPos += step;
}
}
}
#region ILayoutMgr Members
public Control Control {
get {
return con;
}
set {
if (value != null && value != con) {
con = value;
}
}
}
public void DoLayout() {
if (Control.Controls.Count > 0) {
if (dir == Direction.Horizontal) {
HorizontalLayout();
} else { // dir == Direction.Vertical
VerticalLayout();
}
}
}
public void ControlAdded(System.Windows.Forms.Control c) {
// Nothing to do
}
public void ControlRemoved(System.Windows.Forms.Control c) {
if (c != null) {
constrains.Remove(c);
}
}
#endregion
}
}
|