using System;
using System.Drawing;
using System.Windows.Forms;

namespace TD.SandDock.Rendering
{
	public class EverettRenderer : RendererBase
	{
		internal static StringFormat StandardStringFormat
		{
			get
			{
				if (EverettRenderer.a == null)
				{
					EverettRenderer.a = new StringFormat(StringFormat.GenericDefault);
					EverettRenderer.a.Alignment = StringAlignment.Near;
					EverettRenderer.a.LineAlignment = StringAlignment.Center;
					EverettRenderer.a.Trimming = StringTrimming.EllipsisCharacter;
					EverettRenderer.a.FormatFlags |= StringFormatFlags.NoWrap;
				}
				return EverettRenderer.a;
			}
		}

		internal static StringFormat StandardVerticalStringFormat
		{
			get
			{
				if (EverettRenderer.b == null)
				{
					EverettRenderer.b = new StringFormat(StringFormat.GenericDefault);
					EverettRenderer.b.Alignment = StringAlignment.Near;
					EverettRenderer.b.LineAlignment = StringAlignment.Center;
					EverettRenderer.b.Trimming = StringTrimming.EllipsisCharacter;
					EverettRenderer.b.FormatFlags |= StringFormatFlags.NoWrap;
					EverettRenderer.b.FormatFlags |= StringFormatFlags.DirectionVertical;
				}
				return EverettRenderer.b;
			}
		}

		protected override void CalculateBaseColors()
		{
			this.l = this.a(this.BackgroundColor);
		}

		private Color a(Color A_0)
		{
			byte b = A_0.R;
			byte b2 = A_0.G;
			byte b3 = A_0.B;
			byte b4 = Math.Max(Math.Max(b, b2), b3);
			if (b4 == 0)
			{
				return Color.FromArgb(35, 35, 35);
			}
			byte b5 = (b4 > 220) ? (byte.MaxValue - b4) : 35;
			b += (byte)((double)((float)b5 * ((float)b / (float)b4)) + 0.5);
			b2 += (byte)((double)((float)b5 * ((float)b2 / (float)b4)) + 0.5);
			b3 += (byte)((double)((float)b5 * ((float)b3 / (float)b4)) + 0.5);
			return Color.FromArgb((int)b, (int)b2, (int)b3);
		}

		public Color CollapsedTabOutlineColor
		{
			get
			{
				return this.j;
			}
			set
			{
				this.j = value;
				base.CustomColors = true;
			}
		}

		public Color BackgroundTabForeColor
		{
			get
			{
				return this.i;
			}
			set
			{
				this.i = value;
			}
		}

		public Color HighlightColor
		{
			get
			{
				return this.h;
			}
			set
			{
				this.h = value;
				base.CustomColors = true;
			}
		}

		public Color ShadowColor
		{
			get
			{
				return this.g;
			}
			set
			{
				this.g = value;
				base.CustomColors = true;
			}
		}

		public Color TabStripBackgroundColor
		{
			get
			{
				return this.l;
			}
		}

		public Color DocumentContainerBackgroundColor
		{
			get
			{
				return this.f;
			}
			set
			{
				this.f = value;
				base.CustomColors = true;
			}
		}

		public Color BackgroundColor
		{
			get
			{
				return this.e;
			}
			set
			{
				this.e = value;
				base.CustomColors = true;
			}
		}

		public Color InactiveTitleBarColor
		{
			get
			{
				return this.c;
			}
			set
			{
				this.c = value;
				base.CustomColors = true;
			}
		}

		public Color ActiveTitleBarColor
		{
			get
			{
				return this.d;
			}
			set
			{
				this.d = value;
				base.CustomColors = true;
			}
		}

		protected internal override TabTextDisplayMode TabTextDisplay
		{
			get
			{
				return TabTextDisplayMode.SelectedTab;
			}
		}

		protected internal override Size DocumentClientPadding
		{
			get
			{
				return new Size(2, 2);
			}
		}

		protected internal override Size ControlClientPadding
		{
			get
			{
				return new Size(0, 0);
			}
		}

		protected internal override BoxModel TitleBarMetrics
		{
			get
			{
				if (this.v == null)
				{
					this.v = new BoxModel(0, SystemInformation.ToolWindowCaptionHeight + 2, 0, 0, 0, 0, 0, 0, 0, 2);
				}
				return this.v;
			}
		}

