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.

350 lines
11KB

  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. //==============================================================================
  19. String hexString8Digits (int value);
  20. String createAlphaNumericUID();
  21. String createGUID (const String& seed); // Turns a seed into a windows GUID
  22. String escapeSpaces (const String& text); // replaces spaces with blackslash-space
  23. String addQuotesIfContainsSpaces (const String& text);
  24. StringPairArray parsePreprocessorDefs (const String& defs);
  25. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs);
  26. String createGCCPreprocessorFlags (const StringPairArray& defs);
  27. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString);
  28. StringArray getSearchPathsFromString (const String& searchPath);
  29. void setValueIfVoid (Value value, const var& defaultValue);
  30. //==============================================================================
  31. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex);
  32. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX = true, bool scrollY = true);
  33. void drawComponentPlaceholder (Graphics& g, int w, int h, const String& text);
  34. void drawRecessedShadows (Graphics& g, int w, int h, int shadowSize);
  35. void showUTF8ToolWindow();
  36. // Start a callout modally, which will delete the content comp when it's dismissed.
  37. void launchAsyncCallOutBox (Component& attachTo, Component* content);
  38. bool cancelAnyModalComponents();
  39. bool reinvokeCommandAfterCancellingModalComps (const ApplicationCommandTarget::InvocationInfo&);
  40. //==============================================================================
  41. class RolloverHelpComp : public Component,
  42. private Timer
  43. {
  44. public:
  45. RolloverHelpComp();
  46. void paint (Graphics& g);
  47. void timerCallback();
  48. private:
  49. Component* lastComp;
  50. String lastTip;
  51. static String findTip (Component*);
  52. };
  53. //==============================================================================
  54. class PropertyListBuilder
  55. {
  56. public:
  57. PropertyListBuilder() {}
  58. void add (PropertyComponent* propertyComp)
  59. {
  60. components.add (propertyComp);
  61. }
  62. void add (PropertyComponent* propertyComp, const String& tooltip)
  63. {
  64. propertyComp->setTooltip (tooltip);
  65. add (propertyComp);
  66. }
  67. void addSearchPathProperty (const Value& value, const String& name, const String& mainHelpText)
  68. {
  69. add (new TextPropertyComponent (value, name, 16384, true),
  70. mainHelpText + " Use semi-colons or new-lines to separate multiple paths.");
  71. }
  72. void setPreferredHeight (int height)
  73. {
  74. for (int j = components.size(); --j >= 0;)
  75. components.getUnchecked(j)->setPreferredHeight (height);
  76. }
  77. Array <PropertyComponent*> components;
  78. private:
  79. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyListBuilder);
  80. };
  81. //==============================================================================
  82. class FloatingLabelComponent : public Component
  83. {
  84. public:
  85. FloatingLabelComponent();
  86. void remove();
  87. void update (Component* parent, const String& text, const Colour& textColour,
  88. int x, int y, bool toRight, bool below);
  89. void paint (Graphics& g);
  90. private:
  91. Font font;
  92. Colour colour;
  93. GlyphArrangement glyphs;
  94. };
  95. //==============================================================================
  96. // A ValueSource which takes an input source, and forwards any changes in it.
  97. // This class is a handy way to create sources which re-map a value.
  98. class ValueSourceFilter : public Value::ValueSource,
  99. public Value::Listener
  100. {
  101. public:
  102. ValueSourceFilter (const Value& source) : sourceValue (source)
  103. {
  104. sourceValue.addListener (this);
  105. }
  106. void valueChanged (Value&) { sendChangeMessage (true); }
  107. protected:
  108. Value sourceValue;
  109. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueSourceFilter);
  110. };
  111. //==============================================================================
  112. class PopupColourSelector : public Component,
  113. public ChangeListener,
  114. public Value::Listener,
  115. public ButtonListener
  116. {
  117. public:
  118. PopupColourSelector (const Value& colourValue_,
  119. const Colour& defaultColour_,
  120. const bool canResetToDefault)
  121. : defaultButton ("Reset to Default"),
  122. colourValue (colourValue_),
  123. defaultColour (defaultColour_)
  124. {
  125. addAndMakeVisible (&selector);
  126. selector.setName ("Colour");
  127. selector.setCurrentColour (getColour());
  128. selector.addChangeListener (this);
  129. if (canResetToDefault)
  130. {
  131. addAndMakeVisible (&defaultButton);
  132. defaultButton.addListener (this);
  133. }
  134. colourValue.addListener (this);
  135. setSize (300, 400);
  136. }
  137. void resized()
  138. {
  139. if (defaultButton.isVisible())
  140. {
  141. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  142. defaultButton.changeWidthToFitText (22);
  143. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  144. }
  145. else
  146. {
  147. selector.setBounds (0, 0, getWidth(), getHeight());
  148. }
  149. }
  150. Colour getColour() const
  151. {
  152. if (colourValue.toString().isEmpty())
  153. return defaultColour;
  154. return Colour::fromString (colourValue.toString());
  155. }
  156. void setColour (const Colour& newColour)
  157. {
  158. if (getColour() != newColour)
  159. {
  160. if (newColour == defaultColour && defaultButton.isVisible())
  161. colourValue = var::null;
  162. else
  163. colourValue = newColour.toDisplayString (true);
  164. }
  165. }
  166. void buttonClicked (Button*)
  167. {
  168. setColour (defaultColour);
  169. selector.setCurrentColour (defaultColour);
  170. }
  171. void changeListenerCallback (ChangeBroadcaster*)
  172. {
  173. if (selector.getCurrentColour() != getColour())
  174. setColour (selector.getCurrentColour());
  175. }
  176. void valueChanged (Value&)
  177. {
  178. selector.setCurrentColour (getColour());
  179. }
  180. private:
  181. StoredSettings::ColourSelectorWithSwatches selector;
  182. TextButton defaultButton;
  183. Value colourValue;
  184. Colour defaultColour;
  185. };
  186. //==============================================================================
  187. /**
  188. A component that shows a colour swatch with hex ARGB value, and which pops up
  189. a colour selector when you click it.
  190. */
  191. class ColourEditorComponent : public Component,
  192. public Value::Listener
  193. {
  194. public:
  195. ColourEditorComponent (UndoManager* undoManager_, const Value& colourValue_,
  196. const Colour& defaultColour_, const bool canResetToDefault_)
  197. : undoManager (undoManager_), colourValue (colourValue_), defaultColour (defaultColour_),
  198. canResetToDefault (canResetToDefault_)
  199. {
  200. colourValue.addListener (this);
  201. }
  202. void paint (Graphics& g)
  203. {
  204. const Colour colour (getColour());
  205. g.fillAll (Colours::grey);
  206. g.fillCheckerBoard (getLocalBounds().reduced (2, 2),
  207. 10, 10,
  208. Colour (0xffdddddd).overlaidWith (colour),
  209. Colour (0xffffffff).overlaidWith (colour));
  210. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  211. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  212. g.drawFittedText (colour.toDisplayString (true),
  213. 2, 1, getWidth() - 4, getHeight() - 1,
  214. Justification::centred, 1);
  215. }
  216. Colour getColour() const
  217. {
  218. if (colourValue.toString().isEmpty())
  219. return defaultColour;
  220. return Colour::fromString (colourValue.toString());
  221. }
  222. void setColour (const Colour& newColour)
  223. {
  224. if (getColour() != newColour)
  225. {
  226. if (newColour == defaultColour && canResetToDefault)
  227. colourValue = var::null;
  228. else
  229. colourValue = newColour.toDisplayString (true);
  230. }
  231. }
  232. void resetToDefault()
  233. {
  234. setColour (defaultColour);
  235. }
  236. void refresh()
  237. {
  238. const Colour col (getColour());
  239. if (col != lastColour)
  240. {
  241. lastColour = col;
  242. repaint();
  243. }
  244. }
  245. void mouseDown (const MouseEvent&)
  246. {
  247. if (undoManager != nullptr)
  248. undoManager->beginNewTransaction();
  249. launchAsyncCallOutBox (*this, new PopupColourSelector (colourValue, defaultColour, canResetToDefault));
  250. }
  251. void valueChanged (Value&)
  252. {
  253. refresh();
  254. }
  255. private:
  256. UndoManager* undoManager;
  257. Value colourValue;
  258. Colour lastColour;
  259. const Colour defaultColour;
  260. const bool canResetToDefault;
  261. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourEditorComponent);
  262. };
  263. //==============================================================================
  264. class ColourPropertyComponent : public PropertyComponent
  265. {
  266. public:
  267. ColourPropertyComponent (UndoManager* undoManager, const String& name, const Value& colour,
  268. const Colour& defaultColour, bool canResetToDefault)
  269. : PropertyComponent (name),
  270. colourEditor (undoManager, colour, defaultColour, canResetToDefault)
  271. {
  272. addAndMakeVisible (&colourEditor);
  273. }
  274. void resized()
  275. {
  276. colourEditor.setBounds (getLookAndFeel().getPropertyComponentContentPosition (*this));
  277. }
  278. void refresh() {}
  279. protected:
  280. ColourEditorComponent colourEditor;
  281. };