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.

230 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. #ifndef __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  18. #define __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  19. #include "../Project/jucer_Project.h"
  20. #include "../Application/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::empty; }
  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 ("cpp;c;mm;m"))
  43. {
  44. const char* extensions[] = { "h", "hpp", nullptr };
  45. return findCounterpart (file, extensions);
  46. }
  47. if (file.hasFileExtension ("h;hpp"))
  48. {
  49. const char* extensions[] = { "cpp", "mm", "cc", "cxx", "c", "m", nullptr };
  50. return findCounterpart (file, extensions);
  51. }
  52. return File::nonexistent;
  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 File::nonexistent;
  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 ("cpp;h;hpp;mm;m;c;cc;cxx;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. //==============================================================================
  106. class SourceCodeEditor : public DocumentEditorComponent,
  107. private ValueTree::Listener,
  108. private CodeDocument::Listener
  109. {
  110. public:
  111. SourceCodeEditor (OpenDocumentManager::Document*, CodeDocument&);
  112. SourceCodeEditor (OpenDocumentManager::Document*, CodeEditorComponent*);
  113. ~SourceCodeEditor();
  114. void scrollToKeepRangeOnScreen (Range<int> range);
  115. void highlight (Range<int> range, bool cursorAtStart);
  116. ScopedPointer<CodeEditorComponent> editor;
  117. private:
  118. void resized() override;
  119. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  120. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  121. void valueTreeChildRemoved (ValueTree&, ValueTree&) override;
  122. void valueTreeChildOrderChanged (ValueTree&) override;
  123. void valueTreeParentChanged (ValueTree&) override;
  124. void valueTreeRedirected (ValueTree&) override;
  125. void codeDocumentTextInserted (const String&, int) override;
  126. void codeDocumentTextDeleted (int, int) override;
  127. void setEditor (CodeEditorComponent*);
  128. void updateColourScheme();
  129. void checkSaveState();
  130. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor)
  131. };
  132. //==============================================================================
  133. class GenericCodeEditorComponent : public CodeEditorComponent
  134. {
  135. public:
  136. GenericCodeEditorComponent (const File&, CodeDocument&, CodeTokeniser*);
  137. ~GenericCodeEditorComponent();
  138. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  139. void performPopupMenuAction (int menuItemID) override;
  140. void getAllCommands (Array<CommandID>&) override;
  141. void getCommandInfo (CommandID, ApplicationCommandInfo&) override;
  142. bool perform (const InvocationInfo&) override;
  143. void showFindPanel();
  144. void hideFindPanel();
  145. void findSelection();
  146. void findNext (bool forwards, bool skipCurrentSelection);
  147. void handleEscapeKey() override;
  148. void resized() override;
  149. static String getSearchString() { return getAppSettings().getGlobalProperties().getValue ("searchString"); }
  150. static void setSearchString (const String& s) { getAppSettings().getGlobalProperties().setValue ("searchString", s); }
  151. static bool isCaseSensitiveSearch() { return getAppSettings().getGlobalProperties().getBoolValue ("searchCaseSensitive"); }
  152. static void setCaseSensitiveSearch (bool b) { getAppSettings().getGlobalProperties().setValue ("searchCaseSensitive", b); }
  153. private:
  154. File file;
  155. class FindPanel;
  156. ScopedPointer<FindPanel> findPanel;
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GenericCodeEditorComponent)
  158. };
  159. //==============================================================================
  160. class CppCodeEditorComponent : public GenericCodeEditorComponent
  161. {
  162. public:
  163. CppCodeEditorComponent (const File& file, CodeDocument&);
  164. ~CppCodeEditorComponent();
  165. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  166. void performPopupMenuAction (int menuItemID) override;
  167. void handleReturnKey() override;
  168. void insertTextAtCaret (const String& newText) override;
  169. private:
  170. void insertComponentClass();
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CppCodeEditorComponent)
  172. };
  173. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__