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.

148 lines
4.4KB

  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& sourceValue_, const Array<var>& mappings_)
  23. : sourceValue (sourceValue_),
  24. mappings (mappings_)
  25. {
  26. sourceValue.addListener (this);
  27. }
  28. var getValue() const
  29. {
  30. return mappings.indexOf (sourceValue.getValue()) + 1;
  31. }
  32. void setValue (const var& newValue)
  33. {
  34. const var remappedVal (mappings [(int) newValue - 1]);
  35. if (remappedVal != sourceValue)
  36. sourceValue = remappedVal;
  37. }
  38. protected:
  39. Value sourceValue;
  40. Array<var> mappings;
  41. void valueChanged (Value&)
  42. {
  43. sendChangeMessage (true);
  44. }
  45. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSource);
  46. };
  47. //==============================================================================
  48. ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
  49. : PropertyComponent (name),
  50. isCustomClass (true)
  51. {
  52. }
  53. ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
  54. const String& name,
  55. const StringArray& choices_,
  56. const Array <var>& correspondingValues)
  57. : PropertyComponent (name),
  58. choices (choices_),
  59. isCustomClass (false)
  60. {
  61. // The array of corresponding values must contain one value for each of the items in
  62. // the choices array!
  63. jassert (correspondingValues.size() == choices.size());
  64. createComboBox();
  65. comboBox.getSelectedIdAsValue().referTo (Value (new RemapperValueSource (valueToControl, correspondingValues)));
  66. }
  67. ChoicePropertyComponent::~ChoicePropertyComponent()
  68. {
  69. }
  70. //==============================================================================
  71. void ChoicePropertyComponent::createComboBox()
  72. {
  73. addAndMakeVisible (&comboBox);
  74. for (int i = 0; i < choices.size(); ++i)
  75. {
  76. if (choices[i].isNotEmpty())
  77. comboBox.addItem (choices[i], i + 1);
  78. else
  79. comboBox.addSeparator();
  80. }
  81. comboBox.setEditableText (false);
  82. }
  83. void ChoicePropertyComponent::setIndex (const int /*newIndex*/)
  84. {
  85. jassertfalse; // you need to override this method in your subclass!
  86. }
  87. int ChoicePropertyComponent::getIndex() const
  88. {
  89. jassertfalse; // you need to override this method in your subclass!
  90. return -1;
  91. }
  92. const StringArray& ChoicePropertyComponent::getChoices() const
  93. {
  94. return choices;
  95. }
  96. //==============================================================================
  97. void ChoicePropertyComponent::refresh()
  98. {
  99. if (isCustomClass)
  100. {
  101. if (! comboBox.isVisible())
  102. {
  103. createComboBox();
  104. comboBox.addListener (this);
  105. }
  106. comboBox.setSelectedId (getIndex() + 1, true);
  107. }
  108. }
  109. void ChoicePropertyComponent::comboBoxChanged (ComboBox*)
  110. {
  111. if (isCustomClass)
  112. {
  113. const int newIndex = comboBox.getSelectedId() - 1;
  114. if (newIndex != getIndex())
  115. setIndex (newIndex);
  116. }
  117. }