using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using TD.SandDock.Rendering;

namespace TD.SandDock
{
	[TypeConverter(typeof(ControlLayoutSystemConverter))]
	public class ControlLayoutSystem : LayoutSystemBase
	{
		internal new delegate void a(DockControl A_0, DockControl A_1);

		public class DockControlCollection : CollectionBase
		{
			private ControlLayoutSystem m_a = null;

			private bool m_b = false;

			private bool c = false;

			public DockControl this[int index] => (DockControl)base.List[index];

			internal DockControlCollection(ControlLayoutSystem A_0)
			{
				this.m_a = A_0;
			}

			public void SetChildIndex(DockControl control, int index)
			{
				if (control == null)
				{
					throw new ArgumentNullException();
				}
				if (!Contains(control))
				{
					throw new ArgumentOutOfRangeException();
				}
				if (IndexOf(control) < index)
				{
					index--;
				}
				c = true;
				base.List.Remove(control);
				base.List.Insert(index, control);
				c = false;
				this.m_a.f();
			}

			protected override void OnClear()
			{
				base.OnClear();
				DocumentContainer documentContainer = null;
				if (this.m_a != null)
				{
					documentContainer = this.m_a.DockContainer as DocumentContainer;
				}
				IEnumerator enumerator = GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						DockControl dockControl = (DockControl)enumerator.Current;
						dockControl.Enter -= b;
						dockControl.Leave -= a;
						dockControl.b = null;
						documentContainer?.b(dockControl);
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
			}

			private void b(object A_0, EventArgs A_1)
			{
				this.m_a.OnControlEnter((DockControl)A_0);
				if (this.m_a.IsInContainer && this.m_a.DockContainer.Manager != null)
				{
					this.m_a.DockContainer.Manager.c((DockControl)A_0);
				}
			}

			private void a(object A_0, EventArgs A_1)
			{
				this.m_a.OnControlLeave((DockControl)A_0);
			}

			protected override void OnClearComplete()
			{
				base.OnClearComplete();
				this.m_a.SelectedControl = null;
				this.m_a.f();
			}

			protected override void OnInsertComplete(int index, object value)
			{
				base.OnInsertComplete(index, value);
				if (!c)
				{
					DockControl dockControl = (DockControl)value;
					dockControl.b = this.m_a;
					if (this.m_a.IsInContainer && this.m_a.DockContainer.Manager != null && this.m_a.DockContainer.Manager != dockControl.Manager)
					{
						dockControl.Manager = this.m_a.DockContainer.Manager;
					}
					if (this.m_a.IsInContainer)
					{
						dockControl.a(this.m_a.DockContainer);
					}
					if (this.m_a.m_i == null)
					{
						this.m_a.SelectedControl = dockControl;
					}
					dockControl.Enter += b;
					dockControl.Leave += a;
					if (this.m_a != null && this.m_a.DockContainer is DocumentContainer)
					{
						((DocumentContainer)this.m_a.DockContainer).d(dockControl);
					}
					if (!this.m_b)
					{
						this.m_a.f();
					}
				}
			}

			protected override void OnRemoveComplete(int index, object value)
			{
				base.OnRemoveComplete(index, value);
				if (c)
				{
					return;
				}
				DockControl dockControl = (DockControl)value;
				if (this.m_a.IsInContainer && dockControl.Parent == this.m_a.DockContainer)
				{
					TD.SandDock.g.a((Control)dockControl);
				}
				dockControl.b = null;
				dockControl.Enter -= b;
				dockControl.Leave -= a;
				if (this.m_a.m_i == value)
				{
					if (this.m_a.c.Count == 0)
					{
						this.m_a.SelectedControl = null;
					}
					else
					{
						this.m_a.SelectedControl = this[0];
					}
				}
				if (this.m_a != null && this.m_a.DockContainer is DocumentContainer)
				{
					((DocumentContainer)this.m_a.DockContainer).b(dockControl);
				}
				this.m_a.f();
			}

			public void AddRange(DockControl[] controls)
			{
				this.m_b = true;
				foreach (DockControl control in controls)
				{
					Add(control);
				}
				this.m_b = false;
				this.m_a.f();
			}

			public int Add(DockControl control)
			{
				int count = base.Count;
				Insert(count, control);
				return count;
			}

			public void Insert(int index, DockControl control)
			{
				if (control != null)
				{
					if (control.b != null)
					{
						control.b.Controls.Remove(control);
					}
					base.List.Insert(index, control);
				}
			}

