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.

145 lines
4.9KB

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