The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_CODEDOCUMENT_JUCEHEADER__
  19. #define __JUCE_CODEDOCUMENT_JUCEHEADER__
  20. class CodeDocumentLine;
  21. //==============================================================================
  22. /**
  23. A class for storing and manipulating a source code file.
  24. When using a CodeEditorComponent, it takes one of these as its source object.
  25. The CodeDocument stores its content as an array of lines, which makes it
  26. quick to insert and delete.
  27. @see CodeEditorComponent
  28. */
  29. class JUCE_API CodeDocument
  30. {
  31. public:
  32. /** Creates a new, empty document.
  33. */
  34. CodeDocument();
  35. /** Destructor. */
  36. ~CodeDocument();
  37. //==============================================================================
  38. /** A position in a code document.
  39. Using this class you can find a position in a code document and quickly get its
  40. character position, line, and index. By calling setPositionMaintained (true), the
  41. position is automatically updated when text is inserted or deleted in the document,
  42. so that it maintains its original place in the text.
  43. */
  44. class JUCE_API Position
  45. {
  46. public:
  47. /** Creates an uninitialised postion.
  48. Don't attempt to call any methods on this until you've given it an owner document
  49. to refer to!
  50. */
  51. Position() noexcept;
  52. /** Creates a position based on a line and index in a document.
  53. Note that this index is NOT the column number, it's the number of characters from the
  54. start of the line. The "column" number isn't quite the same, because if the line
  55. contains any tab characters, the relationship of the index to its visual column depends on
  56. the number of spaces per tab being used!
  57. Lines are numbered from zero, and if the line or index are beyond the bounds of the document,
  58. they will be adjusted to keep them within its limits.
  59. */
  60. Position (const CodeDocument& ownerDocument,
  61. int line, int indexInLine) noexcept;
  62. /** Creates a position based on a character index in a document.
  63. This position is placed at the specified number of characters from the start of the
  64. document. The line and column are auto-calculated.
  65. If the position is beyond the range of the document, it'll be adjusted to keep it
  66. inside.
  67. */
  68. Position (const CodeDocument& ownerDocument,
  69. int charactersFromStartOfDocument) noexcept;
  70. /** Creates a copy of another position.
  71. This will copy the position, but the new object will not be set to maintain its position,
  72. even if the source object was set to do so.
  73. */
  74. Position (const Position& other) noexcept;
  75. /** Destructor. */
  76. ~Position();
  77. Position& operator= (const Position& other);
  78. bool operator== (const Position& other) const noexcept;
  79. bool operator!= (const Position& other) const noexcept;
  80. /** Points this object at a new position within the document.
  81. If the position is beyond the range of the document, it'll be adjusted to keep it
  82. inside.
  83. @see getPosition, setLineAndIndex
  84. */
  85. void setPosition (int charactersFromStartOfDocument);
  86. /** Returns the position as the number of characters from the start of the document.
  87. @see setPosition, getLineNumber, getIndexInLine
  88. */
  89. int getPosition() const noexcept { return characterPos; }
  90. /** Moves the position to a new line and index within the line.
  91. Note that the index is NOT the column at which the position appears in an editor.
  92. If the line contains any tab characters, the relationship of the index to its
  93. visual position depends on the number of spaces per tab being used!
  94. Lines are numbered from zero, and if the line or index are beyond the bounds of the document,
  95. they will be adjusted to keep them within its limits.
  96. */
  97. void setLineAndIndex (int newLine, int newIndexInLine);
  98. /** Returns the line number of this position.
  99. The first line in the document is numbered zero, not one!
  100. */
  101. int getLineNumber() const noexcept { return line; }
  102. /** Returns the number of characters from the start of the line.
  103. Note that this value is NOT the column at which the position appears in an editor.
  104. If the line contains any tab characters, the relationship of the index to its
  105. visual position depends on the number of spaces per tab being used!
  106. */
  107. int getIndexInLine() const noexcept { return indexInLine; }
  108. /** Allows the position to be automatically updated when the document changes.
  109. If this is set to true, the positon will register with its document so that
  110. when the document has text inserted or deleted, this position will be automatically
  111. moved to keep it at the same position in the text.
  112. */
  113. void setPositionMaintained (bool isMaintained);
  114. //==============================================================================
  115. /** Moves the position forwards or backwards by the specified number of characters.
  116. @see movedBy
  117. */
  118. void moveBy (int characterDelta);
  119. /** Returns a position which is the same as this one, moved by the specified number of
  120. characters.
  121. @see moveBy
  122. */
  123. Position movedBy (int characterDelta) const;
  124. /** Returns a position which is the same as this one, moved up or down by the specified
  125. number of lines.
  126. @see movedBy
  127. */
  128. Position movedByLines (int deltaLines) const;
  129. /** Returns the character in the document at this position.
  130. @see getLineText
  131. */
  132. juce_wchar getCharacter() const;
  133. /** Returns the line from the document that this position is within.
  134. @see getCharacter, getLineNumber
  135. */
  136. String getLineText() const;
  137. //==============================================================================
  138. private:
  139. CodeDocument* owner;
  140. int characterPos, line, indexInLine;
  141. bool positionMaintained;
  142. };
  143. //==============================================================================
  144. /** Returns the full text of the document. */
  145. String getAllContent() const;
  146. /** Returns a section of the document's text. */
  147. String getTextBetween (const Position& start, const Position& end) const;
  148. /** Returns a line from the document. */
  149. String getLine (int lineIndex) const noexcept;
  150. /** Returns the number of characters in the document. */
  151. int getNumCharacters() const noexcept;
  152. /** Returns the number of lines in the document. */
  153. int getNumLines() const noexcept { return lines.size(); }
  154. /** Returns the number of characters in the longest line of the document. */
  155. int getMaximumLineLength() noexcept;
  156. /** Deletes a section of the text.
  157. This operation is undoable.
  158. */
  159. void deleteSection (const Position& startPosition, const Position& endPosition);
  160. /** Inserts some text into the document at a given position.
  161. This operation is undoable.
  162. */
  163. void insertText (const Position& position, const String& text);
  164. /** Clears the document and replaces it with some new text.
  165. This operation is undoable - if you're trying to completely reset the document, you
  166. might want to also call clearUndoHistory() and setSavePoint() after using this method.
  167. */
  168. void replaceAllContent (const String& newContent);
  169. /** Replaces the editor's contents with the contents of a stream.
  170. This will also reset the undo history and save point marker.
  171. */
  172. bool loadFromStream (InputStream& stream);
  173. /** Writes the editor's current contents to a stream. */
  174. bool writeToStream (OutputStream& stream);
  175. //==============================================================================
  176. /** Returns the preferred new-line characters for the document.
  177. This will be either "\n", "\r\n", or (rarely) "\r".
  178. @see setNewLineCharacters
  179. */
  180. String getNewLineCharacters() const noexcept { return newLineChars; }
  181. /** Sets the new-line characters that the document should use.
  182. The string must be either "\n", "\r\n", or (rarely) "\r".
  183. @see getNewLineCharacters
  184. */
  185. void setNewLineCharacters (const String& newLine) noexcept;
  186. //==============================================================================
  187. /** Begins a new undo transaction.
  188. The document itself will not call this internally, so relies on whatever is using the
  189. document to periodically call this to break up the undo sequence into sensible chunks.
  190. @see UndoManager::beginNewTransaction
  191. */
  192. void newTransaction();
  193. /** Undo the last operation.
  194. @see UndoManager::undo
  195. */
  196. void undo();
  197. /** Redo the last operation.
  198. @see UndoManager::redo
  199. */
  200. void redo();
  201. /** Clears the undo history.
  202. @see UndoManager::clearUndoHistory
  203. */
  204. void clearUndoHistory();
  205. /** Returns the document's UndoManager */
  206. UndoManager& getUndoManager() noexcept { return undoManager; }
  207. //==============================================================================
  208. /** Makes a note that the document's current state matches the one that is saved.
  209. After this has been called, hasChangedSinceSavePoint() will return false until
  210. the document has been altered, and then it'll start returning true. If the document is
  211. altered, but then undone until it gets back to this state, hasChangedSinceSavePoint()
  212. will again return false.
  213. @see hasChangedSinceSavePoint
  214. */
  215. void setSavePoint() noexcept;
  216. /** Returns true if the state of the document differs from the state it was in when
  217. setSavePoint() was last called.
  218. @see setSavePoint
  219. */
  220. bool hasChangedSinceSavePoint() const noexcept;
  221. //==============================================================================
  222. /** Searches for a word-break. */
  223. const Position findWordBreakAfter (const Position& position) const noexcept;
  224. /** Searches for a word-break. */
  225. const Position findWordBreakBefore (const Position& position) const noexcept;
  226. //==============================================================================
  227. /** An object that receives callbacks from the CodeDocument when its text changes.
  228. @see CodeDocument::addListener, CodeDocument::removeListener
  229. */
  230. class JUCE_API Listener
  231. {
  232. public:
  233. Listener() {}
  234. virtual ~Listener() {}
  235. /** Called by a CodeDocument when it is altered.
  236. */
  237. virtual void codeDocumentChanged (const Position& affectedTextStart,
  238. const Position& affectedTextEnd) = 0;
  239. };
  240. /** Registers a listener object to receive callbacks when the document changes.
  241. If the listener is already registered, this method has no effect.
  242. @see removeListener
  243. */
  244. void addListener (Listener* listener) noexcept;
  245. /** Deregisters a listener.
  246. @see addListener
  247. */
  248. void removeListener (Listener* listener) noexcept;
  249. //==============================================================================
  250. /** Iterates the text in a CodeDocument.
  251. This class lets you read characters from a CodeDocument. It's designed to be used
  252. by a SyntaxAnalyser object.
  253. @see CodeDocument, SyntaxAnalyser
  254. */
  255. class JUCE_API Iterator
  256. {
  257. public:
  258. Iterator (const CodeDocument& document) noexcept;
  259. Iterator (const Iterator& other) noexcept;
  260. Iterator& operator= (const Iterator& other) noexcept;
  261. ~Iterator() noexcept;
  262. /** Reads the next character and returns it.
  263. @see peekNextChar
  264. */
  265. juce_wchar nextChar() noexcept;
  266. /** Reads the next character without advancing the current position. */
  267. juce_wchar peekNextChar() const noexcept;
  268. /** Advances the position by one character. */
  269. void skip() noexcept;
  270. /** Returns the position as the number of characters from the start of the document. */
  271. int getPosition() const noexcept { return position; }
  272. /** Skips over any whitespace characters until the next character is non-whitespace. */
  273. void skipWhitespace() noexcept;
  274. /** Skips forward until the next character will be the first character on the next line */
  275. void skipToEndOfLine() noexcept;
  276. /** Returns the line number of the next character. */
  277. int getLine() const noexcept { return line; }
  278. /** Returns true if the iterator has reached the end of the document. */
  279. bool isEOF() const noexcept;
  280. private:
  281. const CodeDocument* document;
  282. mutable String::CharPointerType charPointer;
  283. int line, position;
  284. };
  285. private:
  286. //==============================================================================
  287. friend class CodeDocumentInsertAction;
  288. friend class CodeDocumentDeleteAction;
  289. friend class Iterator;
  290. friend class Position;
  291. OwnedArray <CodeDocumentLine> lines;
  292. Array <Position*> positionsToMaintain;
  293. UndoManager undoManager;
  294. int currentActionIndex, indexOfSavedState;
  295. int maximumLineLength;
  296. ListenerList <Listener> listeners;
  297. String newLineChars;
  298. void sendListenerChangeMessage (int startLine, int endLine);
  299. void insert (const String& text, int insertPos, bool undoable);
  300. void remove (int startPos, int endPos, bool undoable);
  301. void checkLastLineStatus();
  302. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CodeDocument);
  303. };
  304. #endif // __JUCE_CODEDOCUMENT_JUCEHEADER__