juce_BooleanPropertyComponent.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. BooleanPropertyComponent::BooleanPropertyComponent (const String& name,
  18. const String& buttonTextWhenTrue,
  19. const String& buttonTextWhenFalse)
  20. : PropertyComponent (name),
  21. onText (buttonTextWhenTrue),
  22. offText (buttonTextWhenFalse)
  23. {
  24. addAndMakeVisible (button);
  25. button.setClickingTogglesState (false);
  26. button.addListener (this);
  27. }
  28. BooleanPropertyComponent::BooleanPropertyComponent (const Value& valueToControl,
  29. const String& name,
  30. const String& buttonText)
  31. : PropertyComponent (name),
  32. onText (buttonText),
  33. offText (buttonText)
  34. {
  35. addAndMakeVisible (button);
  36. button.setClickingTogglesState (false);
  37. button.setButtonText (buttonText);
  38. button.getToggleStateValue().referTo (valueToControl);
  39. button.setClickingTogglesState (true);
  40. }
  41. BooleanPropertyComponent::~BooleanPropertyComponent()
  42. {
  43. }
  44. void BooleanPropertyComponent::setState (const bool newState)
  45. {
  46. button.setToggleState (newState, sendNotification);
  47. }
  48. bool BooleanPropertyComponent::getState() const
  49. {
  50. return button.getToggleState();
  51. }
  52. void BooleanPropertyComponent::paint (Graphics& g)
  53. {
  54. PropertyComponent::paint (g);
  55. g.setColour (findColour (backgroundColourId));
  56. g.fillRect (button.getBounds());
  57. g.setColour (findColour (ComboBox::outlineColourId));
  58. g.drawRect (button.getBounds());
  59. }
  60. void BooleanPropertyComponent::refresh()
  61. {
  62. button.setToggleState (getState(), dontSendNotification);
  63. button.setButtonText (button.getToggleState() ? onText : offText);
  64. }
  65. void BooleanPropertyComponent::buttonClicked (Button*)
  66. {
  67. setState (! getState());
  68. }