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.

juce_ToolbarItemComponent.cpp 7.2KB

9 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. ToolbarItemFactory::ToolbarItemFactory() {}
  18. ToolbarItemFactory::~ToolbarItemFactory() {}
  19. //==============================================================================
  20. class ToolbarItemComponent::ItemDragAndDropOverlayComponent : public Component
  21. {
  22. public:
  23. ItemDragAndDropOverlayComponent()
  24. : isDragging (false)
  25. {
  26. setAlwaysOnTop (true);
  27. setRepaintsOnMouseActivity (true);
  28. setMouseCursor (MouseCursor::DraggingHandCursor);
  29. }
  30. void paint (Graphics& g) override
  31. {
  32. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  33. {
  34. if (isMouseOverOrDragging()
  35. && tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  36. {
  37. g.setColour (findColour (Toolbar::editingModeOutlineColourId, true));
  38. g.drawRect (getLocalBounds(), jmin (2, (getWidth() - 1) / 2,
  39. (getHeight() - 1) / 2));
  40. }
  41. }
  42. }
  43. void mouseDown (const MouseEvent& e) override
  44. {
  45. isDragging = false;
  46. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  47. {
  48. tc->dragOffsetX = e.x;
  49. tc->dragOffsetY = e.y;
  50. }
  51. }
  52. void mouseDrag (const MouseEvent& e) override
  53. {
  54. if (e.mouseWasDraggedSinceMouseDown() && ! isDragging)
  55. {
  56. isDragging = true;
  57. if (DragAndDropContainer* const dnd = DragAndDropContainer::findParentDragContainerFor (this))
  58. {
  59. dnd->startDragging (Toolbar::toolbarDragDescriptor, getParentComponent(), Image::null, true);
  60. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  61. {
  62. tc->isBeingDragged = true;
  63. if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  64. tc->setVisible (false);
  65. }
  66. }
  67. }
  68. }
  69. void mouseUp (const MouseEvent&) override
  70. {
  71. isDragging = false;
  72. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  73. {
  74. tc->isBeingDragged = false;
  75. if (Toolbar* const tb = tc->getToolbar())
  76. tb->updateAllItemPositions (true);
  77. else if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  78. delete tc;
  79. }
  80. }
  81. void parentSizeChanged() override
  82. {
  83. setBounds (0, 0, getParentWidth(), getParentHeight());
  84. }
  85. private:
  86. //==============================================================================
  87. bool isDragging;
  88. ToolbarItemComponent* getToolbarItemComponent() const noexcept
  89. {
  90. return dynamic_cast<ToolbarItemComponent*> (getParentComponent());
  91. }
  92. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemDragAndDropOverlayComponent)
  93. };
  94. //==============================================================================
  95. ToolbarItemComponent::ToolbarItemComponent (const int itemId_,
  96. const String& labelText,
  97. const bool isBeingUsedAsAButton_)
  98. : Button (labelText),
  99. itemId (itemId_),
  100. mode (normalMode),
  101. toolbarStyle (Toolbar::iconsOnly),
  102. dragOffsetX (0),
  103. dragOffsetY (0),
  104. isActive (true),
  105. isBeingDragged (false),
  106. isBeingUsedAsAButton (isBeingUsedAsAButton_)
  107. {
  108. // Your item ID can't be 0!
  109. jassert (itemId_ != 0);
  110. }
  111. ToolbarItemComponent::~ToolbarItemComponent()
  112. {
  113. overlayComp = nullptr;
  114. }
  115. Toolbar* ToolbarItemComponent::getToolbar() const
  116. {
  117. return dynamic_cast<Toolbar*> (getParentComponent());
  118. }
  119. bool ToolbarItemComponent::isToolbarVertical() const
  120. {
  121. const Toolbar* const t = getToolbar();
  122. return t != nullptr && t->isVertical();
  123. }
  124. void ToolbarItemComponent::setStyle (const Toolbar::ToolbarItemStyle& newStyle)
  125. {
  126. if (toolbarStyle != newStyle)
  127. {
  128. toolbarStyle = newStyle;
  129. repaint();
  130. resized();
  131. }
  132. }
  133. void ToolbarItemComponent::paintButton (Graphics& g, const bool over, const bool down)
  134. {
  135. if (isBeingUsedAsAButton)
  136. getLookAndFeel().paintToolbarButtonBackground (g, getWidth(), getHeight(),
  137. over, down, *this);
  138. if (toolbarStyle != Toolbar::iconsOnly)
  139. {
  140. const int indent = contentArea.getX();
  141. int y = indent;
  142. int h = getHeight() - indent * 2;
  143. if (toolbarStyle == Toolbar::iconsWithText)
  144. {
  145. y = contentArea.getBottom() + indent / 2;
  146. h -= contentArea.getHeight();
  147. }
  148. getLookAndFeel().paintToolbarButtonLabel (g, indent, y, getWidth() - indent * 2, h,
  149. getButtonText(), *this);
  150. }
  151. if (! contentArea.isEmpty())
  152. {
  153. Graphics::ScopedSaveState ss (g);
  154. g.reduceClipRegion (contentArea);
  155. g.setOrigin (contentArea.getPosition());
  156. paintButtonArea (g, contentArea.getWidth(), contentArea.getHeight(), over, down);
  157. }
  158. }
  159. void ToolbarItemComponent::resized()
  160. {
  161. if (toolbarStyle != Toolbar::textOnly)
  162. {
  163. const int indent = jmin (proportionOfWidth (0.08f),
  164. proportionOfHeight (0.08f));
  165. contentArea = Rectangle<int> (indent, indent,
  166. getWidth() - indent * 2,
  167. toolbarStyle == Toolbar::iconsWithText ? proportionOfHeight (0.55f)
  168. : (getHeight() - indent * 2));
  169. }
  170. else
  171. {
  172. contentArea = Rectangle<int>();
  173. }
  174. contentAreaChanged (contentArea);
  175. }
  176. void ToolbarItemComponent::setEditingMode (const ToolbarEditingMode newMode)
  177. {
  178. if (mode != newMode)
  179. {
  180. mode = newMode;
  181. repaint();
  182. if (mode == normalMode)
  183. {
  184. overlayComp = nullptr;
  185. }
  186. else if (overlayComp == nullptr)
  187. {
  188. addAndMakeVisible (overlayComp = new ItemDragAndDropOverlayComponent());
  189. overlayComp->parentSizeChanged();
  190. }
  191. resized();
  192. }
  193. }