ui/wpf/UIcore/Container.cs

Sun, 22 Sep 2024 18:16:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Sep 2024 18:16:14 +0200
branch
newapi
changeset 305
98470af75dcf
parent 138
d781436e2490
permissions
-rw-r--r--

simplfy macro usage in container.c (GTK)

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 String Label { 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;
            Label = null;
        }
    }

    public enum BoxOrientation
    {
        VERTICAL,
        HORIZONTAL
    }

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

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

        private bool filled = false;

        public BoxContainer(BoxOrientation orientation, int margin, int spacing) : base()
        {
            Layout = new Layout();
            Margin = new Thickness(margin);
            Spacing = spacing;
            
            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, int margin, int spacing) : this(orientation, margin, spacing)
        {
            parent.Add(this, true);
        }
        
        public void Add(UIElement control, bool fill)
        {
            fill = Layout.IsFill(fill);
            
            if(Orientation == BoxOrientation.HORIZONTAL)
            {
                if(Spacing > 0)
                {
                    ColumnDefinition spaceCol = new ColumnDefinition();
                    spaceCol.Width = new GridLength(Spacing, GridUnitType.Pixel);
                    ColumnDefinitions.Add(spaceCol);
                    x++;
                }

                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
            {
                if (Spacing > 0)
                {
                    RowDefinition spaceRow = new RowDefinition();
                    spaceRow.Height = new GridLength(Spacing, GridUnitType.Pixel);
                    RowDefinitions.Add(spaceRow);
                    y++;
                }

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

    public class ScrollViewerContainer : ScrollViewer, Container
    {
        public Layout Layout { get; set; }

        public ScrollViewerContainer(Container parent) : base()
        {
            Layout = new Layout();

            HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

            parent.Add(this, true);
        }

        public void Add(UIElement control, bool fill)
        {
            Content = control;
        }
    }

    public class TabViewContainer : TabControl, Container
    {
        public Layout Layout { get; set; }

        public TabViewContainer(Container parent) : base()
        {
            Layout = new Layout();
            parent.Add(this, true);
        }

        public void Add(UIElement control, bool fill)
        {
            TabItem tab = new TabItem();
            tab.Header = Layout.Label != null ? Layout.Label : "New Tab";
            Items.Add(tab);
            tab.Content = control;
            Layout.Reset();
        }
    }
}

mercurial