using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace GForms { public class GControl { #region Variables internal GControl parent = null; private GControlCollection controls; #endregion #region Events public event GEventHandler Changed; public void RaiseChanged() { if (this.Changed != null) this.Changed(this); } #endregion #region Constructor public GControl() { this.controls = new GControlCollection(this); this.Visible = true; this.Color = SystemColors.WindowText; this.Background = null; this.Dock = DockStyle.None; this.Font = new Font("Microsoft Sans Serif", 8.25f); } #endregion #region Drawing public virtual void Draw(Graphics g, Rectangle bounds) { // Draw the child controls foreach (GControl c in this.Controls) { if (c.Visible) { Rectangle b = c.Bounds; c.DrawBackground(g, b); c.Draw(g, b); } } } public virtual void DrawBackground(Graphics g, Rectangle bounds) { // fill in the background if (this.Background != null) g.FillRectangle(this.Background, bounds); } #endregion #region Mouse Events public virtual bool OnMouseDown(Point location, MouseButtons buttons) { foreach (GControl c in this.Controls) { if (c.Bounds.Contains(location)) { if (c.OnMouseDown(location, buttons)) return true; } } return false; } public virtual bool OnMouseUp(Point location, MouseButtons buttons) { foreach (GControl c in this.Controls) { if (c.Bounds.Contains(location)) { if (c.OnMouseUp(location, buttons)) return true; } } return false; } public virtual bool OnMouseMove(Point location, MouseButtons buttons) { foreach (GControl c in this.Controls) { if (c.Bounds.Contains(location)) { if (c.OnMouseMove(location, buttons)) return true; } } return false; } #endregion #region Properties public GControl Parent { get { return parent; } } public GControlCollection Controls { get { return controls; } } public Rectangle Bounds { get { Rectangle parentBounds = (this.Parent != null ? this.Parent.Bounds : Rectangle.Empty); Rectangle b = new Rectangle(parentBounds.Left + this.Left, parentBounds.Top + this.Top, this.Width, this.Height); if (this.Parent != null) { switch (this.Dock) { case DockStyle.Bottom: b = new Rectangle(parentBounds.Left, parentBounds.Bottom - this.Height, parentBounds.Width, this.Height); break; case DockStyle.Right: b = new Rectangle(parentBounds.Left - this.Width, parentBounds.Top, this.Width, parentBounds.Height); break; case DockStyle.Top: b = new Rectangle(parentBounds.Left, parentBounds.Top, parentBounds.Width, this.Height); break; case DockStyle.Left: b = new Rectangle(parentBounds.Left, parentBounds.Top, this.Width, parentBounds.Height); break; } } return b; } } #endregion #region CSS-Style Properties private int left, top, width, height; private bool visible; private Color color; private Brush background; private DockStyle dock; private Font font; // position + size public int Left { get { return left; } set { left = value; RaiseChanged(); } } public int Top { get { return top; } set { top = value; RaiseChanged(); } } public int Width { get { return width; } set { width = value; RaiseChanged(); } } public int Height { get { return height; } set { height = value; RaiseChanged(); } } // visibility public bool Visible { get { return visible; } set { visible = value; RaiseChanged(); } } // color public Color Color { get { return color; } set { color = value; RaiseChanged(); } } public Brush Background { get { return background; } set { background = value; RaiseChanged(); } } // docking public DockStyle Dock { get { return dock; } set { dock = value; RaiseChanged(); } } // font public Font Font { get { return font; } set { font = value; RaiseChanged(); } } #endregion } public class GControlCollection : IList { private GControl parent = null; private List innerList = new List(); public GControlCollection(GControl parent) { this.parent = parent; } public bool IsReadOnly { get { return false; } } IEnumerator IEnumerable.GetEnumerator() { return innerList.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return innerList.GetEnumerator(); } public void Clear() { foreach (GControl c in innerList) c.parent = null; innerList.Clear(); } public void CopyTo(GControl[] array) { innerList.CopyTo(array); } public void CopyTo(GControl[] array, int arrayIndex) { innerList.CopyTo(array, arrayIndex); } public void Add(GControl control) { control.parent = parent; if (parent != null) control.Changed += (GControl sender) => parent.RaiseChanged(); innerList.Add(control); } public bool Remove(GControl control) { if (innerList.Remove(control)) { control.parent = null; return true; } return false; } public void RemoveAt(int index) { if (index < innerList.Count) { innerList[index].parent = null; innerList.RemoveAt(index); } else { throw new IndexOutOfRangeException(); } } public int Count { get { return innerList.Count; } } public int IndexOf(GControl control) { return innerList.IndexOf(control); } public bool Contains(GControl control) { return this.IndexOf(control) > -1; } public GControl this[int index] { get { return innerList[index]; } set { innerList[index] = value; } } public void Insert(int index, GControl control) { innerList.Insert(index, control); control.parent = parent; } } public delegate void GEventHandler(GControl sender); }