using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Telerik.QuickStart.WinControls
{
	class MainFormContentControl : Control
	{
		private Bitmap headerleft;
		private Bitmap headermiddle;
		private Bitmap headerright;

		public MainFormContentControl()
		{
			SetStyle(
				ControlStyles.AllPaintingInWmPaint | 
				ControlStyles.OptimizedDoubleBuffer | 
				//ControlStyles.Opaque | 
				ControlStyles.ResizeRedraw, true);

			headerleft = LoadImage("Telerik.QuickStart.WinControls.Resources.Images.headerleft.bmp");
			headermiddle = LoadImage("Telerik.QuickStart.WinControls.Resources.Images.headermiddle.bmp");
			headerright = LoadImage("Telerik.QuickStart.WinControls.Resources.Images.headerright.bmp");
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			e.Graphics.Clear(Color.White);
			Rectangle r = new Rectangle(0, 0, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
			using (GraphicsPath path = new GraphicsPath())
			{
				path.AddArc(new RectangleF(r.Left, r.Top, 18, 18), 180, 90);
				path.AddArc(new RectangleF(r.Right - 18, r.Top, 18, 18), 270, 90);
				path.AddArc(new RectangleF(r.Right - 18, r.Bottom - 18, 18, 18), 0, 90);
				path.AddArc(new RectangleF(r.Left, r.Bottom - 18, 18, 18), 90, 90);
				path.CloseFigure();

				using (Pen pen = new Pen(Color.FromArgb(196, 196, 196)))
				using (SolidBrush brush = new SolidBrush(Color.FromArgb(248, 248, 248)))
				{
					e.Graphics.FillPath(brush, path);
					e.Graphics.DrawPath(pen, path);

					for (int i = 0; i <= this.Width - headermiddle.Width; i += this.headermiddle.Width)
						e.Graphics.DrawImage(headermiddle, i, 0);
					e.Graphics.DrawImage(headerleft, 0, 0);
					e.Graphics.DrawImage(headerright, this.Width - headerright.Width, 0);

					e.Graphics.FillRectangle(Brushes.White, new Rectangle(227, 10, 3, this.Height - 1));
					e.Graphics.DrawLine(pen, 227, 10, 227, this.Height - 1);
					e.Graphics.DrawLine(pen, 230, 10, 230, this.Height - 1);
				}
			}
		}

		private Bitmap LoadImage(string name)
		{
			Stream stream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream(name);
			if (stream == null)
			{
				throw new NullReferenceException("Cannot find " + name);
			}
			return new Bitmap(stream);
		}
	}
}
