1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #pragma once
6 
7 #include "../ivalue.h"
8 #include <vector>
9 
10 //------------------------------------------------------------------------
11 namespace VSTGUI {
12 namespace Standalone {
13 
14 //------------------------------------------------------------------------
15 class IStringListValue : public Interface
16 {
17 public:
18 	using StringType = UTF8String;
19 	using StringList = std::vector<StringType>;
20 	virtual bool updateStringList (const StringList& newStrings) = 0;
21 };
22 
23 //------------------------------------------------------------------------
24 class IMutableStepValue : public Interface
25 {
26 public:
27 	virtual void setNumSteps (IStepValue::StepType numSteps) = 0;
28 };
29 
30 //------------------------------------------------------------------------
31 class IRangeValueConverter : public Interface
32 {
33 public:
34 	virtual void setRange (IValue::Type minValue, IValue::Type maxValue) = 0;
35 };
36 
37 //------------------------------------------------------------------------
38 /**	%value create and helper functions
39  *	@ingroup standalone
40  */
41 namespace Value {
42 
43 //------------------------------------------------------------------------
44 /** @name %Create values
45  *	@{ */
46 
47 //------------------------------------------------------------------------
48 /** make a value in the normalized range [0..1]
49  *
50  *	@param id value ID
51  *	@param initialValue initial value
52  *	@param valueConverter value converter
53  *	@return shared value pointer
54  */
55 ValuePtr make (const UTF8String& id, IValue::Type initialValue = 0.,
56                const ValueConverterPtr& valueConverter = nullptr);
57 
58 //------------------------------------------------------------------------
59 /** make a step value
60  *
61  *	@param id value ID
62  *	@param numSteps number of discrete steps
63  *	@param initialValue initial value in the normalized range [0..1]
64  *	@param valueConverter value converter
65  *	@return shared value pointer
66  */
67 ValuePtr makeStepValue (const UTF8String& id, IStepValue::StepType numSteps,
68                         IValue::Type initialValue = 0.,
69                         const ValueConverterPtr& valueConverter = nullptr);
70 
71 //------------------------------------------------------------------------
72 /** make a string list value
73  *
74  *	a string list value is a step value where each step has a string representation.
75  *
76  *	to modify the string list you can cast the returned value object to IStringListValue
77  *	and use the updateStringList method.
78  *
79  *	@param id value ID
80  *	@param strings string list
81  *	@param initialValue initial value in the normalized range [0..1]
82  *	@return shared value pointer
83  */
84 ValuePtr makeStringListValue (const UTF8String& id,
85                               const std::initializer_list<IStringListValue::StringType>& strings,
86                               IValue::Type initialValue = 0.);
87 
88 //------------------------------------------------------------------------
89 /** make a string list value
90  *
91  *	the returned value object has the IStringListValue interface
92  *
93  *	@param id value ID
94  *	@param strings string list
95  *	@return shared value pointer
96  */
97 ValuePtr makeStringListValue (const UTF8String& id, const IStringListValue::StringList& strings);
98 
99 //------------------------------------------------------------------------
100 /** make a static string value
101  *
102  *	a static string value is an inactive unchangeable value
103  *
104  *	@param id value ID
105  *	@param value static string
106  *	@return shared value pointer
107  */
108 ValuePtr makeStaticStringValue (const UTF8String& id, const UTF8String& value);
109 
110 //------------------------------------------------------------------------
111 /** make a static string value
112  *
113  *	a static string value is an inactive unchangeable value
114  *
115  *	@param id value ID
116  *	@param value static string
117  *	@return shared value pointer
118  */
119 ValuePtr makeStaticStringValue (const UTF8String& id, UTF8String&& value);
120 
121 /** @} */
122 /** @name %Create value converters
123  *	@{ */
124 
125 //------------------------------------------------------------------------
126 /** make a percent value converter
127  *
128  *	converts normalized values to the range [0..100]
129  */
130 ValueConverterPtr makePercentConverter ();
131 
132 //------------------------------------------------------------------------
133 /** make a range value converter
134  *
135  *	converts normalized values to the range [minValue..maxValue]
136  */
137 ValueConverterPtr makeRangeConverter (IValue::Type minValue, IValue::Type maxValue,
138                                       uint32_t stringPrecision = 4);
139 
140 /** @} */
141 
142 //------------------------------------------------------------------------
143 /** @name %Value helper functions
144  *	@{
145  */
plainToNormalize(IValue & value,IValue::Type plainValue)146 inline IValue::Type plainToNormalize (IValue& value, IValue::Type plainValue)
147 {
148 	return value.getConverter ().plainToNormalized (plainValue);
149 }
150 
151 //------------------------------------------------------------------------
normalizeToPlain(IValue & value,IValue::Type normalizeValue)152 inline IValue::Type normalizeToPlain (IValue& value, IValue::Type normalizeValue)
153 {
154 	return value.getConverter ().normalizedToPlain (normalizeValue);
155 }
156 
157 //------------------------------------------------------------------------
stepToNormalize(IValue & value,IStepValue::StepType stepValue)158 inline IValue::Type stepToNormalize (IValue& value, IStepValue::StepType stepValue)
159 {
160 	if (auto sv = value.dynamicCast<IStepValue> ())
161 	{
162 		return sv->stepToValue (stepValue);
163 	}
164 	return IValue::InvalidValue;
165 }
166 
167 //------------------------------------------------------------------------
normalizeToStep(IValue & value,IValue::Type normalizeValue)168 inline IStepValue::StepType normalizeToStep (IValue& value, IValue::Type normalizeValue)
169 {
170 	if (auto sv = value.dynamicCast<IStepValue> ())
171 	{
172 		return sv->valueToStep (normalizeValue);
173 	}
174 	return IStepValue::InvalidStep;
175 }
176 
177 //------------------------------------------------------------------------
currentPlainValue(IValue & value)178 inline IValue::Type currentPlainValue (IValue& value)
179 {
180 	return normalizeToPlain (value, value.getValue ());
181 }
182 
183 //------------------------------------------------------------------------
currentStepValue(IValue & value)184 inline IStepValue::StepType currentStepValue (IValue& value)
185 {
186 	return normalizeToStep (value, value.getValue ());
187 }
188 
189 //------------------------------------------------------------------------
performSingleEdit(IValue & value,IValue::Type newValue)190 inline void performSingleEdit (IValue& value, IValue::Type newValue)
191 {
192 	value.beginEdit ();
193 	value.performEdit (newValue);
194 	value.endEdit ();
195 }
196 
197 //------------------------------------------------------------------------
performSinglePlainEdit(IValue & value,IValue::Type plainValue)198 inline void performSinglePlainEdit (IValue& value, IValue::Type plainValue)
199 {
200 	performSingleEdit (value, plainToNormalize (value, plainValue));
201 }
202 
203 //------------------------------------------------------------------------
performSingleStepEdit(IValue & value,IStepValue::StepType step)204 inline bool performSingleStepEdit (IValue& value, IStepValue::StepType step)
205 {
206 	if (auto stepValue = value.dynamicCast<IStepValue> ())
207 	{
208 		performSingleEdit (value, stepValue->stepToValue (step));
209 		return true;
210 	}
211 	return false;
212 }
213 /** @} */
214 
215 //------------------------------------------------------------------------
216 } // Value
217 } // Standalone
218 } // VSTGUI
219