From 5b6a66edab58e6a653d1b9d6658c85cfc3405a09 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 8 Jul 2007 14:44:44 +0000 Subject: [PATCH] Applied text editor selection patch by Troy Simpson. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2598 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Actions/SelectionActions.cs | 8 +- .../Document/Selection/SelectionManager.cs | 147 +++++++----------- .../Project/Src/Gui/TextAreaMouseHandler.cs | 43 ++++- 3 files changed, 98 insertions(+), 100 deletions(-) diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/SelectionActions.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/SelectionActions.cs index a72fdcae6f..17f73929c8 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/SelectionActions.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/SelectionActions.cs @@ -156,7 +156,13 @@ namespace ICSharpCode.TextEditor.Actions return; } } - textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, startPoint, endPoint)); + textArea.Caret.Position = textArea.SelectionManager.NextValidPosition(endPoint.Y); + textArea.SelectionManager.ExtendSelection(startPoint, endPoint); + // after a SelectWholeDocument selection, the caret is placed correctly, + // but it is not positioned internally. The effect is when the cursor + // is moved up or down a line, the caret will take on the column that + // it was in before the SelectWholeDocument + textArea.SetDesiredColumn(); } } diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs index 975ff69508..3ea265afb4 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs @@ -22,7 +22,7 @@ namespace ICSharpCode.TextEditor.Document TextArea textArea; internal SelectFrom selectFrom = new SelectFrom(); - List selectionCollection = new List(); + internal List selectionCollection = new List(); /// /// A collection containing all selections. @@ -154,11 +154,17 @@ namespace ICSharpCode.TextEditor.Document public void ExtendSelection(Point oldPosition, Point newPosition) { - if (oldPosition == newPosition) { + // where oldposition is where the cursor was, + // and newposition is where it has ended up from a click (both zero based) + + if (oldPosition == newPosition) + { return; } + Point min; Point max; + int oldnewX = newPosition.X; bool oldIsGreater = GreaterEqPos(oldPosition, newPosition); if (oldIsGreater) { min = newPosition; @@ -167,104 +173,57 @@ namespace ICSharpCode.TextEditor.Document min = oldPosition; max = newPosition; } - if (!HasSomethingSelected) { + + if (min == max) { + return; + } + + if (!HasSomethingSelected) + { SetSelection(new DefaultSelection(document, min, max)); + // initialise selectFrom for a cursor selection + if (selectFrom.where == WhereFrom.None) + selectionStart = oldPosition; //textArea.Caret.Position; return; } + ISelection selection = this.selectionCollection[0]; - bool changed = false; - if (selection.ContainsPosition(newPosition)) { - if (oldIsGreater) { - if (selection.EndPosition != newPosition) { - selection.EndPosition = newPosition; - changed = true; - } - } else { - if (selection.StartPosition != newPosition) { - // we're back to the line we started from and it was a gutter selection - if (selectFrom.where == WhereFrom.Gutter && newPosition.Y >= selectionStart.Y) - { - selection.StartPosition = selectionStart; - selection.EndPosition = NextValidPosition(selection.StartPosition.Y); - } else { - selection.StartPosition = newPosition; - } - changed = true; - } - } + + if (min == max) { + //selection.StartPosition = newPosition; + return; } else { - if (oldPosition == selection.StartPosition) { - if (GreaterEqPos(newPosition, selection.EndPosition)) { - if (selection.StartPosition != selection.EndPosition || - selection.EndPosition != newPosition) { - selection.StartPosition = selection.EndPosition; - selection.EndPosition = newPosition; - changed = true; - } - } else { - if (selection.StartPosition != newPosition) { - selection.StartPosition = newPosition; - changed = true; - } + // changed selection via gutter + if (selectFrom.where == WhereFrom.Gutter) + { + // selection new position is always at the left edge for gutter selections + newPosition.X = 0; + } + + if (GreaterEqPos(newPosition, selectionStart)) // selecting forward + { + selection.StartPosition = selectionStart; + // this handles last line selection + if (selectFrom.where == WhereFrom.Gutter ) //&& newPosition.Y != oldPosition.Y) + selection.EndPosition = new Point(textArea.Caret.Column, textArea.Caret.Line); + else { + newPosition.X = oldnewX; + selection.EndPosition = newPosition; } - } else { - if (GreaterEqPos(selection.StartPosition, newPosition)) { - if (selection.EndPosition != selection.StartPosition || - selection.StartPosition != newPosition) { - changed = true; - } - if (selectFrom.where == WhereFrom.Gutter) - { - if (selectFrom.first == WhereFrom.Gutter) - { - if(newPosition.Y == selectionStart.Y) { - selection.EndPosition = NextValidPosition(selection.StartPosition.Y); - selection.StartPosition = new Point(0, newPosition.Y); - } else { - selection.EndPosition = NextValidPosition(selection.StartPosition.Y); - selection.StartPosition = newPosition; - } - } else { - if(newPosition.Y == selectionStart.Y) { - selection.EndPosition = NextValidPosition(selection.StartPosition.Y); - selection.StartPosition = new Point(selectionStart.X, newPosition.Y); - } else { - selection.EndPosition = new Point(selectionStart.X, selection.StartPosition.Y); - selection.StartPosition = newPosition; - } - } - } else { - selection.EndPosition = selection.StartPosition; - selection.StartPosition = newPosition; - } - changed = true; - } else { - if (selection.EndPosition != newPosition) { - selection.EndPosition = newPosition; - changed = true; - } + } else { // selecting back + if (selectFrom.where == WhereFrom.Gutter && selectFrom.first == WhereFrom.Gutter) + { // gutter selection + selection.EndPosition = NextValidPosition(selectionStart.Y); + } else { // internal text selection + selection.EndPosition = selectionStart; //selection.StartPosition; } + selection.StartPosition = newPosition; } } - -// if (GreaterEqPos(selection.StartPosition, min) && GreaterEqPos(selection.EndPosition, max)) { -// if (oldIsGreater) { -// selection.StartPosition = min; -// } else { -// selection.StartPosition = max; -// } -// } else { -// if (oldIsGreater) { -// selection.EndPosition = min; -// } else { -// selection.EndPosition = max; -// } -// } - if (changed) { - document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, min.Y, max.Y)); - document.CommitUpdate(); - OnSelectionChanged(EventArgs.Empty); - } + + document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, min.Y, max.Y)); + document.CommitUpdate(); + OnSelectionChanged(EventArgs.Empty); } // retrieve the next available line @@ -299,8 +258,10 @@ namespace ICSharpCode.TextEditor.Document // positions because it is always called before a new selection selectFrom.first = selectFrom.where; selectionStart = textArea.TextView.GetLogicalPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y); - if(selectFrom.where == WhereFrom.Gutter) + if(selectFrom.where == WhereFrom.Gutter) { selectionStart.X = 0; +// selectionStart.Y = -1; + } ClearWithoutUpdate(); document.CommitUpdate(); @@ -364,7 +325,7 @@ namespace ICSharpCode.TextEditor.Document { return GetSelectionAt(offset) != null; } - + /// /// Returns a object giving the selection in which /// the offset points to. diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs index 7bf8d1c678..354918fa85 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs @@ -146,7 +146,8 @@ namespace ICSharpCode.TextEditor void TextAreaMouseMove(object sender, MouseEventArgs e) { - Point mousepos = textArea.mousepos; + //Point mousepos = textArea.mousepos; + textArea.mousepos = e.Location; // honour the starting selection strategy switch (textArea.SelectionManager.selectFrom.where) @@ -154,7 +155,7 @@ namespace ICSharpCode.TextEditor case WhereFrom.Gutter: //moveGutter(sender, e); ExtendSelectionToMouse(); - break; + return; case WhereFrom.TArea: break; @@ -170,8 +171,8 @@ namespace ICSharpCode.TextEditor textArea.mousepos = new Point(e.X, e.Y); if (clickedOnSelectedText) { - if (Math.Abs(mousedownpos.X - mousepos.X) >= SystemInformation.DragSize.Width / 2 || - Math.Abs(mousedownpos.Y - mousepos.Y) >= SystemInformation.DragSize.Height / 2) + if (Math.Abs(mousedownpos.X - e.X) >= SystemInformation.DragSize.Width / 2 || + Math.Abs(mousedownpos.Y - e.Y) >= SystemInformation.DragSize.Height / 2) { clickedOnSelectedText = false; ISelection selection = textArea.SelectionManager.GetSelectionAt(textArea.Caret.Offset); @@ -226,6 +227,7 @@ namespace ICSharpCode.TextEditor else textArea.Caret.Position = realmousepos; + // moves selection across whole words if (minSelection != nilPoint && textArea.SelectionManager.SelectionCollection.Count > 0) { // Extend selection when selection was started with double-click ISelection selection = textArea.SelectionManager.SelectionCollection[0]; @@ -281,8 +283,24 @@ namespace ICSharpCode.TextEditor break; } + textArea.Caret.Position = maxSelection; textArea.SelectionManager.ExtendSelection(minSelection, maxSelection); } + + if (textArea.SelectionManager.selectionCollection.Count > 0) { + ISelection selection = textArea.SelectionManager.selectionCollection[0]; + + selection.StartPosition = minSelection; + selection.EndPosition = maxSelection; + textArea.SelectionManager.selectionStart = minSelection; + } + + // after a double-click selection, the caret is placed correctly, + // but it is not positioned internally. The effect is when the cursor + // is moved up or down a line, the caret will take on the column first + // clicked on for the double-click + textArea.SetDesiredColumn(); + // HACK WARNING !!! // must refresh here, because when a error tooltip is showed and the underlined // code is double clicked the textArea don't update corrctly, updateline doesn't @@ -292,12 +310,12 @@ namespace ICSharpCode.TextEditor } } - DateTime lastTime = DateTime.Now; void OnMouseDown(object sender, MouseEventArgs e) { Point mousepos; - mousepos = textArea.mousepos; + textArea.mousepos = e.Location; + mousepos = e.Location; if (dodragdrop) { @@ -314,6 +332,7 @@ namespace ICSharpCode.TextEditor textArea.SelectionManager.selectFrom.where = WhereFrom.TArea; button = e.Button; + // double-click if (button == MouseButtons.Left && (DateTime.Now - lastTime).Milliseconds < SystemInformation.DoubleClickTime) { int deltaX = Math.Abs(lastmousedownpos.X - e.X); int deltaY = Math.Abs(lastmousedownpos.Y - e.Y); @@ -322,6 +341,17 @@ namespace ICSharpCode.TextEditor DoubleClickSelectionExtend(); lastTime = DateTime.Now; lastmousedownpos = new Point(e.X, e.Y); + + if(textArea.SelectionManager.selectFrom.where == WhereFrom.Gutter) { + if(minSelection != nilPoint && maxSelection != nilPoint && textArea.SelectionManager.SelectionCollection.Count > 0) { + textArea.SelectionManager.SelectionCollection[0].StartPosition = minSelection; + textArea.SelectionManager.SelectionCollection[0].EndPosition = maxSelection; + textArea.SelectionManager.selectionStart = minSelection; + + minSelection = nilPoint; + maxSelection = nilPoint; + } + } return; } } @@ -457,6 +487,7 @@ namespace ICSharpCode.TextEditor return; } + textArea.SelectionManager.selectFrom.where = WhereFrom.TArea; doubleclick = true; }