/// <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);
        }
		public void OnWordMouseDown(ref WordMouseEventArgs e)
		{
			if (WordMouseDown != null)
				WordMouseDown(this, ref e);
		}
		private void SetMouseCursor(int x, int y)
		{
			if (_CodeEditor.LockCursorUpdate)
			{
				this.Cursor = _CodeEditor.Cursor;
				return;
			}

			if (View.Action == XTextAction.xtDragText)
			{
				this.Cursor = System.Windows.Forms.Cursors.Hand;
				//Cursor.Current = Cursors.Hand;
			}
			else
			{
				if (x < View.TotalMarginWidth)
				{
					if (x < View.GutterMarginWidth)
					{
                        this.Cursor = System.Windows.Forms.Cursors.Arrow;
					}
					else
					{
						Assembly assembly = GetType().Assembly;

                        Stream strm = assembly.GetManifestResourceStream("AIMS.Libraries.CodeEditor.FlippedCursor.cur");
                        this.Cursor = new Cursor(strm);
					}
				}
				else
				{
					if (x > View.TextMargin - 8)
					{
						if (IsOverSelection(x, y))
							this.Cursor = System.Windows.Forms.Cursors.Arrow;
						else
						{
							TextPoint tp = this.Painter.CharFromPixel(x, y);
							Word w = this.Document.GetWordFromPos(tp);
							if (w != null && w.Pattern != null && w.Pattern.Category != null)
							{
								WordMouseEventArgs e = new WordMouseEventArgs();
								e.Pattern = w.Pattern;
								e.Button = MouseButtons.None;
								e.Cursor = System.Windows.Forms.Cursors.Hand;
								e.Word = w;

								this._CodeEditor.OnWordMouseHover(ref e);

								this.Cursor = e.Cursor;
							}
							else
								this.Cursor = System.Windows.Forms.Cursors.IBeam;
						}
					}
					else
					{
						this.Cursor = System.Windows.Forms.Cursors.Arrow;
					}
				}
			}
		}
		public void OnWordMouseHover(ref WordMouseEventArgs e)
		{
			if (WordMouseHover != null)
				WordMouseHover(this, ref e);
		}