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.

357 lines
10KB

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