using System;
using ControlSystem.Windows.Forms.Control;
using RectangleSystem.Drawing.Rectangle;
namespace LayoutManager{
public class GridLayout : ILayoutManager {
private int curRow;
private int curCol;
private Control[][] controls;
private Control con;
public GridLayout(int rows, int cols) {
rows = (rows > 0)?rows:1;
cols = (cols > 0)?cols:1;
controls = new Control[rows][];
for (int i = 0; i < rows; ++i) {
controls[i] = new Control[cols];
}
curCol = 0;
curRow = 0;
}
private int Rows {
get {
return controls.Length;
}
}
private int Cols {
get {
return controls[0].Length;
}
}
public void GoTo(int row, int col) {
if (row >= 0 && row < Rows) {
curRow = row;
}
if (col >= 0 && col < Cols) {
curCol = col;
}
}
#region ILayoutMgr Members
public Control Control {
get {
return con;
}
set {
if (value != null && value != con) {
con = value;
}
}
}
public void DoLayout() {
if (Control != null) {
int rowSpace = Control.ClientSize.Height / Rows;
int colSpace = Control.ClientSize.Width / Cols;
for (int r = 0; r < Rows; ++r) {
for (int c = 0; c < Cols; ++c) {
if (controls[r][c] != null) {
controls[r][c].Bounds = new Rectangle(c * colSpace, r * rowSpace, colSpace, rowSpace);
}
}
}
}
}
public void ControlAdded(Control c) {
if (Control != null && curRow < controls.Length) {
if (controls[curRow][curCol] != null) {
Control.Controls.Remove(controls[curRow][curCol]);
}
controls[curRow][curCol] = c;
curCol += 1;
if (curCol >= Cols) {
curCol = 0;
curRow = curRow + 1;
}
}
}
public void ControlRemoved(Control c) {
if (Control != null) {
for (int row = 0; row < Rows; ++row) {
for (int col = 0; col < Cols; ++col) {
if (controls[row][col] == c) {
controls[row][col] = null;
}
}
}
}
}
#endregion
}
}
|