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.

440 lines
13KB

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