Audio plugin host https://kx.studio/carla
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.

236 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. namespace juce
  14. {
  15. ToolbarItemFactory::ToolbarItemFactory() {}
  16. ToolbarItemFactory::~ToolbarItemFactory() {}
  17. //==============================================================================
  18. class ToolbarItemComponent::ItemDragAndDropOverlayComponent : public Component
  19. {
  20. public:
  21. ItemDragAndDropOverlayComponent()
  22. : isDragging (false)
  23. {
  24. setAlwaysOnTop (true);
  25. setRepaintsOnMouseActivity (true);
  26. setMouseCursor (MouseCursor::DraggingHandCursor);
  27. }
  28. void paint (Graphics& g) override
  29. {
  30. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  31. {
  32. if (isMouseOverOrDragging()
  33. && tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  34. {
  35. g.setColour (findColour (Toolbar::editingModeOutlineColourId, true));
  36. g.drawRect (getLocalBounds(), jmin (2, (getWidth() - 1) / 2,
  37. (getHeight() - 1) / 2));
  38. }
  39. }
  40. }
  41. void mouseDown (const MouseEvent& e) override
  42. {
  43. isDragging = false;
  44. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  45. {
  46. tc->dragOffsetX = e.x;
  47. tc->dragOffsetY = e.y;
  48. }
  49. }
  50. void mouseDrag (const MouseEvent& e) override
  51. {
  52. if (e.mouseWasDraggedSinceMouseDown() && ! isDragging)
  53. {
  54. isDragging = true;
  55. if (DragAndDropContainer* const dnd = DragAndDropContainer::findParentDragContainerFor (this))
  56. {
  57. dnd->startDragging (Toolbar::toolbarDragDescriptor, getParentComponent(), Image(), true, nullptr, &e.source);
  58. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  59. {
  60. tc->isBeingDragged = true;
  61. if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  62. tc->setVisible (false);
  63. }
  64. }
  65. }
  66. }
  67. void mouseUp (const MouseEvent&) override
  68. {
  69. isDragging = false;
  70. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  71. {
  72. tc->isBeingDragged = false;
  73. if (Toolbar* const tb = tc->getToolbar())
  74. tb->updateAllItemPositions (true);
  75. else if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  76. delete tc;
  77. }
  78. }
  79. void parentSizeChanged() override
  80. {
  81. setBounds (0, 0, getParentWidth(), getParentHeight());
  82. }
  83. private:
  84. //==============================================================================
  85. bool isDragging;
  86. ToolbarItemComponent* getToolbarItemComponent() const noexcept
  87. {
  88. return dynamic_cast<ToolbarItemComponent*> (getParentComponent());
  89. }
  90. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemDragAndDropOverlayComponent)
  91. };
  92. //==============================================================================
  93. ToolbarItemComponent::ToolbarItemComponent (const int itemId_,
  94. const String& labelText,
  95. const bool isBeingUsedAsAButton_)
  96. : Button (labelText),
  97. itemId (itemId_),
  98. mode (normalMode),
  99. toolbarStyle (Toolbar::iconsOnly),
  100. dragOffsetX (0),
  101. dragOffsetY (0),
  102. isActive (true),
  103. isBeingDragged (false),
  104. isBeingUsedAsAButton (isBeingUsedAsAButton_)
  105. {
  106. // Your item ID can't be 0!
  107. jassert (itemId_ != 0);
  108. }
  109. ToolbarItemComponent::~ToolbarItemComponent()
  110. {
  111. overlayComp.reset();
  112. }
  113. Toolbar* ToolbarItemComponent::getToolbar() const
  114. {
  115. return dynamic_cast<Toolbar*> (getParentComponent());
  116. }
  117. bool ToolbarItemComponent::isToolbarVertical() const
  118. {
  119. const Toolbar* const t = getToolbar();
  120. return t != nullptr && t->isVertical();
  121. }
  122. void ToolbarItemComponent::setStyle (const Toolbar::ToolbarItemStyle& newStyle)
  123. {
  124. if (toolbarStyle != newStyle)
  125. {
  126. toolbarStyle = newStyle;
  127. repaint();
  128. resized();
  129. }
  130. }
  131. void ToolbarItemComponent::paintButton (Graphics& g, const bool over, const bool down)
  132. {
  133. if (isBeingUsedAsAButton)
  134. getLookAndFeel().paintToolbarButtonBackground (g, getWidth(), getHeight(),
  135. over, down, *this);
  136. if (toolbarStyle != Toolbar::iconsOnly)
  137. {
  138. auto indent = contentArea.getX();
  139. auto y = indent;
  140. auto h = getHeight() - indent * 2;
  141. if (toolbarStyle == Toolbar::iconsWithText)
  142. {
  143. y = contentArea.getBottom() + indent / 2;
  144. h -= contentArea.getHeight();
  145. }
  146. getLookAndFeel().paintToolbarButtonLabel (g, indent, y, getWidth() - indent * 2, h,
  147. getButtonText(), *this);
  148. }
  149. if (! contentArea.isEmpty())
  150. {
  151. Graphics::ScopedSaveState ss (g);
  152. g.reduceClipRegion (contentArea);
  153. g.setOrigin (contentArea.getPosition());
  154. paintButtonArea (g, contentArea.getWidth(), contentArea.getHeight(), over, down);
  155. }
  156. }
  157. void ToolbarItemComponent::resized()
  158. {
  159. if (toolbarStyle != Toolbar::textOnly)
  160. {
  161. const int indent = jmin (proportionOfWidth (0.08f),
  162. proportionOfHeight (0.08f));
  163. contentArea = Rectangle<int> (indent, indent,
  164. getWidth() - indent * 2,
  165. toolbarStyle == Toolbar::iconsWithText ? proportionOfHeight (0.55f)
  166. : (getHeight() - indent * 2));
  167. }
  168. else
  169. {
  170. contentArea = {};
  171. }
  172. contentAreaChanged (contentArea);
  173. }
  174. void ToolbarItemComponent::setEditingMode (const ToolbarEditingMode newMode)
  175. {
  176. if (mode != newMode)
  177. {
  178. mode = newMode;
  179. repaint();
  180. if (mode == normalMode)
  181. {
  182. overlayComp.reset();
  183. }
  184. else if (overlayComp == nullptr)
  185. {
  186. overlayComp.reset (new ItemDragAndDropOverlayComponent());
  187. addAndMakeVisible (overlayComp.get());
  188. overlayComp->parentSizeChanged();
  189. }
  190. resized();
  191. }
  192. }
  193. } // namespace juce