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.

168 lines
6.0KB

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