| 1: | // AbstractDocument.cs | |
| 2: | // Copyright (c) 2001 Mike Krueger | |
| 3: | // | |
| 4: | // This program is free software; you can redistribute it and/or modify | |
| 5: | // it under the terms of the GNU General Public License as published by | |
| 6: | // the Free Software Foundation; either version 2 of the License, or | |
| 7: | // (at your option) any later version. | |
| 8: | // | |
| 9: | // This program is distributed in the hope that it will be useful, | |
| 10: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12: | // GNU General Public License for more details. | |
| 13: | // | |
| 14: | // You should have received a copy of the GNU General Public License | |
| 15: | // along with this program; if not, write to the Free Software | |
| 16: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17: | ||
| 18: | using System; | |
| 19: | using System.IO; | |
| 20: | using System.Diagnostics; | |
| 21: | using System.Drawing; | |
| 22: | ||
| 23: | using Core.Properties; | |
| 24: | using SharpDevelop.Internal.Undo; | |
| 25: | using SharpDevelop.DefaultEditor.Undo; | |
| 26: | using SharpDevelop.Gui.Edit; // for the ProgressEventHandler | |
| 27: | ||
| 28: | namespace SharpDevelop.DefaultEditor.Text { | |
| 29: | ||
| 30: | public abstract class AbstractDocument : IDocument | |
| 31: | { | |
| 32: | bool readOnly = false; | |
| 33: | bool updateDocumentRequested = false; | |
| 34: | bool updateCaretLineRequested = false; | |
| 35: | ||
| 36: | UndoStack undoStack = null; | |
| 37: | ILineTrackingStrategy lineTrackingStrategy = null; | |
| 38: | IBookMarkManager bookmarkManager = null; | |
| 39: | ITextStoreStrategy textStoreStrategy = null; | |
| 40: | ICaret caret = null; | |
| 41: | IFormatingStrategy formatingStrategy = null; | |
| 42: | ITextModel textModel = null; | |
| 43: | IProperties properties = null; | |
| 44: | ||
| 45: | TextSelectionCollection textSelectionCollection = new TextSelectionCollection(); | |
| 46: | ||
| 47: | public IProperties Properties { | |
| 48: | get { | |
| 49: | return properties; | |
| 50: | } | |
| 51: | set { | |
| 52: | properties = value; | |
| 53: | } | |
| 54: | } | |
| 55: | ||
| 56: | public TextSelectionCollection TextSelectionCollection { | |
| 57: | get { | |
| 58: | return textSelectionCollection; | |
| 59: | } | |
| 60: | } | |
| 61: | ||
| 62: | public LineSegmentCollection LineSegmentCollection { | |
| 63: | get { | |
| 64: | return lineTrackingStrategy.LineSegmentCollection; | |
| 65: | } | |
| 66: | } | |
| 67: | ||
| 68: | public UndoStack UndoStack { | |
| 69: | get { | |
| 70: | return undoStack; | |
| 71: | } | |
| 72: | set { | |
| 73: | undoStack = value; | |
| 74: | } | |
| 75: | } | |
| 76: | ||
| 77: | public bool ReadOnly { | |
| 78: | get { | |
| 79: | return readOnly; | |
| 80: | } | |
| 81: | set { | |
| 82: | readOnly = value; | |
| 83: | } | |
| 84: | } | |
| 85: | ||
| 86: | public bool UpdateDocumentRequested { | |
| 87: | get { | |
| 88: | return updateDocumentRequested; | |
| 89: | } | |
| 90: | set { | |
| 91: | updateDocumentRequested = value; | |
| 92: | } | |
| 93: | } | |
| 94: | ||
| 95: | public bool UpdateCaretLineRequested { | |
| 96: | get { | |
| 97: | return updateCaretLineRequested; | |
| 98: | } | |
| 99: | ||
| 100: | set { | |
| 101: | updateCaretLineRequested = value; | |
| 102: | } | |
| 103: | } | |
| 104: | ||
| 105: | ||
| 106: | public ICaret Caret { | |
| 107: | get { | |
| 108: | return caret; | |
| 109: | } | |
| 110: | set { | |
| 111: | caret = value; | |
| 112: | } | |
| 113: | } | |
| 114: | ||
| 115: | public ILineTrackingStrategy LineTrackingStrategy { | |
| 116: | get { | |
| 117: | return lineTrackingStrategy; | |
| 118: | } | |
| 119: | set { | |
| 120: | lineTrackingStrategy = value; | |
| 121: | } | |
| 122: | } | |
| 123: | ||
| 124: | public ITextStoreStrategy TextStoreStrategy { | |
| 125: | get { | |
| 126: | return textStoreStrategy; | |
| 127: | } | |
| 128: | set { | |
| 129: | textStoreStrategy = value; | |
| 130: | } | |
| 131: | } | |
| 132: | ||
| 133: | public IFormatingStrategy FormatingStrategy { | |
| 134: | get { | |
| 135: | return formatingStrategy; | |
| 136: | } | |
| 137: | set { | |
| 138: | formatingStrategy = value; | |
| 139: | } | |
| 140: | } | |
| 141: | ||
| 142: | public IHighlightingStrategy HighlightingStrategy { | |
| 143: | get { | |
| 144: | return lineTrackingStrategy.HighlightingStrategy; | |
| 145: | } | |
| 146: | set { | |
| 147: | lineTrackingStrategy.HighlightingStrategy = value; | |
| 148: | } | |
| 149: | } | |
| 150: | ||
| 151: | public ITextModel TextModel { | |
| 152: | get { | |
| 153: | return textModel; | |
| 154: | } | |
| 155: | set { | |
| 156: | textModel = value; | |
| 157: | } | |
| 158: | } | |
| 159: | ||
| 160: | public int TextLength { | |
| 161: | get { | |
| 162: | return textStoreStrategy.Length; | |
| 163: | } | |
| 164: | } | |
| 165: | ||
| 166: | public IBookMarkManager BookmarkManager { | |
| 167: | get { | |
| 168: | return bookmarkManager; | |
| 169: | } | |
| 170: | set { | |
| 171: | bookmarkManager = value; | |
| 172: | } | |
| 173: | } | |
| 174: | ||
| 175: | public string TextContent { | |
| 176: | get { | |
| 177: | return GetText(0, textStoreStrategy.Length); | |
| 178: | } | |
| 179: | set { | |
| 180: | Debug.Assert(Caret != null); | |
| 181: | Debug.Assert(textSelectionCollection != null); | |
| 182: | Debug.Assert(textStoreStrategy != null); | |
| 183: | Debug.Assert(lineTrackingStrategy != null); | |
| 184: | ||
| 185: | OnDocumentAboutToBeChanged(new DocumentEventArgs(this, 0, 0, value)); | |
| 186: | textStoreStrategy.SetContentTo(value); | |
| 187: | lineTrackingStrategy.SetTracked(value); | |
| 188: | caret.Offset = 0; | |
| 189: | textSelectionCollection.Clear(); | |
| 190: | OnDocumentChanged(new DocumentEventArgs(this, 0, 0, value)); | |
| 191: | } | |
| 192: | } | |
| 193: | ||
| 194: | protected AbstractDocument() | |
| 195: | { | |
| 196: | } | |
| 197: | ||
| 198: | public void SetDesiredColumn() | |
| 199: | { | |
| 200: | LineSegment caretLine = lineTrackingStrategy.GetLineSegmentOfOffset(Caret.Offset); | |
| 201: | Caret.DesiredColumn = TextModel.GetViewXPos(caretLine, Caret.Offset - caretLine.Offset); | |
| 202: | } | |
| 203: | ||
| 204: | public void SetCaretToDesiredColumn(LineSegment caretLine) | |
| 205: | { | |
| 206: | Caret.Offset = caretLine.Offset + TextModel.GetLogicalXPos(caretLine, Caret.DesiredColumn); | |
| 207: | } | |
| 208: | ||
| 209: | public void Insert(int offset, string text) | |
| 210: | { | |
| 211: | OnDocumentAboutToBeChanged(new DocumentEventArgs(this, offset, -1, text)); | |
| 212: | DateTime time = DateTime.Now; | |
| 213: | textStoreStrategy.Insert(offset, text); | |
| 214: | ||
| 215: | time = DateTime.Now; | |
| 216: | lineTrackingStrategy.Insert(offset, text); | |
| 217: | ||
| 218: | time = DateTime.Now; | |
| 219: | if (Caret.Offset > offset) { | |
| 220: | Caret.Offset += text.Length; | |
| 221: | } | |
| 222: | ||
| 223: | foreach (ITextSelection selection in textSelectionCollection) { | |
| 224: | if (selection.Offset > offset) { | |
| 225: | selection.Offset += text.Length; | |
| 226: | } else if (selection.Offset + selection.Length > offset) { | |
| 227: | selection.Length += text.Length; | |
| 228: | } | |
| 229: | } | |
| 230: | ||
| 231: | undoStack.Push(new UndoableInsert(this, offset, text)); | |
| 232: | ||
| 233: | time = DateTime.Now; | |
| 234: | OnDocumentChanged(new DocumentEventArgs(this, offset, -1, text)); | |
| 235: | } | |
| 236: | ||
| 237: | public void Remove(int offset, int length) | |
| 238: | { | |
| 239: | OnDocumentAboutToBeChanged(new DocumentEventArgs(this, offset, length)); | |
| 240: | undoStack.Push(new UndoableDelete(this, offset, GetText(offset, length))); | |
| 241: | ||
| 242: | textStoreStrategy.Remove(offset, length); | |
| 243: | lineTrackingStrategy.Remove(offset, length); | |
| 244: | ||
| 245: | if (Caret.Offset > offset) { | |
| 246: | Caret.Offset -= Math.Min(Caret.Offset - offset, length); | |
| 247: | } | |
| 248: | ||
| 249: | foreach (ITextSelection selection in textSelectionCollection) { | |
| 250: | if (selection.Offset > offset) { | |
| 251: | selection.Offset -= length; | |
| 252: | } else if (selection.Offset + selection.Length > offset) { | |
| 253: | selection.Length -= length; | |
| 254: | } | |
| 255: | } | |
| 256: | ||
| 257: | OnDocumentChanged(new DocumentEventArgs(this, offset, length)); | |
| 258: | } | |
| 259: | ||
| 260: | public void Replace(int offset, int length, string text) | |
| 261: | { | |
| 262: | OnDocumentAboutToBeChanged(new DocumentEventArgs(this, offset, length, text)); | |
| 263: | undoStack.Push(new UndoableReplace(this, offset, GetText(offset, length), text)); | |
| 264: | ||
| 265: | textStoreStrategy.Replace(offset, length, text); | |
| 266: | lineTrackingStrategy.Replace(offset, length, text); | |
| 267: | ||
| 268: | if (Caret.Offset > offset) { | |
| 269: | int caretDelta = Math.Min(Caret.Offset - offset, length); | |
| 270: | Caret.Offset = Caret.Offset - caretDelta + Math.Min(caretDelta, text.Length); | |
| 271: | } | |
| 272: | ||
| 273: | foreach (ITextSelection selection in textSelectionCollection) { | |
| 274: | if (selection.Offset > offset) { | |
| 275: | selection.Offset = selection.Offset - length + text.Length; | |
| 276: | } else if (selection.Offset + selection.Length > offset) { | |
| 277: | selection.Length = selection.Length - length + text.Length; | |
| 278: | } | |
| 279: | } | |
| 280: | ||
| 281: | OnDocumentChanged(new DocumentEventArgs(this, offset, length, text)); | |
| 282: | } | |
| 283: | ||
| 284: | public char GetCharAt(int offset) | |
| 285: | { | |
| 286: | return textStoreStrategy.GetCharAt(offset); | |
| 287: | } | |
| 288: | ||
| 289: | public string GetText(int offset, int length) | |
| 290: | { | |
| 291: | return textStoreStrategy.GetText(offset, length); | |
| 292: | } | |
| 293: | ||
| 294: | public int TotalNumberOfLines { | |
| 295: | get { | |
| 296: | return lineTrackingStrategy.TotalNumberOfLines; | |
| 297: | } | |
| 298: | } | |
| 299: | ||
| 300: | public int ComputeNumberOfLines(string text) | |
| 301: | { | |
| 302: | return lineTrackingStrategy.ComputeNumberOfLines(text); | |
| 303: | } | |
| 304: | ||
| 305: | public int GetNumberOfLines(int offset, int length) | |
| 306: | { | |
| 307: | return lineTrackingStrategy.GetNumberOfLines(offset, length); | |
| 308: | } | |
| 309: | ||
| 310: | public int GetLineNumberOfOffset(int offset) | |
| 311: | { | |
| 312: | return lineTrackingStrategy.GetLineNumberOfOffset(offset); | |
| 313: | } | |
| 314: | ||
| 315: | public int GetLineOffset(int line) | |
| 316: | { | |
| 317: | return lineTrackingStrategy.GetLineOffset(line); | |
| 318: | } | |
| 319: | ||
| 320: | public int GetLineLength(int line) | |
| 321: | { | |
| 322: | return lineTrackingStrategy.GetLineLength(line); | |
| 323: | } | |
| 324: | ||
| 325: | public LineSegment GetLineSegmentOfOffset(int offset) | |
| 326: | { | |
| 327: | return lineTrackingStrategy.GetLineSegmentOfOffset(offset); | |
| 328: | } | |
| 329: | ||
| 330: | public LineSegment GetLineSegment(int line) | |
| 331: | { | |
| 332: | return lineTrackingStrategy.GetLineSegment(line); | |
| 333: | } | |
| 334: | ||
| 335: | public Point OffsetToView(int offset) | |
| 336: | { | |
| 337: | return textModel.OffsetToView(offset); | |
| 338: | } | |
| 339: | ||
| 340: | public int ViewToOffset(Point p) | |
| 341: | { | |
| 342: | return textModel.ViewToOffset(p); | |
| 343: | } | |
| 344: | ||
| 345: | public int GetViewXPos(LineSegment line, int logicalXPos) | |
| 346: | { | |
| 347: | return textModel.GetViewXPos(line, logicalXPos); | |
| 348: | ||
| 349: | } | |
| 350: | ||
| 351: | public int GetLogicalXPos(LineSegment line, int viewXPos) | |
| 352: | { | |
| 353: | return textModel.GetLogicalXPos(line, viewXPos); | |
| 354: | } | |
| 355: | ||
| 356: | protected void OnDocumentAboutToBeChanged(DocumentEventArgs e) | |
| 357: | { | |
| 358: | if (DocumentAboutToBeChanged != null) { | |
| 359: | DocumentAboutToBeChanged(this, e); | |
| 360: | } | |
| 361: | } | |
| 362: | ||
| 363: | protected void OnDocumentChanged(DocumentEventArgs e) | |
| 364: | { | |
| 365: | if (DocumentChanged != null) { | |
| 366: | DocumentChanged(this, e); | |
| 367: | } | |
| 368: | } | |
| 369: | ||
| 370: | public event DocumentEventHandler DocumentAboutToBeChanged; | |
| 371: | public event DocumentEventHandler DocumentChanged; | |
| 372: | } | |
| 373: | } |
This page was automatically generated by SharpDevelop.