openshot-audio  0.1.7
juce_LiveConstantEditor.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_LIVECONSTANTEDITOR_H_INCLUDED
26 #define JUCE_LIVECONSTANTEDITOR_H_INCLUDED
27 
28 #if JUCE_ENABLE_LIVE_CONSTANT_EDITOR && ! DOXYGEN
29 
30 //==============================================================================
34 namespace LiveConstantEditor
35 {
36  int64 parseInt (String);
37  double parseDouble (const String&);
38  String intToString (int, bool preferHex);
39  String intToString (int64, bool preferHex);
40 
41  template <typename Type>
42  static void setFromString (Type& v, const String& s) { v = static_cast<Type> (s); }
43  inline void setFromString (char& v, const String& s) { v = (char) parseInt (s); }
44  inline void setFromString (unsigned char& v, const String& s) { v = (unsigned char) parseInt (s); }
45  inline void setFromString (short& v, const String& s) { v = (short) parseInt (s); }
46  inline void setFromString (unsigned short& v, const String& s) { v = (unsigned short) parseInt (s); }
47  inline void setFromString (int& v, const String& s) { v = (int) parseInt (s); }
48  inline void setFromString (unsigned int& v, const String& s) { v = (unsigned int) parseInt (s); }
49  inline void setFromString (long& v, const String& s) { v = (long) parseInt (s); }
50  inline void setFromString (unsigned long& v, const String& s) { v = (unsigned long) parseInt (s); }
51  inline void setFromString (int64& v, const String& s) { v = (int64) parseInt (s); }
52  inline void setFromString (uint64& v, const String& s) { v = (uint64) parseInt (s); }
53  inline void setFromString (double& v, const String& s) { v = parseDouble (s); }
54  inline void setFromString (float& v, const String& s) { v = (float) parseDouble (s); }
55  inline void setFromString (String& v, const String& s) { v = s; }
56  inline void setFromString (Colour& v, const String& s) { v = Colour ((uint32) parseInt (s)); }
57 
58  template <typename Type>
59  inline String getAsString (const Type& v, bool) { return String (v); }
60  inline String getAsString (char v, bool preferHex) { return intToString ((int) v, preferHex); }
61  inline String getAsString (unsigned char v, bool preferHex) { return intToString ((int) v, preferHex); }
62  inline String getAsString (short v, bool preferHex) { return intToString ((int) v, preferHex); }
63  inline String getAsString (unsigned short v, bool preferHex) { return intToString ((int) v, preferHex); }
64  inline String getAsString (int v, bool preferHex) { return intToString ((int) v, preferHex); }
65  inline String getAsString (unsigned int v, bool preferHex) { return intToString ((int) v, preferHex); }
66  inline String getAsString (int64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
67  inline String getAsString (uint64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
68  inline String getAsString (Colour v, bool) { return intToString ((int) v.getARGB(), true); }
69 
70  template <typename Type> struct isStringType { enum { value = 0 }; };
71  template <> struct isStringType<String> { enum { value = 1 }; };
72 
73  template <typename Type>
74  inline String getAsCode (Type& v, bool preferHex) { return getAsString (v, preferHex); }
75  inline String getAsCode (Colour v, bool) { return "Colour (0x" + String::toHexString ((int) v.getARGB()).paddedLeft ('0', 8) + ")"; }
76  inline String getAsCode (const String& v, bool) { return CppTokeniserFunctions::addEscapeChars(v).quoted(); }
77  inline String getAsCode (const char* v, bool) { return getAsCode (String (v), false); }
78 
79  template <typename Type>
80  inline const char* castToCharPointer (const Type&) { return ""; }
81  inline const char* castToCharPointer (const String& s) { return s.toRawUTF8(); }
82 
83  struct LivePropertyEditorBase;
84 
85  //==============================================================================
86  struct JUCE_API LiveValueBase
87  {
88  LiveValueBase (const char* file, int line);
89  virtual ~LiveValueBase();
90 
91  virtual LivePropertyEditorBase* createPropertyComponent (CodeDocument&) = 0;
92  virtual String getStringValue (bool preferHex) const = 0;
93  virtual String getCodeValue (bool preferHex) const = 0;
94  virtual void setStringValue (const String&) = 0;
95  virtual String getOriginalStringValue (bool preferHex) const = 0;
96  virtual bool isString() const = 0;
97 
98  String name, sourceFile;
99  int sourceLine;
100 
101  JUCE_DECLARE_NON_COPYABLE (LiveValueBase)
102  };
103 
104  //==============================================================================
105  struct JUCE_API LivePropertyEditorBase : public Component,
106  private TextEditor::Listener,
107  private ButtonListener
108  {
109  LivePropertyEditorBase (LiveValueBase&, CodeDocument&);
110 
111  void paint (Graphics&) override;
112  void resized() override;
113  void textEditorTextChanged (TextEditor&) override;
114  void buttonClicked (Button*) override;
115 
116  void applyNewValue (const String&);
117  void selectOriginalValue();
118  void findOriginalValueInCode();
119 
120  LiveValueBase& value;
121  Label name;
122  TextEditor valueEditor;
123  TextButton resetButton;
124  CodeDocument& document;
125  CPlusPlusCodeTokeniser tokeniser;
126  CodeEditorComponent sourceEditor;
127  CodeDocument::Position valueStart, valueEnd;
128  ScopedPointer<Component> customComp;
129  bool wasHex;
130 
131  JUCE_DECLARE_NON_COPYABLE (LivePropertyEditorBase)
132  };
133 
134  //==============================================================================
135  Component* createColourEditor (LivePropertyEditorBase&);
136  Component* createIntegerSlider (LivePropertyEditorBase&);
137  Component* createFloatSlider (LivePropertyEditorBase&);
138 
139  template <typename Type> struct CustomEditor { static Component* create (LivePropertyEditorBase&) { return nullptr; } };
140  template<> struct CustomEditor<char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
141  template<> struct CustomEditor<unsigned char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
142  template<> struct CustomEditor<int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
143  template<> struct CustomEditor<unsigned int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
144  template<> struct CustomEditor<short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
145  template<> struct CustomEditor<unsigned short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
146  template<> struct CustomEditor<int64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
147  template<> struct CustomEditor<uint64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
148  template<> struct CustomEditor<float> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
149  template<> struct CustomEditor<double> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
150  template<> struct CustomEditor<Colour> { static Component* create (LivePropertyEditorBase& e) { return createColourEditor (e); } };
151 
152  template <typename Type>
153  struct LivePropertyEditor : public LivePropertyEditorBase
154  {
155  template <typename ValueType>
156  LivePropertyEditor (ValueType& v, CodeDocument& d) : LivePropertyEditorBase (v, d)
157  {
158  addAndMakeVisible (customComp = CustomEditor<Type>::create (*this));
159  }
160  };
161 
162  //==============================================================================
163  template <typename Type>
164  struct LiveValue : public LiveValueBase
165  {
166  LiveValue (const char* file, int line, const Type& initialValue)
167  : LiveValueBase (file, line), value (initialValue), originalValue (initialValue)
168  {}
169 
170  operator Type() const noexcept { return value; }
171  operator const char*() const { return castToCharPointer (value); }
172 
173  LivePropertyEditorBase* createPropertyComponent (CodeDocument& doc) override
174  {
175  return new LivePropertyEditor<Type> (*this, doc);
176  }
177 
178  String getStringValue (bool preferHex) const override { return getAsString (value, preferHex); }
179  String getCodeValue (bool preferHex) const override { return getAsCode (value, preferHex); }
180  String getOriginalStringValue (bool preferHex) const override { return getAsString (originalValue, preferHex); }
181  void setStringValue (const String& s) override { setFromString (value, s); }
182  bool isString() const override { return isStringType<Type>::value; }
183 
184  Type value, originalValue;
185 
186  JUCE_DECLARE_NON_COPYABLE (LiveValue)
187  };
188 
189  //==============================================================================
190  class JUCE_API ValueList : private AsyncUpdater,
191  private DeletedAtShutdown
192  {
193  public:
194  ValueList();
195  ~ValueList();
196 
197  juce_DeclareSingleton (ValueList, false)
198 
199  template <typename Type>
200  LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
201  {
202  const ScopedLock sl (lock);
203  typedef LiveValue<Type> ValueType;
204 
205  for (int i = 0; i < values.size(); ++i)
206  {
207  LiveValueBase* v = values.getUnchecked(i);
208 
209  if (v->sourceLine == line && v->sourceFile == file)
210  return *static_cast<ValueType*> (v);
211  }
212 
213  ValueType* v = new ValueType (file, line, initialValue);
214  addValue (v);
215  return *v;
216  }
217 
218  private:
220  OwnedArray<CodeDocument> documents;
221  Array<File> documentFiles;
222  class EditorWindow;
223  friend class EditorWindow;
224  friend struct ContainerDeletePolicy<EditorWindow>;
226  CriticalSection lock;
227 
228  CodeDocument& getDocument (const File&);
229  void addValue (LiveValueBase*);
230  void handleAsyncUpdate() override;
231  };
232 
233  template <typename Type>
234  inline LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
235  {
236  return ValueList::getInstance()->getValue (file, line, initialValue);
237  }
238 
239  inline LiveValue<String>& getValue (const char* file, int line, const char* initialValue)
240  {
241  return getValue (file, line, String (initialValue));
242  }
243 }
244 
245 #endif
246 
247 //==============================================================================
248 #if JUCE_ENABLE_LIVE_CONSTANT_EDITOR || DOXYGEN
249 
295  #define JUCE_LIVE_CONSTANT(initialValue) \
296  (juce::LiveConstantEditor::getValue (__FILE__, __LINE__ - 1, initialValue))
297 #else
298  #define JUCE_LIVE_CONSTANT(initialValue) \
299  (initialValue)
300 #endif
301 
302 
303 #endif // JUCE_LIVECONSTANTEDITOR_H_INCLUDED
Definition: juce_TextEditor.h:280
Definition: juce_CPlusPlusCodeTokeniser.h:35
static String toHexString(int number)
Definition: juce_String.cpp:1925
#define noexcept
Definition: juce_CompilerSupport.h:141
String quoted(juce_wchar quoteCharacter= '"') const
Definition: juce_String.cpp:1640
Definition: juce_Component.h:2092
Definition: juce_ScopedLock.h:59
Definition: juce_Button.h:39
Definition: juce_CodeDocument.h:59
Definition: juce_DeletedAtShutdown.h:40
TOUCHINPUT int
Definition: juce_win32_Windowing.cpp:123
Definition: juce_TextButton.h:36
Definition: juce_String.h:43
#define JUCE_API
Definition: juce_StandardHeader.h:139
#define const
Definition: juce_AsyncUpdater.h:39
Definition: juce_CriticalSection.h:47
#define juce_DeclareSingleton(classname, doNotRecreateAfterDeletion)
Definition: juce_Label.h:34
unsigned long long uint64
Definition: juce_MathsFunctions.h:62
static String addEscapeChars(const String &s)
Definition: juce_CPlusPlusCodeTokeniserFunctions.h:632
unsigned int uint32
Definition: juce_MathsFunctions.h:51
Definition: juce_Colour.h:35
Definition: juce_CodeDocument.h:42
Definition: juce_Component.h:33
Definition: juce_TextEditor.h:38
long long int64
Definition: juce_MathsFunctions.h:60
Definition: juce_Button.h:162
Definition: juce_win32_Windowing.cpp:76
Definition: juce_ContainerDeletePolicy.h:44
uint32 getARGB() const noexcept
Definition: juce_Colour.cpp:238
const char * toRawUTF8() const
Definition: juce_String.cpp:2061
Definition: juce_OwnedArray.h:55
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
Definition: juce_CodeEditorComponent.h:38
Definition: juce_GraphicsContext.h:42
Definition: juce_File.h:45