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.

154 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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. #pragma once
  14. //==============================================================================
  15. class TextPropertyComponentWithEnablement : public TextPropertyComponent,
  16. private Value::Listener
  17. {
  18. public:
  19. TextPropertyComponentWithEnablement (ValueWithDefault& valueToControl,
  20. ValueWithDefault valueToListenTo,
  21. const String& propertyName,
  22. int maxNumChars,
  23. bool isMultiLine)
  24. : TextPropertyComponent (valueToControl, propertyName, maxNumChars, isMultiLine),
  25. valueWithDefault (valueToListenTo),
  26. value (valueWithDefault.getPropertyAsValue())
  27. {
  28. value.addListener (this);
  29. setEnabled (valueWithDefault.get());
  30. }
  31. ~TextPropertyComponentWithEnablement() override { value.removeListener (this); }
  32. private:
  33. ValueWithDefault valueWithDefault;
  34. Value value;
  35. void valueChanged (Value&) override { setEnabled (valueWithDefault.get()); }
  36. };
  37. //==============================================================================
  38. class ChoicePropertyComponentWithEnablement : public ChoicePropertyComponent,
  39. private Value::Listener
  40. {
  41. public:
  42. ChoicePropertyComponentWithEnablement (ValueWithDefault& valueToControl,
  43. ValueWithDefault valueToListenTo,
  44. const String& propertyName,
  45. const StringArray& choiceToUse,
  46. const Array<var>& correspondingValues)
  47. : ChoicePropertyComponent (valueToControl, propertyName, choiceToUse, correspondingValues),
  48. valueWithDefault (valueToListenTo),
  49. value (valueToListenTo.getPropertyAsValue())
  50. {
  51. value.addListener (this);
  52. valueChanged (value);
  53. }
  54. ChoicePropertyComponentWithEnablement (ValueWithDefault& valueToControl,
  55. ValueWithDefault valueToListenTo,
  56. const Identifier& multiChoiceID,
  57. const String& propertyName,
  58. const StringArray& choicesToUse,
  59. const Array<var>& correspondingValues)
  60. : ChoicePropertyComponentWithEnablement (valueToControl, valueToListenTo, propertyName, choicesToUse, correspondingValues)
  61. {
  62. jassert (valueToListenTo.get().getArray() != nullptr);
  63. isMultiChoice = true;
  64. idToCheck = multiChoiceID;
  65. valueChanged (value);
  66. }
  67. ChoicePropertyComponentWithEnablement (ValueWithDefault& valueToControl,
  68. ValueWithDefault valueToListenTo,
  69. const String& propertyName)
  70. : ChoicePropertyComponent (valueToControl, propertyName),
  71. valueWithDefault (valueToListenTo),
  72. value (valueToListenTo.getPropertyAsValue())
  73. {
  74. value.addListener (this);
  75. valueChanged (value);
  76. }
  77. ~ChoicePropertyComponentWithEnablement() override { value.removeListener (this); }
  78. private:
  79. ValueWithDefault valueWithDefault;
  80. Value value;
  81. bool isMultiChoice = false;
  82. Identifier idToCheck;
  83. bool checkMultiChoiceVar() const
  84. {
  85. jassert (isMultiChoice);
  86. auto v = valueWithDefault.get();
  87. if (auto* varArray = v.getArray())
  88. return varArray->contains (idToCheck.toString());
  89. jassertfalse;
  90. return false;
  91. }
  92. void valueChanged (Value&) override
  93. {
  94. if (isMultiChoice)
  95. setEnabled (checkMultiChoiceVar());
  96. else
  97. setEnabled (valueWithDefault.get());
  98. }
  99. };
  100. //==============================================================================
  101. class MultiChoicePropertyComponentWithEnablement : public MultiChoicePropertyComponent,
  102. private Value::Listener
  103. {
  104. public:
  105. MultiChoicePropertyComponentWithEnablement (ValueWithDefault& valueToControl,
  106. ValueWithDefault valueToListenTo,
  107. const String& propertyName,
  108. const StringArray& choices,
  109. const Array<var>& correspondingValues)
  110. : MultiChoicePropertyComponent (valueToControl,
  111. propertyName,
  112. choices,
  113. correspondingValues),
  114. valueWithDefault (valueToListenTo),
  115. value (valueToListenTo.getPropertyAsValue())
  116. {
  117. value.addListener (this);
  118. valueChanged (value);
  119. }
  120. ~MultiChoicePropertyComponentWithEnablement() override { value.removeListener (this); }
  121. private:
  122. void valueChanged (Value&) override { setEnabled (valueWithDefault.get()); }
  123. ValueWithDefault valueWithDefault;
  124. Value value;
  125. };