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.

151 lines
4.5KB

  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. public 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. ~RemapperValueSource() {}
  29. var getValue() const
  30. {
  31. return mappings.indexOf (sourceValue.getValue()) + 1;
  32. }
  33. void setValue (const var& newValue)
  34. {
  35. const var remappedVal (mappings [(int) newValue - 1]);
  36. if (remappedVal != sourceValue)
  37. sourceValue = remappedVal;
  38. }
  39. void valueChanged (Value&)
  40. {
  41. sendChangeMessage (true);
  42. }
  43. protected:
  44. //==============================================================================
  45. Value sourceValue;
  46. Array<var> mappings;
  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& choices_,
  58. const Array <var>& correspondingValues)
  59. : PropertyComponent (name),
  60. choices (choices_),
  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, correspondingValues)));
  68. }
  69. ChoicePropertyComponent::~ChoicePropertyComponent()
  70. {
  71. }
  72. //==============================================================================
  73. void ChoicePropertyComponent::createComboBox()
  74. {
  75. addAndMakeVisible (&comboBox);
  76. for (int i = 0; i < choices.size(); ++i)
  77. {
  78. if (choices[i].isNotEmpty())
  79. comboBox.addItem (choices[i], i + 1);
  80. else
  81. comboBox.addSeparator();
  82. }
  83. comboBox.setEditableText (false);
  84. }
  85. void ChoicePropertyComponent::setIndex (const int /*newIndex*/)
  86. {
  87. jassertfalse; // you need to override this method in your subclass!
  88. }
  89. int ChoicePropertyComponent::getIndex() const
  90. {
  91. jassertfalse; // you need to override this method in your subclass!
  92. return -1;
  93. }
  94. const StringArray& ChoicePropertyComponent::getChoices() const
  95. {
  96. return choices;
  97. }
  98. //==============================================================================
  99. void ChoicePropertyComponent::refresh()
  100. {
  101. if (isCustomClass)
  102. {
  103. if (! comboBox.isVisible())
  104. {
  105. createComboBox();
  106. comboBox.addListener (this);
  107. }
  108. comboBox.setSelectedId (getIndex() + 1, true);
  109. }
  110. }
  111. void ChoicePropertyComponent::comboBoxChanged (ComboBox*)
  112. {
  113. if (isCustomClass)
  114. {
  115. const int newIndex = comboBox.getSelectedId() - 1;
  116. if (newIndex != getIndex())
  117. setIndex (newIndex);
  118. }
  119. }