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.

232 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  19. #define __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  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 { return true; }
  28. bool isForFile (const File& file) const { return getFile() == file; }
  29. bool isForNode (const ValueTree&) const { return false; }
  30. bool refersToProject (Project& p) const { return project == &p; }
  31. Project* getProject() const { return project; }
  32. String getName() const { return getFile().getFileName(); }
  33. String getType() const { return getFile().getFileExtension() + " file"; }
  34. File getFile() const { return modDetector.getFile(); }
  35. bool needsSaving() const { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); }
  36. bool hasFileBeenModifiedExternally() { return modDetector.hasBeenModified(); }
  37. void fileHasBeenRenamed (const File& newFile) { modDetector.fileHasBeenRenamed (newFile); }
  38. String getState() const { return lastState != nullptr ? lastState->toString() : String::empty; }
  39. void restoreState (const String& state) { lastState = new CodeEditorComponent::State (state); }
  40. File getCounterpartFile() const
  41. {
  42. const File file (getFile());
  43. if (file.hasFileExtension ("cpp;c;mm;m"))
  44. {
  45. const char* extensions[] = { "h", "hpp", nullptr };
  46. return findCounterpart (file, extensions);
  47. }
  48. if (file.hasFileExtension ("h;hpp"))
  49. {
  50. const char* extensions[] = { "cpp", "mm", "cc", "cxx", "c", "m", nullptr };
  51. return findCounterpart (file, extensions);
  52. }
  53. return File::nonexistent;
  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 File::nonexistent;
  64. }
  65. void reloadFromFile();
  66. bool save();
  67. bool saveAs();
  68. Component* createEditor();
  69. Component* createViewer() { return createEditor(); }
  70. void updateLastState (CodeEditorComponent& editor);
  71. void applyLastState (CodeEditorComponent& editor) const;
  72. CodeDocument& getCodeDocument();
  73. //==============================================================================
  74. struct Type : public OpenDocumentManager::DocumentType
  75. {
  76. bool canOpenFile (const File& file)
  77. {
  78. if (file.hasFileExtension ("cpp;h;hpp;mm;m;c;cc;cxx;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) { 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* document);
  113. ~SourceCodeEditor();
  114. void createEditor (CodeDocument& codeDocument);
  115. void setEditor (CodeEditorComponent*);
  116. void scrollToKeepRangeOnScreen (Range<int> range);
  117. void highlight (Range<int> range, bool cursorAtStart);
  118. ScopedPointer<CodeEditorComponent> editor;
  119. private:
  120. void resized();
  121. void valueTreePropertyChanged (ValueTree&, const Identifier&);
  122. void valueTreeChildAdded (ValueTree&, ValueTree&);
  123. void valueTreeChildRemoved (ValueTree&, ValueTree&);
  124. void valueTreeChildOrderChanged (ValueTree&);
  125. void valueTreeParentChanged (ValueTree&);
  126. void valueTreeRedirected (ValueTree&);
  127. void codeDocumentTextInserted (const String&, int);
  128. void codeDocumentTextDeleted (int, int);
  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*);
  140. void performPopupMenuAction (int menuItemID);
  141. void getAllCommands (Array<CommandID>&);
  142. void getCommandInfo (CommandID, ApplicationCommandInfo&);
  143. bool perform (const InvocationInfo&);
  144. void showFindPanel();
  145. void hideFindPanel();
  146. void findSelection();
  147. void findNext (bool forwards, bool skipCurrentSelection);
  148. void handleEscapeKey();
  149. void resized();
  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& file, CodeDocument& codeDocument);
  165. ~CppCodeEditorComponent();
  166. void addPopupMenuItems (PopupMenu&, const MouseEvent*);
  167. void performPopupMenuAction (int menuItemID);
  168. void handleReturnKey();
  169. void insertTextAtCaret (const String& newText);
  170. private:
  171. void insertComponentClass();
  172. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CppCodeEditorComponent)
  173. };
  174. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__