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.

108 lines
3.0KB

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