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.

176 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. #include "../jucer_GeneratedCode.h"
  20. #include "../UI/jucer_RelativePositionedRectangle.h"
  21. class FillType;
  22. class PaintRoutine;
  23. class JucerDocument;
  24. class ElementSiblingComponent;
  25. //==============================================================================
  26. /**
  27. Base class for objects that can be used in a PaintRoutine.
  28. */
  29. class PaintElement : public Component,
  30. public ComponentBoundsConstrainer,
  31. private ChangeListener
  32. {
  33. public:
  34. //==============================================================================
  35. PaintElement (PaintRoutine* owner, const String& typeName);
  36. ~PaintElement() override;
  37. //==============================================================================
  38. virtual void setInitialBounds (int parentWidth, int parentHeight);
  39. virtual Rectangle<int> getCurrentBounds (const Rectangle<int>& activeArea) const;
  40. virtual void setCurrentBounds (const Rectangle<int>& newBounds, const Rectangle<int>& activeArea, bool undoable);
  41. const RelativePositionedRectangle& getPosition() const;
  42. void setPosition (const RelativePositionedRectangle& newPosition, bool undoable);
  43. void setPaintElementBounds (const Rectangle<int>& newBounds, bool undoable);
  44. void setPaintElementBoundsAndProperties (PaintElement* elementToPosition, const Rectangle<int>& newBounds,
  45. PaintElement* referenceElement, bool undoable);
  46. void updateBounds (const Rectangle<int>& activeArea);
  47. const String& getTypeName() const noexcept { return typeName; }
  48. PaintRoutine* getOwner() const noexcept { return owner; }
  49. //==============================================================================
  50. virtual void draw (Graphics& g,
  51. const ComponentLayout* layout,
  52. const Rectangle<int>& parentArea) = 0;
  53. virtual void drawExtraEditorGraphics (Graphics& g, const Rectangle<int>& relativeTo);
  54. virtual void getEditableProperties (Array<PropertyComponent*>& props, bool multipleSelected);
  55. virtual void showPopupMenu();
  56. //==============================================================================
  57. virtual XmlElement* createXml() const = 0;
  58. virtual bool loadFromXml (const XmlElement& xml) = 0;
  59. //==============================================================================
  60. virtual void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) = 0;
  61. JucerDocument* getDocument() const;
  62. virtual void changed();
  63. bool perform (UndoableAction* action, const String& actionName);
  64. //==============================================================================
  65. void paint (Graphics&) override;
  66. void resized() override;
  67. void mouseDown (const MouseEvent&) override;
  68. void mouseDrag (const MouseEvent&) override;
  69. void mouseUp (const MouseEvent&) override;
  70. void changeListenerCallback (ChangeBroadcaster*) override;
  71. void parentHierarchyChanged() override;
  72. virtual void applyCustomPaintSnippets (StringArray&) {}
  73. int borderThickness;
  74. protected:
  75. PaintRoutine* const owner;
  76. RelativePositionedRectangle position;
  77. void resizeStart() override;
  78. void resizeEnd() override;
  79. void checkBounds (Rectangle<int>& bounds,
  80. const Rectangle<int>& previousBounds,
  81. const Rectangle<int>& limits,
  82. bool isStretchingTop,
  83. bool isStretchingLeft,
  84. bool isStretchingBottom,
  85. bool isStretchingRight) override;
  86. void applyBoundsToComponent (Component&, Rectangle<int>) override;
  87. Rectangle<int> getCurrentAbsoluteBounds() const;
  88. void getCurrentAbsoluteBoundsDouble (double& x, double& y, double& w, double& h) const;
  89. virtual void selectionChanged (bool isSelected);
  90. virtual void createSiblingComponents();
  91. void siblingComponentsChanged();
  92. OwnedArray<ElementSiblingComponent> siblingComponents;
  93. void updateSiblingComps();
  94. private:
  95. std::unique_ptr<ResizableBorderComponent> border;
  96. String typeName;
  97. bool selected, dragging, mouseDownSelectStatus;
  98. double originalAspectRatio;
  99. ChangeBroadcaster selfChangeListenerList;
  100. };
  101. //==============================================================================
  102. template <typename ElementType>
  103. class ElementListener : private ChangeListener
  104. {
  105. public:
  106. ElementListener (ElementType* e)
  107. : owner (e), broadcaster (*owner->getDocument()),
  108. propToRefresh (nullptr)
  109. {
  110. broadcaster.addChangeListener (this);
  111. }
  112. ~ElementListener() override
  113. {
  114. jassert (propToRefresh != nullptr);
  115. broadcaster.removeChangeListener (this);
  116. }
  117. void setPropertyToRefresh (PropertyComponent& pc)
  118. {
  119. propToRefresh = &pc;
  120. }
  121. mutable Component::SafePointer<ElementType> owner;
  122. ChangeBroadcaster& broadcaster;
  123. PropertyComponent* propToRefresh;
  124. private:
  125. void changeListenerCallback (ChangeBroadcaster*) override
  126. {
  127. jassert (propToRefresh != nullptr);
  128. if (propToRefresh != nullptr && owner != nullptr)
  129. propToRefresh->refresh();
  130. }
  131. JUCE_DECLARE_NON_COPYABLE (ElementListener)
  132. };