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.

341 lines
10.0KB

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