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.

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