ui/wpf/UIcore/Container.cs

Sun, 01 Feb 2015 12:37:31 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Feb 2015 12:37:31 +0100
changeset 85
91f45354d1e2
parent 84
a56c2baa9429
child 101
1c943d43fa81
permissions
-rw-r--r--

added ui_layout_fill (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 Layout()
        {
            Fill = null;
        }

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

        public void Reset()
        {
            Fill = null;
        }
    }

    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 : Container
    {
        public Layout Layout { get; set; }
        
        public Grid Grid;

        public GridContainer(System.Windows.Controls.Grid grid)
        {
            Grid = grid;
        }

        public void Add(UIElement control, bool fill)
        {

        }
    }
}

mercurial