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.

243 lines
8.9KB

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