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) 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 "jucer_DocumentEditorComponent.h"
  21. //==============================================================================
  22. class SourceCodeDocument : public OpenDocumentManager::Document
  23. {
  24. public:
  25. SourceCodeDocument (Project*, const File&);
  26. bool loadedOk() const override { return true; }
  27. bool isForFile (const File& file) const override { return getFile() == file; }
  28. bool isForNode (const ValueTree&) const override { return false; }
  29. bool refersToProject (Project& p) const override { return project == &p; }
  30. Project* getProject() const override { return project; }
  31. String getName() const override { return getFile().getFileName(); }
  32. String getType() const override { return getFile().getFileExtension() + " file"; }
  33. File getFile() const override { return modDetector.getFile(); }
  34. bool needsSaving() const override { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); }
  35. bool hasFileBeenModifiedExternally() override { return modDetector.hasBeenModified(); }
  36. void fileHasBeenRenamed (const File& newFile) override { modDetector.fileHasBeenRenamed (newFile); }
  37. String getState() const override { return lastState != nullptr ? lastState->toString() : String(); }
  38. void restoreState (const String& state) override { lastState = new CodeEditorComponent::State (state); }
  39. File getCounterpartFile() const override
  40. {
  41. const File file (getFile());
  42. if (file.hasFileExtension (sourceFileExtensions))
  43. {
  44. static const char* extensions[] = { "h", "hpp", "hxx", "hh", nullptr };
  45. return findCounterpart (file, extensions);
  46. }
  47. if (file.hasFileExtension (headerFileExtensions))
  48. {
  49. static const char* extensions[] = { "cpp", "mm", "cc", "cxx", "c", "m", nullptr };
  50. return findCounterpart (file, extensions);
  51. }
  52. return {};
  53. }
  54. static File findCounterpart (const File& file, const char** extensions)
  55. {
  56. while (*extensions != nullptr)
  57. {
  58. const File f (file.withFileExtension (*extensions++));
  59. if (f.existsAsFile())
  60. return f;
  61. }
  62. return {};
  63. }
  64. void reloadFromFile() override;
  65. bool save() override;
  66. bool saveAs() override;
  67. Component* createEditor() override;
  68. Component* createViewer() override { return createEditor(); }
  69. void updateLastState (CodeEditorComponent&);
  70. void applyLastState (CodeEditorComponent&) const;
  71. CodeDocument& getCodeDocument();
  72. //==============================================================================
  73. struct Type : 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. ScopedPointer<CodeDocument> codeDoc;
  102. Project* project;
  103. ScopedPointer<CodeEditorComponent::State> lastState;
  104. void reloadInternal();
  105. };
  106. class GenericCodeEditorComponent;
  107. //==============================================================================
  108. class SourceCodeEditor : public DocumentEditorComponent,
  109. private ValueTree::Listener,
  110. private CodeDocument::Listener
  111. {
  112. public:
  113. SourceCodeEditor (OpenDocumentManager::Document*, CodeDocument&);
  114. SourceCodeEditor (OpenDocumentManager::Document*, GenericCodeEditorComponent*);
  115. ~SourceCodeEditor();
  116. void scrollToKeepRangeOnScreen (Range<int> range);
  117. void highlight (Range<int> range, bool cursorAtStart);
  118. ScopedPointer<GenericCodeEditorComponent> editor;
  119. private:
  120. void resized() override;
  121. void lookAndFeelChanged() override;
  122. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  123. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  124. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  125. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  126. void valueTreeParentChanged (ValueTree&) override;
  127. void valueTreeRedirected (ValueTree&) override;
  128. void codeDocumentTextInserted (const String&, int) override;
  129. void codeDocumentTextDeleted (int, int) override;
  130. void setEditor (GenericCodeEditorComponent*);
  131. void updateColourScheme();
  132. void checkSaveState();
  133. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor)
  134. };
  135. //==============================================================================
  136. class GenericCodeEditorComponent : public CodeEditorComponent
  137. {
  138. public:
  139. GenericCodeEditorComponent (const File&, CodeDocument&, CodeTokeniser*);
  140. ~GenericCodeEditorComponent();
  141. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  142. void performPopupMenuAction (int menuItemID) override;
  143. void getAllCommands (Array<CommandID>&) override;
  144. void getCommandInfo (CommandID, ApplicationCommandInfo&) override;
  145. bool perform (const InvocationInfo&) override;
  146. void showFindPanel();
  147. void hideFindPanel();
  148. void findSelection();
  149. void findNext (bool forwards, bool skipCurrentSelection);
  150. void handleEscapeKey() override;
  151. void editorViewportPositionChanged() override;
  152. void resized() override;
  153. static String getSearchString() { return getAppSettings().getGlobalProperties().getValue ("searchString"); }
  154. static void setSearchString (const String& s) { getAppSettings().getGlobalProperties().setValue ("searchString", s); }
  155. static bool isCaseSensitiveSearch() { return getAppSettings().getGlobalProperties().getBoolValue ("searchCaseSensitive"); }
  156. static void setCaseSensitiveSearch (bool b) { getAppSettings().getGlobalProperties().setValue ("searchCaseSensitive", b); }
  157. struct Listener
  158. {
  159. virtual ~Listener() {}
  160. virtual void codeEditorViewportMoved (CodeEditorComponent&) = 0;
  161. };
  162. void addListener (Listener* listener);
  163. void removeListener (Listener* listener);
  164. private:
  165. File file;
  166. class FindPanel;
  167. ScopedPointer<FindPanel> findPanel;
  168. ListenerList<Listener> listeners;
  169. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GenericCodeEditorComponent)
  170. };
  171. //==============================================================================
  172. class CppCodeEditorComponent : public GenericCodeEditorComponent
  173. {
  174. public:
  175. CppCodeEditorComponent (const File&, CodeDocument&);
  176. ~CppCodeEditorComponent();
  177. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  178. void performPopupMenuAction (int menuItemID) override;
  179. void handleReturnKey() override;
  180. void insertTextAtCaret (const String& newText) override;
  181. private:
  182. void insertComponentClass();
  183. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CppCodeEditorComponent)
  184. };