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.

231 lines
8.7KB

  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 (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 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 (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. //==============================================================================
  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*, CodeEditorComponent*);
  114. ~SourceCodeEditor();
  115. void scrollToKeepRangeOnScreen (Range<int> range);
  116. void highlight (Range<int> range, bool cursorAtStart);
  117. ScopedPointer<CodeEditorComponent> editor;
  118. private:
  119. void resized() override;
  120. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  121. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  122. void valueTreeChildRemoved (ValueTree&, ValueTree&) override;
  123. void valueTreeChildOrderChanged (ValueTree&) override;
  124. void valueTreeParentChanged (ValueTree&) override;
  125. void valueTreeRedirected (ValueTree&) override;
  126. void codeDocumentTextInserted (const String&, int) override;
  127. void codeDocumentTextDeleted (int, int) override;
  128. void setEditor (CodeEditorComponent*);
  129. void updateColourScheme();
  130. void checkSaveState();
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor)
  132. };
  133. //==============================================================================
  134. class GenericCodeEditorComponent : public CodeEditorComponent
  135. {
  136. public:
  137. GenericCodeEditorComponent (const File&, CodeDocument&, CodeTokeniser*);
  138. ~GenericCodeEditorComponent();
  139. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  140. void performPopupMenuAction (int menuItemID) override;
  141. void getAllCommands (Array<CommandID>&) override;
  142. void getCommandInfo (CommandID, ApplicationCommandInfo&) override;
  143. bool perform (const InvocationInfo&) override;
  144. void showFindPanel();
  145. void hideFindPanel();
  146. void findSelection();
  147. void findNext (bool forwards, bool skipCurrentSelection);
  148. void handleEscapeKey() override;
  149. void resized() override;
  150. static String getSearchString() { return getAppSettings().getGlobalProperties().getValue ("searchString"); }
  151. static void setSearchString (const String& s) { getAppSettings().getGlobalProperties().setValue ("searchString", s); }
  152. static bool isCaseSensitiveSearch() { return getAppSettings().getGlobalProperties().getBoolValue ("searchCaseSensitive"); }
  153. static void setCaseSensitiveSearch (bool b) { getAppSettings().getGlobalProperties().setValue ("searchCaseSensitive", b); }
  154. private:
  155. File file;
  156. class FindPanel;
  157. ScopedPointer<FindPanel> findPanel;
  158. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GenericCodeEditorComponent)
  159. };
  160. //==============================================================================
  161. class CppCodeEditorComponent : public GenericCodeEditorComponent
  162. {
  163. public:
  164. CppCodeEditorComponent (const File&, CodeDocument&);
  165. ~CppCodeEditorComponent();
  166. void addPopupMenuItems (PopupMenu&, const MouseEvent*) override;
  167. void performPopupMenuAction (int menuItemID) override;
  168. void handleReturnKey() override;
  169. void insertTextAtCaret (const String& newText) override;
  170. private:
  171. void insertComponentClass();
  172. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CppCodeEditorComponent)
  173. };
  174. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__