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.

211 lines
7.7KB

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