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.

135 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. //==============================================================================
  16. /**
  17. A PropertyComponent that shows its value as an expandable list of ToggleButtons.
  18. This type of property component contains a list of options where multiple options
  19. can be selected at once.
  20. @see PropertyComponent, PropertyPanel
  21. @tags{GUI}
  22. */
  23. class MultiChoicePropertyComponent : public PropertyComponent
  24. {
  25. private:
  26. /** Delegating constructor. */
  27. MultiChoicePropertyComponent (const String&, const StringArray&, const Array<var>&);
  28. public:
  29. /** Creates the component. Note that the underlying var object that the Value refers to must be an array.
  30. @param valueToControl the value that the ToggleButtons will read and control
  31. @param propertyName the name of the property
  32. @param choices the list of possible values that will be represented
  33. @param correspondingValues a list of values corresponding to each item in the 'choices' StringArray.
  34. These are the values that will be read and written to the
  35. valueToControl value. This array must contain the same number of items
  36. as the choices array
  37. @param maxChoices the maximum number of values which can be selected at once. The default of
  38. -1 will not limit the number that can be selected
  39. */
  40. MultiChoicePropertyComponent (const Value& valueToControl,
  41. const String& propertyName,
  42. const StringArray& choices,
  43. const Array<var>& correspondingValues,
  44. int maxChoices = -1);
  45. /** Creates the component using a ValueTreePropertyWithDefault object. This will select the default options.
  46. @param valueToControl the ValueTreePropertyWithDefault object that contains the Value object that the ToggleButtons will read and control.
  47. @param propertyName the name of the property
  48. @param choices the list of possible values that will be represented
  49. @param correspondingValues a list of values corresponding to each item in the 'choices' StringArray.
  50. These are the values that will be read and written to the
  51. valueToControl value. This array must contain the same number of items
  52. as the choices array
  53. @param maxChoices the maximum number of values which can be selected at once. The default of
  54. -1 will not limit the number that can be selected
  55. */
  56. MultiChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
  57. const String& propertyName,
  58. const StringArray& choices,
  59. const Array<var>& correspondingValues,
  60. int maxChoices = -1);
  61. //==============================================================================
  62. /** Returns true if the list of options is expanded. */
  63. bool isExpanded() const noexcept { return expanded; }
  64. /** Returns true if the list of options has been truncated and can be expanded. */
  65. bool isExpandable() const noexcept { return expandable; }
  66. /** Expands or shrinks the list of options if they are not all visible.
  67. N.B. This will just set the preferredHeight value of the PropertyComponent and attempt to
  68. call PropertyPanel::resized(), so if you are not displaying this object in a PropertyPanel
  69. then you should use the onHeightChange callback to resize it when the height changes.
  70. @see onHeightChange
  71. */
  72. void setExpanded (bool expanded) noexcept;
  73. /** You can assign a lambda to this callback object to have it called when the
  74. height of this component changes in response to being expanded/collapsed.
  75. @see setExpanded
  76. */
  77. std::function<void()> onHeightChange;
  78. //==============================================================================
  79. /** @internal */
  80. void paint (Graphics& g) override;
  81. /** @internal */
  82. void resized() override;
  83. /** @internal */
  84. void refresh() override {}
  85. private:
  86. //==============================================================================
  87. class MultiChoiceRemapperSource;
  88. class MultiChoiceRemapperSourceWithDefault;
  89. //==============================================================================
  90. static int getTotalButtonsHeight (int);
  91. void lookAndFeelChanged() override;
  92. //==============================================================================
  93. static constexpr int collapsedHeight = 125;
  94. static constexpr int buttonHeight = 25;
  95. static constexpr int expandAreaHeight = 20;
  96. int maxHeight = 0, numHidden = 0;
  97. bool expandable = false, expanded = false;
  98. ValueTreePropertyWithDefault value;
  99. OwnedArray<ToggleButton> choiceButtons;
  100. ShapeButton expandButton { "Expand", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack };
  101. //==============================================================================
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiChoicePropertyComponent)
  103. };
  104. } // namespace juce