From 988c450347be108654b9bb19dac7a4903d147399 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 23 Jun 2007 13:22:14 +0000 Subject: [PATCH] Revert text editor selection code to that currently used by SD 2.1 - keyboard selection was broken by an old version of Troy Simpson's patch. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2570 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Document/Selection/SelectionManager.cs | 142 +++++++++++------- .../Project/Src/Gui/TextAreaMouseHandler.cs | 51 ++++--- 2 files changed, 119 insertions(+), 74 deletions(-) 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 6ddaf97e0d..975ff69508 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs @@ -154,17 +154,11 @@ namespace ICSharpCode.TextEditor.Document public void ExtendSelection(Point oldPosition, Point 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) - { + if (oldPosition == newPosition) { return; } - Point min; Point max; - int oldnewX = newPosition.X; bool oldIsGreater = GreaterEqPos(oldPosition, newPosition); if (oldIsGreater) { min = newPosition; @@ -173,59 +167,104 @@ 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]; - - if (min == max) { - //selection.StartPosition = newPosition; - return; - } else { - // changed selection via gutter - if (selectFrom.where == WhereFrom.Gutter) { - // selection new position is always at the left edge for gutter selections - newPosition.X = 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 (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; + } 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 { // 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; + } 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; + } } - selection.StartPosition = newPosition; } } - - document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, min.Y, max.Y)); - document.CommitUpdate(); - OnSelectionChanged(EventArgs.Empty); + +// 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); + } } // retrieve the next available line @@ -260,9 +299,8 @@ 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(); @@ -326,7 +364,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 6c2829d63f..7bf8d1c678 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs @@ -146,14 +146,15 @@ namespace ICSharpCode.TextEditor void TextAreaMouseMove(object sender, MouseEventArgs e) { - textArea.mousepos = e.Location; //textArea.mousepos; + Point mousepos = textArea.mousepos; // honour the starting selection strategy switch (textArea.SelectionManager.selectFrom.where) { case WhereFrom.Gutter: + //moveGutter(sender, e); ExtendSelectionToMouse(); - return; + break; case WhereFrom.TArea: break; @@ -169,8 +170,8 @@ namespace ICSharpCode.TextEditor textArea.mousepos = new Point(e.X, e.Y); if (clickedOnSelectedText) { - if (Math.Abs(mousedownpos.X - e.X) >= SystemInformation.DragSize.Width / 2 || - Math.Abs(mousedownpos.Y - e.Y) >= SystemInformation.DragSize.Height / 2) + if (Math.Abs(mousedownpos.X - mousepos.X) >= SystemInformation.DragSize.Width / 2 || + Math.Abs(mousedownpos.Y - mousepos.Y) >= SystemInformation.DragSize.Height / 2) { clickedOnSelectedText = false; ISelection selection = textArea.SelectionManager.GetSelectionAt(textArea.Caret.Offset); @@ -222,11 +223,30 @@ namespace ICSharpCode.TextEditor textArea.Caret.Position = textArea.SelectionManager.NextValidPosition(realmousepos.Y); } } - else { + else textArea.Caret.Position = realmousepos; - } - textArea.SelectionManager.ExtendSelection(oldPos, textArea.Caret.Position); + 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.SetDesiredColumn(); } @@ -261,9 +281,7 @@ 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 @@ -274,12 +292,12 @@ namespace ICSharpCode.TextEditor } } + DateTime lastTime = DateTime.Now; void OnMouseDown(object sender, MouseEventArgs e) { Point mousepos; - textArea.mousepos = e.Location; - mousepos = e.Location; + mousepos = textArea.mousepos; if (dodragdrop) { @@ -296,7 +314,6 @@ 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); @@ -305,15 +322,6 @@ 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; } } @@ -449,7 +457,6 @@ namespace ICSharpCode.TextEditor return; } - textArea.SelectionManager.selectFrom.where = WhereFrom.TArea; doubleclick = true; }