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) 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 "../jucer_GeneratedCode.h"
  21. #include "../UI/jucer_RelativePositionedRectangle.h"
  22. class FillType;
  23. class PaintRoutine;
  24. class JucerDocument;
  25. class ElementSiblingComponent;
  26. //==============================================================================
  27. /**
  28. Base class for objects that can be used in a PaintRoutine.
  29. */
  30. class PaintElement : public Component,
  31. public ChangeListener,
  32. public ComponentBoundsConstrainer
  33. {
  34. public:
  35. //==============================================================================
  36. PaintElement (PaintRoutine* owner, const String& typeName);
  37. ~PaintElement() override;
  38. //==============================================================================
  39. virtual void setInitialBounds (int parentWidth, int parentHeight);
  40. virtual Rectangle<int> getCurrentBounds (const Rectangle<int>& activeArea) const;
  41. virtual void setCurrentBounds (const Rectangle<int>& newBounds, const Rectangle<int>& activeArea, const bool undoable);
  42. const RelativePositionedRectangle& getPosition() const;
  43. void setPosition (const RelativePositionedRectangle& newPosition, const bool undoable);
  44. void setPaintElementBounds (const Rectangle<int>& newBounds, const bool undoable);
  45. void setPaintElementBoundsAndProperties (PaintElement* elementToPosition, const Rectangle<int>& newBounds,
  46. PaintElement* referenceElement, const bool undoable);
  47. void updateBounds (const Rectangle<int>& activeArea);
  48. const String& getTypeName() const noexcept { return typeName; }
  49. PaintRoutine* getOwner() const noexcept { return owner; }
  50. //==============================================================================
  51. virtual void draw (Graphics& g,
  52. const ComponentLayout* layout,
  53. const Rectangle<int>& parentArea) = 0;
  54. virtual void drawExtraEditorGraphics (Graphics& g, const Rectangle<int>& relativeTo);
  55. virtual void getEditableProperties (Array<PropertyComponent*>& props, bool multipleSelected);
  56. virtual void showPopupMenu();
  57. //==============================================================================
  58. virtual XmlElement* createXml() const = 0;
  59. virtual bool loadFromXml (const XmlElement& xml) = 0;
  60. //==============================================================================
  61. virtual void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) = 0;
  62. JucerDocument* getDocument() const;
  63. virtual void changed();
  64. bool perform (UndoableAction* action, const String& actionName);
  65. //==============================================================================
  66. void paint (Graphics&) override;
  67. void resized() override;
  68. void mouseDown (const MouseEvent&) override;
  69. void mouseDrag (const MouseEvent&) override;
  70. void mouseUp (const MouseEvent&) override;
  71. void changeListenerCallback (ChangeBroadcaster*) override;
  72. void parentHierarchyChanged() override;
  73. virtual void applyCustomPaintSnippets (StringArray&) {}
  74. int borderThickness;
  75. protected:
  76. PaintRoutine* const owner;
  77. RelativePositionedRectangle position;
  78. void resizeStart() override;
  79. void resizeEnd() override;
  80. void checkBounds (Rectangle<int>& bounds,
  81. const Rectangle<int>& previousBounds,
  82. const Rectangle<int>& limits,
  83. bool isStretchingTop,
  84. bool isStretchingLeft,
  85. bool isStretchingBottom,
  86. bool isStretchingRight) override;
  87. void applyBoundsToComponent (Component&, Rectangle<int>) override;
  88. Rectangle<int> getCurrentAbsoluteBounds() const;
  89. void getCurrentAbsoluteBoundsDouble (double& x, double& y, double& w, double& h) const;
  90. virtual void selectionChanged (const bool isSelected);
  91. virtual void createSiblingComponents();
  92. void siblingComponentsChanged();
  93. OwnedArray <ElementSiblingComponent> siblingComponents;
  94. void updateSiblingComps();
  95. private:
  96. std::unique_ptr<ResizableBorderComponent> border;
  97. String typeName;
  98. bool selected, dragging, mouseDownSelectStatus;
  99. double originalAspectRatio;
  100. ChangeBroadcaster selfChangeListenerList;
  101. };
  102. //==============================================================================
  103. template <typename ElementType>
  104. class ElementListener : public ChangeListener
  105. {
  106. public:
  107. ElementListener (ElementType* e)
  108. : owner (e), broadcaster (*owner->getDocument()),
  109. propToRefresh (nullptr)
  110. {
  111. broadcaster.addChangeListener (this);
  112. }
  113. ~ElementListener()
  114. {
  115. jassert (propToRefresh != nullptr);
  116. broadcaster.removeChangeListener (this);
  117. }
  118. void setPropertyToRefresh (PropertyComponent& pc)
  119. {
  120. propToRefresh = &pc;
  121. }
  122. void changeListenerCallback (ChangeBroadcaster*)
  123. {
  124. jassert (propToRefresh != nullptr);
  125. if (propToRefresh != nullptr && owner != nullptr)
  126. propToRefresh->refresh();
  127. }
  128. mutable Component::SafePointer<ElementType> owner;
  129. ChangeBroadcaster& broadcaster;
  130. PropertyComponent* propToRefresh;
  131. JUCE_DECLARE_NON_COPYABLE (ElementListener)
  132. };