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.

246 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. #include "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.reset (new CodeEditorComponent::State (state)); }
  38. File getCounterpartFile() const override
  39. {
  40. auto 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. auto f = file.withFileExtension (*extensions++);
  58. if (f.existsAsFile())
  59. return f;
  60. }
  61. return {};
  62. }
  63. void reloadFromFile() override;
  64. bool saveSyncWithoutAsking() override;
  65. void saveAsync (std::function<void (bool)>) override;
  66. void saveAsAsync (std::function<void (bool)>) override;
  67. std::unique_ptr<Component> createEditor() override;
  68. std::unique_ptr<Component> createViewer() override { return createEditor(); }
  69. void updateLastState (CodeEditorComponent&);
  70. void applyLastState (CodeEditorComponent&) const;
  71. CodeDocument& getCodeDocument();
  72. //==============================================================================
  73. struct Type final : public OpenDocumentManager::DocumentType
  74. {
  75. bool canOpenFile (const File& file) override
  76. {
  77. if (file.hasFileExtension (sourceOrHeaderFileExtensions)
  78. || file.hasFileExtension ("txt;inc;tcc;xml;plist;rtf;html;htm;php;py;rb;cs"))
  79. return true;
  80. MemoryBlock mb;
  81. if (file.loadFileAsData (mb)
  82. && seemsToBeText (static_cast<const char*> (mb.getData()), (int) mb.getSize())
  83. && ! file.hasFileExtension ("svg"))
  84. return true;
  85. return false;
  86. }
  87. static bool seemsToBeText (const char* const chars, const int num) noexcept
  88. {
  89. for (int i = 0; i < num; ++i)
  90. {
  91. const char c = chars[i];
  92. if ((c < 32 && c != '\t' && c != '\r' && c != '\n') || chars[i] > 126)
  93. return false;
  94. }
  95. return true;
  96. }
  97. Document* openFile (Project* p, const File& file) override { return new SourceCodeDocument (p, file); }
  98. };
  99. protected:
  100. FileModificationDetector modDetector;
  101. std::unique_ptr<CodeDocument> codeDoc;
  102. Project* project;
  103. std::unique_ptr<CodeEditorComponent::State> lastState;
  104. void reloadInternal();
  105. private:
  106. std::unique_ptr<FileChooser> chooser;
  107. };
  108. class GenericCodeEditorComponent;
  109. //==============================================================================
  110. class SourceCodeEditor final : public DocumentEditorComponent,
  111. private ValueTree::Listener,
  112. private CodeDocument::Listener
  113. {
  114. public:
  115. SourceCodeEditor (OpenDocumentManager::Document*, CodeDocument&);
  116. SourceCodeEditor (OpenDocumentManager::Document*, GenericCodeEditorComponent*);
  117. ~SourceCodeEditor() override;
  118. void scrollToKeepRangeOnScreen (Range<int> range);
  119. void highlight (Range<int> range, bool cursorAtStart);
  120. std::unique_ptr<GenericCodeEditorComponent> editor;
  121. private:
  122. void resized() override;
  123. void lookAndFeelChanged() override;
  124. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  125. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  126. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  127. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  128. void valueTreeParentChanged (ValueTree&) override;
  129. void valueTreeRedirected (ValueTree&) override;
  130. void codeDocumentTextInserted (const String&, int) override;
  131. void codeDocumentTextDeleted (int, int) override;
  132. void setEditor (GenericCodeEditorComponent*);
  133. void updateColourScheme();
  134. void checkSaveState();
  135. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor)
  136. };
  137. //==============================================================================
  138. class GenericCodeEditorComponent : public CodeEditorComponent
  139. {
  140. public:
  141. GenericCodeEditorComponent (const File&, CodeDocument&, CodeTokeniser*);
  142. ~GenericCodeEditorComponent() override;
  143. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  144. void performPopupMenuAction (int menuItemID) override;
  145. void getAllCommands (Array<CommandID>&) override;
  146. void getCommandInfo (CommandID, ApplicationCommandInfo&) override;
  147. bool perform (const InvocationInfo&) override;
  148. void showFindPanel();
  149. void hideFindPanel();
  150. void findSelection();
  151. void findNext (bool forwards, bool skipCurrentSelection);
  152. void handleEscapeKey() override;
  153. void editorViewportPositionChanged() override;
  154. void resized() override;
  155. static String getSearchString() { return getAppSettings().getGlobalProperties().getValue ("searchString"); }
  156. static void setSearchString (const String& s) { getAppSettings().getGlobalProperties().setValue ("searchString", s); }
  157. static bool isCaseSensitiveSearch() { return getAppSettings().getGlobalProperties().getBoolValue ("searchCaseSensitive"); }
  158. static void setCaseSensitiveSearch (bool b) { getAppSettings().getGlobalProperties().setValue ("searchCaseSensitive", b); }
  159. struct Listener
  160. {
  161. virtual ~Listener() {}
  162. virtual void codeEditorViewportMoved (CodeEditorComponent&) = 0;
  163. };
  164. void addListener (Listener* listener);
  165. void removeListener (Listener* listener);
  166. private:
  167. File file;
  168. class FindPanel;
  169. std::unique_ptr<FindPanel> findPanel;
  170. ListenerList<Listener> listeners;
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GenericCodeEditorComponent)
  172. };
  173. //==============================================================================
  174. class CppCodeEditorComponent final : public GenericCodeEditorComponent
  175. {
  176. public:
  177. CppCodeEditorComponent (const File&, CodeDocument&);
  178. ~CppCodeEditorComponent() override;
  179. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  180. void performPopupMenuAction (int menuItemID) override;
  181. void handleReturnKey() override;
  182. void insertTextAtCaret (const String& newText) override;
  183. private:
  184. void insertComponentClass();
  185. std::unique_ptr<AlertWindow> asyncAlertWindow;
  186. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CppCodeEditorComponent)
  187. };