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.

165 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. ToolbarItemFactory::ToolbarItemFactory() {}
  21. ToolbarItemFactory::~ToolbarItemFactory() {}
  22. //==============================================================================
  23. ToolbarItemComponent::ToolbarItemComponent (const int itemId_,
  24. const String& labelText,
  25. const bool isBeingUsedAsAButton_)
  26. : Button (labelText),
  27. itemId (itemId_),
  28. mode (normalMode),
  29. toolbarStyle (Toolbar::iconsOnly),
  30. dragOffsetX (0),
  31. dragOffsetY (0),
  32. isActive (true),
  33. isBeingDragged (false),
  34. isBeingUsedAsAButton (isBeingUsedAsAButton_)
  35. {
  36. // Your item ID can't be 0!
  37. jassert (itemId_ != 0);
  38. }
  39. ToolbarItemComponent::~ToolbarItemComponent()
  40. {
  41. overlayComp.reset();
  42. }
  43. Toolbar* ToolbarItemComponent::getToolbar() const
  44. {
  45. return dynamic_cast<Toolbar*> (getParentComponent());
  46. }
  47. bool ToolbarItemComponent::isToolbarVertical() const
  48. {
  49. const Toolbar* const t = getToolbar();
  50. return t != nullptr && t->isVertical();
  51. }
  52. void ToolbarItemComponent::setStyle (const Toolbar::ToolbarItemStyle& newStyle)
  53. {
  54. if (toolbarStyle != newStyle)
  55. {
  56. toolbarStyle = newStyle;
  57. repaint();
  58. resized();
  59. }
  60. }
  61. void ToolbarItemComponent::paintButton (Graphics& g, const bool over, const bool down)
  62. {
  63. if (isBeingUsedAsAButton)
  64. getLookAndFeel().paintToolbarButtonBackground (g, getWidth(), getHeight(),
  65. over, down, *this);
  66. if (toolbarStyle != Toolbar::iconsOnly)
  67. {
  68. auto indent = contentArea.getX();
  69. auto y = indent;
  70. auto h = getHeight() - indent * 2;
  71. if (toolbarStyle == Toolbar::iconsWithText)
  72. {
  73. y = contentArea.getBottom() + indent / 2;
  74. h -= contentArea.getHeight();
  75. }
  76. getLookAndFeel().paintToolbarButtonLabel (g, indent, y, getWidth() - indent * 2, h,
  77. getButtonText(), *this);
  78. }
  79. if (! contentArea.isEmpty())
  80. {
  81. Graphics::ScopedSaveState ss (g);
  82. g.reduceClipRegion (contentArea);
  83. g.setOrigin (contentArea.getPosition());
  84. paintButtonArea (g, contentArea.getWidth(), contentArea.getHeight(), over, down);
  85. }
  86. }
  87. void ToolbarItemComponent::resized()
  88. {
  89. if (toolbarStyle != Toolbar::textOnly)
  90. {
  91. const int indent = jmin (proportionOfWidth (0.08f),
  92. proportionOfHeight (0.08f));
  93. contentArea = Rectangle<int> (indent, indent,
  94. getWidth() - indent * 2,
  95. toolbarStyle == Toolbar::iconsWithText ? proportionOfHeight (0.55f)
  96. : (getHeight() - indent * 2));
  97. }
  98. else
  99. {
  100. contentArea = {};
  101. }
  102. contentAreaChanged (contentArea);
  103. }
  104. void ToolbarItemComponent::setEditingMode (const ToolbarEditingMode newMode)
  105. {
  106. if (mode != newMode)
  107. {
  108. mode = newMode;
  109. repaint();
  110. if (mode == normalMode)
  111. {
  112. overlayComp.reset();
  113. }
  114. else if (overlayComp == nullptr)
  115. {
  116. overlayComp.reset (new detail::ToolbarItemDragAndDropOverlayComponent());
  117. addAndMakeVisible (overlayComp.get());
  118. overlayComp->parentSizeChanged();
  119. }
  120. resized();
  121. }
  122. }
  123. //==============================================================================
  124. std::unique_ptr<AccessibilityHandler> ToolbarItemComponent::createAccessibilityHandler()
  125. {
  126. const auto shouldItemBeAccessible = (itemId != ToolbarItemFactory::separatorBarId
  127. && itemId != ToolbarItemFactory::spacerId
  128. && itemId != ToolbarItemFactory::flexibleSpacerId);
  129. if (! shouldItemBeAccessible)
  130. return createIgnoredAccessibilityHandler (*this);
  131. return std::make_unique<detail::ButtonAccessibilityHandler> (*this, AccessibilityRole::button);
  132. }
  133. } // namespace juce