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.

107 lines
2.8KB

  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. ToolbarButton::ToolbarButton (const int iid, const String& buttonText,
  16. std::unique_ptr<Drawable> normalIm,
  17. std::unique_ptr<Drawable> toggledOnIm)
  18. : ToolbarItemComponent (iid, buttonText, true),
  19. normalImage (std::move (normalIm)),
  20. toggledOnImage (std::move (toggledOnIm))
  21. {
  22. jassert (normalImage != nullptr);
  23. }
  24. ToolbarButton::~ToolbarButton()
  25. {
  26. }
  27. //==============================================================================
  28. bool ToolbarButton::getToolbarItemSizes (int toolbarDepth, bool /*isToolbarVertical*/, int& preferredSize, int& minSize, int& maxSize)
  29. {
  30. preferredSize = minSize = maxSize = toolbarDepth;
  31. return true;
  32. }
  33. void ToolbarButton::paintButtonArea (Graphics&, int /*width*/, int /*height*/, bool /*isMouseOver*/, bool /*isMouseDown*/)
  34. {
  35. }
  36. void ToolbarButton::contentAreaChanged (const Rectangle<int>&)
  37. {
  38. buttonStateChanged();
  39. }
  40. void ToolbarButton::setCurrentImage (Drawable* const newImage)
  41. {
  42. if (newImage != currentImage)
  43. {
  44. removeChildComponent (currentImage);
  45. currentImage = newImage;
  46. if (currentImage != nullptr)
  47. {
  48. enablementChanged();
  49. addAndMakeVisible (currentImage);
  50. updateDrawable();
  51. }
  52. }
  53. }
  54. void ToolbarButton::updateDrawable()
  55. {
  56. if (currentImage != nullptr)
  57. {
  58. currentImage->setInterceptsMouseClicks (false, false);
  59. currentImage->setTransformToFit (getContentArea().toFloat(), RectanglePlacement::centred);
  60. currentImage->setAlpha (isEnabled() ? 1.0f : 0.5f);
  61. }
  62. }
  63. void ToolbarButton::resized()
  64. {
  65. ToolbarItemComponent::resized();
  66. updateDrawable();
  67. }
  68. void ToolbarButton::enablementChanged()
  69. {
  70. ToolbarItemComponent::enablementChanged();
  71. updateDrawable();
  72. }
  73. Drawable* ToolbarButton::getImageToUse() const
  74. {
  75. if (getStyle() == Toolbar::textOnly)
  76. return nullptr;
  77. if (getToggleState() && toggledOnImage != nullptr)
  78. return toggledOnImage.get();
  79. return normalImage.get();
  80. }
  81. void ToolbarButton::buttonStateChanged()
  82. {
  83. setCurrentImage (getImageToUse());
  84. }
  85. } // namespace juce