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.

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