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.

212 lines
7.8KB

  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 __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  19. #define __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  20. #include "../Project/jucer_Project.h"
  21. #include "../Application/jucer_DocumentEditorComponent.h"
  22. //==============================================================================
  23. class SourceCodeDocument : public OpenDocumentManager::Document
  24. {
  25. public:
  26. //==============================================================================
  27. SourceCodeDocument (Project*, const File&);
  28. bool loadedOk() const { return true; }
  29. bool isForFile (const File& file) const { return getFile() == file; }
  30. bool isForNode (const ValueTree& node) const { return false; }
  31. bool refersToProject (Project& p) const { return project == &p; }
  32. Project* getProject() const { return project; }
  33. String getName() const { return getFile().getFileName(); }
  34. String getType() const { return getFile().getFileExtension() + " file"; }
  35. File getFile() const { return modDetector.getFile(); }
  36. bool needsSaving() const { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); }
  37. bool hasFileBeenModifiedExternally() { return modDetector.hasBeenModified(); }
  38. void fileHasBeenRenamed (const File& newFile) { modDetector.fileHasBeenRenamed (newFile); }
  39. String getState() const { return lastState != nullptr ? lastState->toString() : String::empty; }
  40. void restoreState (const String& state) { lastState = new CodeEditorComponent::State (state); }
  41. void reloadFromFile();
  42. bool save();
  43. Component* createEditor();
  44. Component* createViewer() { return createEditor(); }
  45. void updateLastState (CodeEditorComponent& editor);
  46. void applyLastState (CodeEditorComponent& editor) const;
  47. CodeDocument& getCodeDocument();
  48. //==============================================================================
  49. struct Type : public OpenDocumentManager::DocumentType
  50. {
  51. bool canOpenFile (const File& file) { return file.hasFileExtension ("cpp;h;hpp;mm;m;c;cc;cxx;txt;inc;tcc;xml;plist;rtf;html;htm;php;py;rb;cs"); }
  52. Document* openFile (Project* project, const File& file) { return new SourceCodeDocument (project, file); }
  53. };
  54. protected:
  55. FileModificationDetector modDetector;
  56. ScopedPointer<CodeDocument> codeDoc;
  57. Project* project;
  58. ScopedPointer<CodeEditorComponent::State> lastState;
  59. void reloadInternal();
  60. };
  61. //==============================================================================
  62. class SourceCodeEditor : public DocumentEditorComponent,
  63. private ValueTree::Listener
  64. {
  65. public:
  66. SourceCodeEditor (OpenDocumentManager::Document* document);
  67. ~SourceCodeEditor();
  68. void createEditor (CodeDocument& codeDocument);
  69. void setEditor (CodeEditorComponent*);
  70. void highlightLine (int lineNum, int characterIndex);
  71. ScopedPointer<CodeEditorComponent> editor;
  72. private:
  73. void resized();
  74. void valueTreePropertyChanged (ValueTree&, const Identifier&);
  75. void valueTreeChildAdded (ValueTree&, ValueTree&);
  76. void valueTreeChildRemoved (ValueTree&, ValueTree&);
  77. void valueTreeChildOrderChanged (ValueTree&);
  78. void valueTreeParentChanged (ValueTree&);
  79. void valueTreeRedirected (ValueTree&);
  80. void updateColourScheme();
  81. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor);
  82. };
  83. //==============================================================================
  84. class CppCodeEditorComponent : public CodeEditorComponent
  85. {
  86. public:
  87. CppCodeEditorComponent (CodeDocument& codeDocument)
  88. : CodeEditorComponent (codeDocument, getCppTokeniser())
  89. {
  90. }
  91. void handleReturnKey()
  92. {
  93. CodeEditorComponent::handleReturnKey();
  94. const CodeDocument::Position pos (getCaretPos());
  95. if (pos.getLineNumber() > 0 && pos.getLineText().trim().isEmpty())
  96. {
  97. String indent (getIndentForCurrentBlock (pos));
  98. const String previousLine (pos.movedByLines (-1).getLineText());
  99. const String trimmedPreviousLine (previousLine.trim());
  100. const String leadingWhitespace (getLeadingWhitespace (previousLine));
  101. insertTextAtCaret (leadingWhitespace);
  102. if (trimmedPreviousLine.endsWithChar ('{')
  103. || ((trimmedPreviousLine.startsWith ("if ")
  104. || trimmedPreviousLine.startsWith ("for ")
  105. || trimmedPreviousLine.startsWith ("while "))
  106. && trimmedPreviousLine.endsWithChar (')')))
  107. insertTabAtCaret();
  108. }
  109. }
  110. void insertTextAtCaret (const String& newText)
  111. {
  112. if (getHighlightedRegion().isEmpty())
  113. {
  114. const CodeDocument::Position pos (getCaretPos());
  115. if ((newText == "{" || newText == "}")
  116. && pos.getLineNumber() > 0
  117. && pos.getLineText().trim().isEmpty())
  118. {
  119. moveCaretToStartOfLine (true);
  120. CodeEditorComponent::insertTextAtCaret (getIndentForCurrentBlock (pos));
  121. if (newText == "{")
  122. insertTabAtCaret();
  123. }
  124. }
  125. CodeEditorComponent::insertTextAtCaret (newText);
  126. }
  127. private:
  128. static CPlusPlusCodeTokeniser* getCppTokeniser()
  129. {
  130. static CPlusPlusCodeTokeniser cppTokeniser;
  131. return &cppTokeniser;
  132. }
  133. static String getLeadingWhitespace (String line)
  134. {
  135. line = line.removeCharacters ("\r\n");
  136. const String::CharPointerType endOfLeadingWS (line.getCharPointer().findEndOfWhitespace());
  137. return String (line.getCharPointer(), endOfLeadingWS);
  138. }
  139. static String getIndentForCurrentBlock (CodeDocument::Position pos)
  140. {
  141. int braceCount = 0;
  142. while (pos.getLineNumber() > 0)
  143. {
  144. pos = pos.movedByLines (-1);
  145. const String line (pos.getLineText());
  146. const String trimmedLine (line.trimStart());
  147. StringArray tokens;
  148. tokens.addTokens (trimmedLine, true);
  149. for (int i = tokens.size(); --i >= 0;)
  150. {
  151. if (tokens[i] == "}")
  152. ++braceCount;
  153. if (tokens[i] == "{")
  154. if (--braceCount < 0)
  155. return getLeadingWhitespace (line);
  156. }
  157. }
  158. return String::empty;
  159. }
  160. };
  161. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__