using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using TD.Util;

namespace TD.SandDock
{
	internal class r : Control
	{
		private DockContainer m_a = null;

		private ControlLayoutSystem m_b = null;

		private k m_c = null;

		private p m_d = null;

		private Rectangle m_e;

		private Rectangle m_f;

		private TD.Util.a g = null;

		public r(DockContainer A_0, k A_1)
		{
			this.m_a = A_0;
			this.m_c = A_1;
			SetStyle(ControlStyles.AllPaintingInWmPaint, value: true);
			SetStyle(ControlStyles.DoubleBuffer, value: true);
			SetStyle(ControlStyles.ResizeRedraw, value: true);
			g = new TD.Util.a(this);
			g.a(A_0: false);
			g.a(a);
		}

		private void b(Point A_0)
		{
			this.m_d = new p(this.m_a, this, A_0);
			this.m_d.a((EventHandler)a);
			this.m_d.b((p.a)a);
		}

		private void a()
		{
			this.m_d.b((EventHandler)a);
			this.m_d.a((p.a)a);
			this.m_d = null;
		}

		private void a(object A_0, EventArgs A_1)
		{
			a();
		}

		private void a(int A_0)
		{
			a();
			Rectangle bounds = base.Bounds;
			switch (this.m_a.Dock)
			{
			case DockStyle.Left:
				bounds.Width = A_0;
				break;
			case DockStyle.Right:
				bounds.X = bounds.Right - A_0;
				bounds.Width = A_0;
				break;
			case DockStyle.Top:
				bounds.Height = A_0;
				break;
			case DockStyle.Bottom:
				bounds.Y = bounds.Bottom - A_0;
				bounds.Height = A_0;
				break;
			}
			base.Bounds = bounds;
			this.m_b.PopupSize = A_0;
		}

		[SpecialName]
		internal int f()
		{
			if (this.m_a.Dock == DockStyle.Left || this.m_a.Dock == DockStyle.Right)
			{
				return this.m_e.Width;
			}
			return this.m_e.Height;
		}

		[SpecialName]
		public bool c()
		{
			return this.m_d != null;
		}

		internal void b()
		{
			this.m_c.b();
		}

		[SpecialName]
		public ControlLayoutSystem d()
		{
			return this.m_b;
		}

		[SpecialName]
		public void a(ControlLayoutSystem A_0)
		{
			if (this.m_b != null)
			{
				this.m_b.PopupContainer = null;
			}
			this.m_b = A_0;
			if (this.m_b != null)
			{
				this.m_b.PopupContainer = this;
			}
			e();
		}

		internal void e()
		{
			if (this.m_b != null)
			{
				this.m_e = base.ClientRectangle;
				switch (this.m_a.Dock)
				{
				case DockStyle.Right:
				{
					ref Rectangle reference4 = ref this.m_f;
					reference4 = new Rectangle(this.m_e.X, this.m_e.Y, 4, this.m_e.Height);
					this.m_e.X += 4;
					this.m_e.Width -= 4;
					break;
				}
				case DockStyle.Left:
				{
					ref Rectangle reference3 = ref this.m_f;
					reference3 = new Rectangle(this.m_e.Right - 4, this.m_e.Y, 4, this.m_e.Height);
					this.m_e.Width -= 4;
					break;
				}
				case DockStyle.Top:
				{
					ref Rectangle reference2 = ref this.m_f;
					reference2 = new Rectangle(this.m_e.X, this.m_e.Bottom - 4, this.m_e.Width, 4);
					this.m_e.Height -= 4;
					break;
				}
				case DockStyle.Bottom:
				{
					ref Rectangle reference = ref this.m_f;
					reference = new Rectangle(this.m_e.X, this.m_e.Y, this.m_e.Width, 4);
					this.m_e.Y += 4;
					this.m_e.Height -= 4;
					break;
				}
				default:
					this.m_f = Rectangle.Empty;
					break;
				}
				foreach (DockControl control in this.m_b.Controls)
				{
					if (control.Parent != this)
					{
						if (control.Parent != null)
						{
							TD.SandDock.g.a((Control)control);
						}
						base.Controls.Add(control);
					}
				}
				this.m_b.LayoutCollapsed(this.m_a.WorkingRenderer, this.m_e);
				Invalidate();
			}
			else
			{
				if (base.ContainsFocus)
				{
					Focus();
				}
				while (base.Controls.get_Count() != 0)
				{
					base.Controls.RemoveAt(0);
				}
			}
		}

