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..6ddaf97e0d 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs @@ -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,59 @@ namespace ICSharpCode.TextEditor.Document min = oldPosition; max = newPosition; } + + if (min == max) { + return; + } + if (!HasSomethingSelected) { SetSelection(new DefaultSelection(document, min, max)); 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; - } - } - } 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) { + // 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 (newPosition.Y >= selectionStart.Y) { + // selecting down + if(GreaterEqPos(newPosition, selectionStart)) { + selection.StartPosition = selectionStart; + // this handles last line selection + //if( textArea.Document.TotalNumberOfLines - 1 == newPosition.Y) + if (selectFrom.where == WhereFrom.Gutter ) //&& newPosition.Y != oldPosition.Y) + //selection.EndPosition = NextValidPosition(newPosition.Y - 1); + selection.EndPosition = new Point(textArea.Caret.Column, textArea.Caret.Line); + else { + newPosition.X = oldnewX; selection.EndPosition = newPosition; - changed = true; } + } else { // generally this occurs if the selection is on the same line, at a point less than the start position + selection.StartPosition = newPosition; + selection.EndPosition = selectionStart; } + } else { // selecting up + 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 +260,9 @@ 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; + } ClearWithoutUpdate(); document.CommitUpdate(); @@ -364,7 +326,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/TextArea.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs index 4bdc2e721f..10addd8b38 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs @@ -442,7 +442,14 @@ namespace ICSharpCode.TextEditor lastMouseInMargin = null; } if (textView.DrawingPosition.Contains(e.X, e.Y)) { - this.Cursor = textView.Cursor; + Point realmousepos = TextView.GetLogicalPosition(e.X - TextView.DrawingPosition.X, e.Y - TextView.DrawingPosition.Y); + if(SelectionManager.IsSelected(Document.PositionToOffset(realmousepos)) && MouseButtons == MouseButtons.None) { + // mouse is hovering over a selection, so show default mouse + this.Cursor = Cursors.Default; + } else { + // mouse is hovering over text area, not a selection, so show the textView cursor + this.Cursor = textView.Cursor; + } return; } this.Cursor = Cursors.Default; diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs index e19e30f98e..2ab6418eba 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs @@ -143,15 +143,14 @@ namespace ICSharpCode.TextEditor void TextAreaMouseMove(object sender, MouseEventArgs e) { - Point mousepos = textArea.mousepos; + textArea.mousepos = e.Location; //textArea.mousepos; // honour the starting selection strategy switch (textArea.SelectionManager.selectFrom.where) { case WhereFrom.Gutter: - //moveGutter(sender, e); ExtendSelectionToMouse(); - break; + return; case WhereFrom.TArea: break; @@ -167,8 +166,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); @@ -220,30 +219,11 @@ namespace ICSharpCode.TextEditor textArea.Caret.Position = textArea.SelectionManager.NextValidPosition(realmousepos.Y); } } - else + else { textArea.Caret.Position = realmousepos; - - if (minSelection != nilPoint && textArea.SelectionManager.SelectionCollection.Count > 0) { - // Extend selection when selection was started with double-click - ISelection selection = textArea.SelectionManager.SelectionCollection[0]; - Point min = textArea.SelectionManager.GreaterEqPos(minSelection, maxSelection) ? maxSelection : minSelection; - Point max = textArea.SelectionManager.GreaterEqPos(minSelection, maxSelection) ? minSelection : maxSelection; - if (textArea.SelectionManager.GreaterEqPos(max, realmousepos) && textArea.SelectionManager.GreaterEqPos(realmousepos, min)) { - textArea.SelectionManager.SetSelection(min, max); - } else if (textArea.SelectionManager.GreaterEqPos(max, realmousepos)) { - //textArea.SelectionManager.SetSelection(realmousepos, max); - int moff = textArea.Document.PositionToOffset(realmousepos); - min = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, moff)); - textArea.SelectionManager.SetSelection(min, max); - } else { - //textArea.SelectionManager.SetSelection(min, realmousepos); - int moff = textArea.Document.PositionToOffset(realmousepos); - max = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, moff)); - textArea.SelectionManager.SetSelection(min, max); - } - } else { - textArea.SelectionManager.ExtendSelection(oldPos, textArea.Caret.Position); } + + textArea.SelectionManager.ExtendSelection(oldPos, textArea.Caret.Position); textArea.SetDesiredColumn(); } @@ -278,7 +258,9 @@ namespace ICSharpCode.TextEditor break; } + textArea.Caret.Position = maxSelection; textArea.SelectionManager.ExtendSelection(minSelection, maxSelection); + } // HACK WARNING !!! // must refresh here, because when a error tooltip is showed and the underlined @@ -289,12 +271,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) { @@ -311,6 +293,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); @@ -319,6 +302,15 @@ namespace ICSharpCode.TextEditor DoubleClickSelectionExtend(); lastTime = DateTime.Now; lastmousedownpos = new Point(e.X, e.Y); + + if(minSelection != nilPoint && maxSelection != nilPoint) { + textArea.SelectionManager.SelectionCollection[0].StartPosition = minSelection; + textArea.SelectionManager.SelectionCollection[0].EndPosition = maxSelection; + textArea.SelectionManager.selectionStart = minSelection; + + minSelection = nilPoint; + maxSelection = nilPoint; + } return; } } @@ -454,6 +446,7 @@ namespace ICSharpCode.TextEditor return; } + textArea.SelectionManager.selectFrom.where = WhereFrom.TArea; doubleclick = true; }