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.

184 lines
7.8KB

  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 "../Application/jucer_OpenDocumentManager.h"
  21. #include "../Code Editor/jucer_SourceCodeEditor.h"
  22. #include "components/jucer_ComponentTypeHandler.h"
  23. #include "jucer_PaintRoutine.h"
  24. #include "jucer_ComponentLayout.h"
  25. #include "jucer_BinaryResources.h"
  26. //==============================================================================
  27. class JucerDocument : public ChangeBroadcaster,
  28. private Timer,
  29. private CodeDocument::Listener,
  30. private OpenDocumentManager::DocumentCloseListener
  31. {
  32. public:
  33. JucerDocument (SourceCodeDocument* cpp);
  34. ~JucerDocument();
  35. static bool isValidJucerCppFile (const File&);
  36. static XmlElement* pullMetaDataFromCppFile (const String& cpp);
  37. static JucerDocument* createForCppFile (Project*, const File&);
  38. void changed();
  39. void beginTransaction();
  40. void beginTransaction (const String& name);
  41. virtual JucerDocument* createCopy() = 0;
  42. virtual String getTypeName() const = 0;
  43. SourceCodeDocument& getCppDocument() const { return *cpp; }
  44. File getCppFile() const { return cpp->getFile(); }
  45. File getHeaderFile() const { return getCppFile().withFileExtension (".h"); }
  46. bool flushChangesToDocuments (Project*);
  47. bool reloadFromDocument();
  48. //==============================================================================
  49. UndoManager& getUndoManager() noexcept { return undoManager; }
  50. bool perform (UndoableAction* const action, const String& actionName);
  51. void refreshAllPropertyComps();
  52. //==============================================================================
  53. const String& getClassName() const noexcept { return className; }
  54. void setClassName (const String& newName);
  55. const String& getComponentName() const noexcept { return componentName; }
  56. void setComponentName (const String& newName);
  57. String getParentClassString() const { return parentClasses; }
  58. void setParentClasses (const String& classes);
  59. String getConstructorParams() const { return constructorParams; }
  60. void setConstructorParams (const String& newParams);
  61. String getVariableInitialisers() const { return variableInitialisers; }
  62. void setVariableInitialisers (const String& newInitlialisers);
  63. void setFixedSize (const bool isFixed);
  64. bool isFixedSize() const noexcept { return fixedSize; }
  65. void setInitialSize (int w, int h);
  66. int getInitialWidth() const noexcept { return initialWidth; }
  67. int getInitialHeight() const noexcept { return initialHeight; }
  68. //==============================================================================
  69. virtual int getNumPaintRoutines() const = 0;
  70. virtual StringArray getPaintRoutineNames() const = 0;
  71. virtual PaintRoutine* getPaintRoutine (const int index) const = 0;
  72. virtual ComponentLayout* getComponentLayout() const = 0;
  73. virtual Component* createTestComponent (const bool alwaysFillBackground) = 0;
  74. virtual void addExtraClassProperties (PropertyPanel&);
  75. //==============================================================================
  76. virtual void getOptionalMethods (StringArray& baseClasses,
  77. StringArray& returnValues,
  78. StringArray& methods,
  79. StringArray& initialContents) const;
  80. void setOptionalMethodEnabled (const String& methodSignature, const bool enable);
  81. bool isOptionalMethodEnabled (const String& methodSignature) const noexcept;
  82. //==============================================================================
  83. BinaryResources& getResources() noexcept { return resources; }
  84. //==============================================================================
  85. void setSnappingGrid (const int numPixels, const bool active, const bool shown);
  86. int getSnappingGridSize() const noexcept { return snapGridPixels; }
  87. bool isSnapActive (const bool disableIfCtrlKeyDown) const noexcept;
  88. bool isSnapShown() const noexcept { return snapShown; }
  89. int snapPosition (int pos) const noexcept;
  90. //==============================================================================
  91. void setComponentOverlayOpacity (const float alpha);
  92. float getComponentOverlayOpacity() const noexcept { return componentOverlayOpacity; }
  93. //==============================================================================
  94. static const char* const jucerCompXmlTag;
  95. bool findTemplateFiles (String& templateH, String& templateCpp) const;
  96. String getTemplateFile() const { return templateFile; }
  97. void setTemplateFile (const String&);
  98. static bool shouldUseTransMacro() noexcept { return true; }
  99. //==============================================================================
  100. void refreshCustomCodeFromDocument();
  101. protected:
  102. SourceCodeDocument* cpp;
  103. String className, componentName, templateFile;
  104. String parentClasses, constructorParams, variableInitialisers;
  105. bool fixedSize = false;
  106. int initialWidth = 600, initialHeight = 400;
  107. BinaryResources resources;
  108. virtual XmlElement* createXml() const;
  109. virtual bool loadFromXml (const XmlElement&);
  110. virtual void fillInGeneratedCode (GeneratedCode&) const;
  111. virtual void fillInPaintCode (GeneratedCode&) const;
  112. virtual void applyCustomPaintSnippets (StringArray&) {}
  113. static void addMethod (const String& base, const String& returnVal,
  114. const String& method, const String& initialContent,
  115. StringArray& baseClasses, StringArray& returnValues,
  116. StringArray& methods, StringArray& initialContents);
  117. private:
  118. UndoManager undoManager;
  119. int snapGridPixels = 8;
  120. bool snapActive = true, snapShown = true;
  121. float componentOverlayOpacity = 0.33f;
  122. StringArray activeExtraMethods;
  123. ScopedPointer<XmlElement> currentXML;
  124. ScopedPointer<Timer> userDocChangeTimer;
  125. void timerCallback() override;
  126. void codeDocumentTextInserted (const String& newText, int insertIndex) override;
  127. void codeDocumentTextDeleted (int startIndex, int endIndex) override;
  128. void userEditedCpp();
  129. bool documentAboutToClose (OpenDocumentManager::Document*) override;
  130. void extractCustomPaintSnippetsFromCppFile (const String& cpp);
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JucerDocument)
  132. };