			public void Remove(DockControl control)
			{
				if (this.m_a.IsInContainer && !this.m_a.DockContainer.IsFloating)
				{
					control.LastIndexInFixedLayoutSystem = IndexOf(control);
				}
				base.List.Remove(control);
			}

			public bool Contains(DockControl control)
			{
				return base.List.Contains(control);
			}

			public int IndexOf(DockControl control)
			{
				return base.List.IndexOf(control);
			}

			public void CopyTo(DockControl[] array, int index)
			{
				base.List.CopyTo(array, index);
			}
		}

		private new const int m_a = 19;

		private new const int m_b = 15;

		private new DockControlCollection c = null;

		private bool d = false;

		internal Rectangle e;

		internal new Rectangle f;

		internal new Rectangle g;

		internal Rectangle h;

		private DockControl m_i = null;

		private r j = null;

		private Guid k = Guid.NewGuid();

		private int l = 0;

		private n m = null;

		private n n = null;

		internal n o = null;

		internal bool p = false;

		private Point q = Point.Empty;

		private bool r = false;

		internal bool s = false;

		private a t;

		private bool u = false;

		private bool v = false;

		internal int PopupSize
		{
			get
			{
				if (l != 0)
				{
					return l;
				}
				if (base.IsInContainer)
				{
					return base.DockContainer.ContentSize;
				}
				return 200;
			}
			set
			{
				l = value;
			}
		}

		internal Guid Guid => k;

		public bool LockControls
		{
			get
			{
				return r;
			}
			set
			{
				r = value;
			}
		}

		public bool IsPoppedUp => PopupContainer != null;

		internal r PopupContainer
		{
			get
			{
				return j;
			}
			set
			{
				j = value;
			}
		}

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public DockControlCollection Controls => c;

		[DefaultValue(false)]
		[Browsable(false)]
		public virtual bool Collapsed
		{
			get
			{
				return d;
			}
			set
			{
				if (d == value)
				{
					return;
				}
				d = value;
				if (!base.IsInContainer || !base.DockContainer.CanShowCollapsed)
				{
					return;
				}
				if (d)
				{
					foreach (DockControl item in c)
					{
						if (item.Parent == base.DockContainer)
						{
							TD.SandDock.g.a((Control)item);
						}
					}
				}
				else
				{
					if (base.Parent != null)
					{
						((SplitLayoutSystem)base.Parent).a(this);
					}
					if (PopupContainer != null)
					{
						PopupContainer.b();
					}
				}
				if (base.IsInContainer)
				{
					base.DockContainer.LayoutSystemsChanged();
				}
			}
		}

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public virtual DockControl SelectedControl
		{
			get
			{
				return this.m_i;
			}
			set
			{
				if (value != null && !c.Contains(value))
				{
					throw new ArgumentOutOfRangeException();
				}
				DockControl a_ = this.m_i;
				this.m_i = value;
				f();
				a(a_, this.m_i);
			}
		}

		private bool ContainsFocus
		{
			get
			{
				foreach (DockControl item in c)
				{
					if (item.ContainsFocus)
					{
						return true;
					}
				}
				return false;
			}
		}

		internal Rectangle JoinCatchmentBounds => h;

		internal event a SelectedControlChanged
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			add
			{
				t = (a)Delegate.Combine(t, value);
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			remove
			{
				t = (a)Delegate.Remove(t, value);
			}
		}

		public ControlLayoutSystem()
		{
			c = new DockControlCollection(this);
			m = new n();
			n = new n();
		}

		public ControlLayoutSystem(int desiredWidth, int desiredHeight)
			: this()
		{
			ref SizeF reference = ref base.f;
			reference = new SizeF(desiredWidth, desiredHeight);
		}

		public ControlLayoutSystem(int desiredWidth, int desiredHeight, DockControl[] controls, DockControl selectedControl)
			: this(desiredWidth, desiredHeight)
		{
			c.AddRange(controls);
			if (selectedControl != null)
			{
				SelectedControl = selectedControl;
			}
		}

		protected virtual void OnCloseButtonClick(EventArgs e)
		{
			if (SelectedControl != null)
			{
				SelectedControl.Close();
			}
		}

