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.

juce_MultiChoicePropertyComponent.h 6.3KB

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