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

namespace vclass.whiteboard
{
	/// <summary>
	/// Summary description for WhiteBoard.
	/// </summary>
	public class WhiteBoard : System.Windows.Forms.Form
	{
		enum DrawingTypes {Paths=0 , Lines =1, Circles =2, Rectangles=3, Texts=4};
		/// <summary>
		///The Menu Items and Butons  
		/// </summary>

		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.MenuItem menuItem9;
		private vclass.whiteboard.NoBackPanel panel1;
		private System.Windows.Forms.Button Pen;
		private System.Windows.Forms.Button Rectangle;
		private System.Windows.Forms.Button Eraser;
		private System.Windows.Forms.Button line;
		private System.Windows.Forms.Button Selecter;
		private System.Windows.Forms.Button TextWrite;
		private System.Windows.Forms.Button Cirlce;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.ColorDialog colorDialog1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.FontDialog fontDialog1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox1;
		private Bitmap localBitmap;
		private string imageName;
		private Font textFont=new Font("Arial", 14);
		private int [] doStack=new int[1000];
		private int objCount=0;
		private int [] undoStack=new int[50];
		private DrawingObject [] redoStack =new DrawingObject[50];
		private int redoCount=0;
		///<summary>
		///The Drawing Objects
		///</summary>
		//private System.Drawing.Drawing2D.GraphicsPath [] mousePath=new System.Drawing.Drawing2D.GraphicsPath[100];
		private MousePath [] myPath = new MousePath[150];
		private int pathCount=0;
		//private System.Drawing.Rectangle [] rectangles = new System.Drawing.Rectangle[20];
		private Rect [] rectangles = new Rect[40];
		private int rectCount=0;
		private Rectangle textRec;

		private Ellips [] ellipses = new Ellips[40];
		private int ellipsCount=0;

		private Line [] lines = new Line[40];
		private int lineCount=0;

		private Text [] texts= new Text[40];
		private int textCount=0;
		
		private Color drawingColor=Color.Black;
		private Pen drawingPen=new Pen(Color.Black,1);
		private Pen textAreaPen=new Pen(Color.Black,1);
		private bool mouseDown=false;

		private Point startPoint=new Point(0,0);
		private int drawType=0;
		private System.Windows.Forms.Button Brush;
		private System.Windows.Forms.Button textButton;
		private System.Windows.Forms.Button undoButton;
		private System.Windows.Forms.Button redoButton;
		private System.Windows.Forms.Button imageButton;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;



