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.

157 lines
4.6KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. class ChoicePropertyComponent::RemapperValueSource : public Value::ValueSource,
  21. public ValueListener
  22. {
  23. public:
  24. RemapperValueSource (const Value& sourceValue_, const Array<var>& mappings_)
  25. : sourceValue (sourceValue_),
  26. mappings (mappings_)
  27. {
  28. sourceValue.addListener (this);
  29. }
  30. ~RemapperValueSource() {}
  31. var getValue() const
  32. {
  33. return mappings.indexOf (sourceValue.getValue()) + 1;
  34. }
  35. void setValue (const var& newValue)
  36. {
  37. const var remappedVal (mappings [(int) newValue - 1]);
  38. if (remappedVal != sourceValue)
  39. sourceValue = remappedVal;
  40. }
  41. void valueChanged (Value&)
  42. {
  43. sendChangeMessage (true);
  44. }
  45. protected:
  46. //==============================================================================
  47. Value sourceValue;
  48. Array<var> mappings;
  49. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSource);
  50. };
  51. //==============================================================================
  52. ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
  53. : PropertyComponent (name),
  54. isCustomClass (true)
  55. {
  56. }
  57. ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
  58. const String& name,
  59. const StringArray& choices_,
  60. const Array <var>& correspondingValues)
  61. : PropertyComponent (name),
  62. choices (choices_),
  63. isCustomClass (false)
  64. {
  65. // The array of corresponding values must contain one value for each of the items in
  66. // the choices array!
  67. jassert (correspondingValues.size() == choices.size());
  68. createComboBox();
  69. comboBox.getSelectedIdAsValue().referTo (Value (new RemapperValueSource (valueToControl, 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. }
  122. END_JUCE_NAMESPACE