using System;
using ControlSystem.Windows.Forms.Control;
using RectangleSystem.Drawing.Rectangle;
using PointSystem.Drawing.Point;
using ArrayListSystem.Collections.ArrayList;
using ICollectionSystem.Collections.ICollection;
namespace LayoutManager{
public class FlowLayout : ILayoutManager {
private int hPad, vPad; // horizontal and vertical padding values
private Control con;
private bool tabOrder;
private ControlTabComparer comp = new ControlTabComparer();
private ArrayList sortedControls;
private class ControlTabComparer : System.Collections.IComparer {
public int Compare(Control x, Control y) {
if (x == null && y == null) { return 0; }
else if (x == null) { return -1; }
else if (y == null) { return 1; }
else { return x.TabIndex - y.TabIndex; }
}
public int Compare(object x, object y) {
return Compare((Control) x, (Control) y);
}
}
public FlowLayout(int horizontalPadding, int verticalPadding, bool tabOrder) {
hPad = horizontalPadding;
vPad = verticalPadding;
this.tabOrder = tabOrder;
sortedControls = new ArrayList();
}
public FlowLayout(int horizontalPadding, int verticalPadding) :
this(horizontalPadding, verticalPadding, false) { }
public FlowLayout() : this(0, 0, false) { }
// true ... controls will be sorted by TabIndex
// false .. controls will be sorted by insertion order
public bool TabOrder {
get {
return tabOrder;
}
set {
if (value != tabOrder) {
tabOrder = value;
DoLayout();
}
}
}
#region ILayoutMgr Members
public Control Control {
get {
return con;
}
set {
if (value != null && value != con) {
con = value;
}
}
}
public void DoLayout() {
int curTopLine, curLeftLine, curBottomLine;
if (Control != null && Control.Controls.Count > 0) {
curBottomLine = 0;
curTopLine = curBottomLine + vPad;
curLeftLine = hPad;
ICollection col = (TabOrder)?(ICollection)sortedControls:(ICollection)con.Controls;
foreach (Control c in col) {
if (curLeftLine + c.Size.Width > con.Width) {
curTopLine = curBottomLine + vPad;
curLeftLine = hPad;
}
c.Location = new Point(curLeftLine, curTopLine);
curLeftLine = curLeftLine + c.Size.Width + hPad;
if (curBottomLine < (curTopLine + c.Size.Height)) {
curBottomLine = (curTopLine + c.Size.Height);
}
}
}
}
public void ControlAdded(Control c) {
sortedControls.Add(c);
sortedControls.Sort(comp);
}
public void ControlRemoved(Control c) {
sortedControls.Remove(c);
}
#endregion
}
}
|