Browse Source

Fixed forum-13686: Read-only custom line not so read-only

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2141 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
a62ddf0575
  1. 180
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs
  2. 6
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs
  3. 14
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs
  4. 16
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaDragDropHandler.cs

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

@ -17,10 +17,10 @@ namespace ICSharpCode.TextEditor.Document @@ -17,10 +17,10 @@ namespace ICSharpCode.TextEditor.Document
/// </summary>
public class SelectionManager : IDisposable
{
public Point selectionStart;
internal Point selectionStart;
IDocument document;
TextArea textArea;
public SelectFrom selectFrom = new SelectFrom();
TextArea textArea;
internal SelectFrom selectFrom = new SelectFrom();
List<ISelection> selectionCollection = new List<ISelection>();
@ -42,6 +42,20 @@ namespace ICSharpCode.TextEditor.Document @@ -42,6 +42,20 @@ namespace ICSharpCode.TextEditor.Document
}
}
public bool SelectionIsReadonly {
get {
if (document.ReadOnly)
return true;
if (document.TextEditorProperties.UseCustomLine) {
foreach (ISelection sel in selectionCollection) {
if (document.CustomLineManager.IsReadOnly(sel, false))
return true;
}
}
return false;
}
}
/// <value>
/// The text that is currently selected.
/// </value>
@ -74,17 +88,17 @@ namespace ICSharpCode.TextEditor.Document @@ -74,17 +88,17 @@ namespace ICSharpCode.TextEditor.Document
document.DocumentChanged += new DocumentEventHandler(DocumentChanged);
}
/// <summary>
/// Creates a new instance of <see cref="SelectionManager"/>
/// </summary>
public SelectionManager(IDocument document, TextArea textArea)
{
this.document = document;
this.textArea = textArea;
document.DocumentChanged += new DocumentEventHandler(DocumentChanged);
}
/// <summary>
/// Creates a new instance of <see cref="SelectionManager"/>
/// </summary>
public SelectionManager(IDocument document, TextArea textArea)
{
this.document = document;
this.textArea = textArea;
document.DocumentChanged += new DocumentEventHandler(DocumentChanged);
}
public void Dispose()
public void Dispose()
{
if (this.document != null) {
document.DocumentChanged -= new DocumentEventHandler(DocumentChanged);
@ -167,14 +181,14 @@ namespace ICSharpCode.TextEditor.Document @@ -167,14 +181,14 @@ namespace ICSharpCode.TextEditor.Document
}
} 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;
}
// 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;
}
}
@ -199,30 +213,30 @@ namespace ICSharpCode.TextEditor.Document @@ -199,30 +213,30 @@ namespace ICSharpCode.TextEditor.Document
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;
}
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) {
@ -253,19 +267,19 @@ namespace ICSharpCode.TextEditor.Document @@ -253,19 +267,19 @@ namespace ICSharpCode.TextEditor.Document
}
}
// retrieve the next available line
// - checks that there are more lines available after the current one
// - if there are then the next line is returned
// - if there are NOT then the last position on the given line is returned
public Point NextValidPosition(int line)
{
if (line < document.TotalNumberOfLines - 1)
return new Point(0, line + 1);
else
return new Point(document.GetLineSegment(document.TotalNumberOfLines - 1).Length + 1, line);
}
// retrieve the next available line
// - checks that there are more lines available after the current one
// - if there are then the next line is returned
// - if there are NOT then the last position on the given line is returned
public Point NextValidPosition(int line)
{
if (line < document.TotalNumberOfLines - 1)
return new Point(0, line + 1);
else
return new Point(document.GetLineSegment(document.TotalNumberOfLines - 1).Length + 1, line);
}
void ClearWithoutUpdate()
void ClearWithoutUpdate()
{
while (selectionCollection.Count > 0) {
ISelection selection = selectionCollection[selectionCollection.Count - 1];
@ -279,16 +293,16 @@ namespace ICSharpCode.TextEditor.Document @@ -279,16 +293,16 @@ namespace ICSharpCode.TextEditor.Document
/// </remarks>
public void ClearSelection()
{
Point mousepos;
mousepos = textArea.mousepos;
// this is the most logical place to reset selection starting
// 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)
selectionStart.X = 0;
Point mousepos;
mousepos = textArea.mousepos;
// this is the most logical place to reset selection starting
// 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)
selectionStart.X = 0;
ClearWithoutUpdate();
ClearWithoutUpdate();
document.CommitUpdate();
}
@ -371,7 +385,7 @@ namespace ICSharpCode.TextEditor.Document @@ -371,7 +385,7 @@ namespace ICSharpCode.TextEditor.Document
/// <remarks>
/// Used internally, do not call.
/// </remarks>
public void Insert(int offset, string text)
internal void Insert(int offset, string text)
{
// foreach (ISelection selection in SelectionCollection) {
// if (selection.Offset > offset) {
@ -385,7 +399,7 @@ namespace ICSharpCode.TextEditor.Document @@ -385,7 +399,7 @@ namespace ICSharpCode.TextEditor.Document
/// <remarks>
/// Used internally, do not call.
/// </remarks>
public void Remove(int offset, int length)
internal void Remove(int offset, int length)
{
// foreach (ISelection selection in selectionCollection) {
// if (selection.Offset > offset) {
@ -399,7 +413,7 @@ namespace ICSharpCode.TextEditor.Document @@ -399,7 +413,7 @@ namespace ICSharpCode.TextEditor.Document
/// <remarks>
/// Used internally, do not call.
/// </remarks>
public void Replace(int offset, int length, string text)
internal void Replace(int offset, int length, string text)
{
// foreach (ISelection selection in selectionCollection) {
// if (selection.Offset > offset) {
@ -449,20 +463,20 @@ namespace ICSharpCode.TextEditor.Document @@ -449,20 +463,20 @@ namespace ICSharpCode.TextEditor.Document
public event EventHandler SelectionChanged;
}
// selection initiated from...
public class SelectFrom {
public int where = WhereFrom.None; // last selection initiator
public int first = WhereFrom.None; // first selection initiator
// selection initiated from...
internal class SelectFrom {
public int where = WhereFrom.None; // last selection initiator
public int first = WhereFrom.None; // first selection initiator
public SelectFrom()
{
}
}
public SelectFrom()
{
}
}
// selection initiated from type...
public class WhereFrom {
public const int None = 0;
public const int Gutter = 1;
public const int TArea = 2;
}
// selection initiated from type...
internal class WhereFrom {
public const int None = 0;
public const int Gutter = 1;
public const int TArea = 2;
}
}

6
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs

@ -554,7 +554,7 @@ namespace ICSharpCode.TextEditor @@ -554,7 +554,7 @@ namespace ICSharpCode.TextEditor
if (TextEditorProperties.UseCustomLine == true) {
if (SelectionManager.HasSomethingSelected) {
if (Document.CustomLineManager.IsReadOnly(SelectionManager.SelectionCollection[0], false))
if (SelectionManager.SelectionIsReadonly)
return;
} else if (Document.CustomLineManager.IsReadOnly(Caret.Line, false) == true)
return;
@ -615,7 +615,7 @@ namespace ICSharpCode.TextEditor @@ -615,7 +615,7 @@ namespace ICSharpCode.TextEditor
if (keyData == Keys.Back || keyData == Keys.Delete || keyData == Keys.Enter) {
if (TextEditorProperties.UseCustomLine == true) {
if (SelectionManager.HasSomethingSelected) {
if (Document.CustomLineManager.IsReadOnly(SelectionManager.SelectionCollection[0], false))
if (SelectionManager.SelectionIsReadonly)
return true;
} else {
int curLineNr = Document.GetLineNumberForOffset(Caret.Offset);
@ -690,7 +690,7 @@ namespace ICSharpCode.TextEditor @@ -690,7 +690,7 @@ namespace ICSharpCode.TextEditor
return false;
if (TextEditorProperties.UseCustomLine == true) {
if (SelectionManager.HasSomethingSelected == true) {
if (Document.CustomLineManager.IsReadOnly(SelectionManager.SelectionCollection[0], false))
if (SelectionManager.SelectionIsReadonly)
return false;
}
if (Document.CustomLineManager.IsReadOnly(Caret.Line, false) == true)

14
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs

@ -111,6 +111,13 @@ namespace ICSharpCode.TextEditor @@ -111,6 +111,13 @@ namespace ICSharpCode.TextEditor
public void Cut(object sender, EventArgs e)
{
if (textArea.TextEditorProperties.UseCustomLine == true) {
if (textArea.SelectionManager.HasSomethingSelected) {
if (textArea.SelectionManager.SelectionIsReadonly)
return;
} else if (textArea.Document.CustomLineManager.IsReadOnly(textArea.Caret.Line, false) == true)
return;
}
if (CopyTextToClipboard(textArea.SelectionManager.SelectedText)) {
// Remove text
textArea.BeginUpdate();
@ -147,6 +154,13 @@ namespace ICSharpCode.TextEditor @@ -147,6 +154,13 @@ namespace ICSharpCode.TextEditor
public void Paste(object sender, EventArgs e)
{
if (textArea.TextEditorProperties.UseCustomLine == true) {
if (textArea.SelectionManager.HasSomethingSelected) {
if (textArea.SelectionManager.SelectionIsReadonly)
return;
} else if (textArea.Document.CustomLineManager.IsReadOnly(textArea.Caret.Line, false) == true)
return;
}
// Clipboard.GetDataObject may throw an exception...
for (int i = 0;; i++) {
try {

16
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaDragDropHandler.cs

@ -52,8 +52,8 @@ namespace ICSharpCode.TextEditor @@ -52,8 +52,8 @@ namespace ICSharpCode.TextEditor
{
textArea.Document.Insert(offset, str);
textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document,
textArea.Document.OffsetToPosition(offset),
textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document,
textArea.Document.OffsetToPosition(offset),
textArea.Document.OffsetToPosition(offset + str.Length)));
textArea.Caret.Position = textArea.Document.OffsetToPosition(offset + str.Length);
textArea.Refresh();
@ -68,12 +68,24 @@ namespace ICSharpCode.TextEditor @@ -68,12 +68,24 @@ namespace ICSharpCode.TextEditor
textArea.BeginUpdate();
try {
int offset = textArea.Caret.Offset;
if (textArea.TextEditorProperties.UseCustomLine
&& textArea.Document.CustomLineManager.IsReadOnly(textArea.Caret.Line, false))
{
// prevent dragging text into readonly section
return;
}
if (e.Data.GetDataPresent(typeof(DefaultSelection))) {
ISelection sel = (ISelection)e.Data.GetData(typeof(DefaultSelection));
if (sel.ContainsPosition(textArea.Caret.Position)) {
return;
}
if (GetDragDropEffect(e) == DragDropEffects.Move) {
if (textArea.TextEditorProperties.UseCustomLine
&& textArea.Document.CustomLineManager.IsReadOnly(sel, false))
{
// prevent dragging text out of readonly section
return;
}
int len = sel.Length;
textArea.Document.Remove(sel.Offset, len);
if (sel.Offset < offset) {

Loading…
Cancel
Save