ui/wpf/UIcore/Container.cs

Thu, 21 Jan 2016 20:40:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 21 Jan 2016 20:40:14 +0100
changeset 104
3efe0210e27e
parent 101
1c943d43fa81
child 135
b9dc9cdfa23a
permissions
-rw-r--r--

added grid spacing (WPF)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace UI
{
    public interface Container
    {
        Layout Layout { get; set; }
        
        void Add(UIElement control, bool fill);
    }

    public class Layout
    {
        public bool? Fill { get; set; }
        public bool Hexpand { get; set; }
        public bool Vexpand { get; set; }
        public bool NewLine { get; set; }

        public Layout()
        {
            Reset();
        }

        public bool IsFill(bool fill)
        {
            if (Fill != null)
            {
                
                return (bool)Fill;
            }
            return fill;
        }

        public void Reset()
        {
            Fill = null;
            Hexpand = false;
            Vexpand = false;
            NewLine = false;
        }
    }

    public enum BoxOrientation
    {
        VERTICAL,
        HORIZONTAL
    }

    public class BoxContainer : Grid, Container
    {
        public Layout Layout { get; set; }
        
        private BoxOrientation Orientation;

        private int x = 0;
        private int y = 0;

        private bool filled = false;

        public BoxContainer(BoxOrientation orientation) : base()
        {
            Layout = new Layout();
            
            Orientation = orientation;
            if(Orientation == BoxOrientation.HORIZONTAL)
            {
                RowDefinition row = new RowDefinition();
                row.Height = new GridLength(1, GridUnitType.Star);
                RowDefinitions.Add(row);
            }
            else
            {
                ColumnDefinition col = new ColumnDefinition();
                col.Width = new GridLength(1, GridUnitType.Star);
                ColumnDefinitions.Add(col);
            }
        }

        public BoxContainer(Container parent, BoxOrientation orientation) : this(orientation)
        {
            parent.Add(this, true);
        }
        
        public void Add(UIElement control, bool fill)
        {
            fill = Layout.IsFill(fill);
            
            if(Orientation == BoxOrientation.HORIZONTAL)
            {
                ColumnDefinition col = new ColumnDefinition();
                if(filled && fill)
                {
                    fill = false;
                    Console.WriteLine("BoxContainer can only contain one filled control");
                }
                if(fill)
                {
                    col.Width = new GridLength(1, GridUnitType.Star);
                    filled = true;
                }
                else
                {
                    col.Width = GridLength.Auto;
                }
                ColumnDefinitions.Add(col);
            }
            else
            {
                RowDefinition row = new RowDefinition();
                if (filled && fill)
                {
                    fill = false;
                    Console.WriteLine("BoxContainer can only contain one filled control");
                }
                if(fill)
                {
                    row.Height = new GridLength(1, GridUnitType.Star);
                    filled = true;
                }
                else
                {
                    row.Height = GridLength.Auto;
                }
                RowDefinitions.Add(row);
            }

            Grid.SetColumn(control, x);
            Grid.SetRow(control, y);
            Children.Add(control);

            if(Orientation == BoxOrientation.HORIZONTAL)
            {
                x++;
            }
            else
            {
                y++;
            }

            Layout.Reset();
        }

        public static BoxContainer CreateBoxContainer(Container parent, BoxOrientation orientation)
        {
            return Application.GetInstance().Exec<BoxContainer>(() => new BoxContainer(parent, orientation));
        }
    }
    
    public class GridContainer : Grid, Container
    {
        public Layout Layout { get; set; }

        private int X = 0;
        private int Y = 0;
        private int CurrentWidth = 0;
        private int CurrentHeight = 0;

        private int ColSpacing;
        private int RowSpacing;

        public GridContainer(Container parent, int margin, int colspacing, int rowspacing) : base()
        {
            Layout = new Layout();

            Margin = new Thickness(margin);
            ColSpacing = colspacing;
            RowSpacing = rowspacing;

            parent.Add(this, true);
        }

        public void Add(UIElement control, bool fill)
        {
            if(Layout.NewLine)
            {
                X = 0;
                Y++;
            }

            ColumnDefinition col;
            RowDefinition row;
            bool getcol = false;
            if(X >= CurrentWidth)
            {
                if (ColSpacing > 0 && X != 0)
                {
                    ColumnDefinition spaceCol = new ColumnDefinition();
                    spaceCol.Width = new GridLength(ColSpacing, GridUnitType.Pixel);
                    ColumnDefinitions.Add(spaceCol);
                    X++;
                }

                col = new ColumnDefinition();
                col.Width = GridLength.Auto;
                ColumnDefinitions.Add(col);

                CurrentWidth = X + 1;
            }
            else
            {
                if (ColSpacing > 0 && X % 2 > 0)
                {
                    X++;
                }
                col = ColumnDefinitions.ElementAt(X);
            }

            if(getcol)
            {
                col = ColumnDefinitions.ElementAt(X);
            }

            if (Y >= CurrentHeight)
            {
                if (RowSpacing > 0 && Y != 0)
                {
                    RowDefinition spaceRow = new RowDefinition();
                    spaceRow.Height = new GridLength(RowSpacing, GridUnitType.Pixel);
                    RowDefinitions.Add(spaceRow);
                    Y++;
                }

                row = new RowDefinition();
                row.Height = GridLength.Auto;
                RowDefinitions.Add(row);
                CurrentHeight = Y + 1;
            }
            else
            {
                row = RowDefinitions.ElementAt(Y);
            }

            if(Layout.Hexpand)
            {
                col.Width = new GridLength(1, GridUnitType.Star);
            }
            if(Layout.Vexpand)
            {
                row.Height = new GridLength(1, GridUnitType.Star);
            }

            Grid.SetColumn(control, X);
            Grid.SetRow(control, Y);
            Children.Add(control);

            Layout.Reset();
            X++;
        }

        public static GridContainer CreateGridContainer(Container parent, int margin, int colspacing, int rowspacing)
        {
            return Application.GetInstance().Exec<GridContainer>(() => new GridContainer(parent, margin, colspacing, rowspacing));
        }
    }
}

mercurial