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.

366 lines
11KB

  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. int64 hashCode64 (const String& s)
  21. {
  22. MD5 md5 (s.toUTF8());
  23. const uint64* const m = reinterpret_cast <const uint64*> (md5.getChecksumDataArray());
  24. return (int64) ByteOrder::swapIfBigEndian (m[0] ^ m[1]);
  25. }
  26. String createAlphaNumericUID()
  27. {
  28. String uid;
  29. static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  30. Random r;
  31. uid << chars [r.nextInt (52)]; // make sure the first character is always a letter
  32. for (int i = 5; --i >= 0;)
  33. {
  34. r.setSeedRandomly();
  35. uid << chars [r.nextInt (numElementsInArray (chars))];
  36. }
  37. return uid;
  38. }
  39. String randomHexString (Random& random, int numChars)
  40. {
  41. String s;
  42. const char hexChars[] = "0123456789ABCDEF";
  43. while (--numChars >= 0)
  44. s << hexChars [random.nextInt (numElementsInArray (hexChars))];
  45. return s;
  46. }
  47. String hexString8Digits (int value)
  48. {
  49. return String::toHexString (value).paddedLeft ('0', 8);
  50. }
  51. String createGUID (const String& seed)
  52. {
  53. String guid;
  54. Random r (hashCode64 (seed + "_jucersalt"));
  55. guid << "{" << randomHexString (r, 8); // (written as separate statements to enforce the order of execution)
  56. guid << "-" << randomHexString (r, 4);
  57. guid << "-" << randomHexString (r, 4);
  58. guid << "-" << randomHexString (r, 4);
  59. guid << "-" << randomHexString (r, 12) << "}";
  60. return guid;
  61. }
  62. String escapeSpaces (const String& s)
  63. {
  64. return s.replace (" ", "\\ ");
  65. }
  66. //==============================================================================
  67. StringPairArray parsePreprocessorDefs (const String& text)
  68. {
  69. StringPairArray result;
  70. String::CharPointerType s (text.getCharPointer());
  71. while (! s.isEmpty())
  72. {
  73. String token, value;
  74. s = s.findEndOfWhitespace();
  75. while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace())
  76. token << s.getAndAdvance();
  77. s = s.findEndOfWhitespace();
  78. if (*s == '=')
  79. {
  80. ++s;
  81. s = s.findEndOfWhitespace();
  82. while ((! s.isEmpty()) && ! s.isWhitespace())
  83. {
  84. if (*s == ',')
  85. {
  86. ++s;
  87. break;
  88. }
  89. if (*s == '\\' && (s[1] == ' ' || s[1] == ','))
  90. ++s;
  91. value << s.getAndAdvance();
  92. }
  93. }
  94. if (token.isNotEmpty())
  95. result.set (token, value);
  96. }
  97. return result;
  98. }
  99. StringPairArray mergePreprocessorDefs (StringPairArray inheritedDefs, const StringPairArray& overridingDefs)
  100. {
  101. for (int i = 0; i < overridingDefs.size(); ++i)
  102. inheritedDefs.set (overridingDefs.getAllKeys()[i], overridingDefs.getAllValues()[i]);
  103. return inheritedDefs;
  104. }
  105. String createGCCPreprocessorFlags (const StringPairArray& defs)
  106. {
  107. String s;
  108. for (int i = 0; i < defs.size(); ++i)
  109. {
  110. String def (defs.getAllKeys()[i]);
  111. const String value (defs.getAllValues()[i]);
  112. if (value.isNotEmpty())
  113. def << "=" << value;
  114. s += " -D " + def.quoted();
  115. }
  116. return s;
  117. }
  118. String replacePreprocessorDefs (const StringPairArray& definitions, String sourceString)
  119. {
  120. for (int i = 0; i < definitions.size(); ++i)
  121. {
  122. const String key (definitions.getAllKeys()[i]);
  123. const String value (definitions.getAllValues()[i]);
  124. sourceString = sourceString.replace ("${" + key + "}", value);
  125. }
  126. return sourceString;
  127. }
  128. //==============================================================================
  129. void autoScrollForMouseEvent (const MouseEvent& e, bool scrollX, bool scrollY)
  130. {
  131. Viewport* const viewport = e.eventComponent->findParentComponentOfClass ((Viewport*) 0);
  132. if (viewport != nullptr)
  133. {
  134. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  135. viewport->autoScroll (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 8, 16);
  136. }
  137. }
  138. void drawComponentPlaceholder (Graphics& g, int w, int h, const String& text)
  139. {
  140. g.fillAll (Colours::white.withAlpha (0.4f));
  141. g.setColour (Colours::grey);
  142. g.drawRect (0, 0, w, h);
  143. g.drawLine (0.5f, 0.5f, w - 0.5f, h - 0.5f);
  144. g.drawLine (0.5f, h - 0.5f, w - 0.5f, 0.5f);
  145. g.setColour (Colours::black);
  146. g.setFont (11.0f);
  147. g.drawFittedText (text, 2, 2, w - 4, h - 4, Justification::centredTop, 2);
  148. }
  149. void drawRecessedShadows (Graphics& g, int w, int h, int shadowSize)
  150. {
  151. ColourGradient cg (Colours::black.withAlpha (0.15f), 0, 0,
  152. Colours::transparentBlack, 0, (float) shadowSize, false);
  153. cg.addColour (0.4, Colours::black.withAlpha (0.07f));
  154. cg.addColour (0.6, Colours::black.withAlpha (0.02f));
  155. g.setGradientFill (cg);
  156. g.fillRect (0, 0, w, shadowSize);
  157. cg.point1.setXY (0.0f, (float) h);
  158. cg.point2.setXY (0.0f, (float) h - shadowSize);
  159. g.setGradientFill (cg);
  160. g.fillRect (0, h - shadowSize, w, shadowSize);
  161. cg.point1.setXY (0.0f, 0.0f);
  162. cg.point2.setXY ((float) shadowSize, 0.0f);
  163. g.setGradientFill (cg);
  164. g.fillRect (0, 0, shadowSize, h);
  165. cg.point1.setXY ((float) w, 0.0f);
  166. cg.point2.setXY ((float) w - shadowSize, 0.0f);
  167. g.setGradientFill (cg);
  168. g.fillRect (w - shadowSize, 0, shadowSize, h);
  169. }
  170. //==============================================================================
  171. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex)
  172. {
  173. startIndex = jmax (0, startIndex);
  174. while (startIndex < lines.size())
  175. {
  176. if (lines[startIndex].trimStart().startsWithIgnoreCase (text))
  177. return startIndex;
  178. ++startIndex;
  179. }
  180. return -1;
  181. }
  182. //==============================================================================
  183. PropertyPanelWithTooltips::PropertyPanelWithTooltips()
  184. : lastComp (nullptr)
  185. {
  186. addAndMakeVisible (&panel);
  187. startTimer (150);
  188. }
  189. PropertyPanelWithTooltips::~PropertyPanelWithTooltips()
  190. {
  191. }
  192. void PropertyPanelWithTooltips::paint (Graphics& g)
  193. {
  194. g.setColour (Colour::greyLevel (0.15f));
  195. g.setFont (13.0f);
  196. TextLayout tl;
  197. tl.appendText (lastTip, Font (14.0f));
  198. tl.layout (getWidth() - 10, Justification::left, true); // try to make it look nice
  199. if (tl.getNumLines() > 3)
  200. tl.layout (getWidth() - 10, Justification::left, false); // too big, so just squash it in..
  201. Rectangle<int> r (getTipArea());
  202. tl.drawWithin (g, r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  203. Justification::bottomLeft);
  204. }
  205. void PropertyPanelWithTooltips::resized()
  206. {
  207. panel.setBounds (0, 0, getWidth(), jmax (getHeight() - 60, proportionOfHeight (0.6f)));
  208. }
  209. Rectangle<int> PropertyPanelWithTooltips::getTipArea() const
  210. {
  211. return Rectangle<int> (5, panel.getBottom() - 50, getWidth() - 10,
  212. getHeight() - (panel.getBottom() - 50) - 4);
  213. }
  214. void PropertyPanelWithTooltips::timerCallback()
  215. {
  216. Component* newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  217. if (newComp != nullptr && newComp->getTopLevelComponent() != getTopLevelComponent())
  218. newComp = nullptr;
  219. if (newComp != lastComp)
  220. {
  221. lastComp = newComp;
  222. String newTip (findTip (newComp));
  223. if (newTip != lastTip)
  224. {
  225. lastTip = newTip;
  226. repaint (getTipArea());
  227. }
  228. }
  229. }
  230. String PropertyPanelWithTooltips::findTip (Component* c)
  231. {
  232. while (c != nullptr && c != this)
  233. {
  234. TooltipClient* const tc = dynamic_cast <TooltipClient*> (c);
  235. if (tc != nullptr)
  236. {
  237. const String tip (tc->getTooltip());
  238. if (tip.isNotEmpty())
  239. return tip;
  240. }
  241. c = c->getParentComponent();
  242. }
  243. return String::empty;
  244. }
  245. //==============================================================================
  246. FloatingLabelComponent::FloatingLabelComponent()
  247. : font (10.0f)
  248. {
  249. setInterceptsMouseClicks (false, false);
  250. }
  251. void FloatingLabelComponent::remove()
  252. {
  253. if (getParentComponent() != nullptr)
  254. getParentComponent()->removeChildComponent (this);
  255. }
  256. void FloatingLabelComponent::update (Component* parent, const String& text, const Colour& textColour, int x, int y, bool toRight, bool below)
  257. {
  258. colour = textColour;
  259. Rectangle<int> r;
  260. if (text != getName())
  261. {
  262. setName (text);
  263. glyphs.clear();
  264. glyphs.addJustifiedText (font, text, 0, 0, 200.0f, Justification::left);
  265. glyphs.justifyGlyphs (0, std::numeric_limits<int>::max(), 0, 0, 1000, 1000, Justification::topLeft);
  266. r = glyphs.getBoundingBox (0, std::numeric_limits<int>::max(), false)
  267. .getSmallestIntegerContainer().expanded (1, 1);
  268. }
  269. else
  270. {
  271. r = getLocalBounds();
  272. }
  273. r.setPosition (x + (toRight ? 3 : -(r.getWidth() + 3)), y + (below ? 2 : -(r.getHeight() + 2)));
  274. setBounds (r);
  275. parent->addAndMakeVisible (this);
  276. }
  277. void FloatingLabelComponent::paint (Graphics& g)
  278. {
  279. g.setFont (font);
  280. g.setColour (Colours::white.withAlpha (0.5f));
  281. g.fillRoundedRectangle (0, 0, (float) getWidth(), (float) getHeight(), 3);
  282. g.setColour (colour);
  283. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  284. }