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.

159 lines
5.3KB

  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 SourceCodeEditor : public DocumentEditorComponent,
  24. private ValueTree::Listener
  25. {
  26. public:
  27. SourceCodeEditor (OpenDocumentManager::Document* document, CodeDocument& codeDocument);
  28. SourceCodeEditor (OpenDocumentManager::Document* document);
  29. ~SourceCodeEditor();
  30. void createEditor (CodeDocument& codeDocument);
  31. void setEditor (CodeEditorComponent*);
  32. private:
  33. ScopedPointer<CodeEditorComponent> editor;
  34. void resized();
  35. void valueTreePropertyChanged (ValueTree&, const Identifier&);
  36. void valueTreeChildAdded (ValueTree&, ValueTree&);
  37. void valueTreeChildRemoved (ValueTree&, ValueTree&);
  38. void valueTreeChildOrderChanged (ValueTree&);
  39. void valueTreeParentChanged (ValueTree&);
  40. void valueTreeRedirected (ValueTree&);
  41. void updateColourScheme();
  42. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor);
  43. };
  44. //==============================================================================
  45. class CppCodeEditorComponent : public CodeEditorComponent
  46. {
  47. public:
  48. CppCodeEditorComponent (CodeDocument& codeDocument)
  49. : CodeEditorComponent (codeDocument, getCppTokeniser())
  50. {
  51. }
  52. void handleReturnKey()
  53. {
  54. CodeEditorComponent::handleReturnKey();
  55. const CodeDocument::Position pos (getCaretPos());
  56. if (pos.getLineNumber() > 0 && pos.getLineText().trim().isEmpty())
  57. {
  58. String indent (getIndentForCurrentBlock (pos));
  59. const String previousLine (pos.movedByLines (-1).getLineText());
  60. const String trimmedPreviousLine (previousLine.trim());
  61. const String leadingWhitespace (getLeadingWhitespace (previousLine));
  62. insertTextAtCaret (leadingWhitespace);
  63. if (trimmedPreviousLine.endsWithChar ('{')
  64. || ((trimmedPreviousLine.startsWith ("if ")
  65. || trimmedPreviousLine.startsWith ("for ")
  66. || trimmedPreviousLine.startsWith ("while "))
  67. && trimmedPreviousLine.endsWithChar (')')))
  68. insertTabAtCaret();
  69. }
  70. }
  71. void insertTextAtCaret (const String& newText)
  72. {
  73. if (getHighlightedRegion().isEmpty())
  74. {
  75. const CodeDocument::Position pos (getCaretPos());
  76. if ((newText == "{" || newText == "}") && pos.getLineNumber() > 0)
  77. {
  78. moveCaretToStartOfLine (true);
  79. CodeEditorComponent::insertTextAtCaret (getIndentForCurrentBlock (pos));
  80. if (newText == "{")
  81. insertTabAtCaret();
  82. }
  83. }
  84. CodeEditorComponent::insertTextAtCaret (newText);
  85. }
  86. private:
  87. static CPlusPlusCodeTokeniser* getCppTokeniser()
  88. {
  89. static CPlusPlusCodeTokeniser cppTokeniser;
  90. return &cppTokeniser;
  91. }
  92. static String getLeadingWhitespace (String line)
  93. {
  94. line = line.removeCharacters ("\r\n");
  95. const String::CharPointerType endOfLeadingWS (line.getCharPointer().findEndOfWhitespace());
  96. return String (line.getCharPointer(), endOfLeadingWS);
  97. }
  98. static String getIndentForCurrentBlock (CodeDocument::Position pos)
  99. {
  100. int braceCount = 0;
  101. while (pos.getLineNumber() > 0)
  102. {
  103. pos = pos.movedByLines (-1);
  104. const String line (pos.getLineText());
  105. const String trimmedLine (line.trimStart());
  106. StringArray tokens;
  107. tokens.addTokens (trimmedLine, true);
  108. for (int i = tokens.size(); --i >= 0;)
  109. {
  110. if (tokens[i] == "}")
  111. ++braceCount;
  112. if (tokens[i] == "{")
  113. if (--braceCount < 0)
  114. return getLeadingWhitespace (line);
  115. }
  116. }
  117. return String::empty;
  118. }
  119. };
  120. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__