juce_ChoicePropertyComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class ChoicePropertyComponent::RemapperValueSource : public Value::ValueSource,
  18. private ValueListener
  19. {
  20. public:
  21. RemapperValueSource (const Value& source, const Array<var>& map)
  22. : sourceValue (source), mappings (map)
  23. {
  24. sourceValue.addListener (this);
  25. }
  26. var getValue() const
  27. {
  28. const var targetValue (sourceValue.getValue());
  29. for (int i = 0; i < mappings.size(); ++i)
  30. if (mappings.getReference(i).equalsWithSameType (targetValue))
  31. return i + 1;
  32. return mappings.indexOf (targetValue) + 1;
  33. }
  34. void setValue (const var& newValue)
  35. {
  36. const var remappedVal (mappings [static_cast <int> (newValue) - 1]);
  37. if (! remappedVal.equalsWithSameType (sourceValue))
  38. sourceValue = remappedVal;
  39. }
  40. protected:
  41. Value sourceValue;
  42. Array<var> mappings;
  43. void valueChanged (Value&)
  44. {
  45. sendChangeMessage (true);
  46. }
  47. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSource)
  48. };
  49. //==============================================================================
  50. ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
  51. : PropertyComponent (name),
  52. isCustomClass (true)
  53. {
  54. }
  55. ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
  56. const String& name,
  57. const StringArray& choiceList,
  58. const Array<var>& correspondingValues)
  59. : PropertyComponent (name),
  60. choices (choiceList),
  61. isCustomClass (false)
  62. {
  63. // The array of corresponding values must contain one value for each of the items in
  64. // the choices array!
  65. jassert (correspondingValues.size() == choices.size());
  66. createComboBox();
  67. comboBox.getSelectedIdAsValue().referTo (Value (new RemapperValueSource (valueToControl,
  68. correspondingValues)));
  69. }
  70. ChoicePropertyComponent::~ChoicePropertyComponent()
  71. {
  72. }
  73. //==============================================================================
  74. void ChoicePropertyComponent::createComboBox()
  75. {
  76. addAndMakeVisible (comboBox);
  77. for (int i = 0; i < choices.size(); ++i)
  78. {
  79. if (choices[i].isNotEmpty())
  80. comboBox.addItem (choices[i], i + 1);
  81. else
  82. comboBox.addSeparator();
  83. }
  84. comboBox.setEditableText (false);
  85. }
  86. void ChoicePropertyComponent::setIndex (const int /*newIndex*/)
  87. {
  88. jassertfalse; // you need to override this method in your subclass!
  89. }
  90. int ChoicePropertyComponent::getIndex() const
  91. {
  92. jassertfalse; // you need to override this method in your subclass!
  93. return -1;
  94. }
  95. const StringArray& ChoicePropertyComponent::getChoices() const
  96. {
  97. return choices;
  98. }
  99. //==============================================================================
  100. void ChoicePropertyComponent::refresh()
  101. {
  102. if (isCustomClass)
  103. {
  104. if (! comboBox.isVisible())
  105. {
  106. createComboBox();
  107. comboBox.addListener (this);
  108. }
  109. comboBox.setSelectedId (getIndex() + 1, dontSendNotification);
  110. }
  111. }
  112. void ChoicePropertyComponent::comboBoxChanged (ComboBox*)
  113. {
  114. if (isCustomClass)
  115. {
  116. const int newIndex = comboBox.getSelectedId() - 1;
  117. if (newIndex != getIndex())
  118. setIndex (newIndex);
  119. }
  120. }