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.

415 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, bool scrollX, bool scrollY)
  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 (scrollX ? e2.x : 20, scrollY ? e2.y : 20, 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 (1, 1);
  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. g.fillRoundedRectangle (0, 0, (float) getWidth(), (float) getHeight(), 3);
  208. g.setColour (colour);
  209. glyphs.draw (g, AffineTransform::translation (1.0f, 1.0f));
  210. }
  211. //==============================================================================
  212. RelativeRectangleLayoutManager::RelativeRectangleLayoutManager (Component* parentComponent)
  213. : parent (parentComponent)
  214. {
  215. parent->addComponentListener (this);
  216. }
  217. RelativeRectangleLayoutManager::~RelativeRectangleLayoutManager()
  218. {
  219. parent->removeComponentListener (this);
  220. for (int i = components.size(); --i >= 0;)
  221. components.getUnchecked(i)->component->removeComponentListener (this);
  222. }
  223. void RelativeRectangleLayoutManager::setMarker (const String& name, const RelativeCoordinate& coord)
  224. {
  225. for (int i = markers.size(); --i >= 0;)
  226. {
  227. MarkerPosition* m = markers.getUnchecked(i);
  228. if (m->markerName == name)
  229. {
  230. m->position = coord;
  231. applyLayout();
  232. return;
  233. }
  234. }
  235. markers.add (new MarkerPosition (name, coord));
  236. applyLayout();
  237. }
  238. void RelativeRectangleLayoutManager::setComponentBounds (Component* comp, const String& name, const RelativeRectangle& coords)
  239. {
  240. jassert (comp != 0);
  241. // All the components that this layout manages must be inside the parent component..
  242. jassert (parent->isParentOf (comp));
  243. for (int i = components.size(); --i >= 0;)
  244. {
  245. ComponentPosition* c = components.getUnchecked(i);
  246. if (c->component == comp)
  247. {
  248. c->name = name;
  249. c->coords = coords;
  250. triggerAsyncUpdate();
  251. return;
  252. }
  253. }
  254. components.add (new ComponentPosition (comp, name, coords));
  255. comp->addComponentListener (this);
  256. triggerAsyncUpdate();
  257. }
  258. void RelativeRectangleLayoutManager::applyLayout()
  259. {
  260. for (int i = components.size(); --i >= 0;)
  261. {
  262. ComponentPosition* c = components.getUnchecked(i);
  263. // All the components that this layout manages must be inside the parent component..
  264. jassert (parent->isParentOf (c->component));
  265. c->component->setBounds (c->coords.resolve (this).getSmallestIntegerContainer());
  266. }
  267. }
  268. const RelativeCoordinate RelativeRectangleLayoutManager::findNamedCoordinate (const String& objectName, const String& edge) const
  269. {
  270. if (objectName == RelativeCoordinate::Strings::parent)
  271. {
  272. if (edge == RelativeCoordinate::Strings::right) return RelativeCoordinate ((double) parent->getWidth(), true);
  273. if (edge == RelativeCoordinate::Strings::bottom) return RelativeCoordinate ((double) parent->getHeight(), false);
  274. }
  275. if (objectName.isNotEmpty() && edge.isNotEmpty())
  276. {
  277. for (int i = components.size(); --i >= 0;)
  278. {
  279. ComponentPosition* c = components.getUnchecked(i);
  280. if (c->name == objectName)
  281. {
  282. if (edge == RelativeCoordinate::Strings::left) return c->coords.left;
  283. if (edge == RelativeCoordinate::Strings::right) return c->coords.right;
  284. if (edge == RelativeCoordinate::Strings::top) return c->coords.top;
  285. if (edge == RelativeCoordinate::Strings::bottom) return c->coords.bottom;
  286. }
  287. }
  288. }
  289. for (int i = markers.size(); --i >= 0;)
  290. {
  291. MarkerPosition* m = markers.getUnchecked(i);
  292. if (m->markerName == objectName)
  293. return m->position;
  294. }
  295. return RelativeCoordinate();
  296. }
  297. void RelativeRectangleLayoutManager::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  298. {
  299. triggerAsyncUpdate();
  300. if (parent == &component)
  301. handleUpdateNowIfNeeded();
  302. }
  303. void RelativeRectangleLayoutManager::componentBeingDeleted (Component& component)
  304. {
  305. for (int i = components.size(); --i >= 0;)
  306. {
  307. ComponentPosition* c = components.getUnchecked(i);
  308. if (c->component == &component)
  309. {
  310. components.remove (i);
  311. break;
  312. }
  313. }
  314. }
  315. void RelativeRectangleLayoutManager::handleAsyncUpdate()
  316. {
  317. applyLayout();
  318. }
  319. RelativeRectangleLayoutManager::MarkerPosition::MarkerPosition (const String& name, const RelativeCoordinate& coord)
  320. : markerName (name), position (coord)
  321. {
  322. }
  323. RelativeRectangleLayoutManager::ComponentPosition::ComponentPosition (Component* component_, const String& name_, const RelativeRectangle& coords_)
  324. : component (component_), name (name_), coords (coords_)
  325. {
  326. }
  327. //==============================================================================
  328. const ColourGradient FillTypeEditorComponent::getDefaultGradient() const
  329. {
  330. FillTypePropertyComponent* p = dynamic_cast <FillTypePropertyComponent*> (getParentComponent());
  331. jassert (p != 0);
  332. return p->getDefaultGradient();
  333. }