		public WhiteBoard()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			myPath[0]= new MousePath(drawingPen);
			rectangles[0]=new Rect(drawingPen, 0, 0, 0, 0);
			ellipses[0]=new Ellips(drawingPen, 0, 0, 0, 0);
			lines[0]= new Line(drawingPen, 0,0,0,0);
			texts[0]=new Text(drawingPen,"", this.textRec, this.textFont);
			// add new event handler methods..
			this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_mouseDown);
			this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);
			this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
			this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer |ControlStyles.ResizeRedraw, true);

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

		public WhiteBoard (string fileName)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			myPath[0]= new MousePath(drawingPen);
			rectangles[0]=new Rect(drawingPen, 0, 0, 0, 0);
			ellipses[0]=new Ellips(drawingPen, 0, 0, 0, 0);
			lines[0]= new Line(drawingPen, 0,0,0,0);
			texts[0]=new Text(drawingPen,"", this.textRec, this.textFont);
			// add new event handler methods..
			this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_mouseDown);
			this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);
			this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
			this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer |ControlStyles.ResizeRedraw, true);
			
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			this.imageName=fileName;
		}

		/// <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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WhiteBoard));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.Pen = new System.Windows.Forms.Button();
			this.Rectangle = new System.Windows.Forms.Button();
			this.Eraser = new System.Windows.Forms.Button();
			this.line = new System.Windows.Forms.Button();
			this.Selecter = new System.Windows.Forms.Button();
			this.TextWrite = new System.Windows.Forms.Button();
			this.Cirlce = new System.Windows.Forms.Button();
			this.button7 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.panel1 = new vclass.whiteboard.NoBackPanel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.colorDialog1 = new System.Windows.Forms.ColorDialog();
			this.button1 = new System.Windows.Forms.Button();
			this.fontDialog1 = new System.Windows.Forms.FontDialog();
			this.button2 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.Brush = new System.Windows.Forms.Button();
			this.textButton = new System.Windows.Forms.Button();
			this.undoButton = new System.Windows.Forms.Button();
			this.redoButton = new System.Windows.Forms.Button();
			this.imageButton = new System.Windows.Forms.Button();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem2,
																					  this.menuItem3,
																					  this.menuItem4});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem5,
																					  this.menuItem6,
																					  this.menuItem7});
			this.menuItem1.Text = "File";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 0;
			this.menuItem5.Text = "Open";
			this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 1;
			this.menuItem6.Text = "New";
			this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 2;
			this.menuItem7.Text = "Close";
			this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem8,
																					  this.menuItem9});
			this.menuItem2.Text = "Edit";
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 0;
			this.menuItem8.Text = "Selet All";
			this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 1;
			this.menuItem9.Text = "Undo";
			this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 2;
			this.menuItem3.Text = "View";
			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 3;
			this.menuItem4.Text = "Tools";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// Pen
			// 
			this.Pen.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Pen.BackgroundImage")));
			this.Pen.Location = new System.Drawing.Point(32, 8);
			this.Pen.Name = "Pen";
			this.Pen.Size = new System.Drawing.Size(32, 32);
			this.Pen.TabIndex = 3;
			this.Pen.Text = "y";
			this.Pen.Click += new System.EventHandler(this.button1_Click);
			// 
			// Rectangle
			// 
			this.Rectangle.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Rectangle.BackgroundImage")));
			this.Rectangle.Location = new System.Drawing.Point(32, 48);
			this.Rectangle.Name = "Rectangle";
			this.Rectangle.Size = new System.Drawing.Size(32, 32);
			this.Rectangle.TabIndex = 4;
			this.Rectangle.Click += new System.EventHandler(this.Rectangle_Click);
			// 
			// Eraser
			// 
			this.Eraser.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Eraser.BackgroundImage")));
			this.Eraser.Location = new System.Drawing.Point(32, 128);
			this.Eraser.Name = "Eraser";
			this.Eraser.Size = new System.Drawing.Size(32, 32);
			this.Eraser.TabIndex = 5;
			this.Eraser.Click += new System.EventHandler(this.Eraser_Click);
			// 
			// line
			// 
			this.line.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("line.BackgroundImage")));
			this.line.Location = new System.Drawing.Point(96, 8);
			this.line.Name = "line";
			this.line.Size = new System.Drawing.Size(32, 32);
			this.line.TabIndex = 6;
			this.line.Click += new System.EventHandler(this.button4_Click);
			// 
			// Selecter
			// 
			this.Selecter.Location = new System.Drawing.Point(0, 0);
			this.Selecter.Name = "Selecter";
			this.Selecter.TabIndex = 0;
			// 
			// TextWrite
			// 
			this.TextWrite.Location = new System.Drawing.Point(0, 0);
			this.TextWrite.Name = "TextWrite";
			this.TextWrite.TabIndex = 0;
			// 
			// Cirlce
			// 
			this.Cirlce.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Cirlce.BackgroundImage")));
			this.Cirlce.Location = new System.Drawing.Point(96, 48);
			this.Cirlce.Name = "Cirlce";
			this.Cirlce.Size = new System.Drawing.Size(32, 32);
			this.Cirlce.TabIndex = 10;
			this.Cirlce.Click += new System.EventHandler(this.button11_Click);
			// 
			// button7
			// 
			this.button7.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button7.BackgroundImage")));
			this.button7.Location = new System.Drawing.Point(96, 88);
			this.button7.Name = "button7";
			this.button7.Size = new System.Drawing.Size(32, 32);
			this.button7.TabIndex = 11;
			this.button7.Click += new System.EventHandler(this.button7_Click);
			// 
			// button6
			// 
			this.button6.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button6.BackgroundImage")));
			this.button6.Location = new System.Drawing.Point(96, 128);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(32, 32);
			this.button6.TabIndex = 6;
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panel1.Location = new System.Drawing.Point(152, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(496, 416);
			this.panel1.TabIndex = 2;
			this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
			// 
			// panel2
			// 
			this.panel2.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.panel2.Location = new System.Drawing.Point(32, 176);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(24, 24);
			this.panel2.TabIndex = 12;
			this.panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(80, 176);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(56, 24);
			this.button1.TabIndex = 13;
			this.button1.Text = "Color";
			this.button1.Click += new System.EventHandler(this.button1_Click_1);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(8, 256);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(48, 24);
			this.button2.TabIndex = 14;
			this.button2.Text = "Font";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(0, 224);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(152, 20);
			this.textBox1.TabIndex = 15;
			this.textBox1.Text = "";
			// 
			// Brush
			// 
			this.Brush.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Brush.BackgroundImage")));
			this.Brush.Location = new System.Drawing.Point(32, 88);
			this.Brush.Name = "Brush";
			this.Brush.Size = new System.Drawing.Size(32, 32);
			this.Brush.TabIndex = 9;
			this.Brush.Click += new System.EventHandler(this.button12_Click);
			// 
			// textButton
			// 
			this.textButton.Location = new System.Drawing.Point(72, 256);
			this.textButton.Name = "textButton";
			this.textButton.Size = new System.Drawing.Size(56, 24);
			this.textButton.TabIndex = 16;
			this.textButton.Text = "AddText";
			this.textButton.Click += new System.EventHandler(this.textButton_Click);
			// 
			// undoButton
			// 
			this.undoButton.Location = new System.Drawing.Point(8, 288);
			this.undoButton.Name = "undoButton";
			this.undoButton.Size = new System.Drawing.Size(48, 24);
			this.undoButton.TabIndex = 17;
			this.undoButton.Text = "Undo";
			this.undoButton.Click += new System.EventHandler(this.undoButton_Click);
			// 
			// redoButton
			// 
			this.redoButton.Location = new System.Drawing.Point(72, 288);
			this.redoButton.Name = "redoButton";
			this.redoButton.Size = new System.Drawing.Size(56, 24);
			this.redoButton.TabIndex = 18;
			this.redoButton.Text = "Redo";
			this.redoButton.Click += new System.EventHandler(this.redoButton_Click);
			// 
			// imageButton
			// 
			this.imageButton.Location = new System.Drawing.Point(72, 320);
			this.imageButton.Name = "imageButton";
			this.imageButton.Size = new System.Drawing.Size(56, 24);
			this.imageButton.TabIndex = 19;
			this.imageButton.Text = "Image";
			this.imageButton.Click += new System.EventHandler(this.imageButton_Click);
			// 
			// WhiteBoard
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(656, 417);
			this.Controls.Add(this.imageButton);
			this.Controls.Add(this.redoButton);
			this.Controls.Add(this.undoButton);
			this.Controls.Add(this.textButton);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.button7);
			this.Controls.Add(this.line);
			this.Controls.Add(this.Eraser);
			this.Controls.Add(this.Rectangle);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.Cirlce);
			this.Controls.Add(this.Brush);
			this.Controls.Add(this.Pen);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.textBox1);
			this.ForeColor = System.Drawing.SystemColors.ControlText;
			this.KeyPreview = true;
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.Name = "WhiteBoard";
			this.Text = "Class++WhiteBoardTool";
			this.Load += new System.EventHandler(this.WhiteBoard_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void WhiteBoard_Load(object sender, System.EventArgs e)
		{
			this.localBitmap=new Bitmap(panel1.Width, panel1.Height);
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.drawType=0;
		}

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

		private void Rectangle_Click(object sender, System.EventArgs e)
		{
			this.drawType=(int)DrawingTypes.Rectangles;
		}

		private void button11_Click(object sender, System.EventArgs e)
		{
			this.drawType=(int)DrawingTypes.Circles;
		}

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

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

		private void button4_Click(object sender, System.EventArgs e)
		{
			this.drawType=(int)DrawingTypes.Lines;
		}

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

		private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			/*Graphics g=e.Graphics;
			Pen myPen=new Pen(this.drawingColor, 3);
			for(int i=0; i<=pathCount; i++)
			{
				g.DrawPath(myPen, mousePath[i]);
			}*/
			Graphics g = e.Graphics;
			//Pen drawingPen = new Pen(drawingColor,1);
			
			//create our offscreen bitmap
			//Bitmap localBitmap = new Bitmap(this.panel1.Width, this.panel1.Height);
			//localBitmap.SetPixel(15, 15, Color.Blue);
			
			//update for presentation
			//imageName="C:/desktop.jpg";
			if(this.imageName!=null)
			{
				Console.WriteLine("Filename : " +imageName);
				this.localBitmap=new Bitmap(Image.FromFile(this.imageName), panel1.Size);
				//bitmapGraphics = Graphics.FromImage(localBitmap);
			} 

			Graphics bitmapGraphics = Graphics.FromImage(localBitmap);
			bitmapGraphics.Clear(Color.White);

			//call our isolated drawing routing
			drawPaths(bitmapGraphics, drawingPen);
			
			//push our bitmap forward to the screen
			g.DrawImage(localBitmap, 0, 0);

			bitmapGraphics.Dispose();
			//drawingPen.Dispose();
			//localBitmap.Dispose();
			g.Dispose();
		}

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

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

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

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

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

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

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

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

		private void button7_Click(object sender, System.EventArgs e)
		{
			this.drawType=(int)DrawingTypes.Texts;
		}

		private void button1_Click_1(object sender, System.EventArgs e)
		{
			Color clr=new Color();
			this.colorDialog1.ShowDialog();
			clr = this.colorDialog1.Color;
			Console.WriteLine("Color Send : "+clr.ToArgb());
			string message = "<command><btn><color>"+clr.ToArgb()+"</color></btn></command>";
			((MainForm)(this.MdiParent)).getChatClient().Write(message);
			//panel2.BackColor=clr;
			//this.drawingColor=clr;
			//this.drawingPen=new Pen(drawingColor, 1);
		}

		public void changeColor(Color color)
		{
			this.drawingColor=color;
			panel2.BackColor=color;
			this.drawingPen=new Pen(drawingColor, 1);
		}
	
		private void panel2_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
		
		}
		
		/// <summary>
		/// Mouse Event Handler Methods For the Main Panel
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			mouseDown=false;
			if(this.drawType==4)
			{
				//this.textBox1=new TextBox();
				//this.textBox1.SetBounds(textRec.X, textRec.Y, textRec.Width, textRec.Height);
				//this.textBox1.Text="oldu";
				//this.textBox1.Size= new Size(textRec.Width, textRec.Height);
				//this.textBox1.Visible=true;
				//panel1.Invalidate();
			}
		}
		private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(mouseDown==true)
			{
				int mouseX = e.X;
				int mouseY = e.Y;
				string message = "<command><draw><point><x>"+mouseX+"</x><y>"+mouseY+"</y></point><type>"+this.drawType+"</type><mouse>move</mouse></draw></command>";
				((MainForm)(this.MdiParent)).getChatClient().Write(message);
			}

		}

		private void panel1_mouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			mouseDown=true;
			int mouseX = e.X;
			int mouseY = e.Y;
			string message = "<command><draw><point><x>"+mouseX+"</x><y>"+mouseY+"</y></point><type>"+this.drawType+"</type><mouse>down</mouse></draw></command>";
			((MainForm)(this.MdiParent)).getChatClient().Write(message);

		}

		public void enterMouseMove(int x,int y,int type)
		{	
			int mouseX = x;
			int mouseY = y;

			switch (type)
			{
				case 0 :
				{
					myPath[pathCount].addLine(mouseX,mouseY,mouseX,mouseY);
					panel1.Invalidate();
				}
					break;
				case 1:
				{
					lines[lineCount]=new Line(drawingPen, startPoint.X, startPoint.Y, mouseX, mouseY);
					panel1.Invalidate();
					break;
				}
					
				case 2:
				{
					int width=mouseX-startPoint.X;
					int height=mouseY-startPoint.Y;
					if(width>=0)
					{
						if(height>=0)
							ellipses[ellipsCount]=new Ellips(drawingPen, startPoint.X, startPoint.Y, width, height);
						else
							ellipses[ellipsCount]=new Ellips(drawingPen, startPoint.X, mouseY, width, -height);
					}
					else
					{
						if(height>=0)
							ellipses[ellipsCount]=new Ellips(drawingPen, mouseX, startPoint.Y, -width, height);
						else
							ellipses[ellipsCount]=new Ellips(drawingPen, mouseX, mouseY, -width, -height);
					}
					//rectangles[rectCount]=new Rect(drawingPen, startPoint.X, startPoint.Y, width, height);
					panel1.Invalidate();
					break;
				}
					
				case 3:
				{
					int width=mouseX-startPoint.X;
					int height=mouseY-startPoint.Y;
					if(width>=0)
					{
						if(height>=0)
							rectangles[rectCount]=new Rect(drawingPen, startPoint.X, startPoint.Y, width, height);
						else
							rectangles[rectCount]=new Rect(drawingPen, startPoint.X, mouseY, width, -height);
					}
					else
					{
						if(height>=0)
							rectangles[rectCount]=new Rect(drawingPen, mouseX, startPoint.Y, -width, height);
						else
							rectangles[rectCount]=new Rect(drawingPen, mouseX, mouseY, -width, -height);
					}
					//rectangles[rectCount]=new Rect(drawingPen, startPoint.X, startPoint.Y, width, height);
					panel1.Invalidate();
					break;
				}
				case 4:
				{
					int width=mouseX-startPoint.X;
					int height=mouseY-startPoint.Y;
					if(width>=0)
					{
						if(height>=0)
							this.textRec=new Rectangle(startPoint.X, startPoint.Y, width, height);
						else
							this.textRec=new Rectangle(startPoint.X, mouseY, width, -height);
					}
					else
					{
						if(height>=0)
							this.textRec = new Rectangle(mouseX, startPoint.Y, -width, height);
						else
							this.textRec = new Rectangle(mouseX, mouseY, -width, -height);
					}
					//rectangles[rectCount]=new Rect(drawingPen, startPoint.X, startPoint.Y, width, height);
					panel1.Invalidate();
					break;
				}
				default:
					break;
			}
			
		}
		public void enterMouseDown(int x,int y,int type)
		{
			int mouseX = x;
			int mouseY = y;
			switch (type)
			{
				case 0:
				{
					myPath[++pathCount]=new MousePath (drawingPen);
					myPath[pathCount].addLine(mouseX,mouseY,mouseX,mouseY);
					this.doStack[objCount++]=0;
					panel1.Invalidate();
					break;
				}
				case 1:
				{
					startPoint.X=mouseX;
					startPoint.Y=mouseY;
					lines[++lineCount]=new Line(drawingPen, startPoint.X, startPoint.Y, mouseX, mouseY);
					this.doStack[++objCount]=1;
					panel1.Invalidate();
					break;
				}

				case 2:
				{
					startPoint.X=mouseX;
					startPoint.Y=mouseY;
					ellipses[++ellipsCount]= new Ellips(drawingPen,startPoint.X, startPoint.Y, 0,0);
					this.doStack[++objCount]=2;
					panel1.Invalidate();
					break;
				}

				case 3:
				{
					startPoint.X=mouseX;
					startPoint.Y=mouseY;
					rectangles[++rectCount]= new Rect(drawingPen,startPoint.X, startPoint.Y, 0,0);
					this.doStack[++objCount]=3;
					panel1.Invalidate();
					break;
				}
				case 4:
				{
					
					startPoint.X=mouseX;
					startPoint.Y=mouseY;
					this.textRec = new Rectangle(startPoint.X, startPoint.Y, 0,0);
					//texts[++textCount]= new Text(drawingPen, startPoint);
					panel1.Invalidate();
					break;
				}
				default:
				{
					//startPoint.X=mouseX;
					//startPoint.Y=mouseY;
					//rectangles[++rectCount]= new Rect(drawingPen,startPoint.X, startPoint.Y, 0,0);
					//panel1.Invalidate();
					break;
				}
			}
		}
		private void drawPaths(Graphics g, Pen myPen)
		{
			int i;
			for(i=0; i<=pathCount; i++)
			{
				myPath[i].DrawObject(g);
				//g.DrawPath(myPen, mousePath[i]);
			}
			for(i=0; i<=rectCount; i++)
			{
				rectangles[i].DrawObject(g);
			}
			g.DrawRectangle(textAreaPen, this.textRec);
			//this.textRec.DrawObject(g);
			
			for(i=0; i<=textCount; i++)
			{
				texts[i].DrawObject(g);
			}

			for(i=0; i<=ellipsCount; i++)
			{
				ellipses[i].DrawObject(g);
			}
			for(i=0; i<=lineCount; i++)
			{
				lines[i].DrawObject(g);
			}
			for(i=0; i<=textCount; i++)
			{
				texts[i].DrawObject(g);
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.fontDialog1.ShowDialog();
			Font fnt=this.fontDialog1.Font;
			Console.WriteLine("Sending Font : " + fnt.ToHfont().ToInt32().ToString());
			string message = "<command><btn><font>"+fnt.ToHfont().ToInt32().ToString()+"</font></btn></command>";
			((MainForm)(this.MdiParent)).getChatClient().Write(message);
		}
		public void changeFont(Font fnt)
		{
			this.textFont=fnt;
		}

		private void textButton_Click(object sender, System.EventArgs e)
		{
			if(this.drawType==(int)DrawingTypes.Texts)
				Console.WriteLine("Written Text is " + this.textBox1.Text );
			string message = "<command><btn><text>"+ this.textBox1.Text+" </text></btn></command>";
			((MainForm)(this.MdiParent)).getChatClient().Write(message);
		}
		public void pushButton(String textToWrite)
		{
			texts[++textCount]=new Text(this.drawingPen, textToWrite, this.textRec, this.textFont);
			this.doStack[++objCount]=4;
			this.textRec = new Rectangle(0,0,0,0);
			panel1.Invalidate();
		}

		private void undoButton_Click(object sender, System.EventArgs e)
		{
			string message = "<command><btn><undo></undo></btn></command>";
			((MainForm)(this.MdiParent)).getChatClient().Write(message);
		}
		public void undo()
		{
			if(objCount==0)
			{
				return;
			}
			int objtype=doStack[objCount];
			//enum DrawingTypes {Paths=0 , Lines =1, Circles =2, Rectangles=3, Texts=4};
			switch (objtype)
			{
				case 0:
				{
					this.redoStack[++this.redoCount]=this.myPath[pathCount--];
					break;
				}
				case 1:
				{
					this.redoStack[++this.redoCount]=this.lines[lineCount--];
					break;
				}
				case 2:
				{
					this.redoStack[++this.redoCount]=this.ellipses[ellipsCount--];
					break;
				}
				case 3:
				{
					this.redoStack[++this.redoCount]=this.rectangles[rectCount--];
					break;
				}
				case 4:
				{
					this.redoStack[++this.redoCount]=this.texts[textCount--];
					break;
				}
				default : 
					break;
			}
			this.undoStack[this.redoCount]=objtype;
			this.objCount--;
			panel1.Invalidate();
		}

		private void redoButton_Click(object sender, System.EventArgs e)
		{
			string message = "<command><btn><redo></redo></btn></command>";
			((MainForm)(this.MdiParent)).getChatClient().Write(message);
		}

		public void redo()
		{
			if(redoCount==0)
			{
				return;
			}
			int objtype=undoStack[redoCount];
			//enum DrawingTypes {Paths=0 , Lines =1, Circles =2, Rectangles=3, Texts=4};
			switch (objtype)
			{
				case 0:
				{
					this.myPath[++pathCount]=(MousePath)this.redoStack[this.redoCount--];
					break;
				}
				case 1:
				{
					this.lines[++lineCount]=(Line)this.redoStack[this.redoCount--];
					break;
				}
				case 2:
				{
					this.ellipses[++ellipsCount]=(Ellips)this.redoStack[this.redoCount--];
					break;
				}
				case 3:
				{
					this.rectangles[++rectCount]=(Rect)this.redoStack[this.redoCount--];
					break;
				}
				case 4:
				{
					this.texts[++textCount]=(Text)this.redoStack[this.redoCount--];
					break;
				}
				default : 
					break;
			}
			this.objCount++;
			panel1.Invalidate();
		}

		private void imageButton_Click(object sender, System.EventArgs e)
		{
			this.openFileDialog1.ShowDialog();
			this.imageName=this.openFileDialog1.FileName;
			string message = "<command><btn><image>"+ this.imageName+"</image></btn></command>";
			((MainForm)(this.MdiParent)).getChatClient().Write(message);
		}
		public void loadImage(string image)
		{
			this.imageName=image;
			Console.WriteLine("Image loaded :" + this.imageName);
			panel1.Invalidate();
		}
	}
}