/// <summary>
		///  Overrides the default OnDoubleClick
		/// </summary>
		/// <param name="e"></param>
        protected override void OnDoubleClick(EventArgs e)
        {
            TextPoint pos = Painter.CharFromPixel(MouseX, MouseY);
            Row row = null;
            if (pos.Y >= 0 && pos.Y < this.Document.Count)
                row = this.Document[pos.Y];

            #region RowEvent

            RowMouseEventArgs rea = new RowMouseEventArgs();
            rea.Row = row;
            rea.Button = MouseButtons.None;
            rea.MouseX = MouseX;
            rea.MouseY = MouseY;
            if (MouseX >= this.View.TextMargin - 7)
            {
                rea.Area = RowArea.TextArea;
            }
            else if (MouseX < this.View.GutterMarginWidth)
            {
                rea.Area = RowArea.GutterArea;
            }
            else if (MouseX < this.View.LineNumberMarginWidth + this.View.GutterMarginWidth)
            {
                rea.Area = RowArea.LineNumberArea;
            }
            else if (MouseX < this.View.TextMargin - 7)
            {
                rea.Area = RowArea.FoldingArea;
            }

            this.OnRowDoubleClick(rea);

            #endregion

            //try
            //{
            Row r2 = Document[pos.Y];
            if (r2 != null)
            {
                if (MouseX >= r2.Expansion_PixelEnd && r2.IsCollapsed)
                {
                    if (r2.Expansion_StartSegment != null)
                    {
                        if (r2.Expansion_StartSegment.StartRow != null && r2.Expansion_StartSegment.EndRow != null && r2.Expansion_StartSegment.Expanded == false)
                        {
                            r2.Expanded = true;
                            this.Document.ResetVisibleRows();
                            this.Redraw();
                            return;
                        }
                    }
                }
            }
            //}
            //catch
            //{
            //    //this is untested code...
            //}

            if (MouseX > this.View.TotalMarginWidth)
                SelectCurrentWord();
        }
		protected override void OnClick(EventArgs e)
		{
			TextPoint pos = Painter.CharFromPixel(MouseX, MouseY);
			Row row = null;
			if (pos.Y >= 0 && pos.Y < this.Document.Count)
				row = this.Document[pos.Y];

			#region RowEvent

			RowMouseEventArgs rea = new RowMouseEventArgs();
			rea.Row = row;
			rea.Button = MouseButtons.None;
			rea.MouseX = MouseX;
			rea.MouseY = MouseY;
			if (MouseX >= this.View.TextMargin - 7)
			{
				rea.Area = RowArea.TextArea;
			}
			else if (MouseX < this.View.GutterMarginWidth)
			{
				rea.Area = RowArea.GutterArea;
			}
			else if (MouseX < this.View.LineNumberMarginWidth + this.View.GutterMarginWidth)
			{
				rea.Area = RowArea.LineNumberArea;
			}
			else if (MouseX < this.View.TextMargin - 7)
			{
				rea.Area = RowArea.FoldingArea;
			}

			this.OnRowClick(rea);

			#endregion
		}
		/// <summary>
		/// Overrides the default OnMouseMove
		/// </summary>
		/// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            MouseX = e.X;
            MouseY = e.Y;
            MouseButton = e.Button;

            TextPoint pos = Painter.CharFromPixel(e.X, e.Y);
            Row row = null;
            if (pos.Y >= 0 && pos.Y < this.Document.Count)
                row = this.Document[pos.Y];

            #region RowEvent

            RowMouseEventArgs rea = new RowMouseEventArgs();
            rea.Row = row;
            rea.Button = e.Button;
            rea.MouseX = MouseX;
            rea.MouseY = MouseY;
            if (e.X >= this.View.TextMargin - 7)
            {
                rea.Area = RowArea.TextArea;
            }
            else if (e.X < this.View.GutterMarginWidth)
            {
                rea.Area = RowArea.GutterArea;
            }
            else if (e.X < this.View.LineNumberMarginWidth + this.View.GutterMarginWidth)
            {
                rea.Area = RowArea.LineNumberArea;
            }
            else if (e.X < this.View.TextMargin - 7)
            {
                rea.Area = RowArea.FoldingArea;
            }

            this.OnRowMouseMove(rea);

            #endregion

            //try
            //{
            if (Document != null)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (View.Action == XTextAction.xtSelect)
                    {
                        //Selection ACTIONS!!!!!!!!!!!!!!
                        Caret.Blink = true;
                        Caret.SetPos(pos);
                        if (e.X <= View.TotalMarginWidth)
                            Caret.MoveDown(true);
                        Caret.CropPosition();
                        Selection.MakeSelection();
                        ScrollIntoView();
                        this.Redraw();

                    }
                    else if (View.Action == XTextAction.xtNone)
                    {
                        if (IsOverSelection(e.X, e.Y))
                        {
                            BeginDragDrop();
                        }
                    }
                    else if (View.Action == XTextAction.xtDragText)
                    {
                    }
                }
                else
                {
                    TextPoint p = pos;
                    Row r = Document[p.Y];
                    bool DidShow = false;

                    if (r != null)
                    {
                        if (e.X >= r.Expansion_PixelEnd && r.IsCollapsed)
                        {
                            if (r.Expansion_StartSegment != null)
                            {
                                if (r.Expansion_StartSegment.StartRow != null && r.Expansion_StartSegment.EndRow != null && r.Expansion_StartSegment.Expanded == false)
                                {
                                    string t = "";
                                    int j = 0;
                                    for (int i = r.Expansion_StartSegment.StartRow.Index; i <= r.Expansion_StartSegment.EndRow.Index; i++)
                                    {
                                        if (j > 0)
                                            t += "\n";
                                        Row tmp = Document[i];
                                        string tmpstr = tmp.Text.Replace("\t", "    ");
                                        t += tmpstr;
                                        if (j > 20)
                                        {
                                            t += "...";
                                            break;
                                        }

                                        j++;
                                    }


                                    //tooltip.res
                                    tooltip.InitialDelay = this.TooltipDelay;
                                    if (tooltip.GetToolTip(this) != t)
                                        tooltip.SetToolTip(this, t);
                                    tooltip.Active = true;
                                    DidShow = true;
                                }
                            }
                        }
                        else
                        {
                            Word w = this.Document.GetFormatWordFromPos(p);
                            if (w != null)
                            {
                                if (w.InfoTip != null)
                                {
                                    tooltip.InitialDelay = this.TooltipDelay;
                                    if (tooltip.GetToolTip(this) != w.InfoTip)
                                        tooltip.SetToolTip(this, w.InfoTip);
                                    tooltip.Active = true;
                                    DidShow = true;
                                }
                            }
                        }
                    }

                    if (this.tooltip != null)
                    {
                        if (!DidShow)
                            tooltip.SetToolTip(this, "");
                    }
                }

                SetMouseCursor(e.X, e.Y);
                base.OnMouseMove(e);
            }

            //}
            //catch
            //{
            //}
        }
		/// <summary>
		/// Overrides the default OnMouseUp
		/// </summary>
		/// <param name="e"></param>
		protected override void OnMouseUp(MouseEventArgs e)
		{
			MouseX = e.X;
			MouseY = e.Y;
			MouseButton = e.Button;

			TextPoint pos = Painter.CharFromPixel(e.X, e.Y);
			Row row = null;
			if (pos.Y >= 0 && pos.Y < this.Document.Count)
				row = this.Document[pos.Y];

			#region RowEvent

			RowMouseEventArgs rea = new RowMouseEventArgs();
			rea.Row = row;
			rea.Button = e.Button;
			rea.MouseX = MouseX;
			rea.MouseY = MouseY;
			if (e.X >= this.View.TextMargin - 7)
			{
				rea.Area = RowArea.TextArea;
			}
			else if (e.X < this.View.GutterMarginWidth)
			{
				rea.Area = RowArea.GutterArea;
			}
			else if (e.X < this.View.LineNumberMarginWidth + this.View.GutterMarginWidth)
			{
				rea.Area = RowArea.LineNumberArea;
			}
			else if (e.X < this.View.TextMargin - 7)
			{
				rea.Area = RowArea.FoldingArea;
			}

			this.OnRowMouseUp(rea);

			#endregion

			if (View.Action == XTextAction.xtNone)
			{
				if (e.X > View.TotalMarginWidth)
				{
					if (IsOverSelection(e.X, e.Y) && e.Button == MouseButtons.Left)
					{
						View.Action = XTextAction.xtSelect;
						Caret.SetPos(Painter.CharFromPixel(e.X, e.Y));
						Selection.ClearSelection();
						this.Redraw();
					}
				}
			}

			View.Action = XTextAction.xtNone;
			base.OnMouseUp(e);
		}
		private void OnRowDoubleClick(RowMouseEventArgs e)
		{
			if (RowDoubleClick != null)
				RowDoubleClick(this, e);
		}
		/// <summary>
		/// Overrides the default OnMouseDown
		/// </summary>
		/// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            // this.Select();
            MouseX = e.X;
            MouseY = e.Y;
            MouseButton = e.Button;

            SetFocus();
            base.OnMouseDown(e);
            TextPoint pos = Painter.CharFromPixel(e.X, e.Y);
            Row row = null;
            if (pos.Y >= 0 && pos.Y < this.Document.Count)
                row = this.Document[pos.Y];

            #region RowEvent

            RowMouseEventArgs rea = new RowMouseEventArgs();
            rea.Row = row;
            rea.Button = e.Button;
            rea.MouseX = MouseX;
            rea.MouseY = MouseY;
            if (e.X >= this.View.TextMargin - 7)
            {
                rea.Area = RowArea.TextArea;
            }
            else if (e.X < this.View.GutterMarginWidth)
            {
                rea.Area = RowArea.GutterArea;
            }
            else if (e.X < this.View.LineNumberMarginWidth + this.View.GutterMarginWidth)
            {
                rea.Area = RowArea.LineNumberArea;
            }
            else if (e.X < this.View.TextMargin - 7)
            {
                rea.Area = RowArea.FoldingArea;
            }

            this.OnRowMouseDown(rea);

            #endregion

            //try
            //{
            Row r2 = Document[pos.Y];
            if (r2 != null)
            {
                if (e.X >= r2.Expansion_PixelEnd && r2.IsCollapsed)
                {
                    if (r2.Expansion_StartSegment != null)
                    {
                        if (r2.Expansion_StartSegment.StartRow != null && r2.Expansion_StartSegment.EndRow != null && r2.Expansion_StartSegment.Expanded == false)
                        {
                            if (!IsOverSelection(e.X, e.Y))
                            {
                                this.Caret.Position.X = pos.X;
                                this.Caret.Position.Y = pos.Y;
                                this.Selection.ClearSelection();

                                Row r3 = r2.Expansion_EndRow;
                                int x3 = r3.Expansion_StartChar;

                                this.Caret.Position.X = x3;
                                this.Caret.Position.Y = r3.Index;
                                this.Selection.MakeSelection();

                                this.Redraw();
                                View.Action = XTextAction.xtSelect;

                                return;

                            }
                        }
                    }
                }
            }
            //}
            //catch
            //{
            //    //this is untested code...
            //}

            bool shift = NativeUser32Api.IsKeyPressed(Keys.ShiftKey);

            if (e.X > View.TotalMarginWidth)
            {
                if (e.X > View.TextMargin - 8)
                {
                    if (!IsOverSelection(e.X, e.Y))
                    {
                        //selecting
                        if (e.Button == MouseButtons.Left)
                        {
                            if (!shift)
                            {
                                TextPoint tp = pos;
                                Word w = this.Document.GetWordFromPos(tp);
                                if (w != null && w.Pattern != null && w.Pattern.Category != null)
                                {
                                    WordMouseEventArgs pe = new WordMouseEventArgs();
                                    pe.Pattern = w.Pattern;
                                    pe.Button = e.Button;
                                    pe.Cursor = System.Windows.Forms.Cursors.Hand;
                                    pe.Word = w;

                                    this._CodeEditor.OnWordMouseDown(ref pe);

                                    this.Cursor = pe.Cursor;
                                    return;
                                }

                                View.Action = XTextAction.xtSelect;
                                Caret.SetPos(pos);
                                Selection.ClearSelection();
                                Caret.Blink = true;
                                this.Redraw();
                            }
                            else
                            {
                                View.Action = XTextAction.xtSelect;
                                Caret.SetPos(pos);
                                Selection.MakeSelection();
                                Caret.Blink = true;
                                this.Redraw();
                            }
                        }
                    }
                }
                else
                {
                    if (row.Expansion_StartSegment != null)
                    {
                        Caret.SetPos(new TextPoint(0, pos.Y));
                        Selection.ClearSelection();
                        this.Document.ToggleRow(row);
                        this.Redraw();
                    }
                }
            }
            else
            {
                if (e.X < View.GutterMarginWidth)
                {
                    if (_CodeEditor.AllowBreakPoints)
                    {
                        Row r = Document[Painter.CharFromPixel(e.X, e.Y).Y];
                        r.Breakpoint = !r.Breakpoint;
                        this.Redraw();
                    }
                    else
                    {
                        Row r = Document[Painter.CharFromPixel(e.X, e.Y).Y];
                        r.Breakpoint = false;
                        this.Redraw();
                    }
                }
                else
                {
                    View.Action = XTextAction.xtSelect;
                    Caret.SetPos(Painter.CharFromPixel(0, e.Y));
                    Selection.ClearSelection();
                    Caret.MoveDown(true);

                    this.Redraw();
                }
            }
            SetMouseCursor(e.X, e.Y);
        }
		private void OnRowMouseMove(RowMouseEventArgs e)
		{
			if (RowMouseMove != null)
				RowMouseMove(this, e);
		}
		private void OnRowMouseUp(RowMouseEventArgs e)
		{
			if (RowMouseUp != null)
				RowMouseUp(this, e);
		}
		private void View_RowMouseUp(object sender, RowMouseEventArgs e)
		{
			OnRowMouseUp(e);
		}
		private void OnRowMouseDown(RowMouseEventArgs e)
		{
			if (RowMouseDown != null)
				RowMouseDown(this, e);
		}
		private void View_RowDoubleClick(object sender, RowMouseEventArgs e)
		{
			OnRowDoubleClick(e);
		}
		protected virtual void OnRowDoubleClick(RowMouseEventArgs e)
		{
			if (RowDoubleClick != null)
				RowDoubleClick(this, e);
		}
		protected virtual void OnRowMouseUp(RowMouseEventArgs e)
		{
			if (RowMouseUp != null)
				RowMouseUp(this, e);
		}
		protected virtual void OnRowMouseMove(RowMouseEventArgs e)
		{
			if (RowMouseMove != null)
				RowMouseMove(this, e);
		}
		protected virtual void OnRowMouseDown(RowMouseEventArgs e)
		{
			if (RowMouseDown != null)
				RowMouseDown(this, e);
		}