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

namespace RyzStudio.Windows.Forms
{
    public class StackLayoutPanel : FlowLayoutPanel
    {
        public StackLayoutPanel() : base()
        {
            this.AutoScroll = true;
            this.FlowDirection = FlowDirection.TopDown;
            this.WrapContents = false;
        }

        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);

            //int w = this.ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth;
            int w = this.ClientRectangle.Width - 1;

            foreach (Control item in this.Controls)
            {
                if (item.Width != w)
                {
                    item.Width = w;
                }
            }
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);

            OnResize(null);
        }

        protected override void OnControlRemoved(ControlEventArgs e)
        {
            base.OnControlRemoved(e);

            OnResize(null);
        }

        public void AddControl(Control value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() =>
                {
                    value.Margin = new Padding(0, 3, 0, 3);

                    this.Controls.Add(value);
                }));
            }
            else
            {
                value.Margin = new Padding(0, 3, 0, 3);

                this.Controls.Add(value);
            }
        }

    }
}