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.

410 lines
16KB

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