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.

506 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. //==============================================================================
  18. #ifndef JUCER_MISCUTILITIES_H_INCLUDED
  19. #define JUCER_MISCUTILITIES_H_INCLUDED
  20. String hexString8Digits (int value);
  21. String createAlphaNumericUID();
  22. String createGUID (const String& seed); // Turns a seed into a windows GUID
  23. String escapeSpaces (const String& text); // replaces spaces with blackslash-space
  24. String addQuotesIfContainsSpaces (const String& text);
  25. StringPairArray parsePreprocessorDefs (const String& defs);
  26. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs);
  27. String createGCCPreprocessorFlags (const StringPairArray& defs);
  28. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString);
  29. StringArray getSearchPathsFromString (const String& searchPath);
  30. StringArray getCommaOrWhitespaceSeparatedItems (const String&);
  31. void setValueIfVoid (Value value, const var& defaultValue);
  32. void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value);
  33. void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, bool value);
  34. void addPlistDictionaryKeyInt (XmlElement* xml, const String& key, int value);
  35. //==============================================================================
  36. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex);
  37. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX = true, bool scrollY = true);
  38. void showUTF8ToolWindow (ScopedPointer<Component>& ownerPointer);
  39. void showSVGPathDataToolWindow (ScopedPointer<Component>& ownerPointer);
  40. bool cancelAnyModalComponents();
  41. bool reinvokeCommandAfterCancellingModalComps (const ApplicationCommandTarget::InvocationInfo&);
  42. StringArray getCleanedStringArray (StringArray);
  43. //==============================================================================
  44. class RolloverHelpComp : public Component,
  45. private Timer
  46. {
  47. public:
  48. RolloverHelpComp();
  49. void paint (Graphics&) override;
  50. void timerCallback() override;
  51. private:
  52. Component* lastComp;
  53. String lastTip;
  54. static String findTip (Component*);
  55. };
  56. //==============================================================================
  57. class PropertyListBuilder
  58. {
  59. public:
  60. PropertyListBuilder() {}
  61. void add (PropertyComponent* propertyComp)
  62. {
  63. components.add (propertyComp);
  64. }
  65. void add (PropertyComponent* propertyComp, const String& tooltip)
  66. {
  67. propertyComp->setTooltip (tooltip);
  68. add (propertyComp);
  69. }
  70. void addSearchPathProperty (const Value& value, const String& name, const String& mainHelpText)
  71. {
  72. add (new TextPropertyComponent (value, name, 16384, true),
  73. mainHelpText + " Use semi-colons or new-lines to separate multiple paths.");
  74. }
  75. void setPreferredHeight (int height)
  76. {
  77. for (int j = components.size(); --j >= 0;)
  78. components.getUnchecked(j)->setPreferredHeight (height);
  79. }
  80. Array <PropertyComponent*> components;
  81. private:
  82. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyListBuilder)
  83. };
  84. //==============================================================================
  85. // A ValueSource which takes an input source, and forwards any changes in it.
  86. // This class is a handy way to create sources which re-map a value.
  87. class ValueSourceFilter : public Value::ValueSource,
  88. public Value::Listener
  89. {
  90. public:
  91. ValueSourceFilter (const Value& source) : sourceValue (source)
  92. {
  93. sourceValue.addListener (this);
  94. }
  95. void valueChanged (Value&) override { sendChangeMessage (true); }
  96. protected:
  97. Value sourceValue;
  98. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueSourceFilter)
  99. };
  100. //==============================================================================
  101. class FloatingToolWindow : public DialogWindow
  102. {
  103. public:
  104. FloatingToolWindow (const String& title,
  105. const String& windowPosPropertyName,
  106. Component* content,
  107. ScopedPointer<Component>& ownerPointer,
  108. int defaultW, int defaultH,
  109. int minW, int minH,
  110. int maxW, int maxH)
  111. : DialogWindow (title, Colours::darkgrey, true, true),
  112. windowPosProperty (windowPosPropertyName),
  113. owner (ownerPointer)
  114. {
  115. setUsingNativeTitleBar (true);
  116. setResizable (true, true);
  117. setResizeLimits (minW, minH, maxW, maxH);
  118. setContentOwned (content, false);
  119. const String windowState (getGlobalProperties().getValue (windowPosProperty));
  120. if (windowState.isNotEmpty())
  121. restoreWindowStateFromString (windowState);
  122. else
  123. centreAroundComponent (Component::getCurrentlyFocusedComponent(), defaultW, defaultH);
  124. setVisible (true);
  125. owner = this;
  126. }
  127. ~FloatingToolWindow()
  128. {
  129. getGlobalProperties().setValue (windowPosProperty, getWindowStateAsString());
  130. }
  131. void closeButtonPressed() override
  132. {
  133. owner = nullptr;
  134. }
  135. private:
  136. String windowPosProperty;
  137. ScopedPointer<Component>& owner;
  138. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FloatingToolWindow)
  139. };
  140. //==============================================================================
  141. class PopupColourSelector : public Component,
  142. public ChangeListener,
  143. public Value::Listener,
  144. public ButtonListener
  145. {
  146. public:
  147. PopupColourSelector (const Value& colour,
  148. Colour defaultCol,
  149. const bool canResetToDefault)
  150. : defaultButton ("Reset to Default"),
  151. colourValue (colour),
  152. defaultColour (defaultCol)
  153. {
  154. addAndMakeVisible (selector);
  155. selector.setName ("Colour");
  156. selector.setCurrentColour (getColour());
  157. selector.addChangeListener (this);
  158. if (canResetToDefault)
  159. {
  160. addAndMakeVisible (defaultButton);
  161. defaultButton.addListener (this);
  162. }
  163. colourValue.addListener (this);
  164. setSize (300, 400);
  165. }
  166. void resized()
  167. {
  168. if (defaultButton.isVisible())
  169. {
  170. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  171. defaultButton.changeWidthToFitText (22);
  172. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  173. }
  174. else
  175. {
  176. selector.setBounds (getLocalBounds());
  177. }
  178. }
  179. Colour getColour() const
  180. {
  181. if (colourValue.toString().isEmpty())
  182. return defaultColour;
  183. return Colour::fromString (colourValue.toString());
  184. }
  185. void setColour (Colour newColour)
  186. {
  187. if (getColour() != newColour)
  188. {
  189. if (newColour == defaultColour && defaultButton.isVisible())
  190. colourValue = var();
  191. else
  192. colourValue = newColour.toDisplayString (true);
  193. }
  194. }
  195. void buttonClicked (Button*) override
  196. {
  197. setColour (defaultColour);
  198. selector.setCurrentColour (defaultColour);
  199. }
  200. void changeListenerCallback (ChangeBroadcaster*) override
  201. {
  202. if (selector.getCurrentColour() != getColour())
  203. setColour (selector.getCurrentColour());
  204. }
  205. void valueChanged (Value&) override
  206. {
  207. selector.setCurrentColour (getColour());
  208. }
  209. private:
  210. StoredSettings::ColourSelectorWithSwatches selector;
  211. TextButton defaultButton;
  212. Value colourValue;
  213. Colour defaultColour;
  214. };
  215. //==============================================================================
  216. /**
  217. A component that shows a colour swatch with hex ARGB value, and which pops up
  218. a colour selector when you click it.
  219. */
  220. class ColourEditorComponent : public Component,
  221. public Value::Listener
  222. {
  223. public:
  224. ColourEditorComponent (UndoManager* um, const Value& colour,
  225. Colour defaultCol, const bool canReset)
  226. : undoManager (um), colourValue (colour), defaultColour (defaultCol),
  227. canResetToDefault (canReset)
  228. {
  229. colourValue.addListener (this);
  230. }
  231. void paint (Graphics& g)
  232. {
  233. const Colour colour (getColour());
  234. g.fillAll (Colours::grey);
  235. g.fillCheckerBoard (getLocalBounds().reduced (2),
  236. 10, 10,
  237. Colour (0xffdddddd).overlaidWith (colour),
  238. Colour (0xffffffff).overlaidWith (colour));
  239. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  240. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  241. g.drawFittedText (colour.toDisplayString (true), getLocalBounds().reduced (2, 1),
  242. Justification::centred, 1);
  243. }
  244. Colour getColour() const
  245. {
  246. if (colourValue.toString().isEmpty())
  247. return defaultColour;
  248. return Colour::fromString (colourValue.toString());
  249. }
  250. void setColour (Colour newColour)
  251. {
  252. if (getColour() != newColour)
  253. {
  254. if (newColour == defaultColour && canResetToDefault)
  255. colourValue = var::null;
  256. else
  257. colourValue = newColour.toDisplayString (true);
  258. }
  259. }
  260. void resetToDefault()
  261. {
  262. setColour (defaultColour);
  263. }
  264. void refresh()
  265. {
  266. const Colour col (getColour());
  267. if (col != lastColour)
  268. {
  269. lastColour = col;
  270. repaint();
  271. }
  272. }
  273. void mouseDown (const MouseEvent&) override
  274. {
  275. if (undoManager != nullptr)
  276. undoManager->beginNewTransaction();
  277. CallOutBox::launchAsynchronously (new PopupColourSelector (colourValue,
  278. defaultColour,
  279. canResetToDefault),
  280. getScreenBounds(), nullptr);
  281. }
  282. void valueChanged (Value&) override
  283. {
  284. refresh();
  285. }
  286. private:
  287. UndoManager* undoManager;
  288. Value colourValue;
  289. Colour lastColour;
  290. const Colour defaultColour;
  291. const bool canResetToDefault;
  292. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourEditorComponent)
  293. };
  294. //==============================================================================
  295. class ColourPropertyComponent : public PropertyComponent
  296. {
  297. public:
  298. ColourPropertyComponent (UndoManager* undoManager, const String& name, const Value& colour,
  299. Colour defaultColour, bool canResetToDefault)
  300. : PropertyComponent (name),
  301. colourEditor (undoManager, colour, defaultColour, canResetToDefault)
  302. {
  303. addAndMakeVisible (colourEditor);
  304. }
  305. void resized() override
  306. {
  307. colourEditor.setBounds (getLookAndFeel().getPropertyComponentContentPosition (*this));
  308. }
  309. void refresh() override {}
  310. protected:
  311. ColourEditorComponent colourEditor;
  312. };
  313. //==============================================================================
  314. class FilePathPropertyComponent : public PropertyComponent
  315. {
  316. public:
  317. /** A Property Component for selecting files or folders.
  318. The user may drag files over the property box, enter the path
  319. manually and/or click the '...' button to open a file selection
  320. dialog box
  321. */
  322. FilePathPropertyComponent (Value valueToControl,
  323. const String& propertyDescription,
  324. bool isDirectory,
  325. const String& wildcards = "*",
  326. const File& rootToUseForRelativePaths = File::nonexistent)
  327. : PropertyComponent (propertyDescription),
  328. innerComp (valueToControl, isDirectory, wildcards, rootToUseForRelativePaths)
  329. {
  330. addAndMakeVisible (innerComp);
  331. }
  332. void refresh() override {} // N/A
  333. private:
  334. struct InnerComponent : public Component,
  335. public FileDragAndDropTarget,
  336. private Button::Listener
  337. {
  338. InnerComponent (Value v, bool isDir, const String& wc, const File& rt)
  339. : value (v),
  340. isDirectory (isDir),
  341. highlightForDragAndDrop (false),
  342. wildcards (wc),
  343. root (rt),
  344. button ("...")
  345. {
  346. addAndMakeVisible (textbox);
  347. textbox.getTextValue().referTo (value);
  348. addAndMakeVisible (button);
  349. button.addListener (this);
  350. }
  351. void paintOverChildren (Graphics& g) override
  352. {
  353. if (highlightForDragAndDrop)
  354. {
  355. g.setColour (Colours::green.withAlpha (0.1f));
  356. g.fillRect (getLocalBounds());
  357. }
  358. }
  359. void resized() override
  360. {
  361. juce::Rectangle<int> r (getLocalBounds());
  362. button.setBounds (r.removeFromRight (24));
  363. textbox.setBounds (r);
  364. }
  365. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  366. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  367. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  368. void filesDropped (const StringArray& files, int, int) override
  369. {
  370. const File firstFile (files[0]);
  371. if (isDirectory)
  372. setTo (firstFile.isDirectory() ? firstFile
  373. : firstFile.getParentDirectory());
  374. else
  375. setTo (firstFile);
  376. }
  377. void buttonClicked (Button*) override
  378. {
  379. const File currentFile (root.getChildFile (value.toString()));
  380. if (isDirectory)
  381. {
  382. FileChooser chooser ("Select directory", currentFile);
  383. if (chooser.browseForDirectory())
  384. setTo (chooser.getResult());
  385. }
  386. else
  387. {
  388. FileChooser chooser ("Select file", currentFile, wildcards);
  389. if (chooser.browseForFileToOpen())
  390. setTo (chooser.getResult());
  391. }
  392. }
  393. void setTo (const File& f)
  394. {
  395. value = (root == File::nonexistent) ? f.getFullPathName()
  396. : f.getRelativePathFrom (root);
  397. }
  398. Value value;
  399. bool isDirectory, highlightForDragAndDrop;
  400. String wildcards;
  401. File root;
  402. TextEditor textbox;
  403. TextButton button;
  404. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InnerComponent)
  405. };
  406. InnerComponent innerComp; // Used so that the PropertyComponent auto first-child positioning works
  407. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  408. };
  409. #endif // JUCER_MISCUTILITIES_H_INCLUDED