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.

494 lines
14KB

  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. #include "../jucer_Headers.h"
  19. //==============================================================================
  20. String createAlphaNumericUID()
  21. {
  22. String uid;
  23. const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  24. Random r;
  25. uid << chars [r.nextInt (52)]; // make sure the first character is always a letter
  26. for (int i = 5; --i >= 0;)
  27. {
  28. r.setSeedRandomly();
  29. uid << chars [r.nextInt (62)];
  30. }
  31. return uid;
  32. }
  33. String hexString8Digits (int value)
  34. {
  35. return String::toHexString (value).paddedLeft ('0', 8);
  36. }
  37. String createGUID (const String& seed)
  38. {
  39. const String hex (MD5 ((seed + "_guidsalt").toUTF8()).toHexString().toUpperCase());
  40. return "{" + hex.substring (0, 8)
  41. + "-" + hex.substring (8, 12)
  42. + "-" + hex.substring (12, 16)
  43. + "-" + hex.substring (16, 20)
  44. + "-" + hex.substring (20, 32)
  45. + "}";
  46. }
  47. String escapeSpaces (const String& s)
  48. {
  49. return s.replace (" ", "\\ ");
  50. }
  51. String addQuotesIfContainsSpaces (const String& text)
  52. {
  53. return (text.containsChar (' ') && ! text.isQuotedString()) ? text.quoted() : text;
  54. }
  55. void setValueIfVoid (Value value, const var& defaultValue)
  56. {
  57. if (value.getValue().isVoid())
  58. value = defaultValue;
  59. }
  60. //==============================================================================
  61. StringPairArray parsePreprocessorDefs (const String& text)
  62. {
  63. StringPairArray result;
  64. String::CharPointerType s (text.getCharPointer());
  65. while (! s.isEmpty())
  66. {
  67. String token, value;
  68. s = s.findEndOfWhitespace();
  69. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  70. token << s.getAndAdvance();
  71. s = s.findEndOfWhitespace();
  72. if (*s == '=')
  73. {
  74. ++s;
  75. s = s.findEndOfWhitespace();
  76. while ((! s.isEmpty()) && ! s.isWhitespace())
  77. {
  78. if (*s == ',')
  79. {
  80. ++s;
  81. break;
  82. }
  83. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  84. ++s;
  85. value << s.getAndAdvance();
  86. }
  87. }
  88. if (token.isNotEmpty())
  89. result.set (token, value);
  90. }
  91. return result;
  92. }
  93. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  94. {
  95. for (int i = 0; i < overridingDefs.size(); ++i)
  96. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  97. return inheritedDefs;
  98. }
  99. String createGCCPreprocessorFlags (const StringPairArray& defs)
  100. {
  101. String s;
  102. for (int i = 0; i < defs.size(); ++i)
  103. {
  104. String def (defs.getAllKeys()[i]);
  105. const String value (defs.getAllValues()[i]);
  106. if (value.isNotEmpty())
  107. def << "=" << value;
  108. if (! def.endsWithChar ('"'))
  109. def = def.quoted();
  110. s += " -D " + def;
  111. }
  112. return s;
  113. }
  114. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString)
  115. {
  116. for (int i = 0; i < definitions.size(); ++i)
  117. {
  118. const String key (definitions.getAllKeys()[i]);
  119. const String value (definitions.getAllValues()[i]);
  120. sourceString = sourceString.replace ("${" + key + "}", value);
  121. }
  122. return sourceString;
  123. }
  124. //==============================================================================
  125. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  126. {
  127. Viewport* const viewport = e.eventComponent->findParentComponentOfClass<Viewport>();
  128. if (viewport != nullptr)
  129. {
  130. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  131. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  132. }
  133. }
  134. void drawComponentPlaceholder (Graphics& g, int w, int h, const String& text)
  135. {
  136. g.fillAll (Colours::white.withAlpha (0.4f));
  137. g.setColour (Colours::grey);
  138. g.drawRect (0, 0, w, h);
  139. g.drawLine (0.5f, 0.5f, w - 0.5f, h - 0.5f);
  140. g.drawLine (0.5f, h - 0.5f, w - 0.5f, 0.5f);
  141. g.setColour (Colours::black);
  142. g.setFont (11.0f);
  143. g.drawFittedText (text, 2, 2, w - 4, h - 4, Justification::centredTop, 2);
  144. }
  145. void drawRecessedShadows (Graphics& g, int w, int h, int shadowSize)
  146. {
  147. ColourGradient cg (Colours::black.withAlpha (0.15f), 0, 0,
  148. Colours::transparentBlack, 0, (float) shadowSize, false);
  149. cg.addColour (0.4, Colours::black.withAlpha (0.07f));
  150. cg.addColour (0.6, Colours::black.withAlpha (0.02f));
  151. g.setGradientFill (cg);
  152. g.fillRect (0, 0, w, shadowSize);
  153. cg.point1.setXY (0.0f, (float) h);
  154. cg.point2.setXY (0.0f, (float) h - shadowSize);
  155. g.setGradientFill (cg);
  156. g.fillRect (0, h - shadowSize, w, shadowSize);
  157. cg.point1.setXY (0.0f, 0.0f);
  158. cg.point2.setXY ((float) shadowSize, 0.0f);
  159. g.setGradientFill (cg);
  160. g.fillRect (0, 0, shadowSize, h);
  161. cg.point1.setXY ((float) w, 0.0f);
  162. cg.point2.setXY ((float) w - shadowSize, 0.0f);
  163. g.setGradientFill (cg);
  164. g.fillRect (w - shadowSize, 0, shadowSize, h);
  165. }
  166. //==============================================================================
  167. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex)
  168. {
  169. startIndex = jmax (0, startIndex);
  170. while (startIndex < lines.size())
  171. {
  172. if (lines[startIndex].trimStart().startsWithIgnoreCase (text))
  173. return startIndex;
  174. ++startIndex;
  175. }
  176. return -1;
  177. }
  178. //==============================================================================
  179. RolloverHelpComp::RolloverHelpComp()
  180. : lastComp (nullptr)
  181. {
  182. setInterceptsMouseClicks (false, false);
  183. startTimer (150);
  184. }
  185. void RolloverHelpComp::paint (Graphics& g)
  186. {
  187. AttributedString s;
  188. s.setJustification (Justification::centredLeft);
  189. s.append (lastTip, Font (14.0f), Colour::greyLevel (0.15f));
  190. TextLayout tl;
  191. tl.createLayoutWithBalancedLineLengths (s, getWidth() - 10.0f);
  192. if (tl.getNumLines() > 3)
  193. tl.createLayout (s, getWidth() - 10.0f);
  194. tl.draw (g, getLocalBounds().toFloat());
  195. }
  196. void RolloverHelpComp::timerCallback()
  197. {
  198. Component* newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  199. if (newComp != nullptr
  200. && (newComp->getTopLevelComponent() != getTopLevelComponent()
  201. || newComp->isCurrentlyBlockedByAnotherModalComponent()))
  202. newComp = nullptr;
  203. if (newComp != lastComp)
  204. {
  205. lastComp = newComp;
  206. String newTip (findTip (newComp));
  207. if (newTip != lastTip)
  208. {
  209. lastTip = newTip;
  210. repaint();
  211. }
  212. }
  213. }
  214. String RolloverHelpComp::findTip (Component* c)
  215. {
  216. while (c != nullptr)
  217. {
  218. TooltipClient* const tc = dynamic_cast <TooltipClient*> (c);
  219. if (tc != nullptr)
  220. {
  221. const String tip (tc->getTooltip());
  222. if (tip.isNotEmpty())
  223. return tip;
  224. }
  225. c = c->getParentComponent();
  226. }
  227. return String::empty;
  228. }
  229. //==============================================================================
  230. PropertyPanelWithTooltips::PropertyPanelWithTooltips()
  231. {
  232. addAndMakeVisible (&panel);
  233. addAndMakeVisible (&rollover);
  234. }
  235. void PropertyPanelWithTooltips::resized()
  236. {
  237. panel.setBounds (0, 0, getWidth(), jmax (getHeight() - 60, proportionOfHeight (0.6f)));
  238. rollover.setBounds (3, panel.getBottom() - 50, getWidth() - 6,
  239. getHeight() - (panel.getBottom() - 50) - 4);
  240. }
  241. //==============================================================================
  242. FloatingLabelComponent::FloatingLabelComponent()
  243. : font (10.0f)
  244. {
  245. setInterceptsMouseClicks (false, false);
  246. }
  247. void FloatingLabelComponent::remove()
  248. {
  249. if (getParentComponent() != nullptr)
  250. getParentComponent()->removeChildComponent (this);
  251. }
  252. void FloatingLabelComponent::update (Component* parent, const String& text, const Colour& textColour, int x, int y, bool toRight, bool below)
  253. {
  254. colour = textColour;
  255. Rectangle<int> r;
  256. if (text != getName())
  257. {
  258. setName (text);
  259. glyphs.clear();
  260. glyphs.addJustifiedText (font, text, 0, 0, 200.0f, Justification::left);
  261. glyphs.justifyGlyphs (0, std::numeric_limits<int>::max(), 0, 0, 1000, 1000, Justification::topLeft);
  262. r = glyphs.getBoundingBox (0, std::numeric_limits<int>::max(), false)
  263. .getSmallestIntegerContainer().expanded (1, 1);
  264. }
  265. else
  266. {
  267. r = getLocalBounds();
  268. }
  269. r.setPosition (x + (toRight ? 3 : -(r.getWidth() + 3)), y + (below ? 2 : -(r.getHeight() + 2)));
  270. setBounds (r);
  271. parent->addAndMakeVisible (this);
  272. }
  273. void FloatingLabelComponent::paint (Graphics& g)
  274. {
  275. g.setFont (font);
  276. g.setColour (Colours::white.withAlpha (0.5f));
  277. g.fillRoundedRectangle (0, 0, (float) getWidth(), (float) getHeight(), 3);
  278. g.setColour (colour);
  279. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  280. }
  281. //==============================================================================
  282. class UTF8Component : public Component,
  283. private TextEditorListener
  284. {
  285. public:
  286. UTF8Component()
  287. : desc (String::empty,
  288. "Type any string into the box, and it'll be shown below as a portable UTF-8 literal, ready to cut-and-paste into your source-code...")
  289. {
  290. setSize (400, 300);
  291. desc.setBounds ("8, 8, parent.width - 8, 55");
  292. desc.setJustificationType (Justification::centred);
  293. addAndMakeVisible (&desc);
  294. userText.setMultiLine (true, true);
  295. userText.setBounds ("8, 60, parent.width - 8, parent.height / 2 - 4");
  296. addAndMakeVisible (&userText);
  297. userText.addListener (this);
  298. resultText.setMultiLine (true, true);
  299. resultText.setReadOnly (true);
  300. resultText.setBounds ("8, parent.height / 2 + 4, parent.width - 8, parent.height - 8");
  301. addAndMakeVisible (&resultText);
  302. userText.setText (getLastText());
  303. }
  304. void textEditorTextChanged (TextEditor&)
  305. {
  306. update();
  307. }
  308. void textEditorEscapeKeyPressed (TextEditor&)
  309. {
  310. getTopLevelComponent()->exitModalState (0);
  311. }
  312. void update()
  313. {
  314. getLastText() = userText.getText();
  315. resultText.setText (CodeHelpers::stringLiteral (getLastText()), false);
  316. }
  317. private:
  318. Label desc;
  319. TextEditor userText, resultText;
  320. String& getLastText()
  321. {
  322. static String t;
  323. return t;
  324. }
  325. };
  326. void showUTF8ToolWindow()
  327. {
  328. UTF8Component comp;
  329. DialogWindow::showModalDialog ("UTF-8 String Literal Converter", &comp,
  330. nullptr, Colours::white, true, true);
  331. }
  332. bool cancelAnyModalComponents()
  333. {
  334. const int numModal = ModalComponentManager::getInstance()->getNumModalComponents();
  335. for (int i = numModal; --i >= 0;)
  336. if (ModalComponentManager::getInstance()->getModalComponent(i) != nullptr)
  337. ModalComponentManager::getInstance()->getModalComponent(i)->exitModalState (0);
  338. return numModal > 0;
  339. }
  340. //==============================================================================
  341. class AsyncCommandRetrier : public Timer
  342. {
  343. public:
  344. AsyncCommandRetrier (const ApplicationCommandTarget::InvocationInfo& info_)
  345. : info (info_)
  346. {
  347. info.originatingComponent = nullptr;
  348. startTimer (500);
  349. }
  350. void timerCallback()
  351. {
  352. stopTimer();
  353. commandManager->invoke (info, true);
  354. delete this;
  355. }
  356. ApplicationCommandTarget::InvocationInfo info;
  357. JUCE_DECLARE_NON_COPYABLE (AsyncCommandRetrier);
  358. };
  359. bool reinvokeCommandAfterCancellingModalComps (const ApplicationCommandTarget::InvocationInfo& info)
  360. {
  361. if (cancelAnyModalComponents())
  362. {
  363. new AsyncCommandRetrier (info);
  364. return true;
  365. }
  366. return false;
  367. }
  368. //==============================================================================
  369. class CallOutBoxCallback : public ModalComponentManager::Callback
  370. {
  371. public:
  372. CallOutBoxCallback (Component& attachTo, Component* content_)
  373. : content (content_),
  374. callout (*content_, attachTo, attachTo.getTopLevelComponent())
  375. {
  376. callout.setVisible (true);
  377. callout.enterModalState (true, this);
  378. }
  379. void modalStateFinished (int) {}
  380. private:
  381. ScopedPointer<Component> content;
  382. CallOutBox callout;
  383. JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback);
  384. };
  385. void launchAsyncCallOutBox (Component& attachTo, Component* content)
  386. {
  387. new CallOutBoxCallback (attachTo, content);
  388. }