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.

166 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. #pragma once
  19. //==============================================================================
  20. class TextPropertyComponentWithEnablement final : public TextPropertyComponent,
  21. private Value::Listener
  22. {
  23. public:
  24. TextPropertyComponentWithEnablement (const ValueTreePropertyWithDefault& valueToControl,
  25. ValueTreePropertyWithDefault valueToListenTo,
  26. const String& propertyName,
  27. int maxNumChars,
  28. bool multiLine)
  29. : TextPropertyComponent (valueToControl, propertyName, maxNumChars, multiLine),
  30. propertyWithDefault (valueToListenTo),
  31. value (propertyWithDefault.getPropertyAsValue())
  32. {
  33. value.addListener (this);
  34. setEnabled (propertyWithDefault.get());
  35. }
  36. ~TextPropertyComponentWithEnablement() override { value.removeListener (this); }
  37. private:
  38. ValueTreePropertyWithDefault propertyWithDefault;
  39. Value value;
  40. void valueChanged (Value&) override { setEnabled (propertyWithDefault.get()); }
  41. };
  42. //==============================================================================
  43. class ChoicePropertyComponentWithEnablement final : public ChoicePropertyComponent,
  44. private Value::Listener
  45. {
  46. public:
  47. ChoicePropertyComponentWithEnablement (const ValueTreePropertyWithDefault& valueToControl,
  48. ValueTreePropertyWithDefault valueToListenTo,
  49. const String& propertyName,
  50. const StringArray& choiceToUse,
  51. const Array<var>& correspondingValues)
  52. : ChoicePropertyComponent (valueToControl, propertyName, choiceToUse, correspondingValues),
  53. propertyWithDefault (valueToListenTo),
  54. value (valueToListenTo.getPropertyAsValue())
  55. {
  56. value.addListener (this);
  57. handleValueChanged();
  58. }
  59. ChoicePropertyComponentWithEnablement (const ValueTreePropertyWithDefault& valueToControl,
  60. ValueTreePropertyWithDefault valueToListenTo,
  61. const Identifier& multiChoiceID,
  62. const String& propertyName,
  63. const StringArray& choicesToUse,
  64. const Array<var>& correspondingValues)
  65. : ChoicePropertyComponentWithEnablement (valueToControl, valueToListenTo, propertyName, choicesToUse, correspondingValues)
  66. {
  67. jassert (valueToListenTo.get().getArray() != nullptr);
  68. isMultiChoice = true;
  69. idToCheck = multiChoiceID;
  70. handleValueChanged();
  71. }
  72. ChoicePropertyComponentWithEnablement (const ValueTreePropertyWithDefault& valueToControl,
  73. ValueTreePropertyWithDefault valueToListenTo,
  74. const String& propertyName)
  75. : ChoicePropertyComponent (valueToControl, propertyName),
  76. propertyWithDefault (valueToListenTo),
  77. value (valueToListenTo.getPropertyAsValue())
  78. {
  79. value.addListener (this);
  80. handleValueChanged();
  81. }
  82. ~ChoicePropertyComponentWithEnablement() override { value.removeListener (this); }
  83. private:
  84. ValueTreePropertyWithDefault propertyWithDefault;
  85. Value value;
  86. bool isMultiChoice = false;
  87. Identifier idToCheck;
  88. bool checkMultiChoiceVar() const
  89. {
  90. jassert (isMultiChoice);
  91. auto v = propertyWithDefault.get();
  92. if (auto* varArray = v.getArray())
  93. return varArray->contains (idToCheck.toString());
  94. jassertfalse;
  95. return false;
  96. }
  97. void handleValueChanged()
  98. {
  99. if (isMultiChoice)
  100. setEnabled (checkMultiChoiceVar());
  101. else
  102. setEnabled (propertyWithDefault.get());
  103. }
  104. void valueChanged (Value&) override
  105. {
  106. handleValueChanged();
  107. }
  108. };
  109. //==============================================================================
  110. class MultiChoicePropertyComponentWithEnablement final : public MultiChoicePropertyComponent,
  111. private Value::Listener
  112. {
  113. public:
  114. MultiChoicePropertyComponentWithEnablement (const ValueTreePropertyWithDefault& valueToControl,
  115. ValueTreePropertyWithDefault valueToListenTo,
  116. const String& propertyName,
  117. const StringArray& choices,
  118. const Array<var>& correspondingValues)
  119. : MultiChoicePropertyComponent (valueToControl,
  120. propertyName,
  121. choices,
  122. correspondingValues),
  123. propertyWithDefault (valueToListenTo),
  124. value (valueToListenTo.getPropertyAsValue())
  125. {
  126. value.addListener (this);
  127. valueChanged (value);
  128. }
  129. ~MultiChoicePropertyComponentWithEnablement() override { value.removeListener (this); }
  130. private:
  131. void valueChanged (Value&) override { setEnabled (propertyWithDefault.get()); }
  132. ValueTreePropertyWithDefault propertyWithDefault;
  133. Value value;
  134. };