		protected override void c(EventArgs A_0)
		{
			base.OnResize(A_0);
			e();
		}

		protected override void a(bool A_0)
		{
			if (A_0)
			{
				this.m_a = null;
				a((ControlLayoutSystem)null);
				this.m_c = null;
				if (g != null)
				{
					g.c();
					g = null;
				}
				if (this.m_d != null)
				{
					a();
				}
			}
			Dispose(A_0);
		}

		protected override void d(EventArgs A_0)
		{
			base.OnEnter(A_0);
			if (this.m_b != null)
			{
				this.m_b.InvalidateTitleBar();
			}
		}

		protected override void a(EventArgs A_0)
		{
			base.OnLeave(A_0);
			if (this.m_b != null)
			{
				this.m_b.InvalidateTitleBar();
			}
		}

		private string a(Point A_0)
		{
			if (this.m_e.Contains(A_0) && this.m_b != null)
			{
				return this.m_b.GetToolTipText(A_0);
			}
			return "";
		}

		protected override void c(MouseEventArgs A_0)
		{
			base.OnMouseMove(A_0);
			if (this.m_f.Contains(A_0.X, A_0.Y) || this.m_d != null)
			{
				if (this.m_a.Dock == DockStyle.Left || this.m_a.Dock == DockStyle.Right)
				{
					Cursor = Cursors.VSplit;
				}
				else
				{
					Cursor = Cursors.HSplit;
				}
			}
			else
			{
				Cursor = Cursors.Default;
			}
			if (base.Capture && this.m_d != null)
			{
				this.m_d.a(new Point(A_0.X, A_0.Y));
			}
			else if (this.m_e.Contains(A_0.X, A_0.Y) && this.m_b != null)
			{
				this.m_b.OnMouseMove(A_0);
			}
		}

		protected override void b(EventArgs A_0)
		{
			base.OnMouseLeave(A_0);
			Cursor = Cursors.Default;
			if (this.m_b != null)
			{
				this.m_b.OnMouseLeave();
			}
		}

		protected override void b(MouseEventArgs A_0)
		{
			base.OnMouseDown(A_0);
			if (this.m_f.Contains(A_0.X, A_0.Y) && A_0.Button == MouseButtons.Left)
			{
				b(new Point(A_0.X, A_0.Y));
			}
			else if (this.m_e.Contains(A_0.X, A_0.Y) && this.m_b != null)
			{
				this.m_b.OnMouseDown(A_0);
			}
		}

		protected override void a(MouseEventArgs A_0)
		{
			base.OnMouseUp(A_0);
			if (this.m_d != null && A_0.Button == MouseButtons.Left)
			{
				this.m_d.a();
			}
			else if (this.m_e.Contains(A_0.X, A_0.Y) && this.m_b != null)
			{
				this.m_b.OnMouseUp(A_0);
			}
		}

		protected override void a(PaintEventArgs A_0)
		{
			this.m_a.WorkingRenderer.DrawDockContainerBackground(A_0.Graphics, base.ClientRectangle);
		}

		protected override void b(PaintEventArgs A_0)
		{
			try
			{
				this.m_a.WorkingRenderer.StartRenderSession();
				if (this.m_b != null)
				{
					this.m_b.Paint(this.m_a.WorkingRenderer, A_0.Graphics, Font);
				}
				this.m_a.WorkingRenderer.FinishRenderSession();
			}
			catch
			{
			}
		}
	}
}
