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 event ControlLayoutSystem.a SelectedControlChanged
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			add
			{
				this.t = (ControlLayoutSystem.a)Delegate.Combine(this.t, value);
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			remove
			{
				this.t = (ControlLayoutSystem.a)Delegate.Remove(this.t, value);
			}
		}

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

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

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

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

		internal Guid Guid
		{
			get
			{
				return this.k;
			}
		}

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

		public bool IsPoppedUp
		{
			get
			{
				return this.PopupContainer != null;
			}
		}

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

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public ControlLayoutSystem.DockControlCollection Controls
		{
			get
			{
				return this.c;
			}
		}

		[DefaultValue(false)]
		[Browsable(false)]
		public virtual bool Collapsed
		{
			get
			{
				return this.d;
			}
			set
			{
				if (this.d == value)
				{
					return;
				}
				this.d = value;
				if (base.IsInContainer && base.DockContainer.CanShowCollapsed)
				{
					if (this.d)
					{
						using (IEnumerator enumerator = this.c.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								object obj = enumerator.Current;
								DockControl dockControl = (DockControl)obj;
								if (dockControl.Parent == base.DockContainer)
								{
									TD.SandDock.g.a(dockControl);
								}
							}
							goto IL_A9;
						}
					}
					if (base.Parent != null)
					{
						((SplitLayoutSystem)base.Parent).a(this);
					}
					if (this.PopupContainer != null)
					{
						this.PopupContainer.b();
					}
					IL_A9:
					if (base.IsInContainer)
					{
						base.DockContainer.LayoutSystemsChanged();
					}
				}
			}
		}

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

		private bool ContainsFocus
		{
			get
			{
				foreach (object obj in this.c)
				{
					DockControl dockControl = (DockControl)obj;
					if (dockControl.ContainsFocus)
					{
						return true;
					}
				}
				return false;
			}
		}

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

		protected virtual void OnPinButtonClick()
		{
			this.o = null;
			this.Collapsed = !this.Collapsed;
			if (base.IsInContainer && this.SelectedControl != null)
			{
				if (this.Collapsed)
				{
					base.DockContainer.AutoHideManager.a(this.SelectedControl, true, false);
					base.DockContainer.AutoHideManager.a(false);
					return;
				}
				this.SelectedControl.Activate();
			}
		}

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

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

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

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

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

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

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

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

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

		internal virtual void OnButtonPress(n button)
		{
		}

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

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

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

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

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

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

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

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

		internal new void a(Graphics A_0, RendererBase A_1, n A_2, SandDockButtonType A_3, bool A_4)
		{
			if (A_2.b)
			{
				DrawItemState drawItemState = DrawItemState.Default;
				if (this.o == A_2)
				{
					drawItemState |= DrawItemState.HotLight;
					if (this.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 (this.j != null)
			{
				this.j.Invalidate(this.e);
				return;
			}
			if (base.IsInContainer)
			{
				base.DockContainer.Invalidate(this.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 = this.SelectedControl;
			DockContainer dockContainer = base.DockContainer;
			SandDockManager manager = base.DockContainer.Manager;
			if (this.u)
			{
				((SplitLayoutSystem)base.Parent).LayoutSystems.Remove(this);
			}
			else
			{
				TD.SandDock.g.a(selectedControl);
			}
			if (target.a != TD.SandDock.j.c.c)
			{
				if (target.b != null)
				{
					this.a(selectedControl, this.u, target);
					if (selectedControl != null)
					{
						selectedControl.Activate();
					}
				}
				return;
			}
			if (this.u)
			{
				this.Float(manager, target.e);
				return;
			}
			ControlLayoutSystem controlLayoutSystem = this.CreateNewLayoutSystem(selectedControl.Width, selectedControl.Height, new DockControl[]
			{
				selectedControl
			}, selectedControl);
			controlLayoutSystem.Float(manager, target.e);
		}

		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 : this.CreateNewLayoutSystem((int)this.f.Width, (int)this.f.Height, new DockControl[]
				{
					A_0
				}, A_0);
				if (A_2.b.Empty)
				{
					A_2.b.LayoutSystem.LayoutSystems.Add(controlLayoutSystem);
					return;
				}
				A_2.b.b(controlLayoutSystem);
				return;
			}
			else
			{
				if (A_2.a != TD.SandDock.j.c.e)
				{
					if (A_2.a == TD.SandDock.j.c.d)
					{
						if (A_1)
						{
							A_2.c.SplitForLayoutSystem(this, A_2.d);
							return;
						}
						ControlLayoutSystem layoutSystem = this.CreateNewLayoutSystem(A_0.Width, A_0.Height, new DockControl[]
						{
							A_0
						}, A_0);
						A_2.c.SplitForLayoutSystem(layoutSystem, A_2.d);
					}
					return;
				}
				if (A_1)
				{
					this.Dock(A_2.c, A_2.f);
					return;
				}
				A_0.PerformDock(A_2.c, A_2.f);
				return;
			}
		}

		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 (splitLayoutSystem.SplitMode == Orientation.Vertical)
				{
					if (side == DockSide.Left || side == DockSide.Right)
					{
						this.a(layoutSystem, (side == DockSide.Left) ? splitLayoutSystem.LayoutSystems.IndexOf(this) : (splitLayoutSystem.LayoutSystems.IndexOf(this) + 1), false);
						return;
					}
					this.a(layoutSystem, Orientation.Horizontal, side == DockSide.Top);
				}
				return;
			}
			if (side == DockSide.Top || side == DockSide.Bottom)
			{
				this.a(layoutSystem, (side == DockSide.Top) ? splitLayoutSystem.LayoutSystems.IndexOf(this) : (splitLayoutSystem.LayoutSystems.IndexOf(this) + 1), true);
				return;
			}
			this.a(layoutSystem, Orientation.Vertical, side == DockSide.Left);
		}

		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 = this.f.Height - 4f;
				this.f.Height = num / 2f;
				A_0.f.Height = num / 2f;
			}
			else
			{
				float num2 = this.f.Width - 4f;
				this.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 = this.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 m = manager.b();
			m.LayoutSystem.LayoutSystems.Add(this);
			m.a(bounds, true, true);
		}

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

		public void Dock(ControlLayoutSystem layoutSystem)
		{
			this.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 = this.SelectedControl;
			while (this.c.Count != 0)
			{
				DockControl control = this.c[0];
				this.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 (object obj in this.Controls)
				{
					DockControl dockControl = (DockControl)obj;
					if (dockControl.Parent == base.DockContainer)
					{
						TD.SandDock.g.a(dockControl);
					}
				}
			}
			foreach (object obj2 in this.Controls)
			{
				DockControl dockControl2 = (DockControl)obj2;
				dockControl2.a(dockContainer);
			}
			base.SetDockContainer(dockContainer);
		}

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

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

		internal new void f()
		{
			if (base.IsInContainer)
			{
				if (this.PopupContainer != null)
				{
					this.PopupContainer.e();
				}
				if (this.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.i == null)
			{
				this.m.b = false;
				this.n.b = false;
				return;
			}
			int y = this.e.Top + this.e.Height / 2 - 7;
			int num = this.e.Right - 2;
			if (this.i.Closable)
			{
				this.m.b = true;
				this.m.a = new Rectangle(num - 19, y, 19, 15);
				num -= 21;
			}
			else
			{
				this.m.b = false;
			}
			bool flag = true;
			foreach (object obj in this.c)
			{
				DockControl dockControl = (DockControl)obj;
				if (!dockControl.Collapsible)
				{
					flag = false;
					break;
				}
			}
			if (base.IsInContainer && base.DockContainer.IsFloating)
			{
				flag = false;
			}
			if (flag)
			{
				this.n.b = true;
				this.n.a = new Rectangle(num - 19, y, 19, 15);
				num -= 21;
				return;
			}
			this.n.b = false;
		}

		protected internal virtual void LayoutCollapsed(RendererBase renderer, Rectangle bounds)
		{
			this.e = bounds;
			this.e.Offset(0, renderer.TitleBarMetrics.Margin.Top);
			this.e.Height = renderer.TitleBarMetrics.Height - (renderer.TitleBarMetrics.Margin.Top + renderer.TitleBarMetrics.Margin.Bottom);
			this.a();
			bounds.Offset(0, renderer.TitleBarMetrics.Height);
			bounds.Height -= renderer.TitleBarMetrics.Height;
			this.g = bounds;
			this.f = Rectangle.Empty;
			foreach (object obj in this.c)
			{
				DockControl dockControl = (DockControl)obj;
				dockControl.Visible = (dockControl == this.i);
				dockControl.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);
				this.a();
				bounds.Offset(0, renderer.TitleBarMetrics.Height);
				bounds.Height -= renderer.TitleBarMetrics.Height;
			}
			if (this.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;
		}

		internal Rectangle JoinCatchmentBounds
		{
			get
			{
				return this.h;
			}
		}

		protected internal override void Layout(RendererBase renderer, Graphics graphics, Rectangle bounds, bool floating)
		{
			base.Layout(renderer, graphics, bounds, floating);
			if (this.Collapsed && base.DockContainer.CanShowCollapsed)
			{
				return;
			}
			this.CalculateLayout(renderer, bounds, floating, out this.e, out this.f, out this.g, out this.h);
			if (this is DocumentLayoutSystem)
			{
				return;
			}
			Rectangle bounds2 = this.g;
			bounds2.Inflate(-renderer.ControlClientPadding.Width, -renderer.ControlClientPadding.Height);
			this.s = true;
			try
			{
				if (this.e != Rectangle.Empty)
				{
					this.a();
				}
				this.a(renderer, graphics, this.f);
				foreach (object obj in this.c)
				{
					DockControl dockControl = (DockControl)obj;
					if (dockControl.Parent != base.DockContainer)
					{
						if (dockControl.Parent != null)
						{
							TD.SandDock.g.a(dockControl);
						}
						base.DockContainer.Controls.Add(dockControl);
					}
					dockControl.Visible = (dockControl == this.i);
					dockControl.Bounds = bounds2;
				}
			}
			finally
			{
				this.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)this.c.Count)];
			int num3 = 0;
			foreach (object obj in this.c)
			{
				DockControl dockControl = (DockControl)obj;
				int num4 = (int)Math.Ceiling((double)A_1.MeasureString(dockControl.TabText, dockControl.Font, 999, EverettRenderer.StandardStringFormat).Width);
				num4 += 30;
				num += num4;
				array[num3++] = num4;
				dockControl.h = false;
			}
			if (num > num2)
			{
				int num5 = num - num2;
				for (int i = 0; i < num3; i++)
				{
					int[] array2;
					IntPtr intPtr;
					(array2 = array)[(int)(intPtr = (IntPtr)i)] = array2[(int)intPtr] - (int)((float)num5 * ((float)array[i] / (float)num));
					this.c[i].h = true;
				}
			}
			A_2 = A_0.TabStripMetrics.RemovePadding(A_2);
			int num6 = A_2.Left;
			num3 = 0;
			for (int j = 0; j < this.c.Count; j++)
			{
				DockControl dockControl2 = this.c[j];
				BoxModel tabMetrics = A_0.TabMetrics;
				Rectangle rectangle = new Rectangle(num6 + 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));
				dockControl2.c = rectangle;
				num6 += rectangle.Width + tabMetrics.ExtraWidth;
				num3++;
			}
		}

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

		private new const int a = 19;

		private new const int b = 15;

		private new ControlLayoutSystem.DockControlCollection c = null;

		private bool d = false;

		internal Rectangle e;

		internal new Rectangle f;

		internal new Rectangle g;

		internal Rectangle h;

		private DockControl 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 ControlLayoutSystem.a t;

		private bool u = false;

		private bool v = false;

		internal new delegate void a(DockControl A_0, DockControl A_1);

		public class DockControlCollection : CollectionBase
		{
			internal DockControlCollection(ControlLayoutSystem A_0)
			{
				this.a = A_0;
			}

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

			protected override void OnClear()
			{
				base.OnClear();
				DocumentContainer documentContainer = null;
				if (this.a != null)
				{
					documentContainer = (this.a.DockContainer as DocumentContainer);
				}
				foreach (object obj in this)
				{
					DockControl dockControl = (DockControl)obj;
					dockControl.Enter -= this.b;
					dockControl.Leave -= this.a;
					dockControl.b = null;
					if (documentContainer != null)
					{
						documentContainer.b(dockControl);
					}
				}
			}

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

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

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

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

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

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

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

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

			public DockControl this[int index]
			{
				get
				{
					return (DockControl)base.List[index];
				}
			}

			public void Remove(DockControl control)
			{
				if (this.a.IsInContainer && !this.a.DockContainer.IsFloating)
				{
					control.LastIndexInFixedLayoutSystem = this.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 ControlLayoutSystem a = null;

			private bool b = false;

			private bool c = false;
		}
	}
}
