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.

410 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. g.setColour (Colour::greyLevel (0.15f));
  180. g.setFont (13.0f);
  181. TextLayout tl;
  182. tl.appendText (lastTip, Font (14.0f));
  183. tl.layout (getWidth() - 10, Justification::left, true); // try to make it look nice
  184. if (tl.getNumLines() > 3)
  185. tl.layout (getWidth() - 10, Justification::left, false); // too big, so just squash it in..
  186. Rectangle<int> r (getTipArea());
  187. tl.drawWithin (g, r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  188. Justification::bottomLeft);
  189. }
  190. void PropertyPanelWithTooltips::resized()
  191. {
  192. panel.setBounds (0, 0, getWidth(), jmax (getHeight() - 60, proportionOfHeight (0.6f)));
  193. }
  194. Rectangle<int> PropertyPanelWithTooltips::getTipArea() const
  195. {
  196. return Rectangle<int> (5, panel.getBottom() - 50, getWidth() - 10,
  197. getHeight() - (panel.getBottom() - 50) - 4);
  198. }
  199. void PropertyPanelWithTooltips::timerCallback()
  200. {
  201. Component* newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  202. if (newComp != nullptr && newComp->getTopLevelComponent() != getTopLevelComponent())
  203. newComp = nullptr;
  204. if (newComp != lastComp)
  205. {
  206. lastComp = newComp;
  207. String newTip (findTip (newComp));
  208. if (newTip != lastTip)
  209. {
  210. lastTip = newTip;
  211. repaint (getTipArea());
  212. }
  213. }
  214. }
  215. String PropertyPanelWithTooltips::findTip (Component* c)
  216. {
  217. while (c != nullptr && c != this)
  218. {
  219. TooltipClient* const tc = dynamic_cast <TooltipClient*> (c);
  220. if (tc != nullptr)
  221. {
  222. const String tip (tc->getTooltip());
  223. if (tip.isNotEmpty())
  224. return tip;
  225. }
  226. c = c->getParentComponent();
  227. }
  228. return String::empty;
  229. }
  230. //==============================================================================
  231. FloatingLabelComponent::FloatingLabelComponent()
  232. : font (10.0f)
  233. {
  234. setInterceptsMouseClicks (false, false);
  235. }
  236. void FloatingLabelComponent::remove()
  237. {
  238. if (getParentComponent() != nullptr)
  239. getParentComponent()->removeChildComponent (this);
  240. }
  241. void FloatingLabelComponent::update (Component* parent, const String& text, const Colour& textColour, int x, int y, bool toRight, bool below)
  242. {
  243. colour = textColour;
  244. Rectangle<int> r;
  245. if (text != getName())
  246. {
  247. setName (text);
  248. glyphs.clear();
  249. glyphs.addJustifiedText (font, text, 0, 0, 200.0f, Justification::left);
  250. glyphs.justifyGlyphs (0, std::numeric_limits<int>::max(), 0, 0, 1000, 1000, Justification::topLeft);
  251. r = glyphs.getBoundingBox (0, std::numeric_limits<int>::max(), false)
  252. .getSmallestIntegerContainer().expanded (1, 1);
  253. }
  254. else
  255. {
  256. r = getLocalBounds();
  257. }
  258. r.setPosition (x + (toRight ? 3 : -(r.getWidth() + 3)), y + (below ? 2 : -(r.getHeight() + 2)));
  259. setBounds (r);
  260. parent->addAndMakeVisible (this);
  261. }
  262. void FloatingLabelComponent::paint (Graphics& g)
  263. {
  264. g.setFont (font);
  265. g.setColour (Colours::white.withAlpha (0.5f));
  266. g.fillRoundedRectangle (0, 0, (float) getWidth(), (float) getHeight(), 3);
  267. g.setColour (colour);
  268. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  269. }
  270. //==============================================================================
  271. class UTF8Component : public Component,
  272. private TextEditorListener
  273. {
  274. public:
  275. UTF8Component()
  276. : desc (String::empty,
  277. "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...")
  278. {
  279. setSize (400, 300);
  280. desc.setBounds ("8, 8, parent.width - 8, 55");
  281. desc.setJustificationType (Justification::centred);
  282. addAndMakeVisible (&desc);
  283. userText.setMultiLine (true, true);
  284. userText.setBounds ("8, 60, parent.width - 8, parent.height / 2 - 4");
  285. addAndMakeVisible (&userText);
  286. userText.addListener (this);
  287. resultText.setMultiLine (true, true);
  288. resultText.setReadOnly (true);
  289. resultText.setBounds ("8, parent.height / 2 + 4, parent.width - 8, parent.height - 8");
  290. addAndMakeVisible (&resultText);
  291. userText.setText (getLastText());
  292. }
  293. void textEditorTextChanged (TextEditor&)
  294. {
  295. update();
  296. }
  297. void textEditorEscapeKeyPressed (TextEditor&)
  298. {
  299. getTopLevelComponent()->exitModalState (0);
  300. }
  301. void update()
  302. {
  303. getLastText() = userText.getText();
  304. resultText.setText (CodeHelpers::stringLiteral (getLastText()), false);
  305. }
  306. private:
  307. Label desc;
  308. TextEditor userText, resultText;
  309. String& getLastText()
  310. {
  311. static String t;
  312. return t;
  313. }
  314. };
  315. void showUTF8ToolWindow()
  316. {
  317. UTF8Component comp;
  318. DialogWindow::showModalDialog ("UTF-8 String Literal Converter", &comp,
  319. nullptr, Colours::white, true, true);
  320. }