		protected internal override BoxModel TabMetrics
		{
			get
			{
				if (this.u == null)
				{
					this.u = new BoxModel(0, 0, 0, 0, 0, 0, 0, 0, 1, 0);
				}
				return this.u;
			}
		}

		protected internal override BoxModel TabStripMetrics
		{
			get
			{
				if (this.t == null)
				{
					this.t = new BoxModel(0, Control.DefaultFont.Height + 9, 4, 0, 5, 1, 0, 1, 0, 0);
				}
				return this.t;
			}
		}

		protected internal override void DrawControlClientBackground(Graphics graphics, Rectangle bounds, Color backColor)
		{
		}

		protected internal override void DrawDocumentClientBackground(Graphics graphics, Rectangle bounds, Color backColor)
		{
			using (SolidBrush solidBrush = new SolidBrush(backColor))
			{
				graphics.FillRectangle(solidBrush, bounds);
			}
		}

		protected internal override void DrawDocumentStripBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.FillRectangle(this.m, bounds);
			graphics.DrawLine(this.o, bounds.X, bounds.Bottom - 1, bounds.Right, bounds.Bottom - 1);
		}

		protected internal override void DrawDocumentContainerBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.Clear(this.f);
		}

		protected internal override Size MeasureDocumentStripTab(Graphics graphics, Image image, string text, Font font, DrawItemState state)
		{
			int num;
			if ((state & DrawItemState.Focus) == DrawItemState.Focus)
			{
				using (Font font2 = new Font(font, FontStyle.Bold))
				{
					num = (int)Math.Ceiling((double)graphics.MeasureString(text, font2, 999, EverettRenderer.StandardStringFormat).Width);
					goto IL_63;
				}
			}
			num = (int)Math.Ceiling((double)graphics.MeasureString(text, font, 999, EverettRenderer.StandardStringFormat).Width);
			IL_63:
			num += 2 + this.DocumentTabPadding * 2 + 2;
			if (image != null)
			{
				num += 20;
			}
			num += this.DocumentTabExtra;
			return new Size(num, 0);
		}

		protected internal override void DrawDocumentStripTab(Graphics graphics, Rectangle bounds, Image image, string text, Font font, Color backColor, Color foreColor, DrawItemState state, bool drawSeparator)
		{
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				using (SolidBrush solidBrush = new SolidBrush(backColor))
				{
					graphics.FillRectangle(solidBrush, bounds);
				}
				graphics.DrawLine(this.o, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 1);
				graphics.DrawLine(this.o, bounds.Left, bounds.Top, bounds.Right - 1, bounds.Top);
				graphics.DrawLine(this.n, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom - 1);
			}
			else if (drawSeparator)
			{
				graphics.DrawLine(SystemPens.ControlDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);
			}
			if (image != null)
			{
				graphics.DrawImage(image, bounds.X + 4, bounds.Y + 2, 16, 16);
				bounds.X += 20;
				bounds.Width -= 20;
			}
			if (bounds.Width > 8)
			{
				Font font2 = font;
				if ((state & DrawItemState.Focus) == DrawItemState.Focus)
				{
					font2 = new Font(font, FontStyle.Bold);
				}
				if ((state & DrawItemState.Selected) == DrawItemState.Selected)
				{
					using (SolidBrush solidBrush2 = new SolidBrush(foreColor))
					{
						graphics.DrawString(text, font2, solidBrush2, bounds, this.s);
						goto IL_186;
					}
				}
				graphics.DrawString(text, font2, this.r, bounds, this.s);
				IL_186:
				if ((state & DrawItemState.Focus) == DrawItemState.Focus)
				{
					font2.Dispose();
				}
			}
		}

		internal virtual int DocumentTabPadding
		{
			get
			{
				return 5;
			}
		}

		protected internal override int DocumentTabSize
		{
			get
			{
				return Control.DefaultFont.Height + 6;
			}
		}

		protected internal override int DocumentTabStripSize
		{
			get
			{
				return Control.DefaultFont.Height + 8;
			}
		}

		protected internal override int DocumentTabExtra
		{
			get
			{
				return 0;
			}
		}

		protected internal override void StartRenderSession()
		{
			this.m = new SolidBrush(this.l);
			this.n = new Pen(this.g);
			this.p = new Pen(this.e);
			this.o = new Pen(this.h);
			this.r = new SolidBrush(this.i);
			this.q = new Pen(this.j);
			this.s = new StringFormat(StringFormat.GenericDefault);
			this.s.FormatFlags = StringFormatFlags.NoWrap;
			this.s.Alignment = StringAlignment.Center;
			this.s.LineAlignment = StringAlignment.Center;
		}

		protected internal override void DrawSplitter(Graphics graphics, Rectangle bounds, Orientation orientation)
		{
			graphics.FillRectangle(SystemBrushes.Control, bounds);
		}

		protected internal override void DrawDocumentStripButton(Graphics graphics, Rectangle bounds, SandDockButtonType buttonType, DrawItemState state)
		{
			this.DrawButtonHighlight(graphics, bounds, state);
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				bounds.Offset(1, 1);
			}
			switch (buttonType)
			{
			case SandDockButtonType.Close:
				using (Pen pen = new Pen(this.k))
				{
					TD.SandDock.o.b(graphics, bounds, pen);
				}
				break;
			case SandDockButtonType.Pin:
				break;
			case SandDockButtonType.ScrollLeft:
				TD.SandDock.o.b(graphics, bounds, this.k, (state & DrawItemState.Disabled) != DrawItemState.Disabled);
				return;
			case SandDockButtonType.ScrollRight:
				TD.SandDock.o.a(graphics, bounds, this.k, (state & DrawItemState.Disabled) != DrawItemState.Disabled);
				return;
			default:
				return;
			}
		}

		internal virtual void DrawButtonHighlight(Graphics graphics, Rectangle bounds, DrawItemState state)
		{
			if ((state & DrawItemState.HotLight) == DrawItemState.HotLight)
			{
				Pen pen;
				Pen pen2;
				if ((state & DrawItemState.Selected) == DrawItemState.Selected)
				{
					pen = this.n;
					pen2 = this.o;
				}
				else
				{
					pen2 = this.n;
					pen = this.o;
				}
				graphics.DrawLine(pen, bounds.Left, bounds.Top, bounds.Right - 1, bounds.Top);
				graphics.DrawLine(pen, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 1);
				graphics.DrawLine(pen2, bounds.Right - 1, bounds.Bottom - 1, bounds.Right - 1, bounds.Top);
				graphics.DrawLine(pen2, bounds.Right - 1, bounds.Bottom - 1, bounds.Left, bounds.Bottom - 1);
			}
		}

		protected internal override void DrawTitleBarButton(Graphics graphics, Rectangle bounds, SandDockButtonType buttonType, DrawItemState state, bool focused, bool toggled)
		{
			bounds.Width--;
			bounds.Height--;
			this.DrawButtonHighlight(graphics, bounds, state);
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				bounds.Offset(1, 1);
			}
			switch (buttonType)
			{
			case SandDockButtonType.Close:
				TD.SandDock.o.a(graphics, bounds, focused ? SystemPens.ActiveCaptionText : SystemPens.ControlText);
				return;
			case SandDockButtonType.Pin:
				TD.SandDock.o.a(graphics, bounds, focused ? SystemPens.ActiveCaptionText : SystemPens.ControlText, toggled);
				return;
			default:
				return;
			}
		}

		protected internal override void DrawDockContainerBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.Clear(this.e);
		}

		protected internal override void DrawTabStripBackground(Graphics graphics, Rectangle bounds, int selectedTabOffset)
		{
			graphics.FillRectangle(this.m, bounds);
			graphics.DrawLine(this.n, bounds.X, bounds.Y, bounds.Right, bounds.Y);
		}

		protected internal override void DrawTabStripTab(Graphics graphics, Rectangle bounds, Image image, string text, Font font, Color backColor, Color foreColor, DrawItemState state, bool drawSeparator)
		{
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				using (SolidBrush solidBrush = new SolidBrush(backColor))
				{
					graphics.FillRectangle(solidBrush, bounds);
				}
				graphics.DrawLine(this.o, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 1);
				graphics.DrawLine(this.n, bounds.Left, bounds.Bottom - 1, bounds.Right, bounds.Bottom - 1);
				graphics.DrawLine(this.n, bounds.Right, bounds.Top, bounds.Right, bounds.Bottom - 1);
			}
			else if (drawSeparator)
			{
				graphics.DrawLine(SystemPens.ControlDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);
			}
			if (bounds.Width >= 24)
			{
				graphics.DrawImage(image, new Rectangle(bounds.X + 4, bounds.Y + 2, image.Width, image.Height));
			}
			bounds.X += 23;
			bounds.Width -= 25;
			if (bounds.Width > 8)
			{
				if ((state & DrawItemState.Selected) == DrawItemState.Selected)
				{
					using (SolidBrush solidBrush2 = new SolidBrush(foreColor))
					{
						graphics.DrawString(text, font, solidBrush2, bounds, EverettRenderer.StandardStringFormat);
						return;
					}
				}
				graphics.DrawString(text, font, this.r, bounds, EverettRenderer.StandardStringFormat);
			}
		}

		protected internal override void DrawCollapsedBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.FillRectangle(this.m, bounds);
		}

		protected internal override void DrawCollapsedTab(Graphics graphics, Rectangle bounds, DockSide dockSide, Image image, string text, Font font, Color backColor, Color foreColor, DrawItemState state, bool vertical)
		{
			using (SolidBrush solidBrush = new SolidBrush(backColor))
			{
				graphics.FillRectangle(solidBrush, bounds);
			}
			if (dockSide != DockSide.Top)
			{
				graphics.DrawLine(this.q, bounds.Left, bounds.Top, bounds.Right, bounds.Top);
			}
			if (dockSide != DockSide.Right)
			{
				graphics.DrawLine(this.q, bounds.Right, bounds.Top, bounds.Right, bounds.Bottom);
			}
			if (dockSide != DockSide.Bottom)
			{
				graphics.DrawLine(this.q, bounds.Left, bounds.Bottom, bounds.Right, bounds.Bottom);
			}
			if (dockSide != DockSide.Left)
			{
				graphics.DrawLine(this.q, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom);
			}
			bounds.Inflate(-2, -2);
			if (vertical)
			{
				bounds.Offset(0, 1);
			}
			else
			{
				bounds.Offset(1, 0);
			}
			graphics.DrawImage(image, new Rectangle(bounds.Left, bounds.Top, image.Width, image.Height));
			if (text.Length != 0)
			{
				if (vertical)
				{
					bounds.Offset(0, 23);
					graphics.DrawString(text, font, this.r, bounds, EverettRenderer.StandardVerticalStringFormat);
					return;
				}
				bounds.Offset(23, 0);
				graphics.DrawString(text, font, this.r, bounds, EverettRenderer.StandardStringFormat);
			}
		}

		protected internal override void DrawTitleBarBackground(Graphics graphics, Rectangle bounds, bool focused)
		{
			if (focused)
			{
				graphics.FillRectangle(SystemBrushes.ActiveCaption, bounds);
				return;
			}
			graphics.FillRectangle(SystemBrushes.Control, bounds);
			graphics.DrawLine(SystemPens.ControlDark, bounds.X + 1, bounds.Y, bounds.Right - 2, bounds.Y);
			graphics.DrawLine(SystemPens.ControlDark, bounds.X + 1, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);
			graphics.DrawLine(SystemPens.ControlDark, bounds.X, bounds.Y + 1, bounds.X, bounds.Bottom - 2);
			graphics.DrawLine(SystemPens.ControlDark, bounds.Right - 1, bounds.Y + 1, bounds.Right - 1, bounds.Bottom - 2);
		}

		protected internal override void DrawTitleBarText(Graphics graphics, Rectangle bounds, bool focused, string text, Font font)
		{
			Brush brush = focused ? SystemBrushes.ActiveCaptionText : SystemBrushes.ControlText;
			bounds.Inflate(-3, 0);
			graphics.DrawString(text, font, brush, bounds, EverettRenderer.StandardStringFormat);
		}

		protected internal override void FinishRenderSession()
		{
			this.m.Dispose();
			this.n.Dispose();
			this.p.Dispose();
			this.o.Dispose();
			this.r.Dispose();
			this.q.Dispose();
			this.s.Dispose();
		}

		public override string ToString()
		{
			return "Everett";
		}

		private static StringFormat a = null;

		private static StringFormat b = null;

		private Color c = SystemColors.InactiveCaption;

		private Color d = SystemColors.ActiveCaption;

		private Color e = SystemColors.Control;

		private Color f = SystemColors.AppWorkspace;

		private Color g = SystemColors.ControlText;

		private Color h = SystemColors.ControlLightLight;

		private Color i = SystemColors.ControlDarkDark;

		private Color j = SystemColors.ControlDark;

		private Color k = SystemColors.ControlDarkDark;

		private Color l;

		private SolidBrush m = null;

		private Pen n;

		private Pen o;

		private Pen p;

		private Pen q;

		private SolidBrush r;

		private StringFormat s;

		private BoxModel t = null;

		private BoxModel u = null;

		private BoxModel v = null;
	}
}
