ui/wpf/UIcore/Container.cs

Sun, 22 Jan 2017 18:02:45 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Jan 2017 18:02:45 +0100
changeset 136
1df2fb3d079c
parent 135
b9dc9cdfa23a
child 138
d781436e2490
permissions
-rw-r--r--

adds gridwidth layout option (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 int GridWidth { 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;
            GridWidth = 1;
        }
    }

    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 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);
            }

            int gridwidth = Layout.GridWidth;
            if(gridwidth > 1)
            {
                gridwidth++;
            }

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

            Layout.Reset();
            X += gridwidth;
        }
    }
}

mercurial