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.

623 lines
19KB

  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. #include "jucer_FillTypePropertyComponent.h"
  20. //==============================================================================
  21. const int64 hashCode64 (const String& s)
  22. {
  23. return s.hashCode64() + s.length() * s.hashCode() + s.toUpperCase().hashCode();
  24. }
  25. const String createAlphaNumericUID()
  26. {
  27. String uid;
  28. static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  29. Random r (Random::getSystemRandom().nextInt64());
  30. for (int i = 7; --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. void autoScrollForMouseEvent (const MouseEvent& e)
  62. {
  63. Viewport* const viewport = e.eventComponent->findParentComponentOfClass ((Viewport*) 0);
  64. if (viewport != 0)
  65. {
  66. const MouseEvent e2 (e.getEventRelativeTo (viewport));
  67. viewport->autoScroll (e2.x, e2.y, 8, 16);
  68. }
  69. }
  70. void drawComponentPlaceholder (Graphics& g, int w, int h, const String& text)
  71. {
  72. g.fillAll (Colours::white.withAlpha (0.4f));
  73. g.setColour (Colours::grey);
  74. g.drawRect (0, 0, w, h);
  75. g.drawLine (0.5f, 0.5f, w - 0.5f, h - 0.5f);
  76. g.drawLine (0.5f, h - 0.5f, w - 0.5f, 0.5f);
  77. g.setColour (Colours::black);
  78. g.setFont (11.0f);
  79. g.drawFittedText (text, 2, 2, w - 4, h - 4, Justification::centredTop, 2);
  80. }
  81. void drawRecessedShadows (Graphics& g, int w, int h, int shadowSize)
  82. {
  83. ColourGradient cg (Colours::black.withAlpha (0.15f), 0, 0,
  84. Colours::transparentBlack, 0, (float) shadowSize, false);
  85. cg.addColour (0.4, Colours::black.withAlpha (0.07f));
  86. cg.addColour (0.6, Colours::black.withAlpha (0.02f));
  87. g.setGradientFill (cg);
  88. g.fillRect (0, 0, w, shadowSize);
  89. cg.point1.setXY (0.0f, (float) h);
  90. cg.point2.setXY (0.0f, (float) h - shadowSize);
  91. g.setGradientFill (cg);
  92. g.fillRect (0, h - shadowSize, w, shadowSize);
  93. cg.point1.setXY (0.0f, 0.0f);
  94. cg.point2.setXY ((float) shadowSize, 0.0f);
  95. g.setGradientFill (cg);
  96. g.fillRect (0, 0, shadowSize, h);
  97. cg.point1.setXY ((float) w, 0.0f);
  98. cg.point2.setXY ((float) w - shadowSize, 0.0f);
  99. g.setGradientFill (cg);
  100. g.fillRect (w - shadowSize, 0, shadowSize, h);
  101. }
  102. //==============================================================================
  103. int indexOfLineStartingWith (const StringArray& lines, const String& text, int startIndex)
  104. {
  105. startIndex = jmax (0, startIndex);
  106. while (startIndex < lines.size())
  107. {
  108. if (lines[startIndex].trimStart().startsWithIgnoreCase (text))
  109. return startIndex;
  110. ++startIndex;
  111. }
  112. return -1;
  113. }
  114. //==============================================================================
  115. PropertyPanelWithTooltips::PropertyPanelWithTooltips()
  116. : lastComp (0)
  117. {
  118. addAndMakeVisible (panel = new PropertyPanel());
  119. startTimer (150);
  120. }
  121. PropertyPanelWithTooltips::~PropertyPanelWithTooltips()
  122. {
  123. deleteAllChildren();
  124. }
  125. void PropertyPanelWithTooltips::paint (Graphics& g)
  126. {
  127. g.setColour (Colour::greyLevel (0.15f));
  128. g.setFont (13.0f);
  129. TextLayout tl;
  130. tl.appendText (lastTip, Font (14.0f));
  131. tl.layout (getWidth() - 10, Justification::left, true); // try to make it look nice
  132. if (tl.getNumLines() > 3)
  133. tl.layout (getWidth() - 10, Justification::left, false); // too big, so just squash it in..
  134. tl.drawWithin (g, 5, panel->getBottom() + 2, getWidth() - 10,
  135. getHeight() - panel->getBottom() - 4,
  136. Justification::centredLeft);
  137. }
  138. void PropertyPanelWithTooltips::resized()
  139. {
  140. panel->setBounds (0, 0, getWidth(), jmax (getHeight() - 60, proportionOfHeight (0.6f)));
  141. }
  142. void PropertyPanelWithTooltips::timerCallback()
  143. {
  144. Component* const newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  145. if (newComp != lastComp)
  146. {
  147. lastComp = newComp;
  148. String newTip (findTip (newComp));
  149. if (newTip != lastTip)
  150. {
  151. lastTip = newTip;
  152. repaint (0, panel->getBottom(), getWidth(), getHeight());
  153. }
  154. }
  155. }
  156. const String PropertyPanelWithTooltips::findTip (Component* c)
  157. {
  158. while (c != 0 && c != this)
  159. {
  160. TooltipClient* const tc = dynamic_cast <TooltipClient*> (c);
  161. if (tc != 0)
  162. {
  163. const String tip (tc->getTooltip());
  164. if (tip.isNotEmpty())
  165. return tip;
  166. }
  167. c = c->getParentComponent();
  168. }
  169. return String::empty;
  170. }
  171. //==============================================================================
  172. FloatingLabelComponent::FloatingLabelComponent()
  173. : font (10.0f)
  174. {
  175. setInterceptsMouseClicks (false ,false);
  176. }
  177. void FloatingLabelComponent::remove()
  178. {
  179. if (getParentComponent() != 0)
  180. getParentComponent()->removeChildComponent (this);
  181. }
  182. void FloatingLabelComponent::update (Component* parent, const String& text, const Colour& textColour, int x, int y, bool toRight, bool below)
  183. {
  184. colour = textColour;
  185. Rectangle<int> r;
  186. if (text != getName())
  187. {
  188. setName (text);
  189. glyphs.clear();
  190. glyphs.addJustifiedText (font, text, 0, 0, 200.0f, Justification::left);
  191. glyphs.justifyGlyphs (0, std::numeric_limits<int>::max(), 0, 0, 1000, 1000, Justification::topLeft);
  192. r = glyphs.getBoundingBox (0, std::numeric_limits<int>::max(), false)
  193. .getSmallestIntegerContainer().expanded (2, 2);
  194. }
  195. else
  196. {
  197. r = getLocalBounds();
  198. }
  199. r.setPosition (x + (toRight ? 3 : -(r.getWidth() + 3)), y + (below ? 2 : -(r.getHeight() + 2)));
  200. setBounds (r);
  201. parent->addAndMakeVisible (this);
  202. }
  203. void FloatingLabelComponent::paint (Graphics& g)
  204. {
  205. g.setFont (font);
  206. g.setColour (Colours::white.withAlpha (0.5f));
  207. for (int y = -1; y <= 1; ++y)
  208. for (int x = -1; x <= 1; ++x)
  209. glyphs.draw (g, AffineTransform::translation (1.0f + x, 1.0f + y));
  210. g.setColour (colour);
  211. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  212. }
  213. //==============================================================================
  214. RelativeRectangleLayoutManager::RelativeRectangleLayoutManager (Component* parentComponent)
  215. : parent (parentComponent)
  216. {
  217. parent->addComponentListener (this);
  218. }
  219. RelativeRectangleLayoutManager::~RelativeRectangleLayoutManager()
  220. {
  221. parent->removeComponentListener (this);
  222. for (int i = components.size(); --i >= 0;)
  223. components.getUnchecked(i)->component->removeComponentListener (this);
  224. }
  225. void RelativeRectangleLayoutManager::setMarker (const String& name, const RelativeCoordinate& coord)
  226. {
  227. for (int i = markers.size(); --i >= 0;)
  228. {
  229. MarkerPosition* m = markers.getUnchecked(i);
  230. if (m->markerName == name)
  231. {
  232. m->position = coord;
  233. applyLayout();
  234. return;
  235. }
  236. }
  237. markers.add (new MarkerPosition (name, coord));
  238. applyLayout();
  239. }
  240. void RelativeRectangleLayoutManager::setComponentBounds (Component* comp, const String& name, const RelativeRectangle& coords)
  241. {
  242. jassert (comp != 0);
  243. // All the components that this layout manages must be inside the parent component..
  244. jassert (parent->isParentOf (comp));
  245. for (int i = components.size(); --i >= 0;)
  246. {
  247. ComponentPosition* c = components.getUnchecked(i);
  248. if (c->component == comp)
  249. {
  250. c->name = name;
  251. c->coords = coords;
  252. triggerAsyncUpdate();
  253. return;
  254. }
  255. }
  256. components.add (new ComponentPosition (comp, name, coords));
  257. comp->addComponentListener (this);
  258. triggerAsyncUpdate();
  259. }
  260. void RelativeRectangleLayoutManager::applyLayout()
  261. {
  262. for (int i = components.size(); --i >= 0;)
  263. {
  264. ComponentPosition* c = components.getUnchecked(i);
  265. // All the components that this layout manages must be inside the parent component..
  266. jassert (parent->isParentOf (c->component));
  267. c->component->setBounds (c->coords.resolve (this).getSmallestIntegerContainer());
  268. }
  269. }
  270. const RelativeCoordinate RelativeRectangleLayoutManager::findNamedCoordinate (const String& objectName, const String& edge) const
  271. {
  272. if (objectName == RelativeCoordinate::Strings::parent)
  273. {
  274. if (edge == RelativeCoordinate::Strings::right) return RelativeCoordinate ((double) parent->getWidth(), true);
  275. if (edge == RelativeCoordinate::Strings::bottom) return RelativeCoordinate ((double) parent->getHeight(), false);
  276. }
  277. if (objectName.isNotEmpty() && edge.isNotEmpty())
  278. {
  279. for (int i = components.size(); --i >= 0;)
  280. {
  281. ComponentPosition* c = components.getUnchecked(i);
  282. if (c->name == objectName)
  283. {
  284. if (edge == RelativeCoordinate::Strings::left) return c->coords.left;
  285. if (edge == RelativeCoordinate::Strings::right) return c->coords.right;
  286. if (edge == RelativeCoordinate::Strings::top) return c->coords.top;
  287. if (edge == RelativeCoordinate::Strings::bottom) return c->coords.bottom;
  288. }
  289. }
  290. }
  291. for (int i = markers.size(); --i >= 0;)
  292. {
  293. MarkerPosition* m = markers.getUnchecked(i);
  294. if (m->markerName == objectName)
  295. return m->position;
  296. }
  297. return RelativeCoordinate();
  298. }
  299. void RelativeRectangleLayoutManager::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  300. {
  301. triggerAsyncUpdate();
  302. if (parent == &component)
  303. handleUpdateNowIfNeeded();
  304. }
  305. void RelativeRectangleLayoutManager::componentBeingDeleted (Component& component)
  306. {
  307. for (int i = components.size(); --i >= 0;)
  308. {
  309. ComponentPosition* c = components.getUnchecked(i);
  310. if (c->component == &component)
  311. {
  312. components.remove (i);
  313. break;
  314. }
  315. }
  316. }
  317. void RelativeRectangleLayoutManager::handleAsyncUpdate()
  318. {
  319. applyLayout();
  320. }
  321. RelativeRectangleLayoutManager::MarkerPosition::MarkerPosition (const String& name, const RelativeCoordinate& coord)
  322. : markerName (name), position (coord)
  323. {
  324. }
  325. RelativeRectangleLayoutManager::ComponentPosition::ComponentPosition (Component* component_, const String& name_, const RelativeRectangle& coords_)
  326. : component (component_), name (name_), coords (coords_)
  327. {
  328. }
  329. //==============================================================================
  330. const ColourGradient FillTypeEditorComponent::getDefaultGradient() const
  331. {
  332. FillTypePropertyComponent* p = dynamic_cast <FillTypePropertyComponent*> (getParentComponent());
  333. jassert (p != 0);
  334. return p->getDefaultGradient();
  335. }
  336. //==============================================================================
  337. PopupComponent::PopupComponent (Component* const content_)
  338. : edge (20), content (content_)
  339. {
  340. addAndMakeVisible (content);
  341. }
  342. PopupComponent::~PopupComponent()
  343. {
  344. }
  345. void PopupComponent::show (Component* content, Component* targetComp, Component* parentComp)
  346. {
  347. show (content,
  348. parentComp,
  349. parentComp == 0 ? targetComp->getParentMonitorArea()
  350. : parentComp->getLocalBounds(),
  351. parentComp == 0 ? targetComp->getScreenBounds()
  352. : (targetComp->getLocalBounds() + targetComp->relativePositionToOtherComponent (parentComp, Point<int>())));
  353. }
  354. void PopupComponent::show (Component* content, Component* parent,
  355. const Rectangle<int>& availableAreaInParent,
  356. const Rectangle<int>& targetAreaInParent)
  357. {
  358. PopupComponent p (content);
  359. p.updatePosition (targetAreaInParent, availableAreaInParent);
  360. if (parent != 0)
  361. parent->addAndMakeVisible (&p);
  362. else
  363. p.addToDesktop (ComponentPeer::windowIsTemporary);
  364. p.runModalLoop();
  365. }
  366. void PopupComponent::inputAttemptWhenModal()
  367. {
  368. exitModalState (0);
  369. setVisible (false);
  370. }
  371. void PopupComponent::paint (Graphics& g)
  372. {
  373. if (background.isNull())
  374. {
  375. DropShadowEffect shadow;
  376. shadow.setShadowProperties (5.0f, 0.4f, 0.0f, 2.0f);
  377. Image im (Image::ARGB, getWidth(), getHeight(), true);
  378. {
  379. Graphics g (im);
  380. g.setColour (Colour::greyLevel (0.23f).withAlpha (0.9f));
  381. g.fillPath (outline);
  382. g.setColour (Colours::white.withAlpha (0.6f));
  383. g.strokePath (outline, PathStrokeType (2.0f));
  384. }
  385. background = Image (Image::ARGB, getWidth(), getHeight(), true);
  386. Graphics g (background);
  387. shadow.applyEffect (im, g);
  388. }
  389. g.setColour (Colours::black);
  390. g.drawImageAt (background, 0, 0);
  391. }
  392. void PopupComponent::resized()
  393. {
  394. content->setTopLeftPosition (edge, edge);
  395. refreshPath();
  396. }
  397. void PopupComponent::moved()
  398. {
  399. refreshPath();
  400. }
  401. void PopupComponent::childBoundsChanged (Component*)
  402. {
  403. updatePosition (targetArea, availableArea);
  404. }
  405. bool PopupComponent::hitTest (int x, int y)
  406. {
  407. return outline.contains (x, y);
  408. }
  409. void PopupComponent::refreshPath()
  410. {
  411. background = Image();
  412. outline.clear();
  413. const float gap = 4.0f;
  414. const float x = content->getX() - gap, y = content->getY() - gap, r = content->getRight() + gap, b = content->getBottom() + gap;
  415. const float targetX = targetPoint.getX() - getX(), targetY = targetPoint.getY() - getY();
  416. const float cs = 8.0f;
  417. const float cs2 = 2.0f * cs;
  418. const float arrowWidth = edge * 0.8f;
  419. outline.startNewSubPath (x + cs, y);
  420. if (targetY < edge)
  421. {
  422. outline.lineTo (targetX - arrowWidth, y);
  423. outline.lineTo (targetX, targetY);
  424. outline.lineTo (targetX + arrowWidth, y);
  425. }
  426. outline.lineTo (r - cs, y);
  427. outline.addArc (r - cs2, y, cs2, cs2, 0, float_Pi * 0.5f);
  428. if (targetX > r)
  429. {
  430. outline.lineTo (r, targetY - arrowWidth);
  431. outline.lineTo (targetX, targetY);
  432. outline.lineTo (r, targetY + arrowWidth);
  433. }
  434. outline.lineTo (r, b - cs);
  435. outline.addArc (r - cs2, b - cs2, cs2, cs2, float_Pi * 0.5f, float_Pi);
  436. if (targetY > b)
  437. {
  438. outline.lineTo (targetX + arrowWidth, b);
  439. outline.lineTo (targetX, targetY);
  440. outline.lineTo (targetX - arrowWidth, b);
  441. }
  442. outline.lineTo (x + cs, b);
  443. outline.addArc (x, b - cs2, cs2, cs2, float_Pi, float_Pi * 1.5f);
  444. if (targetX < x)
  445. {
  446. outline.lineTo (x, targetY + arrowWidth);
  447. outline.lineTo (targetX, targetY);
  448. outline.lineTo (x, targetY - arrowWidth);
  449. }
  450. outline.lineTo (x, y + cs);
  451. outline.addArc (x, y, cs2, cs2, float_Pi * 1.5f, float_Pi * 2.0f - 0.05f);
  452. outline.closeSubPath();
  453. repaint();
  454. }
  455. void PopupComponent::updatePosition (const Rectangle<int>& newTargetArea, const Rectangle<int>& newArea)
  456. {
  457. targetArea = newTargetArea;
  458. availableArea = newArea;
  459. Rectangle<int> r (0, 0,
  460. content->getWidth() + edge * 2,
  461. content->getHeight() + edge * 2);
  462. const float hw = r.getWidth() / 2.0f;
  463. const float hh = r.getHeight() / 2.0f;
  464. const float hwReduced = hw - edge * 3;
  465. const float hhReduced = hh - edge * 3;
  466. Point<float> centres[4];
  467. Point<float> targets[4] = { Point<float> (targetArea.getCentreX(), targetArea.getBottom()),
  468. Point<float> (targetArea.getRight(), targetArea.getCentreY()),
  469. Point<float> (targetArea.getX(), targetArea.getCentreY()),
  470. Point<float> (targetArea.getCentreX(), targetArea.getY()) };
  471. Line<float> lines[4] = { Line<float> (targets[0] + Point<float> (-hwReduced, hh), targets[0] + Point<float> (hwReduced, hh)),
  472. Line<float> (targets[1] + Point<float> (hw, -hhReduced), targets[1] + Point<float> (hw, hhReduced)),
  473. Line<float> (targets[2] + Point<float> (-hw, -hhReduced), targets[2] + Point<float> (-hw, hhReduced)),
  474. Line<float> (targets[3] + Point<float> (-hwReduced, -hh), targets[3] + Point<float> (hwReduced, -hh)) };
  475. int best = 0;
  476. float bestDist = 1.0e9f;
  477. for (int i = 0; i < 4; ++i)
  478. {
  479. const Rectangle<float> reducedArea (newArea.reduced (hw, hh).toFloat());
  480. Line<float> constrainedLine (reducedArea.getConstrainedPoint (lines[i].getStart()),
  481. reducedArea.getConstrainedPoint (lines[i].getEnd()));
  482. centres[i] = constrainedLine.findNearestPointTo (reducedArea.getCentre());
  483. float dist = centres[i].getDistanceFrom (reducedArea.getCentre());
  484. if (! (reducedArea.contains (lines[i].getStart()) || reducedArea.contains (lines[i].getEnd())))
  485. dist *= 2.0f;
  486. if (dist < bestDist)
  487. {
  488. bestDist = dist;
  489. best = i;
  490. }
  491. }
  492. targetPoint = targets[best];
  493. r.setPosition (centres[best].getX() - hw, centres[best].getY() - hh);
  494. setBounds (r);
  495. }