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.

135 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A PropertyComponent that shows its value as an expandable list of ToggleButtons.
  24. This type of property component contains a list of options where multiple options
  25. can be selected at once.
  26. @see PropertyComponent, PropertyPanel
  27. @tags{GUI}
  28. */
  29. class MultiChoicePropertyComponent : public PropertyComponent
  30. {
  31. private:
  32. /** Delegating constructor. */
  33. MultiChoicePropertyComponent (const String&, const StringArray&, const Array<var>&);
  34. public:
  35. /** Creates the component. Note that the underlying var object that the Value refers to must be an array.
  36. @param valueToControl the value that the ToggleButtons will read and control
  37. @param propertyName the name of the property
  38. @param choices the list of possible values that will be represented
  39. @param correspondingValues a list of values corresponding to each item in the 'choices' StringArray.
  40. These are the values that will be read and written to the
  41. valueToControl value. This array must contain the same number of items
  42. as the choices array
  43. @param maxChoices the maximum number of values which can be selected at once. The default of
  44. -1 will not limit the number that can be selected
  45. */
  46. MultiChoicePropertyComponent (const Value& valueToControl,
  47. const String& propertyName,
  48. const StringArray& choices,
  49. const Array<var>& correspondingValues,
  50. int maxChoices = -1);
  51. /** Creates the component using a ValueWithDefault object. This will select the default options.
  52. @param valueToControl the ValueWithDefault object that contains the Value object that the ToggleButtons will read and control.
  53. @param propertyName the name of the property
  54. @param choices the list of possible values that will be represented
  55. @param correspondingValues a list of values corresponding to each item in the 'choices' StringArray.
  56. These are the values that will be read and written to the
  57. valueToControl value. This array must contain the same number of items
  58. as the choices array
  59. @param maxChoices the maximum number of values which can be selected at once. The default of
  60. -1 will not limit the number that can be selected
  61. */
  62. MultiChoicePropertyComponent (ValueWithDefault& valueToControl,
  63. const String& propertyName,
  64. const StringArray& choices,
  65. const Array<var>& correspondingValues,
  66. int maxChoices = -1);
  67. ~MultiChoicePropertyComponent() override;
  68. //==============================================================================
  69. /** Returns true if the list of options is expanded. */
  70. bool isExpanded() const noexcept { return expanded; }
  71. /** Expands or shrinks the list of options.
  72. N.B. This will just set the preferredHeight value of the PropertyComponent and attempt to
  73. call PropertyPanel::resized(), so if you are not displaying this object in a PropertyPanel
  74. then you should use the onHeightChange callback to resize it when the height changes.
  75. @see onHeightChange
  76. */
  77. void setExpanded (bool expanded) noexcept;
  78. /** You can assign a lambda to this callback object to have it called when the MultiChoicePropertyComponent height changes. */
  79. std::function<void()> onHeightChange;
  80. //==============================================================================
  81. /** @internal */
  82. void paint (Graphics& g) override;
  83. /** @internal */
  84. void resized() override;
  85. /** @internal */
  86. void refresh() override {}
  87. private:
  88. //==============================================================================
  89. class MultiChoiceRemapperSource;
  90. class MultiChoiceRemapperSourceWithDefault;
  91. //==============================================================================
  92. void lookAndFeelChanged() override;
  93. //==============================================================================
  94. WeakReference<ValueWithDefault> valueWithDefault;
  95. int maxHeight = 0;
  96. int numHidden = 0;
  97. bool expanded = false;
  98. OwnedArray<ToggleButton> choiceButtons;
  99. ShapeButton expandButton { "Expand", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack };
  100. //==============================================================================
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoicePropertyComponent)
  102. };
  103. } // namespace juce