From a62ddf05753f70cd610a6ec30f33b88c1b611e9f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 8 Dec 2006 23:13:06 +0000 Subject: [PATCH] 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 --- .../Document/Selection/SelectionManager.cs | 180 ++++++++++-------- .../Project/Src/Gui/TextArea.cs | 6 +- .../Src/Gui/TextAreaClipboardHandler.cs | 14 ++ .../Src/Gui/TextAreaDragDropHandler.cs | 16 +- 4 files changed, 128 insertions(+), 88 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 b20de1c301..975ff69508 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/Selection/SelectionManager.cs @@ -17,10 +17,10 @@ namespace ICSharpCode.TextEditor.Document /// 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 selectionCollection = new List(); @@ -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; + } + } + /// /// The text that is currently selected. /// @@ -74,17 +88,17 @@ namespace ICSharpCode.TextEditor.Document document.DocumentChanged += new DocumentEventHandler(DocumentChanged); } - /// - /// Creates a new instance of - /// - public SelectionManager(IDocument document, TextArea textArea) - { - this.document = document; - this.textArea = textArea; - document.DocumentChanged += new DocumentEventHandler(DocumentChanged); - } + /// + /// Creates a new instance of + /// + 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 } } 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 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 } } - // 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 /// 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 /// /// Used internally, do not call. /// - 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 /// /// Used internally, do not call. /// - 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 /// /// Used internally, do not call. /// - 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 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; + } } diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs index 90c05197c1..806d70d49b 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextArea.cs @@ -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 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 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) diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs index 0bd0fcb7f8..3b6031021c 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs @@ -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 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 { diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaDragDropHandler.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaDragDropHandler.cs index 3d01ca234f..1e1a1b9f22 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaDragDropHandler.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaDragDropHandler.cs @@ -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 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) {