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.

174 lines
5.9KB

  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. virtual ~PaintElement();
  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 updateBounds (const Rectangle<int>& activeArea);
  45. const String& getTypeName() const noexcept { return typeName; }
  46. PaintRoutine* getOwner() const noexcept { return owner; }
  47. //==============================================================================
  48. virtual void draw (Graphics& g,
  49. const ComponentLayout* layout,
  50. const Rectangle<int>& parentArea) = 0;
  51. virtual void drawExtraEditorGraphics (Graphics& g, const Rectangle<int>& relativeTo);
  52. virtual void getEditableProperties (Array<PropertyComponent*>& props);
  53. virtual void showPopupMenu();
  54. //==============================================================================
  55. virtual XmlElement* createXml() const = 0;
  56. virtual bool loadFromXml (const XmlElement& xml) = 0;
  57. //==============================================================================
  58. virtual void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) = 0;
  59. JucerDocument* getDocument() const;
  60. virtual void changed();
  61. bool perform (UndoableAction* action, const String& actionName);
  62. //==============================================================================
  63. void paint (Graphics&) override;
  64. void resized() override;
  65. void mouseDown (const MouseEvent&) override;
  66. void mouseDrag (const MouseEvent&) override;
  67. void mouseUp (const MouseEvent&) override;
  68. void changeListenerCallback (ChangeBroadcaster*) override;
  69. void parentHierarchyChanged() override;
  70. virtual void applyCustomPaintSnippets (StringArray&) {}
  71. int borderThickness;
  72. protected:
  73. PaintRoutine* const owner;
  74. RelativePositionedRectangle position;
  75. void resizeStart() override;
  76. void resizeEnd() override;
  77. void checkBounds (Rectangle<int>& bounds,
  78. const Rectangle<int>& previousBounds,
  79. const Rectangle<int>& limits,
  80. bool isStretchingTop,
  81. bool isStretchingLeft,
  82. bool isStretchingBottom,
  83. bool isStretchingRight) override;
  84. void applyBoundsToComponent (Component&, Rectangle<int>) override;
  85. Rectangle<int> getCurrentAbsoluteBounds() const;
  86. void getCurrentAbsoluteBoundsDouble (double& x, double& y, double& w, double& h) const;
  87. virtual void selectionChanged (const bool isSelected);
  88. virtual void createSiblingComponents();
  89. void siblingComponentsChanged();
  90. OwnedArray <ElementSiblingComponent> siblingComponents;
  91. void updateSiblingComps();
  92. private:
  93. ScopedPointer<ResizableBorderComponent> border;
  94. String typeName;
  95. bool selected, dragging, mouseDownSelectStatus;
  96. double originalAspectRatio;
  97. ChangeBroadcaster selfChangeListenerList;
  98. };
  99. //==============================================================================
  100. template <typename ElementType>
  101. class ElementListener : public ChangeListener
  102. {
  103. public:
  104. ElementListener (ElementType* e)
  105. : owner (e), broadcaster (*owner->getDocument()),
  106. propToRefresh (nullptr)
  107. {
  108. broadcaster.addChangeListener (this);
  109. }
  110. ~ElementListener()
  111. {
  112. jassert (propToRefresh != nullptr);
  113. broadcaster.removeChangeListener (this);
  114. }
  115. void setPropertyToRefresh (PropertyComponent& pc)
  116. {
  117. propToRefresh = &pc;
  118. }
  119. void changeListenerCallback (ChangeBroadcaster*)
  120. {
  121. jassert (propToRefresh != nullptr);
  122. if (propToRefresh != nullptr && owner != nullptr)
  123. propToRefresh->refresh();
  124. }
  125. mutable Component::SafePointer<ElementType> owner;
  126. ChangeBroadcaster& broadcaster;
  127. PropertyComponent* propToRefresh;
  128. JUCE_DECLARE_NON_COPYABLE (ElementListener)
  129. };