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.

192 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. #ifndef __JUCER_VALUEREMAPPERSOURCE_JUCEHEADER__
  19. #define __JUCER_VALUEREMAPPERSOURCE_JUCEHEADER__
  20. //==============================================================================
  21. /** A ValueSource that remaps specific values to new values.
  22. */
  23. class ValueRemapperSource : public Value::ValueSource,
  24. public Value::Listener
  25. {
  26. public:
  27. ValueRemapperSource (const Value& sourceValue_)
  28. : sourceValue (sourceValue_)
  29. {
  30. sourceValue.addListener (this);
  31. }
  32. ValueRemapperSource (const Value& sourceValue_, const char** mappings_)
  33. : sourceValue (sourceValue_)
  34. {
  35. addMappings (mappings_);
  36. sourceValue.addListener (this);
  37. }
  38. ~ValueRemapperSource() {}
  39. void addMappings (const char* const* values)
  40. {
  41. while (values[0] != 0 && values[1] != 0)
  42. {
  43. addMapping (values[0], values[1]);
  44. values += 2;
  45. }
  46. }
  47. void addMapping (const var& sourceValue_, const var& remappedValue)
  48. {
  49. mappings.add (sourceValue_);
  50. mappings.add (remappedValue);
  51. }
  52. const var getValue() const
  53. {
  54. const var sourceVar (sourceValue.getValue());
  55. for (int i = 0; i < mappings.size(); i += 2)
  56. if (sourceVar == mappings.getReference(i))
  57. return mappings.getReference (i + 1);
  58. return sourceVar;
  59. }
  60. void setValue (const var& newValue)
  61. {
  62. var remappedVal (newValue);
  63. for (int i = 1; i < mappings.size(); i += 2)
  64. {
  65. if (newValue == mappings.getReference(i))
  66. {
  67. remappedVal = mappings.getReference (i - 1);
  68. break;
  69. }
  70. }
  71. if (remappedVal != sourceValue)
  72. sourceValue = remappedVal;
  73. }
  74. void valueChanged (Value&)
  75. {
  76. sendChangeMessage (true);
  77. }
  78. //==============================================================================
  79. juce_UseDebuggingNewOperator
  80. protected:
  81. Value sourceValue;
  82. Array<var> mappings;
  83. ValueRemapperSource (const ValueRemapperSource&);
  84. const ValueRemapperSource& operator= (const ValueRemapperSource&);
  85. };
  86. //==============================================================================
  87. /** A ValueSource that converts strings into an ID suitable for a combo box.
  88. */
  89. class StringListValueSource : public Value::ValueSource,
  90. public Value::Listener
  91. {
  92. public:
  93. StringListValueSource (const Value& sourceValue_, const StringArray& strings_)
  94. : sourceValue (sourceValue_), strings (strings_)
  95. {
  96. sourceValue.addListener (this);
  97. }
  98. ~StringListValueSource() {}
  99. const var getValue() const { return jmax (0, strings.indexOf (sourceValue.toString())) + 1; }
  100. void setValue (const var& newValue)
  101. {
  102. const String newVal (strings [((int) newValue) - 1]);
  103. if (newVal != getValue().toString()) // this test is important, because if a property is missing, it won't
  104. sourceValue = newVal; // create it (causing an unwanted undo action) when a control sets it to empty
  105. }
  106. void valueChanged (Value&) { sendChangeMessage (true); }
  107. static ChoicePropertyComponent* create (const String& title, const Value& value, const StringArray& strings)
  108. {
  109. return new ChoicePropertyComponent (Value (new StringListValueSource (value, strings)), title, strings);
  110. }
  111. //==============================================================================
  112. juce_UseDebuggingNewOperator
  113. protected:
  114. Value sourceValue;
  115. StringArray strings;
  116. StringListValueSource (const StringListValueSource&);
  117. const StringListValueSource& operator= (const StringListValueSource&);
  118. };
  119. //==============================================================================
  120. /**
  121. */
  122. template <typename Type>
  123. class NumericValueSource : public Value::ValueSource,
  124. public Value::Listener
  125. {
  126. public:
  127. NumericValueSource (const Value& sourceValue_)
  128. : sourceValue (sourceValue_)
  129. {
  130. sourceValue.addListener (this);
  131. }
  132. ~NumericValueSource() {}
  133. void valueChanged (Value&) { sendChangeMessage (true); }
  134. const var getValue() const { return (Type) sourceValue.getValue(); }
  135. void setValue (const var& newValue)
  136. {
  137. const Type newVal = (Type) newValue;
  138. if (newVal != (Type) getValue()) // this test is important, because if a property is missing, it won't
  139. sourceValue = newVal; // create it (causing an unwanted undo action) when a control sets it to 0
  140. }
  141. //==============================================================================
  142. juce_UseDebuggingNewOperator
  143. protected:
  144. Value sourceValue;
  145. NumericValueSource (const NumericValueSource&);
  146. const NumericValueSource& operator= (const NumericValueSource&);
  147. };
  148. #endif // __JUCER_VALUEREMAPPERSOURCE_JUCEHEADER__