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.

109 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. ToolbarButton::ToolbarButton (const int itemId_, const String& buttonText,
  19. Drawable* const normalImage_, Drawable* const toggledOnImage_)
  20. : ToolbarItemComponent (itemId_, buttonText, true),
  21. normalImage (normalImage_),
  22. toggledOnImage (toggledOnImage_),
  23. currentImage (nullptr)
  24. {
  25. jassert (normalImage_ != nullptr);
  26. }
  27. ToolbarButton::~ToolbarButton()
  28. {
  29. }
  30. //==============================================================================
  31. bool ToolbarButton::getToolbarItemSizes (int toolbarDepth, bool /*isToolbarVertical*/, int& preferredSize, int& minSize, int& maxSize)
  32. {
  33. preferredSize = minSize = maxSize = toolbarDepth;
  34. return true;
  35. }
  36. void ToolbarButton::paintButtonArea (Graphics&, int /*width*/, int /*height*/, bool /*isMouseOver*/, bool /*isMouseDown*/)
  37. {
  38. }
  39. void ToolbarButton::contentAreaChanged (const Rectangle<int>&)
  40. {
  41. buttonStateChanged();
  42. }
  43. void ToolbarButton::setCurrentImage (Drawable* const newImage)
  44. {
  45. if (newImage != currentImage)
  46. {
  47. removeChildComponent (currentImage);
  48. currentImage = newImage;
  49. if (currentImage != nullptr)
  50. {
  51. enablementChanged();
  52. addAndMakeVisible (currentImage);
  53. updateDrawable();
  54. }
  55. }
  56. }
  57. void ToolbarButton::updateDrawable()
  58. {
  59. if (currentImage != nullptr)
  60. {
  61. currentImage->setInterceptsMouseClicks (false, false);
  62. currentImage->setTransformToFit (getContentArea().toFloat(), RectanglePlacement::centred);
  63. currentImage->setAlpha (isEnabled() ? 1.0f : 0.5f);
  64. }
  65. }
  66. void ToolbarButton::resized()
  67. {
  68. ToolbarItemComponent::resized();
  69. updateDrawable();
  70. }
  71. void ToolbarButton::enablementChanged()
  72. {
  73. ToolbarItemComponent::enablementChanged();
  74. updateDrawable();
  75. }
  76. Drawable* ToolbarButton::getImageToUse() const
  77. {
  78. if (getStyle() == Toolbar::textOnly)
  79. return nullptr;
  80. if (getToggleState() && toggledOnImage != nullptr)
  81. return toggledOnImage;
  82. return normalImage;
  83. }
  84. void ToolbarButton::buttonStateChanged()
  85. {
  86. setCurrentImage (getImageToUse());
  87. }