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.

241 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. #include "../Project/jucer_Project.h"
  19. #include "../Application/jucer_DocumentEditorComponent.h"
  20. //==============================================================================
  21. class SourceCodeDocument : public OpenDocumentManager::Document
  22. {
  23. public:
  24. SourceCodeDocument (Project*, const File&);
  25. bool loadedOk() const override { return true; }
  26. bool isForFile (const File& file) const override { return getFile() == file; }
  27. bool isForNode (const ValueTree&) const override { return false; }
  28. bool refersToProject (Project& p) const override { return project == &p; }
  29. Project* getProject() const override { return project; }
  30. String getName() const override { return getFile().getFileName(); }
  31. String getType() const override { return getFile().getFileExtension() + " file"; }
  32. File getFile() const override { return modDetector.getFile(); }
  33. bool needsSaving() const override { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); }
  34. bool hasFileBeenModifiedExternally() override { return modDetector.hasBeenModified(); }
  35. void fileHasBeenRenamed (const File& newFile) override { modDetector.fileHasBeenRenamed (newFile); }
  36. String getState() const override { return lastState != nullptr ? lastState->toString() : String(); }
  37. void restoreState (const String& state) override { lastState = new CodeEditorComponent::State (state); }
  38. File getCounterpartFile() const override
  39. {
  40. const File file (getFile());
  41. if (file.hasFileExtension (sourceFileExtensions))
  42. {
  43. static const char* extensions[] = { "h", "hpp", "hxx", "hh", nullptr };
  44. return findCounterpart (file, extensions);
  45. }
  46. if (file.hasFileExtension (headerFileExtensions))
  47. {
  48. static const char* extensions[] = { "cpp", "mm", "cc", "cxx", "c", "m", nullptr };
  49. return findCounterpart (file, extensions);
  50. }
  51. return {};
  52. }
  53. static File findCounterpart (const File& file, const char** extensions)
  54. {
  55. while (*extensions != nullptr)
  56. {
  57. const File f (file.withFileExtension (*extensions++));
  58. if (f.existsAsFile())
  59. return f;
  60. }
  61. return {};
  62. }
  63. void reloadFromFile() override;
  64. bool save() override;
  65. bool saveAs() override;
  66. Component* createEditor() override;
  67. Component* createViewer() override { return createEditor(); }
  68. void updateLastState (CodeEditorComponent&);
  69. void applyLastState (CodeEditorComponent&) const;
  70. CodeDocument& getCodeDocument();
  71. //==============================================================================
  72. struct Type : public OpenDocumentManager::DocumentType
  73. {
  74. bool canOpenFile (const File& file) override
  75. {
  76. if (file.hasFileExtension (sourceOrHeaderFileExtensions)
  77. || file.hasFileExtension ("txt;inc;tcc;xml;plist;rtf;html;htm;php;py;rb;cs"))
  78. return true;
  79. MemoryBlock mb;
  80. if (file.loadFileAsData (mb)
  81. && seemsToBeText (static_cast<const char*> (mb.getData()), (int) mb.getSize())
  82. && ! file.hasFileExtension ("svg"))
  83. return true;
  84. return false;
  85. }
  86. static bool seemsToBeText (const char* const chars, const int num) noexcept
  87. {
  88. for (int i = 0; i < num; ++i)
  89. {
  90. const char c = chars[i];
  91. if ((c < 32 && c != '\t' && c != '\r' && c != '\n') || chars[i] > 126)
  92. return false;
  93. }
  94. return true;
  95. }
  96. Document* openFile (Project* p, const File& file) override { return new SourceCodeDocument (p, file); }
  97. };
  98. protected:
  99. FileModificationDetector modDetector;
  100. ScopedPointer<CodeDocument> codeDoc;
  101. Project* project;
  102. ScopedPointer<CodeEditorComponent::State> lastState;
  103. void reloadInternal();
  104. };
  105. class GenericCodeEditorComponent;
  106. //==============================================================================
  107. class SourceCodeEditor : public DocumentEditorComponent,
  108. private ValueTree::Listener,
  109. private CodeDocument::Listener
  110. {
  111. public:
  112. SourceCodeEditor (OpenDocumentManager::Document*, CodeDocument&);
  113. SourceCodeEditor (OpenDocumentManager::Document*, GenericCodeEditorComponent*);
  114. ~SourceCodeEditor();
  115. void scrollToKeepRangeOnScreen (Range<int> range);
  116. void highlight (Range<int> range, bool cursorAtStart);
  117. ScopedPointer<GenericCodeEditorComponent> editor;
  118. private:
  119. void resized() override;
  120. void lookAndFeelChanged() override;
  121. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  122. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  123. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  124. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  125. void valueTreeParentChanged (ValueTree&) override;
  126. void valueTreeRedirected (ValueTree&) override;
  127. void codeDocumentTextInserted (const String&, int) override;
  128. void codeDocumentTextDeleted (int, int) override;
  129. void setEditor (GenericCodeEditorComponent*);
  130. void updateColourScheme();
  131. void checkSaveState();
  132. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor)
  133. };
  134. //==============================================================================
  135. class GenericCodeEditorComponent : public CodeEditorComponent
  136. {
  137. public:
  138. GenericCodeEditorComponent (const File&, CodeDocument&, CodeTokeniser*);
  139. ~GenericCodeEditorComponent();
  140. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  141. void performPopupMenuAction (int menuItemID) override;
  142. void getAllCommands (Array<CommandID>&) override;
  143. void getCommandInfo (CommandID, ApplicationCommandInfo&) override;
  144. bool perform (const InvocationInfo&) override;
  145. void showFindPanel();
  146. void hideFindPanel();
  147. void findSelection();
  148. void findNext (bool forwards, bool skipCurrentSelection);
  149. void handleEscapeKey() override;
  150. void editorViewportPositionChanged() override;
  151. void resized() override;
  152. static String getSearchString() { return getAppSettings().getGlobalProperties().getValue ("searchString"); }
  153. static void setSearchString (const String& s) { getAppSettings().getGlobalProperties().setValue ("searchString", s); }
  154. static bool isCaseSensitiveSearch() { return getAppSettings().getGlobalProperties().getBoolValue ("searchCaseSensitive"); }
  155. static void setCaseSensitiveSearch (bool b) { getAppSettings().getGlobalProperties().setValue ("searchCaseSensitive", b); }
  156. struct Listener
  157. {
  158. virtual ~Listener() {}
  159. virtual void codeEditorViewportMoved (CodeEditorComponent&) = 0;
  160. };
  161. void addListener (Listener* listener);
  162. void removeListener (Listener* listener);
  163. private:
  164. File file;
  165. class FindPanel;
  166. ScopedPointer<FindPanel> findPanel;
  167. ListenerList<Listener> listeners;
  168. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GenericCodeEditorComponent)
  169. };
  170. //==============================================================================
  171. class CppCodeEditorComponent : public GenericCodeEditorComponent
  172. {
  173. public:
  174. CppCodeEditorComponent (const File&, CodeDocument&);
  175. ~CppCodeEditorComponent();
  176. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  177. void performPopupMenuAction (int menuItemID) override;
  178. void handleReturnKey() override;
  179. void insertTextAtCaret (const String& newText) override;
  180. private:
  181. void insertComponentClass();
  182. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CppCodeEditorComponent)
  183. };