using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Drawing.Drawing2D;

namespace Canvas_Bubbles
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private C1.C1Flash.C1FlashCanvas c1FlashCanvas1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Panel panel_DrawColor;
		private System.Windows.Forms.NumericUpDown upd_LineWidth;
		private System.Windows.Forms.RadioButton rb_Chord;
		private System.Windows.Forms.RadioButton rb_Rectangle;
		private System.Windows.Forms.RadioButton rb_Oval;
		private System.Windows.Forms.RadioButton rb_Pie;
		private System.Windows.Forms.ColorDialog colorDialog1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.RadioButton rb_Solid;
		private System.Windows.Forms.RadioButton rb_Image;
		private System.Windows.Forms.RadioButton rb_Gradient;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.c1FlashCanvas1 = new C1.C1Flash.C1FlashCanvas();
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.rb_Chord = new System.Windows.Forms.RadioButton();
			this.rb_Rectangle = new System.Windows.Forms.RadioButton();
			this.rb_Oval = new System.Windows.Forms.RadioButton();
			this.rb_Pie = new System.Windows.Forms.RadioButton();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.upd_LineWidth = new System.Windows.Forms.NumericUpDown();
			this.label2 = new System.Windows.Forms.Label();
			this.panel_DrawColor = new System.Windows.Forms.Panel();
			this.label3 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.rb_Gradient = new System.Windows.Forms.RadioButton();
			this.button2 = new System.Windows.Forms.Button();
			this.rb_Solid = new System.Windows.Forms.RadioButton();
			this.rb_Image = new System.Windows.Forms.RadioButton();
			this.colorDialog1 = new System.Windows.Forms.ColorDialog();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.upd_LineWidth)).BeginInit();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(392, 32);
			this.label1.TabIndex = 3;
			this.label1.Text = "This sample demonstrates methods of drawing shapes or filling shapes using the C1" +
				"FlashCanvas class.";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(16, 112);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(136, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "Draw Shapes";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// rb_Chord
			// 
			this.rb_Chord.Location = new System.Drawing.Point(256, 32);
			this.rb_Chord.Name = "rb_Chord";
			this.rb_Chord.Size = new System.Drawing.Size(64, 24);
			this.rb_Chord.TabIndex = 12;
			this.rb_Chord.Text = "Chord";
			// 
			// rb_Rectangle
			// 
			this.rb_Rectangle.Checked = true;
			this.rb_Rectangle.Location = new System.Drawing.Point(8, 32);
			this.rb_Rectangle.Name = "rb_Rectangle";
			this.rb_Rectangle.Size = new System.Drawing.Size(80, 24);
			this.rb_Rectangle.TabIndex = 10;
			this.rb_Rectangle.TabStop = true;
			this.rb_Rectangle.Text = "Rectangle";
			// 
			// rb_Oval
			// 
			this.rb_Oval.Location = new System.Drawing.Point(96, 32);
			this.rb_Oval.Name = "rb_Oval";
			this.rb_Oval.Size = new System.Drawing.Size(64, 24);
			this.rb_Oval.TabIndex = 9;
			this.rb_Oval.Text = "Oval";
			// 
			// rb_Pie
			// 
			this.rb_Pie.Location = new System.Drawing.Point(176, 32);
			this.rb_Pie.Name = "rb_Pie";
			this.rb_Pie.Size = new System.Drawing.Size(56, 24);
			this.rb_Pie.TabIndex = 11;
			this.rb_Pie.Text = "Pie";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rb_Oval,
																					this.rb_Rectangle,
																					this.rb_Pie,
																					this.rb_Chord});
			this.groupBox1.Location = new System.Drawing.Point(24, 72);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(384, 72);
			this.groupBox1.TabIndex = 13;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Choose a shape";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.upd_LineWidth,
																					this.label2,
																					this.panel_DrawColor,
																					this.label3,
																					this.button1});
			this.groupBox2.Location = new System.Drawing.Point(24, 160);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(176, 160);
			this.groupBox2.TabIndex = 14;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Draw";
			// 
			// upd_LineWidth
			// 
			this.upd_LineWidth.Location = new System.Drawing.Point(88, 24);
			this.upd_LineWidth.Maximum = new System.Decimal(new int[] {
																		  10,
																		  0,
																		  0,
																		  0});
			this.upd_LineWidth.Minimum = new System.Decimal(new int[] {
																		  1,
																		  0,
																		  0,
																		  0});
			this.upd_LineWidth.Name = "upd_LineWidth";
			this.upd_LineWidth.Size = new System.Drawing.Size(64, 20);
			this.upd_LineWidth.TabIndex = 16;
			this.upd_LineWidth.Value = new System.Decimal(new int[] {
																		1,
																		0,
																		0,
																		0});
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 16;
			this.label2.Text = "Line Width:";
			// 
			// panel_DrawColor
			// 
			this.panel_DrawColor.BackColor = System.Drawing.Color.Red;
			this.panel_DrawColor.Location = new System.Drawing.Point(96, 56);
			this.panel_DrawColor.Name = "panel_DrawColor";
			this.panel_DrawColor.Size = new System.Drawing.Size(32, 32);
			this.panel_DrawColor.TabIndex = 16;
			this.panel_DrawColor.Click += new System.EventHandler(this.panel_DrawColor_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 64);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 17;
			this.label3.Text = "Line Color:";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rb_Gradient,
																					this.button2,
																					this.rb_Solid,
																					this.rb_Image});
			this.groupBox3.Location = new System.Drawing.Point(216, 160);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(192, 160);
			this.groupBox3.TabIndex = 15;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Fill";
			// 
			// rb_Gradient
			// 
			this.rb_Gradient.Location = new System.Drawing.Point(24, 48);
			this.rb_Gradient.Name = "rb_Gradient";
			this.rb_Gradient.TabIndex = 4;
			this.rb_Gradient.Text = "Gradient";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(32, 112);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(136, 24);
			this.button2.TabIndex = 3;
			this.button2.Text = "Fill Shapes";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// rb_Solid
			// 
			this.rb_Solid.Checked = true;
			this.rb_Solid.Location = new System.Drawing.Point(24, 24);
			this.rb_Solid.Name = "rb_Solid";
			this.rb_Solid.TabIndex = 0;
			this.rb_Solid.TabStop = true;
			this.rb_Solid.Text = "Solid Color";
			// 
			// rb_Image
			// 
			this.rb_Image.Location = new System.Drawing.Point(24, 72);
			this.rb_Image.Name = "rb_Image";
			this.rb_Image.TabIndex = 17;
			this.rb_Image.Text = "Texture";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(432, 358);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox3,
																		  this.groupBox2,
																		  this.groupBox1,
																		  this.label1});
			this.Name = "Form1";
			this.Text = "ComponentOne C1Flash Sample - Bubbles";
			this.Disposed += new System.EventHandler(this.Form1_Dispose);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.upd_LineWidth)).EndInit();
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private string tempdir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

		private void Form1_Dispose(object sender, System.EventArgs e)
		{
			if (_pen != null)
				_pen.Dispose();
		}

		private void panel_DrawColor_Click(object sender, System.EventArgs e)
		{
			this.colorDialog1.Color = this.panel_DrawColor.BackColor;
			this.colorDialog1.ShowDialog();
			this.panel_DrawColor.BackColor = this.colorDialog1.Color;
		}

		private void LaunchViewer(string filename)
		{
			try
			{
				System.Diagnostics.Process.Start(filename);
			}
			catch (Exception e)
			{
				System.Diagnostics.Process.Start("IEXPLORE.EXE", filename);
			}
		}

		private Random _rand = new Random();
		private Color RandomColor()
		{
			return Color.FromArgb(_rand.Next(255), _rand.Next(255), _rand.Next(255), _rand.Next(255));
		}

		private int _minSize = 10;
		private Rectangle RandomRectangle()
		{
			int maxWidth = this.c1FlashCanvas1.Width;
			int maxHeight = this.c1FlashCanvas1.Height;
			Point pos = new Point(_rand.Next(_minSize, maxWidth), _rand.Next(_minSize, maxHeight));
			int width = _rand.Next(_minSize, maxWidth/2);
			int height = _rand.Next(_minSize, maxHeight/2);

			return new Rectangle(pos, new Size(width, height));
		}

		private int RandomAngle()
		{
			return _rand.Next(0, 360);
		}

		private void FillShapes(Brush brush)
		{
			if (rb_Rectangle.Checked)
				this.c1FlashCanvas1.FillRectangle(brush, RandomRectangle());

			if (rb_Oval.Checked)
				this.c1FlashCanvas1.FillEllipse(brush, RandomRectangle());

			if (rb_Pie.Checked)
				this.c1FlashCanvas1.FillPie(brush, RandomRectangle(), RandomAngle(), RandomAngle());

			if (rb_Chord.Checked)
				this.c1FlashCanvas1.FillChord(brush, RandomRectangle(), RandomAngle(), RandomAngle());
		}

		private void FillImage(TextureBrush brush)
		{
			Rectangle rect =  RandomRectangle();
			if (rb_Rectangle.Checked)
			{

				this.c1FlashCanvas1.FillRectangle(brush, rect);
				this.c1FlashCanvas1.DrawRectangle(Pens.SkyBlue, rect);
			}

			if (rb_Oval.Checked)
			{
				this.c1FlashCanvas1.FillEllipse(brush, rect);
				this.c1FlashCanvas1.DrawEllipse(Pens.SkyBlue, rect);
			}

			if (rb_Pie.Checked)
			{
				int sa = RandomAngle();
				int ea = RandomAngle();
				this.c1FlashCanvas1.FillPie(brush, rect, sa, ea);
				this.c1FlashCanvas1.DrawPie(Pens.SkyBlue, rect, sa, ea);
			}

			if (rb_Chord.Checked)
			{
				int sa = RandomAngle();
				int ea = RandomAngle();
				this.c1FlashCanvas1.FillChord(brush, rect, sa, ea);
				this.c1FlashCanvas1.DrawChord(Pens.SkyBlue, rect, sa, ea);
			}
		}

		private Pen	_pen = new Pen(Color.Black);
		private void button1_Click(object sender, System.EventArgs e)
		{
			this.c1FlashCanvas1.Clear(Color.White);

			_pen.Color = this.panel_DrawColor.BackColor;
			_pen.Width = (int)this.upd_LineWidth.Value;

			if (rb_Rectangle.Checked)
			{
				for(int i = 0; i < 20; i++)
					this.c1FlashCanvas1.DrawRectangle(_pen, RandomRectangle());
			}

			if (rb_Oval.Checked)
			{
				for(int i = 0; i < 20; i++)
					this.c1FlashCanvas1.DrawEllipse(_pen, RandomRectangle());
			}

			if (rb_Pie.Checked)
			{
				for(int i = 0; i < 20; i++)
					this.c1FlashCanvas1.DrawPie(_pen, RandomRectangle(), RandomAngle(), RandomAngle());
			}

			if (rb_Chord.Checked)
			{
				for(int i = 0; i < 20; i++)
					this.c1FlashCanvas1.DrawChord(_pen, RandomRectangle(), RandomAngle(), RandomAngle());
			}

			this.c1FlashCanvas1.RenderToFile(tempdir + @"\c1flash_canvas_bubbles.swf");
			LaunchViewer(tempdir + @"\c1flash_canvas_bubbles.swf");
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.c1FlashCanvas1.Clear(Color.White);
			if (this.rb_Solid.Checked)
			{
				SolidBrush brush = new SolidBrush(Color.Black);
				for(int i = 0; i < 20; i++)
				{
					brush.Color = RandomColor();
					this.FillShapes(brush);
				}
				brush.Dispose();
			}

			if (this.rb_Image.Checked)
			{
				Assembly a = Assembly.GetExecutingAssembly();
				string an = a.GetName().Name;
				Bitmap bmp = new Bitmap(a.GetManifestResourceStream(an + ".paper.gif"));
				TextureBrush tb = new TextureBrush(bmp);

				for(int i = 0; i < 10; i++)
					this.FillImage(tb);

				bmp.Dispose();
				tb.Dispose();
			}

			if (this.rb_Gradient.Checked)
			{
				LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(100, 0), Color.Blue, Color.Red);
				for(int i = 0; i < 20; i++)
				{
					brush.LinearColors = new Color[2] {RandomColor(), RandomColor()};
					this.FillShapes(brush);
				}
				brush.Dispose();
			}

			this.c1FlashCanvas1.RenderToFile(tempdir + @"\c1flash_canvas_bubbles.swf");
			LaunchViewer(tempdir + @"\c1flash_canvas_bubbles.swf");
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}
	}
}
