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.

479 lines
14KB

  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. for (int i = 7; --i >= 0;)
  30. {
  31. r.setSeedRandomly();
  32. uid << chars [r.nextInt (numElementsInArray (chars))];
  33. }
  34. return uid;
  35. }
  36. const String randomHexString (Random& random, int numChars)
  37. {
  38. String s;
  39. const char hexChars[] = "0123456789ABCDEF";
  40. while (--numChars >= 0)
  41. s << hexChars [random.nextInt (16)];
  42. return s;
  43. }
  44. const String hexString8Digits (int value)
  45. {
  46. return String::toHexString (value).paddedLeft ('0', 8);
  47. }
  48. const String createGUID (const String& seed)
  49. {
  50. String guid;
  51. Random r (hashCode64 (seed + "_jucersalt"));
  52. guid << "{" << randomHexString (r, 8); // (written as separate statements to enforce the order of execution)
  53. guid << "-" << randomHexString (r, 4);
  54. guid << "-" << randomHexString (r, 4);
  55. guid << "-" << randomHexString (r, 4);
  56. guid << "-" << randomHexString (r, 12) << "}";
  57. return guid;
  58. }
  59. //==============================================================================
  60. static void skipWhitespace (const String& s, int& i)
  61. {
  62. while (CharacterFunctions::isWhitespace (s[i]))
  63. ++i;
  64. }
  65. const StringPairArray parsePreprocessorDefs (const String& s)
  66. {
  67. StringPairArray result;
  68. int i = 0;
  69. while (s[i] != 0)
  70. {
  71. String token, value;
  72. skipWhitespace (s, i);
  73. while (s[i] != 0 && s[i] != '=' && ! CharacterFunctions::isWhitespace (s[i]))
  74. token << s[i++];
  75. skipWhitespace (s, i);
  76. if (s[i] == '=')
  77. {
  78. ++i;
  79. skipWhitespace (s, i);
  80. while (s[i] != 0 && ! CharacterFunctions::isWhitespace (s[i]))
  81. {
  82. if (s[i] == ',')
  83. {
  84. ++i;
  85. break;
  86. }
  87. if (s[i] == '\\' && (s[i + 1] == ' ' || s[i + 1] == ','))
  88. ++i;
  89. value << s[i++];
  90. }
  91. }
  92. if (token.isNotEmpty())
  93. result.set (token, value);
  94. }
  95. return result;
  96. }
  97. const 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. const 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 != 0)
  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 (0)
  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. tl.drawWithin (g, 5, panel.getBottom() + 2, getWidth() - 10,
  187. getHeight() - panel.getBottom() - 4,
  188. Justification::centredLeft);
  189. }
  190. void PropertyPanelWithTooltips::resized()
  191. {
  192. panel.setBounds (0, 0, getWidth(), jmax (getHeight() - 60, proportionOfHeight (0.6f)));
  193. }
  194. void PropertyPanelWithTooltips::timerCallback()
  195. {
  196. Component* newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  197. if (newComp != 0 && newComp->getTopLevelComponent() != getTopLevelComponent())
  198. newComp = 0;
  199. if (newComp != lastComp)
  200. {
  201. lastComp = newComp;
  202. String newTip (findTip (newComp));
  203. if (newTip != lastTip)
  204. {
  205. lastTip = newTip;
  206. repaint (0, panel.getBottom(), getWidth(), getHeight());
  207. }
  208. }
  209. }
  210. const String PropertyPanelWithTooltips::findTip (Component* c)
  211. {
  212. while (c != 0 && c != this)
  213. {
  214. TooltipClient* const tc = dynamic_cast <TooltipClient*> (c);
  215. if (tc != 0)
  216. {
  217. const String tip (tc->getTooltip());
  218. if (tip.isNotEmpty())
  219. return tip;
  220. }
  221. c = c->getParentComponent();
  222. }
  223. return String::empty;
  224. }
  225. //==============================================================================
  226. FloatingLabelComponent::FloatingLabelComponent()
  227. : font (10.0f)
  228. {
  229. setInterceptsMouseClicks (false, false);
  230. }
  231. void FloatingLabelComponent::remove()
  232. {
  233. if (getParentComponent() != 0)
  234. getParentComponent()->removeChildComponent (this);
  235. }
  236. void FloatingLabelComponent::update (Component* parent, const String& text, const Colour& textColour, int x, int y, bool toRight, bool below)
  237. {
  238. colour = textColour;
  239. Rectangle<int> r;
  240. if (text != getName())
  241. {
  242. setName (text);
  243. glyphs.clear();
  244. glyphs.addJustifiedText (font, text, 0, 0, 200.0f, Justification::left);
  245. glyphs.justifyGlyphs (0, std::numeric_limits<int>::max(), 0, 0, 1000, 1000, Justification::topLeft);
  246. r = glyphs.getBoundingBox (0, std::numeric_limits<int>::max(), false)
  247. .getSmallestIntegerContainer().expanded (1, 1);
  248. }
  249. else
  250. {
  251. r = getLocalBounds();
  252. }
  253. r.setPosition (x + (toRight ? 3 : -(r.getWidth() + 3)), y + (below ? 2 : -(r.getHeight() + 2)));
  254. setBounds (r);
  255. parent->addAndMakeVisible (this);
  256. }
  257. void FloatingLabelComponent::paint (Graphics& g)
  258. {
  259. g.setFont (font);
  260. g.setColour (Colours::white.withAlpha (0.5f));
  261. g.fillRoundedRectangle (0, 0, (float) getWidth(), (float) getHeight(), 3);
  262. g.setColour (colour);
  263. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  264. }
  265. //==============================================================================
  266. RelativeRectangleLayoutManager::RelativeRectangleLayoutManager (Component* parentComponent)
  267. : parent (parentComponent)
  268. {
  269. parent->addComponentListener (this);
  270. }
  271. RelativeRectangleLayoutManager::~RelativeRectangleLayoutManager()
  272. {
  273. parent->removeComponentListener (this);
  274. for (int i = components.size(); --i >= 0;)
  275. components.getUnchecked(i)->component->removeComponentListener (this);
  276. }
  277. void RelativeRectangleLayoutManager::setMarker (const String& name, const RelativeCoordinate& coord)
  278. {
  279. for (int i = markers.size(); --i >= 0;)
  280. {
  281. MarkerPosition* m = markers.getUnchecked(i);
  282. if (m->markerName == name)
  283. {
  284. m->position = coord;
  285. applyLayout();
  286. return;
  287. }
  288. }
  289. markers.add (new MarkerPosition (name, coord));
  290. applyLayout();
  291. }
  292. void RelativeRectangleLayoutManager::setComponentBounds (Component* comp, const String& name, const RelativeRectangle& coords)
  293. {
  294. jassert (comp != 0);
  295. // All the components that this layout manages must be inside the parent component..
  296. jassert (parent->isParentOf (comp));
  297. for (int i = components.size(); --i >= 0;)
  298. {
  299. ComponentPosition* c = components.getUnchecked(i);
  300. if (c->component == comp)
  301. {
  302. c->name = name;
  303. c->coords = coords;
  304. triggerAsyncUpdate();
  305. return;
  306. }
  307. }
  308. components.add (new ComponentPosition (comp, name, coords));
  309. comp->addComponentListener (this);
  310. triggerAsyncUpdate();
  311. }
  312. void RelativeRectangleLayoutManager::applyLayout()
  313. {
  314. for (int i = components.size(); --i >= 0;)
  315. {
  316. ComponentPosition* c = components.getUnchecked(i);
  317. // All the components that this layout manages must be inside the parent component..
  318. jassert (parent->isParentOf (c->component));
  319. c->component->setBounds (c->coords.resolve (this).getSmallestIntegerContainer());
  320. }
  321. }
  322. const Expression RelativeRectangleLayoutManager::getSymbolValue (const String& objectName, const String& edge) const
  323. {
  324. if (objectName == RelativeCoordinate::Strings::parent)
  325. {
  326. if (edge == RelativeCoordinate::Strings::right) return Expression ((double) parent->getWidth());
  327. if (edge == RelativeCoordinate::Strings::bottom) return Expression ((double) parent->getHeight());
  328. }
  329. if (objectName.isNotEmpty() && edge.isNotEmpty())
  330. {
  331. for (int i = components.size(); --i >= 0;)
  332. {
  333. ComponentPosition* c = components.getUnchecked(i);
  334. if (c->name == objectName)
  335. {
  336. if (edge == RelativeCoordinate::Strings::left) return c->coords.left.getExpression();
  337. if (edge == RelativeCoordinate::Strings::right) return c->coords.right.getExpression();
  338. if (edge == RelativeCoordinate::Strings::top) return c->coords.top.getExpression();
  339. if (edge == RelativeCoordinate::Strings::bottom) return c->coords.bottom.getExpression();
  340. }
  341. }
  342. }
  343. for (int i = markers.size(); --i >= 0;)
  344. {
  345. MarkerPosition* m = markers.getUnchecked(i);
  346. if (m->markerName == objectName)
  347. return m->position.getExpression();
  348. }
  349. return Expression();
  350. }
  351. void RelativeRectangleLayoutManager::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  352. {
  353. triggerAsyncUpdate();
  354. if (parent == &component)
  355. handleUpdateNowIfNeeded();
  356. }
  357. void RelativeRectangleLayoutManager::componentBeingDeleted (Component& component)
  358. {
  359. for (int i = components.size(); --i >= 0;)
  360. {
  361. ComponentPosition* c = components.getUnchecked(i);
  362. if (c->component == &component)
  363. {
  364. components.remove (i);
  365. break;
  366. }
  367. }
  368. }
  369. void RelativeRectangleLayoutManager::handleAsyncUpdate()
  370. {
  371. applyLayout();
  372. }
  373. RelativeRectangleLayoutManager::MarkerPosition::MarkerPosition (const String& name, const RelativeCoordinate& coord)
  374. : markerName (name), position (coord)
  375. {
  376. }
  377. RelativeRectangleLayoutManager::ComponentPosition::ComponentPosition (Component* component_, const String& name_, const RelativeRectangle& coords_)
  378. : component (component_), name (name_), coords (coords_)
  379. {
  380. }