		protected virtual void OnPinButtonClick()
		{
			o = null;
			Collapsed = !Collapsed;
			if (base.IsInContainer && SelectedControl != null)
			{
				if (Collapsed)
				{
					base.DockContainer.AutoHideManager.a(SelectedControl, A_1: true, A_2: false);
					base.DockContainer.AutoHideManager.a(A_0: false);
				}
				else
				{
					SelectedControl.Activate();
				}
			}
		}

		private new void a(DockControl A_0, DockControl A_1)
		{
			if (t != null)
			{
				t(A_0, A_1);
			}
		}

		protected internal override void OnMouseDoubleClick()
		{
			Point point = base.DockContainer.PointToClient(Cursor.Position);
			if (base.DockContainer.Manager == null)
			{
				return;
			}
			if (e.Contains(point) && !m.a.Contains(point) && !n.a.Contains(point) && !LockControls && !base.DockContainer.IsFloating)
			{
				Float(base.DockContainer.Manager);
			}
			if (LockControls)
			{
				return;
			}
			DockControl controlAt = GetControlAt(point);
			if (controlAt != null)
			{
				if (controlAt.IsFloating)
				{
					controlAt.PerformDock();
					controlAt.Activate();
				}
				else
				{
					controlAt.Float();
				}
			}
		}

		protected internal override void OnMouseMove(MouseEventArgs e)
		{
			base.DockContainer.Cursor = Cursors.Default;
			if (s)
			{
				return;
			}
			if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
			{
				if (base.g != null)
				{
					base.g.a(Cursor.Position);
					return;
				}
				Rectangle rectangle = new Rectangle(q, SystemInformation.DragSize);
				rectangle.Offset(-(rectangle.Width / 2), -(rectangle.Height / 2));
				if (!rectangle.Contains(e.X, e.Y) && base.IsInContainer && q != Point.Empty && !Collapsed && !LockControls)
				{
					DockControl controlAt = GetControlAt(q);
					u = controlAt == null;
					a(A_5: (base.DockContainer.Manager == null) ? base.DockContainer.DockingHints : base.DockContainer.Manager.DockingHints, A_6: (base.DockContainer.Manager == null) ? base.DockContainer.DockingManager : base.DockContainer.Manager.DockingManager, A_0: base.DockContainer.Manager, A_1: base.DockContainer, A_2: this, A_3: controlAt, A_4: q);
					return;
				}
			}
			n buttonAt = GetButtonAt(e.X, e.Y);
			if (buttonAt != o)
			{
				o = buttonAt;
				InvalidateTitleBar();
			}
		}

		internal virtual string GetToolTipText(Point position)
		{
			DockControl controlAt = GetControlAt(position);
			if (controlAt != null)
			{
				if (controlAt.ToolTipText.Length != 0)
				{
					return controlAt.ToolTipText;
				}
				if (controlAt.h)
				{
					return controlAt.Text;
				}
				return "";
			}
			n buttonAt = GetButtonAt(position.X, position.Y);
			if (buttonAt == m)
			{
				return SandDockLanguage.CloseText;
			}
			if (buttonAt == n)
			{
				return SandDockLanguage.AutoHideText;
			}
			return "";
		}

		internal virtual n GetButtonAt(int x, int y)
		{
			if (m.b && m.a.Contains(x, y))
			{
				return m;
			}
			if (n.b && n.a.Contains(x, y))
			{
				return n;
			}
			return null;
		}

		internal override void OnDockingManagerCancelled(object sender, EventArgs e)
		{
			base.OnDockingManagerCancelled(sender, e);
			q = Point.Empty;
		}

		protected internal override void OnDragOver(DragEventArgs drgevent)
		{
			base.OnDragOver(drgevent);
			DockControl controlAt = GetControlAt(base.DockContainer.PointToClient(new Point(drgevent.X, drgevent.Y)));
			if (controlAt != null && SelectedControl != controlAt)
			{
				SelectedControl = controlAt;
			}
		}

