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.

429 lines
13KB

  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 showUTF8ToolWindow (ScopedPointer<Component>& ownerPointer);
  34. bool cancelAnyModalComponents();
  35. bool reinvokeCommandAfterCancellingModalComps (const ApplicationCommandTarget::InvocationInfo&);
  36. //==============================================================================
  37. struct Icon
  38. {
  39. Icon() : path (nullptr) {}
  40. Icon (const Path& p, const Colour& c) : path (&p), colour (c) {}
  41. Icon (const Path* p, const Colour& c) : path (p), colour (c) {}
  42. void draw (Graphics& g, const Rectangle<float>& area, bool isCrossedOut) const
  43. {
  44. if (path != nullptr)
  45. {
  46. g.setColour (colour);
  47. const RectanglePlacement placement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize);
  48. g.fillPath (*path, placement.getTransformToFit (path->getBounds(), area));
  49. if (isCrossedOut)
  50. {
  51. g.setColour (Colours::red.withAlpha (0.8f));
  52. g.drawLine ((float) area.getX(), area.getY() + area.getHeight() * 0.2f,
  53. (float) area.getRight(), area.getY() + area.getHeight() * 0.8f, 3.0f);
  54. }
  55. }
  56. }
  57. Icon withContrastingColourTo (const Colour& background) const
  58. {
  59. return Icon (path, background.contrasting (colour, 0.6f));
  60. }
  61. const Path* path;
  62. Colour colour;
  63. };
  64. //==============================================================================
  65. class RolloverHelpComp : public Component,
  66. private Timer
  67. {
  68. public:
  69. RolloverHelpComp();
  70. void paint (Graphics& g);
  71. void timerCallback();
  72. private:
  73. Component* lastComp;
  74. String lastTip;
  75. static String findTip (Component*);
  76. };
  77. //==============================================================================
  78. class PropertyListBuilder
  79. {
  80. public:
  81. PropertyListBuilder() {}
  82. void add (PropertyComponent* propertyComp)
  83. {
  84. components.add (propertyComp);
  85. }
  86. void add (PropertyComponent* propertyComp, const String& tooltip)
  87. {
  88. propertyComp->setTooltip (tooltip);
  89. add (propertyComp);
  90. }
  91. void addSearchPathProperty (const Value& value, const String& name, const String& mainHelpText)
  92. {
  93. add (new TextPropertyComponent (value, name, 16384, true),
  94. mainHelpText + " Use semi-colons or new-lines to separate multiple paths.");
  95. }
  96. void setPreferredHeight (int height)
  97. {
  98. for (int j = components.size(); --j >= 0;)
  99. components.getUnchecked(j)->setPreferredHeight (height);
  100. }
  101. Array <PropertyComponent*> components;
  102. private:
  103. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyListBuilder)
  104. };
  105. //==============================================================================
  106. class FloatingLabelComponent : public Component
  107. {
  108. public:
  109. FloatingLabelComponent();
  110. void remove();
  111. void update (Component* parent, const String& text, const Colour& textColour,
  112. int x, int y, bool toRight, bool below);
  113. void paint (Graphics& g);
  114. private:
  115. Font font;
  116. Colour colour;
  117. GlyphArrangement glyphs;
  118. };
  119. //==============================================================================
  120. // A ValueSource which takes an input source, and forwards any changes in it.
  121. // This class is a handy way to create sources which re-map a value.
  122. class ValueSourceFilter : public Value::ValueSource,
  123. public Value::Listener
  124. {
  125. public:
  126. ValueSourceFilter (const Value& source) : sourceValue (source)
  127. {
  128. sourceValue.addListener (this);
  129. }
  130. void valueChanged (Value&) { sendChangeMessage (true); }
  131. protected:
  132. Value sourceValue;
  133. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueSourceFilter)
  134. };
  135. //==============================================================================
  136. class FloatingToolWindow : public DialogWindow
  137. {
  138. public:
  139. FloatingToolWindow (const String& title,
  140. const String& windowPosPropertyName,
  141. Component* content,
  142. ScopedPointer<Component>& ownerPointer,
  143. int defaultW, int defaultH,
  144. int minW, int minH,
  145. int maxW, int maxH)
  146. : DialogWindow (title, Colours::darkgrey, true, true),
  147. windowPosProperty (windowPosPropertyName),
  148. owner (ownerPointer)
  149. {
  150. setUsingNativeTitleBar (true);
  151. setResizable (true, true);
  152. setResizeLimits (minW, minH, maxW, maxH);
  153. setContentOwned (content, false);
  154. const String windowState (getGlobalProperties().getValue (windowPosProperty));
  155. if (windowState.isNotEmpty())
  156. restoreWindowStateFromString (windowState);
  157. else
  158. centreAroundComponent (Component::getCurrentlyFocusedComponent(), defaultW, defaultH);
  159. setVisible (true);
  160. owner = this;
  161. }
  162. ~FloatingToolWindow()
  163. {
  164. getGlobalProperties().setValue (windowPosProperty, getWindowStateAsString());
  165. }
  166. void closeButtonPressed()
  167. {
  168. owner = nullptr;
  169. }
  170. private:
  171. String windowPosProperty;
  172. ScopedPointer<Component>& owner;
  173. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FloatingToolWindow)
  174. };
  175. //==============================================================================
  176. class PopupColourSelector : public Component,
  177. public ChangeListener,
  178. public Value::Listener,
  179. public ButtonListener
  180. {
  181. public:
  182. PopupColourSelector (const Value& colour,
  183. const Colour& defaultCol,
  184. const bool canResetToDefault)
  185. : defaultButton ("Reset to Default"),
  186. colourValue (colour),
  187. defaultColour (defaultCol)
  188. {
  189. addAndMakeVisible (&selector);
  190. selector.setName ("Colour");
  191. selector.setCurrentColour (getColour());
  192. selector.addChangeListener (this);
  193. if (canResetToDefault)
  194. {
  195. addAndMakeVisible (&defaultButton);
  196. defaultButton.addListener (this);
  197. }
  198. colourValue.addListener (this);
  199. setSize (300, 400);
  200. }
  201. void resized()
  202. {
  203. if (defaultButton.isVisible())
  204. {
  205. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  206. defaultButton.changeWidthToFitText (22);
  207. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  208. }
  209. else
  210. {
  211. selector.setBounds (0, 0, getWidth(), getHeight());
  212. }
  213. }
  214. Colour getColour() const
  215. {
  216. if (colourValue.toString().isEmpty())
  217. return defaultColour;
  218. return Colour::fromString (colourValue.toString());
  219. }
  220. void setColour (const Colour& newColour)
  221. {
  222. if (getColour() != newColour)
  223. {
  224. if (newColour == defaultColour && defaultButton.isVisible())
  225. colourValue = var::null;
  226. else
  227. colourValue = newColour.toDisplayString (true);
  228. }
  229. }
  230. void buttonClicked (Button*)
  231. {
  232. setColour (defaultColour);
  233. selector.setCurrentColour (defaultColour);
  234. }
  235. void changeListenerCallback (ChangeBroadcaster*)
  236. {
  237. if (selector.getCurrentColour() != getColour())
  238. setColour (selector.getCurrentColour());
  239. }
  240. void valueChanged (Value&)
  241. {
  242. selector.setCurrentColour (getColour());
  243. }
  244. private:
  245. StoredSettings::ColourSelectorWithSwatches selector;
  246. TextButton defaultButton;
  247. Value colourValue;
  248. Colour defaultColour;
  249. };
  250. //==============================================================================
  251. /**
  252. A component that shows a colour swatch with hex ARGB value, and which pops up
  253. a colour selector when you click it.
  254. */
  255. class ColourEditorComponent : public Component,
  256. public Value::Listener
  257. {
  258. public:
  259. ColourEditorComponent (UndoManager* um, const Value& colour,
  260. const Colour& defaultCol, const bool canReset)
  261. : undoManager (um), colourValue (colour), defaultColour (defaultCol),
  262. canResetToDefault (canReset)
  263. {
  264. colourValue.addListener (this);
  265. }
  266. void paint (Graphics& g)
  267. {
  268. const Colour colour (getColour());
  269. g.fillAll (Colours::grey);
  270. g.fillCheckerBoard (getLocalBounds().reduced (2),
  271. 10, 10,
  272. Colour (0xffdddddd).overlaidWith (colour),
  273. Colour (0xffffffff).overlaidWith (colour));
  274. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  275. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  276. g.drawFittedText (colour.toDisplayString (true), getLocalBounds().reduced (2, 1),
  277. Justification::centred, 1);
  278. }
  279. Colour getColour() const
  280. {
  281. if (colourValue.toString().isEmpty())
  282. return defaultColour;
  283. return Colour::fromString (colourValue.toString());
  284. }
  285. void setColour (const Colour& newColour)
  286. {
  287. if (getColour() != newColour)
  288. {
  289. if (newColour == defaultColour && canResetToDefault)
  290. colourValue = var::null;
  291. else
  292. colourValue = newColour.toDisplayString (true);
  293. }
  294. }
  295. void resetToDefault()
  296. {
  297. setColour (defaultColour);
  298. }
  299. void refresh()
  300. {
  301. const Colour col (getColour());
  302. if (col != lastColour)
  303. {
  304. lastColour = col;
  305. repaint();
  306. }
  307. }
  308. void mouseDown (const MouseEvent&)
  309. {
  310. if (undoManager != nullptr)
  311. undoManager->beginNewTransaction();
  312. CallOutBox::launchAsynchronously (new PopupColourSelector (colourValue,
  313. defaultColour,
  314. canResetToDefault),
  315. getScreenBounds(), nullptr);
  316. }
  317. void valueChanged (Value&)
  318. {
  319. refresh();
  320. }
  321. private:
  322. UndoManager* undoManager;
  323. Value colourValue;
  324. Colour lastColour;
  325. const Colour defaultColour;
  326. const bool canResetToDefault;
  327. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourEditorComponent)
  328. };
  329. //==============================================================================
  330. class ColourPropertyComponent : public PropertyComponent
  331. {
  332. public:
  333. ColourPropertyComponent (UndoManager* undoManager, const String& name, const Value& colour,
  334. const Colour& defaultColour, bool canResetToDefault)
  335. : PropertyComponent (name),
  336. colourEditor (undoManager, colour, defaultColour, canResetToDefault)
  337. {
  338. addAndMakeVisible (&colourEditor);
  339. }
  340. void resized()
  341. {
  342. colourEditor.setBounds (getLookAndFeel().getPropertyComponentContentPosition (*this));
  343. }
  344. void refresh() {}
  345. protected:
  346. ColourEditorComponent colourEditor;
  347. };