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.

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