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.

514 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. bool escapeKeyPressed() override
  137. {
  138. closeButtonPressed();
  139. return true;
  140. }
  141. private:
  142. String windowPosProperty;
  143. ScopedPointer<Component>& owner;
  144. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FloatingToolWindow)
  145. };
  146. //==============================================================================
  147. class PopupColourSelector : public Component,
  148. public ChangeListener,
  149. public Value::Listener,
  150. public ButtonListener
  151. {
  152. public:
  153. PopupColourSelector (const Value& colour,
  154. Colour defaultCol,
  155. const bool canResetToDefault)
  156. : defaultButton ("Reset to Default"),
  157. colourValue (colour),
  158. defaultColour (defaultCol)
  159. {
  160. addAndMakeVisible (selector);
  161. selector.setName ("Colour");
  162. selector.setCurrentColour (getColour());
  163. selector.addChangeListener (this);
  164. if (canResetToDefault)
  165. {
  166. addAndMakeVisible (defaultButton);
  167. defaultButton.addListener (this);
  168. }
  169. colourValue.addListener (this);
  170. setSize (300, 400);
  171. }
  172. void resized() override
  173. {
  174. if (defaultButton.isVisible())
  175. {
  176. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  177. defaultButton.changeWidthToFitText (22);
  178. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  179. }
  180. else
  181. {
  182. selector.setBounds (getLocalBounds());
  183. }
  184. }
  185. Colour getColour() const
  186. {
  187. if (colourValue.toString().isEmpty())
  188. return defaultColour;
  189. return Colour::fromString (colourValue.toString());
  190. }
  191. void setColour (Colour newColour)
  192. {
  193. if (getColour() != newColour)
  194. {
  195. if (newColour == defaultColour && defaultButton.isVisible())
  196. colourValue = var();
  197. else
  198. colourValue = newColour.toDisplayString (true);
  199. }
  200. }
  201. void buttonClicked (Button*) override
  202. {
  203. setColour (defaultColour);
  204. selector.setCurrentColour (defaultColour);
  205. }
  206. void changeListenerCallback (ChangeBroadcaster*) override
  207. {
  208. if (selector.getCurrentColour() != getColour())
  209. setColour (selector.getCurrentColour());
  210. }
  211. void valueChanged (Value&) override
  212. {
  213. selector.setCurrentColour (getColour());
  214. }
  215. private:
  216. StoredSettings::ColourSelectorWithSwatches selector;
  217. TextButton defaultButton;
  218. Value colourValue;
  219. Colour defaultColour;
  220. };
  221. //==============================================================================
  222. /**
  223. A component that shows a colour swatch with hex ARGB value, and which pops up
  224. a colour selector when you click it.
  225. */
  226. class ColourEditorComponent : public Component,
  227. public Value::Listener
  228. {
  229. public:
  230. ColourEditorComponent (UndoManager* um, const Value& colour,
  231. Colour defaultCol, const bool canReset)
  232. : undoManager (um), colourValue (colour), defaultColour (defaultCol),
  233. canResetToDefault (canReset)
  234. {
  235. colourValue.addListener (this);
  236. }
  237. void paint (Graphics& g) override
  238. {
  239. const Colour colour (getColour());
  240. g.fillAll (Colours::grey);
  241. g.fillCheckerBoard (getLocalBounds().reduced (2),
  242. 10, 10,
  243. Colour (0xffdddddd).overlaidWith (colour),
  244. Colour (0xffffffff).overlaidWith (colour));
  245. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  246. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  247. g.drawFittedText (colour.toDisplayString (true), getLocalBounds().reduced (2, 1),
  248. Justification::centred, 1);
  249. }
  250. Colour getColour() const
  251. {
  252. if (colourValue.toString().isEmpty())
  253. return defaultColour;
  254. return Colour::fromString (colourValue.toString());
  255. }
  256. void setColour (Colour newColour)
  257. {
  258. if (getColour() != newColour)
  259. {
  260. if (newColour == defaultColour && canResetToDefault)
  261. colourValue = var::null;
  262. else
  263. colourValue = newColour.toDisplayString (true);
  264. }
  265. }
  266. void resetToDefault()
  267. {
  268. setColour (defaultColour);
  269. }
  270. void refresh()
  271. {
  272. const Colour col (getColour());
  273. if (col != lastColour)
  274. {
  275. lastColour = col;
  276. repaint();
  277. }
  278. }
  279. void mouseDown (const MouseEvent&) override
  280. {
  281. if (undoManager != nullptr)
  282. undoManager->beginNewTransaction();
  283. CallOutBox::launchAsynchronously (new PopupColourSelector (colourValue,
  284. defaultColour,
  285. canResetToDefault),
  286. getScreenBounds(), nullptr);
  287. }
  288. void valueChanged (Value&) override
  289. {
  290. refresh();
  291. }
  292. private:
  293. UndoManager* undoManager;
  294. Value colourValue;
  295. Colour lastColour;
  296. const Colour defaultColour;
  297. const bool canResetToDefault;
  298. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourEditorComponent)
  299. };
  300. //==============================================================================
  301. class ColourPropertyComponent : public PropertyComponent
  302. {
  303. public:
  304. ColourPropertyComponent (UndoManager* undoManager, const String& name, const Value& colour,
  305. Colour defaultColour, bool canResetToDefault)
  306. : PropertyComponent (name),
  307. colourEditor (undoManager, colour, defaultColour, canResetToDefault)
  308. {
  309. addAndMakeVisible (colourEditor);
  310. }
  311. void resized() override
  312. {
  313. colourEditor.setBounds (getLookAndFeel().getPropertyComponentContentPosition (*this));
  314. }
  315. void refresh() override {}
  316. protected:
  317. ColourEditorComponent colourEditor;
  318. };
  319. //==============================================================================
  320. class FilePathPropertyComponent : public PropertyComponent
  321. {
  322. public:
  323. /** A Property Component for selecting files or folders.
  324. The user may drag files over the property box, enter the path
  325. manually and/or click the '...' button to open a file selection
  326. dialog box
  327. */
  328. FilePathPropertyComponent (Value valueToControl,
  329. const String& propertyDescription,
  330. bool isDirectory,
  331. const String& wildcards = "*",
  332. const File& rootToUseForRelativePaths = File::nonexistent)
  333. : PropertyComponent (propertyDescription),
  334. innerComp (valueToControl, isDirectory, wildcards, rootToUseForRelativePaths)
  335. {
  336. addAndMakeVisible (innerComp);
  337. }
  338. void refresh() override {} // N/A
  339. private:
  340. struct InnerComponent : public Component,
  341. public FileDragAndDropTarget,
  342. private Button::Listener
  343. {
  344. InnerComponent (Value v, bool isDir, const String& wc, const File& rt)
  345. : value (v),
  346. isDirectory (isDir),
  347. highlightForDragAndDrop (false),
  348. wildcards (wc),
  349. root (rt),
  350. button ("...")
  351. {
  352. addAndMakeVisible (textbox);
  353. textbox.getTextValue().referTo (value);
  354. addAndMakeVisible (button);
  355. button.addListener (this);
  356. }
  357. void paintOverChildren (Graphics& g) override
  358. {
  359. if (highlightForDragAndDrop)
  360. {
  361. g.setColour (Colours::green.withAlpha (0.1f));
  362. g.fillRect (getLocalBounds());
  363. }
  364. }
  365. void resized() override
  366. {
  367. juce::Rectangle<int> r (getLocalBounds());
  368. button.setBounds (r.removeFromRight (24));
  369. textbox.setBounds (r);
  370. }
  371. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  372. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  373. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  374. void filesDropped (const StringArray& files, int, int) override
  375. {
  376. const File firstFile (files[0]);
  377. if (isDirectory)
  378. setTo (firstFile.isDirectory() ? firstFile
  379. : firstFile.getParentDirectory());
  380. else
  381. setTo (firstFile);
  382. }
  383. void buttonClicked (Button*) override
  384. {
  385. const File currentFile (root.getChildFile (value.toString()));
  386. if (isDirectory)
  387. {
  388. FileChooser chooser ("Select directory", currentFile);
  389. if (chooser.browseForDirectory())
  390. setTo (chooser.getResult());
  391. }
  392. else
  393. {
  394. FileChooser chooser ("Select file", currentFile, wildcards);
  395. if (chooser.browseForFileToOpen())
  396. setTo (chooser.getResult());
  397. }
  398. }
  399. void setTo (const File& f)
  400. {
  401. value = (root == File::nonexistent) ? f.getFullPathName()
  402. : f.getRelativePathFrom (root);
  403. }
  404. Value value;
  405. bool isDirectory, highlightForDragAndDrop;
  406. String wildcards;
  407. File root;
  408. TextEditor textbox;
  409. TextButton button;
  410. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InnerComponent)
  411. };
  412. InnerComponent innerComp; // Used so that the PropertyComponent auto first-child positioning works
  413. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  414. };
  415. #endif // JUCER_MISCUTILITIES_H_INCLUDED