		protected internal override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);
			if (this.e.Contains(e.X, e.Y) && SelectedControl != null)
			{
				SelectedControl.Activate();
			}
			if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
			{
				if (this.e.Contains(e.X, e.Y))
				{
					ref Point reference = ref q;
					reference = new Point(e.X, e.Y);
				}
				if (o != null)
				{
					p = true;
					InvalidateTitleBar();
					OnButtonPress(o);
					q = Point.Empty;
					return;
				}
			}
			DockControl controlAt = GetControlAt(new Point(e.X, e.Y));
			if (controlAt != null)
			{
				controlAt.Activate();
				if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
				{
					ref Point reference2 = ref q;
					reference2 = new Point(e.X, e.Y);
				}
			}
		}

		protected internal override void OnMouseUp(MouseEventArgs e)
		{
			base.OnMouseUp(e);
			q = Point.Empty;
			if (base.g != null)
			{
				base.g.a();
				return;
			}
			if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
			{
				DockControl dockControl = GetControlAt(new Point(e.X, e.Y));
				if (dockControl == null && this.e.Contains(e.X, e.Y))
				{
					dockControl = SelectedControl;
				}
				if (dockControl != null && base.IsInContainer)
				{
					Point point = new Point(e.X, e.Y);
					point = base.DockContainer.PointToScreen(point);
					point = dockControl.PointToClient(point);
					base.DockContainer.OnShowControlContextMenu(new ShowControlContextMenuEventArgs(dockControl, point));
					return;
				}
			}
			if ((e.Button & MouseButtons.Left) == MouseButtons.Left && o != null)
			{
				p = false;
				InvalidateTitleBar();
				OnButtonPressed(o);
			}
		}

		internal virtual void OnButtonPress(n button)
		{
		}

		internal virtual void OnButtonPressed(n button)
		{
			if (o == m)
			{
				OnCloseButtonClick(EventArgs.Empty);
			}
			else if (o == n)
			{
				OnPinButtonClick();
			}
		}

		protected internal override void OnMouseLeave()
		{
			base.OnMouseLeave();
			if (o != null)
			{
				o = null;
				InvalidateTitleBar();
			}
			p = false;
		}

		internal bool i()
		{
			if (base.IsInContainer && ContainsFocus)
			{
				base.DockContainer.Invalidate(e);
				if (SelectedControl != null)
				{
					base.DockContainer.Manager.c(SelectedControl);
				}
				return true;
			}
			return false;
		}

		internal new void g()
		{
			if (v && base.IsInContainer)
			{
				base.DockContainer.Invalidate(e);
			}
		}

		protected internal virtual void OnControlEnter(DockControl control)
		{
			b();
		}

		protected internal virtual void OnControlLeave(DockControl control)
		{
			b();
		}

		private new void b()
		{
			if (base.IsInContainer)
			{
				base.DockContainer.Invalidate(e);
			}
		}

		internal override void Paint(RendererBase renderer, Graphics graphics, Font font)
		{
			if (SelectedControl != null && !SelectedControl.p)
			{
				SelectedControl.OnBeforeFirstShown(EventArgs.Empty);
				SelectedControl.p = true;
			}
			if (base.IsInContainer && base.DockContainer.FriendDesignMode)
			{
				ISelectionService selectionService = (ISelectionService)base.DockContainer.a(typeof(ISelectionService));
				v = false;
				foreach (DockControl control in Controls)
				{
					if (selectionService.GetComponentSelected(control))
					{
						v = true;
						break;
					}
				}
			}
			else
			{
				v = ContainsFocus;
			}
			if (SelectedControl != null)
			{
				renderer.DrawControlClientBackground(graphics, this.g, SelectedControl.BackColor);
			}
			else
			{
				renderer.DrawControlClientBackground(graphics, this.g, SystemColors.Control);
			}
			if ((c.Count > 1 || base.DockContainer.FriendDesignMode) && this.f != Rectangle.Empty)
			{
				int selectedTabOffset = 0;
				if (this.m_i != null)
				{
					Rectangle rectangle = this.m_i.c;
					selectedTabOffset = rectangle.X - base.Bounds.Left;
				}
				renderer.DrawTabStripBackground(graphics, this.f, selectedTabOffset);
				foreach (DockControl item in c)
				{
					DrawItemState drawItemState = DrawItemState.Default;
					if (this.m_i == item)
					{
						drawItemState |= DrawItemState.Selected;
					}
					bool drawSeparator = true;
					if (this.m_i != null && c.IndexOf(item) == c.IndexOf(this.m_i) - 1)
					{
						drawSeparator = false;
					}
					renderer.DrawTabStripTab(graphics, item.c, item.WorkingTabImage, item.TabText, item.Font, item.BackColor, item.ForeColor, drawItemState, drawSeparator);
				}
			}
			Rectangle rectangle2 = e;
			if (!(rectangle2 != Rectangle.Empty) || rectangle2.Width <= 0 || rectangle2.Height <= 0)
			{
				return;
			}
			renderer.DrawTitleBarBackground(graphics, rectangle2, v);
			if (m.b)
			{
				rectangle2.Width -= 21;
			}
			if (n.b)
			{
				rectangle2.Width -= 21;
			}
			rectangle2 = renderer.TitleBarMetrics.RemovePadding(rectangle2);
			if (rectangle2.Width > 8)
			{
				renderer.DrawTitleBarText(graphics, rectangle2, v, (this.m_i == null) ? "Empty Layout System" : this.m_i.Text, base.DockContainer.Font);
			}
			DrawItemState drawItemState2;
			if (m.b && m.a.Left > base.Bounds.Left)
			{
				drawItemState2 = DrawItemState.Default;
				if (o == m)
				{
					drawItemState2 |= DrawItemState.HotLight;
					if (p)
					{
						drawItemState2 |= DrawItemState.Selected;
					}
				}
				renderer.DrawTitleBarButton(graphics, m.a, SandDockButtonType.Close, drawItemState2, v, toggled: false);
			}
			if (!n.b || n.a.Left <= base.Bounds.Left)
			{
				return;
			}
			drawItemState2 = DrawItemState.Default;
			if (o == n)
			{
				drawItemState2 |= DrawItemState.HotLight;
				if (p)
				{
					drawItemState2 |= DrawItemState.Selected;
				}
			}
			renderer.DrawTitleBarButton(graphics, n.a, SandDockButtonType.Pin, drawItemState2, v, Collapsed);
		}

		internal new void a(Graphics A_0, RendererBase A_1, n A_2, SandDockButtonType A_3, bool A_4)
		{
			if (!A_2.b)
			{
				return;
			}
			DrawItemState drawItemState = DrawItemState.Default;
			if (o == A_2)
			{
				drawItemState |= DrawItemState.HotLight;
				if (p)
				{
					drawItemState |= DrawItemState.Selected;
				}
			}
			if (!A_4)
			{
				drawItemState |= DrawItemState.Disabled;
			}
			A_1.DrawDocumentStripButton(A_0, A_2.a, A_3, drawItemState);
		}

		internal virtual void InvalidateTitleBar()
		{
			if (j != null)
			{
				j.Invalidate(e);
			}
			else if (base.IsInContainer)
			{
				base.DockContainer.Invalidate(e);
			}
		}

		protected internal virtual ControlLayoutSystem CreateNewLayoutSystem(int desiredWidth, int desiredHeight, DockControl[] controls, DockControl selectedControl)
		{
			return new ControlLayoutSystem(desiredWidth, desiredHeight, controls, selectedControl);
		}

		protected internal virtual ControlLayoutSystem CreateNewLayoutSystem()
		{
			return new ControlLayoutSystem();
		}

		internal override void OnDockingManagerCommitted(j.b target)
		{
			base.OnDockingManagerCommitted(target);
			if (target == null || target.a == TD.SandDock.j.c.b || target.a == TD.SandDock.j.c.g)
			{
				return;
			}
			DockControl selectedControl = SelectedControl;
			_ = base.DockContainer;
			SandDockManager manager = base.DockContainer.Manager;
			if (u)
			{
				((SplitLayoutSystem)base.Parent).LayoutSystems.Remove(this);
			}
			else
			{
				TD.SandDock.g.a(selectedControl);
			}
			if (target.a == TD.SandDock.j.c.c)
			{
				if (u)
				{
					Float(manager, target.e);
					return;
				}
				ControlLayoutSystem controlLayoutSystem = CreateNewLayoutSystem(selectedControl.Width, selectedControl.Height, new DockControl[1] { selectedControl }, selectedControl);
				controlLayoutSystem.Float(manager, target.e);
			}
			else if (target.b != null)
			{
				a(selectedControl, u, target);
				selectedControl?.Activate();
			}
		}

		internal new void a(DockControl A_0, bool A_1, j.b A_2)
		{
			if (A_2.a == TD.SandDock.j.c.f)
			{
				ControlLayoutSystem controlLayoutSystem = (A_1 ? this : CreateNewLayoutSystem((int)base.f.Width, (int)base.f.Height, new DockControl[1] { A_0 }, A_0));
				if (A_2.b.Empty)
				{
					A_2.b.LayoutSystem.LayoutSystems.Add(controlLayoutSystem);
				}
				else
				{
					A_2.b.b(controlLayoutSystem);
				}
			}
			else if (A_2.a == TD.SandDock.j.c.e)
			{
				if (A_1)
				{
					Dock(A_2.c, A_2.f);
				}
				else
				{
					A_0.PerformDock(A_2.c, A_2.f);
				}
			}
			else if (A_2.a == TD.SandDock.j.c.d)
			{
				if (A_1)
				{
					A_2.c.SplitForLayoutSystem(this, A_2.d);
					return;
				}
				ControlLayoutSystem layoutSystem = CreateNewLayoutSystem(A_0.Width, A_0.Height, new DockControl[1] { A_0 }, A_0);
				A_2.c.SplitForLayoutSystem(layoutSystem, A_2.d);
			}
		}

		public void SplitForLayoutSystem(LayoutSystemBase layoutSystem, DockSide side)
		{
			if (layoutSystem == null)
			{
				throw new ArgumentNullException();
			}
			if (side == DockSide.None)
			{
				throw new ArgumentException();
			}
			if (layoutSystem.Parent != null)
			{
				throw new InvalidOperationException("This layout system must be removed from its parent before it can be moved to a new layout system.");
			}
			if (base.Parent == null)
			{
				throw new InvalidOperationException("This layout system is not parented yet.");
			}
			SplitLayoutSystem splitLayoutSystem = (SplitLayoutSystem)base.Parent;
			if (splitLayoutSystem.SplitMode == Orientation.Horizontal)
			{
				if (side == DockSide.Top || side == DockSide.Bottom)
				{
					a(layoutSystem, (side == DockSide.Top) ? splitLayoutSystem.LayoutSystems.IndexOf(this) : (splitLayoutSystem.LayoutSystems.IndexOf(this) + 1), A_2: true);
				}
				else
				{
					a(layoutSystem, Orientation.Vertical, side == DockSide.Left);
				}
			}
			else if (splitLayoutSystem.SplitMode == Orientation.Vertical)
			{
				if (side == DockSide.Left || side == DockSide.Right)
				{
					a(layoutSystem, (side == DockSide.Left) ? splitLayoutSystem.LayoutSystems.IndexOf(this) : (splitLayoutSystem.LayoutSystems.IndexOf(this) + 1), A_2: false);
				}
				else
				{
					a(layoutSystem, Orientation.Horizontal, side == DockSide.Top);
				}
			}
		}

		private new void a(LayoutSystemBase A_0, int A_1, bool A_2)
		{
			SplitLayoutSystem splitLayoutSystem = (SplitLayoutSystem)base.Parent;
			splitLayoutSystem.LayoutSystems.b = true;
			splitLayoutSystem.LayoutSystems.Insert(A_1, A_0);
			splitLayoutSystem.LayoutSystems.b = false;
			if (A_2)
			{
				float num = base.f.Height - 4f;
				base.f.Height = num / 2f;
				A_0.f.Height = num / 2f;
			}
			else
			{
				float num2 = base.f.Width - 4f;
				base.f.Width = num2 / 2f;
				A_0.f.Width = num2 / 2f;
			}
			splitLayoutSystem.b();
		}

		private new void a(LayoutSystemBase A_0, Orientation A_1, bool A_2)
		{
			SplitLayoutSystem splitLayoutSystem = (SplitLayoutSystem)base.Parent;
			SplitLayoutSystem splitLayoutSystem2 = new SplitLayoutSystem();
			splitLayoutSystem2.SplitMode = A_1;
			splitLayoutSystem2.f = base.f;
			int index = splitLayoutSystem.LayoutSystems.IndexOf(this);
			splitLayoutSystem.LayoutSystems.b = true;
			splitLayoutSystem.LayoutSystems.Remove(this);
			splitLayoutSystem.LayoutSystems.Insert(index, splitLayoutSystem2);
			splitLayoutSystem.LayoutSystems.b = false;
			splitLayoutSystem2.LayoutSystems.Add(this);
			if (A_2)
			{
				splitLayoutSystem2.LayoutSystems.Insert(0, A_0);
			}
			else
			{
				splitLayoutSystem2.LayoutSystems.Add(A_0);
			}
			splitLayoutSystem.b();
		}

		public void Float(SandDockManager manager, Rectangle bounds)
		{
			if (base.Parent is SplitLayoutSystem)
			{
				((SplitLayoutSystem)base.Parent).LayoutSystems.Remove(this);
			}
			m m2 = manager.b();
			m2.LayoutSystem.LayoutSystems.Add(this);
			m2.a(bounds, A_1: true, A_2: true);
		}

		public void Float(SandDockManager manager)
		{
			if (SelectedControl == null)
			{
				throw new InvalidOperationException("The layout system must have a selected control to be floated.");
			}
			Float(manager, SelectedControl.h());
		}

		public void Dock(ControlLayoutSystem layoutSystem)
		{
			Dock(layoutSystem, 0);
		}

		public void Dock(ControlLayoutSystem layoutSystem, int index)
		{
			if (base.Parent != null)
			{
				throw new InvalidOperationException("This layout system already has a parent. To remove it, use the parent layout system's LayoutSystems.Remove method.");
			}
			DockControl selectedControl = SelectedControl;
			while (c.Count != 0)
			{
				DockControl control = c[0];
				c.RemoveAt(0);
				layoutSystem.Controls.Insert(index, control);
			}
			if (selectedControl != null)
			{
				layoutSystem.SelectedControl = selectedControl;
			}
		}

		internal override void SetDockContainer(DockContainer dockContainer)
		{
			if (dockContainer == null && base.IsInContainer)
			{
				foreach (DockControl control in Controls)
				{
					if (control.Parent == base.DockContainer)
					{
						TD.SandDock.g.a((Control)control);
					}
				}
			}
			foreach (DockControl control2 in Controls)
			{
				control2.a(dockContainer);
			}
			base.SetDockContainer(dockContainer);
		}

		public virtual DockControl GetControlAt(Point position)
		{
			if (this.f.Contains(position) && !m.a.Contains(position) && !n.a.Contains(position))
			{
				foreach (DockControl item in c)
				{
					Rectangle rectangle = item.c;
					if (rectangle.Contains(position))
					{
						return item;
					}
				}
			}
			return null;
		}

		internal new int a(Point A_0)
		{
			int num = 0;
			foreach (DockControl item in c)
			{
				Rectangle rectangle = item.c;
				if (A_0.X > rectangle.Left + rectangle.Width / 2)
				{
					num++;
				}
			}
			return num;
		}

		internal new void f()
		{
			if (base.IsInContainer)
			{
				if (PopupContainer != null)
				{
					PopupContainer.e();
				}
				if (Collapsed && base.DockContainer.CanShowCollapsed)
				{
					base.DockContainer.Invalidate(base.DockContainer.AutoHideBounds);
				}
				if (base.DockContainer.IsFloating)
				{
					base.DockContainer.u();
				}
				else
				{
					base.DockContainer.a(this, base.Bounds);
				}
				base.DockContainer.Invalidate(base.Bounds);
			}
		}

		private new void a()
		{
			if (this.m_i == null)
			{
				m.b = false;
				n.b = false;
				return;
			}
			int y = e.Top + e.Height / 2 - 7;
			int num = e.Right - 2;
			if (this.m_i.Closable)
			{
				m.b = true;
				ref Rectangle reference = ref m.a;
				reference = new Rectangle(num - 19, y, 19, 15);
				num -= 21;
			}
			else
			{
				m.b = false;
			}
			bool flag = true;
			foreach (DockControl item in c)
			{
				if (!item.Collapsible)
				{
					flag = false;
					break;
				}
			}
			if (base.IsInContainer && base.DockContainer.IsFloating)
			{
				flag = false;
			}
			if (flag)
			{
				n.b = true;
				ref Rectangle reference2 = ref n.a;
				reference2 = new Rectangle(num - 19, y, 19, 15);
				num -= 21;
			}
			else
			{
				n.b = false;
			}
		}

		protected internal virtual void LayoutCollapsed(RendererBase renderer, Rectangle bounds)
		{
			e = bounds;
			e.Offset(0, renderer.TitleBarMetrics.Margin.Top);
			e.Height = renderer.TitleBarMetrics.Height - (renderer.TitleBarMetrics.Margin.Top + renderer.TitleBarMetrics.Margin.Bottom);
			a();
			bounds.Offset(0, renderer.TitleBarMetrics.Height);
			bounds.Height -= renderer.TitleBarMetrics.Height;
			this.g = bounds;
			this.f = Rectangle.Empty;
			foreach (DockControl item in c)
			{
				item.Visible = item == this.m_i;
				item.Bounds = this.g;
			}
		}

		protected virtual void CalculateLayout(RendererBase renderer, Rectangle bounds, bool floating, out Rectangle titlebarBounds, out Rectangle tabstripBounds, out Rectangle clientBounds, out Rectangle joinCatchmentBounds)
		{
			if (floating)
			{
				titlebarBounds = Rectangle.Empty;
			}
			else
			{
				titlebarBounds = bounds;
				titlebarBounds.Offset(0, renderer.TitleBarMetrics.Margin.Top);
				titlebarBounds.Height = renderer.TitleBarMetrics.Height - (renderer.TitleBarMetrics.Margin.Top + renderer.TitleBarMetrics.Margin.Bottom);
				a();
				bounds.Offset(0, renderer.TitleBarMetrics.Height);
				bounds.Height -= renderer.TitleBarMetrics.Height;
			}
			if (Controls.Count > 1 || base.DockContainer.FriendDesignMode)
			{
				tabstripBounds = bounds;
				tabstripBounds.Y = tabstripBounds.Bottom - renderer.TabStripMetrics.Height;
				tabstripBounds.Height = renderer.TabStripMetrics.Height;
				tabstripBounds = renderer.TabStripMetrics.RemoveMargin(tabstripBounds);
				bounds.Height -= renderer.TabStripMetrics.Height;
			}
			else
			{
				tabstripBounds = Rectangle.Empty;
			}
			clientBounds = bounds;
			joinCatchmentBounds = titlebarBounds;
		}

		protected internal override void Layout(RendererBase renderer, Graphics graphics, Rectangle bounds, bool floating)
		{
			base.Layout(renderer, graphics, bounds, floating);
			if (Collapsed && base.DockContainer.CanShowCollapsed)
			{
				return;
			}
			CalculateLayout(renderer, bounds, floating, out e, out this.f, out this.g, out h);
			if (this is DocumentLayoutSystem)
			{
				return;
			}
			Rectangle bounds2 = this.g;
			bounds2.Inflate(-renderer.ControlClientPadding.Width, -renderer.ControlClientPadding.Height);
			s = true;
			try
			{
				if (e != Rectangle.Empty)
				{
					a();
				}
				a(renderer, graphics, this.f);
				foreach (DockControl item in c)
				{
					if (item.Parent != base.DockContainer)
					{
						if (item.Parent != null)
						{
							TD.SandDock.g.a((Control)item);
						}
						base.DockContainer.Controls.Add(item);
					}
					item.Visible = item == this.m_i;
					item.Bounds = bounds2;
				}
			}
			finally
			{
				s = false;
			}
		}

		private new void a(RendererBase A_0, Graphics A_1, Rectangle A_2)
		{
			int num = 0;
			int num2 = A_2.Width - (A_0.TabStripMetrics.Padding.Left + A_0.TabStripMetrics.Padding.Right);
			int[] array = new int[checked((uint)c.Count)];
			int num3 = 0;
			foreach (DockControl item in c)
			{
				int num4 = (int)Math.Ceiling(A_1.MeasureString(item.TabText, item.Font, 999, EverettRenderer.StandardStringFormat).Width);
				num4 += 30;
				num += num4;
				array[num3++] = num4;
				item.h = false;
			}
			if (num > num2)
			{
				int num5 = num - num2;
				for (int num6 = 0; num6 < num3; num6++)
				{
					int[] array2;
					int[] array3 = (array2 = array);
					int num7 = num6;
					nint num8 = num7;
					array3[num7] = array2[num8] - (int)((float)num5 * ((float)array[num6] / (float)num));
					c[num6].h = true;
				}
			}
			A_2 = A_0.TabStripMetrics.RemovePadding(A_2);
			int num9 = A_2.Left;
			num3 = 0;
			for (int num10 = 0; num10 < c.Count; num10++)
			{
				DockControl dockControl2 = c[num10];
				BoxModel tabMetrics = A_0.TabMetrics;
				Rectangle rectangle = (dockControl2.c = new Rectangle(num9 + tabMetrics.Margin.Left, A_2.Top + tabMetrics.Margin.Top, tabMetrics.Padding.Left + array[num3] + tabMetrics.Padding.Right, A_2.Height - (tabMetrics.Margin.Top + tabMetrics.Margin.Bottom)));
				num9 += rectangle.Width + tabMetrics.ExtraWidth;
				num3++;
			}
		}

		public override void Dispose()
		{
			DockControl[] array = new DockControl[checked((uint)Controls.Count)];
			Controls.CopyTo(array, 0);
			Controls.Clear();
			DockControl[] array2 = array;
			foreach (DockControl dockControl in array2)
			{
				dockControl.Dispose();
			}
			base.Dispose();
		}
	}
}
