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.

127 lines
5.1KB

  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& node) 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. void reloadFromFile();
  41. bool save();
  42. Component* createEditor();
  43. Component* createViewer() { return createEditor(); }
  44. void updateLastState (CodeEditorComponent& editor);
  45. void applyLastState (CodeEditorComponent& editor) const;
  46. CodeDocument& getCodeDocument();
  47. //==============================================================================
  48. struct Type : public OpenDocumentManager::DocumentType
  49. {
  50. bool canOpenFile (const File& file) { return file.hasFileExtension ("cpp;h;hpp;mm;m;c;cc;cxx;txt;inc;tcc;xml;plist;rtf;html;htm;php;py;rb;cs"); }
  51. Document* openFile (Project* project, const File& file) { return new SourceCodeDocument (project, file); }
  52. };
  53. protected:
  54. FileModificationDetector modDetector;
  55. ScopedPointer<CodeDocument> codeDoc;
  56. Project* project;
  57. ScopedPointer<CodeEditorComponent::State> lastState;
  58. void reloadInternal();
  59. };
  60. //==============================================================================
  61. class SourceCodeEditor : public DocumentEditorComponent,
  62. private ValueTree::Listener
  63. {
  64. public:
  65. SourceCodeEditor (OpenDocumentManager::Document* document);
  66. ~SourceCodeEditor();
  67. void createEditor (CodeDocument& codeDocument);
  68. void setEditor (CodeEditorComponent*);
  69. void highlightLine (int lineNum, int characterIndex);
  70. ScopedPointer<CodeEditorComponent> editor;
  71. private:
  72. void resized();
  73. void valueTreePropertyChanged (ValueTree&, const Identifier&);
  74. void valueTreeChildAdded (ValueTree&, ValueTree&);
  75. void valueTreeChildRemoved (ValueTree&, ValueTree&);
  76. void valueTreeChildOrderChanged (ValueTree&);
  77. void valueTreeParentChanged (ValueTree&);
  78. void valueTreeRedirected (ValueTree&);
  79. void updateColourScheme();
  80. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor);
  81. };
  82. //==============================================================================
  83. class CppCodeEditorComponent : public CodeEditorComponent
  84. {
  85. public:
  86. CppCodeEditorComponent (CodeDocument& codeDocument);
  87. void handleReturnKey();
  88. void insertTextAtCaret (const String& newText);
  89. private:
  90. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CppCodeEditorComponent);
  91. };
  92. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__