Browse Source

Applied text editor selection patch by Troy Simpson.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2598 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
5b6a66edab
  1. 8
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/SelectionActions.cs
  2. 147
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs
  3. 43
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs

8
src/Libraries/ICSharpCode.TextEditor/Project/Src/Actions/SelectionActions.cs

@ -156,7 +156,13 @@ namespace ICSharpCode.TextEditor.Actions
return; 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();
} }
} }

147
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs

@ -22,7 +22,7 @@ namespace ICSharpCode.TextEditor.Document
TextArea textArea; TextArea textArea;
internal SelectFrom selectFrom = new SelectFrom(); internal SelectFrom selectFrom = new SelectFrom();
List<ISelection> selectionCollection = new List<ISelection>(); internal List<ISelection> selectionCollection = new List<ISelection>();
/// <value> /// <value>
/// A collection containing all selections. /// A collection containing all selections.
@ -154,11 +154,17 @@ namespace ICSharpCode.TextEditor.Document
public void ExtendSelection(Point oldPosition, Point newPosition) 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; return;
} }
Point min; Point min;
Point max; Point max;
int oldnewX = newPosition.X;
bool oldIsGreater = GreaterEqPos(oldPosition, newPosition); bool oldIsGreater = GreaterEqPos(oldPosition, newPosition);
if (oldIsGreater) { if (oldIsGreater) {
min = newPosition; min = newPosition;
@ -167,104 +173,57 @@ namespace ICSharpCode.TextEditor.Document
min = oldPosition; min = oldPosition;
max = newPosition; max = newPosition;
} }
if (!HasSomethingSelected) {
if (min == max) {
return;
}
if (!HasSomethingSelected)
{
SetSelection(new DefaultSelection(document, min, max)); SetSelection(new DefaultSelection(document, min, max));
// initialise selectFrom for a cursor selection
if (selectFrom.where == WhereFrom.None)
selectionStart = oldPosition; //textArea.Caret.Position;
return; return;
} }
ISelection selection = this.selectionCollection[0]; ISelection selection = this.selectionCollection[0];
bool changed = false;
if (selection.ContainsPosition(newPosition)) { if (min == max) {
if (oldIsGreater) { //selection.StartPosition = newPosition;
if (selection.EndPosition != newPosition) { return;
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;
}
}
} else { } else {
if (oldPosition == selection.StartPosition) { // changed selection via gutter
if (GreaterEqPos(newPosition, selection.EndPosition)) { if (selectFrom.where == WhereFrom.Gutter)
if (selection.StartPosition != selection.EndPosition || {
selection.EndPosition != newPosition) { // selection new position is always at the left edge for gutter selections
selection.StartPosition = selection.EndPosition; newPosition.X = 0;
selection.EndPosition = newPosition; }
changed = true;
} if (GreaterEqPos(newPosition, selectionStart)) // selecting forward
} else { {
if (selection.StartPosition != newPosition) { selection.StartPosition = selectionStart;
selection.StartPosition = newPosition; // this handles last line selection
changed = true; 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 { } else { // selecting back
if (GreaterEqPos(selection.StartPosition, newPosition)) { if (selectFrom.where == WhereFrom.Gutter && selectFrom.first == WhereFrom.Gutter)
if (selection.EndPosition != selection.StartPosition || { // gutter selection
selection.StartPosition != newPosition) { selection.EndPosition = NextValidPosition(selectionStart.Y);
changed = true; } else { // internal text selection
} selection.EndPosition = selectionStart; //selection.StartPosition;
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;
} }
} }
// if (GreaterEqPos(selection.StartPosition, min) && GreaterEqPos(selection.EndPosition, max)) { document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, min.Y, max.Y));
// if (oldIsGreater) { document.CommitUpdate();
// selection.StartPosition = min; OnSelectionChanged(EventArgs.Empty);
// } 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 // retrieve the next available line
@ -299,8 +258,10 @@ namespace ICSharpCode.TextEditor.Document
// positions because it is always called before a new selection // positions because it is always called before a new selection
selectFrom.first = selectFrom.where; selectFrom.first = selectFrom.where;
selectionStart = textArea.TextView.GetLogicalPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y); 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.X = 0;
// selectionStart.Y = -1;
}
ClearWithoutUpdate(); ClearWithoutUpdate();
document.CommitUpdate(); document.CommitUpdate();
@ -364,7 +325,7 @@ namespace ICSharpCode.TextEditor.Document
{ {
return GetSelectionAt(offset) != null; return GetSelectionAt(offset) != null;
} }
/// <remarks> /// <remarks>
/// Returns a <see cref="ISelection"/> object giving the selection in which /// Returns a <see cref="ISelection"/> object giving the selection in which
/// the offset points to. /// the offset points to.

43
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaMouseHandler.cs

@ -146,7 +146,8 @@ namespace ICSharpCode.TextEditor
void TextAreaMouseMove(object sender, MouseEventArgs e) void TextAreaMouseMove(object sender, MouseEventArgs e)
{ {
Point mousepos = textArea.mousepos; //Point mousepos = textArea.mousepos;
textArea.mousepos = e.Location;
// honour the starting selection strategy // honour the starting selection strategy
switch (textArea.SelectionManager.selectFrom.where) switch (textArea.SelectionManager.selectFrom.where)
@ -154,7 +155,7 @@ namespace ICSharpCode.TextEditor
case WhereFrom.Gutter: case WhereFrom.Gutter:
//moveGutter(sender, e); //moveGutter(sender, e);
ExtendSelectionToMouse(); ExtendSelectionToMouse();
break; return;
case WhereFrom.TArea: case WhereFrom.TArea:
break; break;
@ -170,8 +171,8 @@ namespace ICSharpCode.TextEditor
textArea.mousepos = new Point(e.X, e.Y); textArea.mousepos = new Point(e.X, e.Y);
if (clickedOnSelectedText) { if (clickedOnSelectedText) {
if (Math.Abs(mousedownpos.X - mousepos.X) >= SystemInformation.DragSize.Width / 2 || if (Math.Abs(mousedownpos.X - e.X) >= SystemInformation.DragSize.Width / 2 ||
Math.Abs(mousedownpos.Y - mousepos.Y) >= SystemInformation.DragSize.Height / 2) Math.Abs(mousedownpos.Y - e.Y) >= SystemInformation.DragSize.Height / 2)
{ {
clickedOnSelectedText = false; clickedOnSelectedText = false;
ISelection selection = textArea.SelectionManager.GetSelectionAt(textArea.Caret.Offset); ISelection selection = textArea.SelectionManager.GetSelectionAt(textArea.Caret.Offset);
@ -226,6 +227,7 @@ namespace ICSharpCode.TextEditor
else else
textArea.Caret.Position = realmousepos; textArea.Caret.Position = realmousepos;
// moves selection across whole words
if (minSelection != nilPoint && textArea.SelectionManager.SelectionCollection.Count > 0) { if (minSelection != nilPoint && textArea.SelectionManager.SelectionCollection.Count > 0) {
// Extend selection when selection was started with double-click // Extend selection when selection was started with double-click
ISelection selection = textArea.SelectionManager.SelectionCollection[0]; ISelection selection = textArea.SelectionManager.SelectionCollection[0];
@ -281,8 +283,24 @@ namespace ICSharpCode.TextEditor
break; break;
} }
textArea.Caret.Position = maxSelection;
textArea.SelectionManager.ExtendSelection(minSelection, 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 !!! // HACK WARNING !!!
// must refresh here, because when a error tooltip is showed and the underlined // 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 // code is double clicked the textArea don't update corrctly, updateline doesn't
@ -292,12 +310,12 @@ namespace ICSharpCode.TextEditor
} }
} }
DateTime lastTime = DateTime.Now; DateTime lastTime = DateTime.Now;
void OnMouseDown(object sender, MouseEventArgs e) void OnMouseDown(object sender, MouseEventArgs e)
{ {
Point mousepos; Point mousepos;
mousepos = textArea.mousepos; textArea.mousepos = e.Location;
mousepos = e.Location;
if (dodragdrop) if (dodragdrop)
{ {
@ -314,6 +332,7 @@ namespace ICSharpCode.TextEditor
textArea.SelectionManager.selectFrom.where = WhereFrom.TArea; textArea.SelectionManager.selectFrom.where = WhereFrom.TArea;
button = e.Button; button = e.Button;
// double-click
if (button == MouseButtons.Left && (DateTime.Now - lastTime).Milliseconds < SystemInformation.DoubleClickTime) { if (button == MouseButtons.Left && (DateTime.Now - lastTime).Milliseconds < SystemInformation.DoubleClickTime) {
int deltaX = Math.Abs(lastmousedownpos.X - e.X); int deltaX = Math.Abs(lastmousedownpos.X - e.X);
int deltaY = Math.Abs(lastmousedownpos.Y - e.Y); int deltaY = Math.Abs(lastmousedownpos.Y - e.Y);
@ -322,6 +341,17 @@ namespace ICSharpCode.TextEditor
DoubleClickSelectionExtend(); DoubleClickSelectionExtend();
lastTime = DateTime.Now; lastTime = DateTime.Now;
lastmousedownpos = new Point(e.X, e.Y); 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; return;
} }
} }
@ -457,6 +487,7 @@ namespace ICSharpCode.TextEditor
return; return;
} }
textArea.SelectionManager.selectFrom.where = WhereFrom.TArea;
doubleclick = true; doubleclick = true;
} }

Loading…
Cancel
Save