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
4.7KB

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