QConstrainedLayout.cs :  » Game » RealmForge » LayoutManager » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » LayoutManager » QConstrainedLayout.cs

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
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.