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.

418 lines
13KB

  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. }