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.

434 lines
12KB

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