1 // Scintilla source code edit control 2 /** @file CellBuffer.h 3 ** Manages the text of the document. 4 **/ 5 // Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org> 6 // The License.txt file describes the conditions under which this software may be distributed. 7 8 #ifndef CELLBUFFER_H 9 #define CELLBUFFER_H 10 11 #ifdef SCI_NAMESPACE 12 namespace Scintilla { 13 #endif 14 15 // Interface to per-line data that wants to see each line insertion and deletion 16 class PerLine { 17 public: ~PerLine()18 virtual ~PerLine() {} 19 virtual void Init()=0; 20 virtual void InsertLine(int)=0; 21 virtual void RemoveLine(int)=0; 22 }; 23 24 /** 25 * The line vector contains information about each of the lines in a cell buffer. 26 */ 27 class LineVector { 28 29 Partitioning starts; 30 PerLine *perLine; 31 32 public: 33 34 LineVector(); 35 ~LineVector(); 36 void Init(); 37 void SetPerLine(PerLine *pl); 38 39 void InsertText(int line, int delta); 40 void InsertLine(int line, int position, bool lineStart); 41 void SetLineStart(int line, int position); 42 void RemoveLine(int line); Lines()43 int Lines() const { 44 return starts.Partitions(); 45 } 46 int LineFromPosition(int pos) const; LineStart(int line)47 int LineStart(int line) const { 48 return starts.PositionFromPartition(line); 49 } 50 51 int MarkValue(int line); 52 int AddMark(int line, int marker); 53 void MergeMarkers(int pos); 54 void DeleteMark(int line, int markerNum, bool all); 55 void DeleteMarkFromHandle(int markerHandle); 56 int LineFromHandle(int markerHandle); 57 58 void ClearLevels(); 59 int SetLevel(int line, int level); 60 int GetLevel(int line); 61 62 int SetLineState(int line, int state); 63 int GetLineState(int line); 64 int GetMaxLineState(); 65 66 }; 67 68 enum actionType { insertAction, removeAction, startAction, containerAction }; 69 70 /** 71 * Actions are used to store all the information required to perform one undo/redo step. 72 */ 73 class Action { 74 public: 75 actionType at; 76 int position; 77 char *data; 78 int lenData; 79 bool mayCoalesce; 80 81 Action(); 82 ~Action(); 83 void Create(actionType at_, int position_=0, const char *data_=0, int lenData_=0, bool mayCoalesce_=true); 84 void Destroy(); 85 void Grab(Action *source); 86 }; 87 88 /** 89 * 90 */ 91 class UndoHistory { 92 Action *actions; 93 int lenActions; 94 int maxAction; 95 int currentAction; 96 int undoSequenceDepth; 97 int savePoint; 98 99 void EnsureUndoRoom(); 100 101 // Private so UndoHistory objects can not be copied 102 UndoHistory(const UndoHistory &); 103 104 public: 105 UndoHistory(); 106 ~UndoHistory(); 107 108 const char *AppendAction(actionType at, int position, const char *data, int length, bool &startSequence, bool mayCoalesce=true); 109 110 void BeginUndoAction(); 111 void EndUndoAction(); 112 void DropUndoSequence(); 113 void DeleteUndoHistory(); 114 115 /// The save point is a marker in the undo stack where the container has stated that 116 /// the buffer was saved. Undo and redo can move over the save point. 117 void SetSavePoint(); 118 bool IsSavePoint() const; 119 120 /// To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is 121 /// called that many times. Similarly for redo. 122 bool CanUndo() const; 123 int StartUndo(); 124 const Action &GetUndoStep() const; 125 void CompletedUndoStep(); 126 bool CanRedo() const; 127 int StartRedo(); 128 const Action &GetRedoStep() const; 129 void CompletedRedoStep(); 130 }; 131 132 /** 133 * Holder for an expandable array of characters that supports undo and line markers. 134 * Based on article "Data Structures in a Bit-Mapped Text Editor" 135 * by Wilfred J. Hansen, Byte January 1987, page 183. 136 */ 137 class CellBuffer { 138 private: 139 SplitVector<char> substance; 140 SplitVector<char> style; 141 bool readOnly; 142 int utf8LineEnds; 143 144 bool collectingUndo; 145 UndoHistory uh; 146 147 LineVector lv; 148 149 bool UTF8LineEndOverlaps(int position) const; 150 void ResetLineEnds(); 151 /// Actions without undo 152 void BasicInsertString(int position, const char *s, int insertLength); 153 void BasicDeleteChars(int position, int deleteLength); 154 155 public: 156 157 CellBuffer(); 158 ~CellBuffer(); 159 160 /// Retrieving positions outside the range of the buffer works and returns 0 161 char CharAt(int position) const; 162 void GetCharRange(char *buffer, int position, int lengthRetrieve) const; 163 char StyleAt(int position) const; 164 void GetStyleRange(unsigned char *buffer, int position, int lengthRetrieve) const; 165 const char *BufferPointer(); 166 const char *RangePointer(int position, int rangeLength); 167 int GapPosition() const; 168 169 int Length() const; 170 void Allocate(int newSize); GetLineEndTypes()171 int GetLineEndTypes() const { return utf8LineEnds; } 172 void SetLineEndTypes(int utf8LineEnds_); 173 void SetPerLine(PerLine *pl); 174 int Lines() const; 175 int LineStart(int line) const; LineFromPosition(int pos)176 int LineFromPosition(int pos) const { return lv.LineFromPosition(pos); } 177 void InsertLine(int line, int position, bool lineStart); 178 void RemoveLine(int line); 179 const char *InsertString(int position, const char *s, int insertLength, bool &startSequence); 180 181 /// Setting styles for positions outside the range of the buffer is safe and has no effect. 182 /// @return true if the style of a character is changed. 183 bool SetStyleAt(int position, char styleValue, char mask='\377'); 184 bool SetStyleFor(int position, int length, char styleValue, char mask); 185 186 const char *DeleteChars(int position, int deleteLength, bool &startSequence); 187 188 bool IsReadOnly() const; 189 void SetReadOnly(bool set); 190 191 /// The save point is a marker in the undo stack where the container has stated that 192 /// the buffer was saved. Undo and redo can move over the save point. 193 void SetSavePoint(); 194 bool IsSavePoint() const; 195 196 bool SetUndoCollection(bool collectUndo); 197 bool IsCollectingUndo() const; 198 void BeginUndoAction(); 199 void EndUndoAction(); 200 void AddUndoAction(int token, bool mayCoalesce); 201 void DeleteUndoHistory(); 202 203 /// To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is 204 /// called that many times. Similarly for redo. 205 bool CanUndo() const; 206 int StartUndo(); 207 const Action &GetUndoStep() const; 208 void PerformUndoStep(); 209 bool CanRedo() const; 210 int StartRedo(); 211 const Action &GetRedoStep() const; 212 void PerformRedoStep(); 213 }; 214 215 #ifdef SCI_NAMESPACE 216 } 217 #endif 218 219 #endif 220