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.

409 lines
12KB

  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 = 9; --i >= 0;)
  30. {
  31. r.setSeedRandomly();
  32. uid << (juce_wchar) chars [r.nextInt (sizeof (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. void autoScrollForMouseEvent (const MouseEvent& e)
  61. {
  62. Viewport* const viewport = e.eventComponent->findParentComponentOfClass ((Viewport*) 0);
  63. if (viewport != 0)
  64. {
  65. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  66. viewport->autoScroll (e2.x, e2.y, 8, 16);
  67. }
  68. }
  69. void drawComponentPlaceholder (Graphics& g, int w, int h, const String& text)
  70. {
  71. g.fillAll (Colours::white.withAlpha (0.4f));
  72. g.setColour (Colours::grey);
  73. g.drawRect (0, 0, w, h);
  74. g.drawLine (0.5f, 0.5f, w - 0.5f, h - 0.5f);
  75. g.drawLine (0.5f, h - 0.5f, w - 0.5f, 0.5f);
  76. g.setColour (Colours::black);
  77. g.setFont (11.0f);
  78. g.drawFittedText (text, 2, 2, w - 4, h - 4, Justification::centredTop, 2);
  79. }
  80. void drawRecessedShadows (Graphics& g, int w, int h, int shadowSize)
  81. {
  82. ColourGradient cg (Colours::black.withAlpha (0.15f), 0, 0,
  83. Colours::transparentBlack, 0, (float) shadowSize, false);
  84. cg.addColour (0.4, Colours::black.withAlpha (0.07f));
  85. cg.addColour (0.6, Colours::black.withAlpha (0.02f));
  86. g.setGradientFill (cg);
  87. g.fillRect (0, 0, w, shadowSize);
  88. cg.point1.setXY (0.0f, (float) h);
  89. cg.point2.setXY (0.0f, (float) h - shadowSize);
  90. g.setGradientFill (cg);
  91. g.fillRect (0, h - shadowSize, w, shadowSize);
  92. cg.point1.setXY (0.0f, 0.0f);
  93. cg.point2.setXY ((float) shadowSize, 0.0f);
  94. g.setGradientFill (cg);
  95. g.fillRect (0, 0, shadowSize, h);
  96. cg.point1.setXY ((float) w, 0.0f);
  97. cg.point2.setXY ((float) w - shadowSize, 0.0f);
  98. g.setGradientFill (cg);
  99. g.fillRect (w - shadowSize, 0, shadowSize, h);
  100. }
  101. //==============================================================================
  102. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex)
  103. {
  104. startIndex = jmax (0, startIndex);
  105. while (startIndex < lines.size())
  106. {
  107. if (lines[startIndex].trimStart().startsWithIgnoreCase (text))
  108. return startIndex;
  109. ++startIndex;
  110. }
  111. return -1;
  112. }
  113. //==============================================================================
  114. PropertyPanelWithTooltips::PropertyPanelWithTooltips()
  115. : lastComp (0)
  116. {
  117. addAndMakeVisible (panel = new PropertyPanel());
  118. startTimer (150);
  119. }
  120. PropertyPanelWithTooltips::~PropertyPanelWithTooltips()
  121. {
  122. deleteAllChildren();
  123. }
  124. void PropertyPanelWithTooltips::paint (Graphics& g)
  125. {
  126. g.setColour (Colour::greyLevel (0.15f));
  127. g.setFont (13.0f);
  128. TextLayout tl;
  129. tl.appendText (lastTip, Font (14.0f));
  130. tl.layout (getWidth() - 10, Justification::left, true); // try to make it look nice
  131. if (tl.getNumLines() > 3)
  132. tl.layout (getWidth() - 10, Justification::left, false); // too big, so just squash it in..
  133. tl.drawWithin (g, 5, panel->getBottom() + 2, getWidth() - 10,
  134. getHeight() - panel->getBottom() - 4,
  135. Justification::centredLeft);
  136. }
  137. void PropertyPanelWithTooltips::resized()
  138. {
  139. panel->setBounds (0, 0, getWidth(), jmax (getHeight() - 60, proportionOfHeight (0.6f)));
  140. }
  141. void PropertyPanelWithTooltips::timerCallback()
  142. {
  143. Component* const newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  144. if (newComp != lastComp)
  145. {
  146. lastComp = newComp;
  147. String newTip (findTip (newComp));
  148. if (newTip != lastTip)
  149. {
  150. lastTip = newTip;
  151. repaint (0, panel->getBottom(), getWidth(), getHeight());
  152. }
  153. }
  154. }
  155. const String PropertyPanelWithTooltips::findTip (Component* c)
  156. {
  157. while (c != 0 && c != this)
  158. {
  159. TooltipClient* const tc = dynamic_cast <TooltipClient*> (c);
  160. if (tc != 0)
  161. {
  162. const String tip (tc->getTooltip());
  163. if (tip.isNotEmpty())
  164. return tip;
  165. }
  166. c = c->getParentComponent();
  167. }
  168. return String::empty;
  169. }
  170. //==============================================================================
  171. FloatingLabelComponent::FloatingLabelComponent()
  172. : font (10.0f)
  173. {
  174. setInterceptsMouseClicks (false ,false);
  175. }
  176. void FloatingLabelComponent::remove()
  177. {
  178. if (getParentComponent() != 0)
  179. getParentComponent()->removeChildComponent (this);
  180. }
  181. void FloatingLabelComponent::update (Component* parent, const String& text, const Colour& textColour, int x, int y, bool toRight, bool below)
  182. {
  183. colour = textColour;
  184. Rectangle<int> r;
  185. if (text != getName())
  186. {
  187. setName (text);
  188. glyphs.clear();
  189. glyphs.addJustifiedText (font, text, 0, 0, 200.0f, Justification::left);
  190. glyphs.justifyGlyphs (0, std::numeric_limits<int>::max(), 0, 0, 1000, 1000, Justification::topLeft);
  191. r = glyphs.getBoundingBox (0, std::numeric_limits<int>::max(), false)
  192. .getSmallestIntegerContainer().expanded (2, 2);
  193. }
  194. else
  195. {
  196. r = getLocalBounds();
  197. }
  198. r.setPosition (x + (toRight ? 3 : -(r.getWidth() + 3)), y + (below ? 2 : -(r.getHeight() + 2)));
  199. setBounds (r);
  200. parent->addAndMakeVisible (this);
  201. }
  202. void FloatingLabelComponent::paint (Graphics& g)
  203. {
  204. g.setFont (font);
  205. g.setColour (Colours::white.withAlpha (0.5f));
  206. for (int y = -1; y <= 1; ++y)
  207. for (int x = -1; x <= 1; ++x)
  208. glyphs.draw (g, AffineTransform::translation (1.0f + x, 1.0f + y));
  209. g.setColour (colour);
  210. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  211. }
  212. //==============================================================================
  213. RelativeRectangleLayoutManager::RelativeRectangleLayoutManager (Component* parentComponent)
  214. : parent (parentComponent)
  215. {
  216. parent->addComponentListener (this);
  217. }
  218. RelativeRectangleLayoutManager::~RelativeRectangleLayoutManager()
  219. {
  220. parent->removeComponentListener (this);
  221. for (int i = components.size(); --i >= 0;)
  222. components.getUnchecked(i)->component->removeComponentListener (this);
  223. }
  224. void RelativeRectangleLayoutManager::setMarker (const String& name, const RelativeCoordinate& coord)
  225. {
  226. for (int i = markers.size(); --i >= 0;)
  227. {
  228. MarkerPosition* m = markers.getUnchecked(i);
  229. if (m->markerName == name)
  230. {
  231. m->position = coord;
  232. applyLayout();
  233. return;
  234. }
  235. }
  236. markers.add (new MarkerPosition (name, coord));
  237. applyLayout();
  238. }
  239. void RelativeRectangleLayoutManager::setComponentBounds (Component* comp, const String& name, const RelativeRectangle& coords)
  240. {
  241. jassert (comp != 0);
  242. // All the components that this layout manages must be inside the parent component..
  243. jassert (parent->isParentOf (comp));
  244. for (int i = components.size(); --i >= 0;)
  245. {
  246. ComponentPosition* c = components.getUnchecked(i);
  247. if (c->component == comp)
  248. {
  249. c->name = name;
  250. c->coords = coords;
  251. triggerAsyncUpdate();
  252. return;
  253. }
  254. }
  255. components.add (new ComponentPosition (comp, name, coords));
  256. comp->addComponentListener (this);
  257. triggerAsyncUpdate();
  258. }
  259. void RelativeRectangleLayoutManager::applyLayout()
  260. {
  261. for (int i = components.size(); --i >= 0;)
  262. {
  263. ComponentPosition* c = components.getUnchecked(i);
  264. // All the components that this layout manages must be inside the parent component..
  265. jassert (parent->isParentOf (c->component));
  266. c->component->setBounds (c->coords.resolve (this).getSmallestIntegerContainer());
  267. }
  268. }
  269. const RelativeCoordinate RelativeRectangleLayoutManager::findNamedCoordinate (const String& objectName, const String& edge) const
  270. {
  271. if (objectName == RelativeCoordinate::Strings::parent)
  272. {
  273. if (edge == RelativeCoordinate::Strings::right) return RelativeCoordinate ((double) parent->getWidth(), true);
  274. if (edge == RelativeCoordinate::Strings::bottom) return RelativeCoordinate ((double) parent->getHeight(), false);
  275. }
  276. if (objectName.isNotEmpty() && edge.isNotEmpty())
  277. {
  278. for (int i = components.size(); --i >= 0;)
  279. {
  280. ComponentPosition* c = components.getUnchecked(i);
  281. if (c->name == objectName)
  282. {
  283. if (edge == RelativeCoordinate::Strings::left) return c->coords.left;
  284. if (edge == RelativeCoordinate::Strings::right) return c->coords.right;
  285. if (edge == RelativeCoordinate::Strings::top) return c->coords.top;
  286. if (edge == RelativeCoordinate::Strings::bottom) return c->coords.bottom;
  287. }
  288. }
  289. }
  290. for (int i = markers.size(); --i >= 0;)
  291. {
  292. MarkerPosition* m = markers.getUnchecked(i);
  293. if (m->markerName == objectName)
  294. return m->position;
  295. }
  296. return RelativeCoordinate();
  297. }
  298. void RelativeRectangleLayoutManager::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  299. {
  300. triggerAsyncUpdate();
  301. if (parent == &component)
  302. handleUpdateNowIfNeeded();
  303. }
  304. void RelativeRectangleLayoutManager::componentBeingDeleted (Component& component)
  305. {
  306. for (int i = components.size(); --i >= 0;)
  307. {
  308. ComponentPosition* c = components.getUnchecked(i);
  309. if (c->component == &component)
  310. {
  311. components.remove (i);
  312. break;
  313. }
  314. }
  315. }
  316. void RelativeRectangleLayoutManager::handleAsyncUpdate()
  317. {
  318. applyLayout();
  319. }
  320. RelativeRectangleLayoutManager::MarkerPosition::MarkerPosition (const String& name, const RelativeCoordinate& coord)
  321. : markerName (name), position (coord)
  322. {
  323. }
  324. RelativeRectangleLayoutManager::ComponentPosition::ComponentPosition (Component* component_, const String& name_, const RelativeRectangle& coords_)
  325. : component (component_), name (name_), coords (coords_)
  326. {
  327. }