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.

360 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. struct ContentViewHeader : public Component
  22. {
  23. ContentViewHeader (String headerName, Icon headerIcon)
  24. : name (headerName), icon (headerIcon)
  25. {
  26. }
  27. void paint (Graphics& g) override
  28. {
  29. g.fillAll (findColour (contentHeaderBackgroundColourId));
  30. auto bounds = getLocalBounds().reduced (20, 0);
  31. icon.withColour (Colours::white).draw (g, bounds.toFloat().removeFromRight (30), false);
  32. g.setColour (Colours::white);
  33. g.setFont (Font (18.0f));
  34. g.drawFittedText (name, bounds, Justification::centredLeft, 1);
  35. }
  36. String name;
  37. Icon icon;
  38. };
  39. //==============================================================================
  40. class ListBoxHeader : public Component
  41. {
  42. public:
  43. ListBoxHeader (Array<String> columnHeaders)
  44. {
  45. for (auto s : columnHeaders)
  46. {
  47. addAndMakeVisible (headers.add (new Label (s, s)));
  48. widths.add (1.0f / columnHeaders.size());
  49. }
  50. setSize (200, 40);
  51. }
  52. ListBoxHeader (Array<String> columnHeaders, Array<float> columnWidths)
  53. {
  54. jassert (columnHeaders.size() == columnWidths.size());
  55. auto index = 0;
  56. for (auto s : columnHeaders)
  57. {
  58. addAndMakeVisible (headers.add (new Label (s, s)));
  59. widths.add (columnWidths.getUnchecked (index++));
  60. }
  61. recalculateWidths();
  62. setSize (200, 40);
  63. }
  64. void resized() override
  65. {
  66. auto bounds = getLocalBounds();
  67. auto width = bounds.getWidth();
  68. auto index = 0;
  69. for (auto h : headers)
  70. {
  71. auto headerWidth = roundToInt (width * widths.getUnchecked (index));
  72. h->setBounds (bounds.removeFromLeft (headerWidth));
  73. ++index;
  74. }
  75. }
  76. void setColumnHeaderWidth (int index, float proportionOfWidth)
  77. {
  78. if (! (isPositiveAndBelow (index, headers.size()) && isPositiveAndNotGreaterThan (proportionOfWidth, 1.0f)))
  79. {
  80. jassertfalse;
  81. return;
  82. }
  83. widths.set (index, proportionOfWidth);
  84. recalculateWidths (index);
  85. }
  86. int getColumnX (int index)
  87. {
  88. auto prop = 0.0f;
  89. for (auto i = 0; i < index; ++i)
  90. prop += widths.getUnchecked (i);
  91. return roundToInt (prop * getWidth());
  92. }
  93. float getProportionAtIndex (int index)
  94. {
  95. jassert (isPositiveAndBelow (index, widths.size()));
  96. return widths.getUnchecked (index);
  97. }
  98. private:
  99. OwnedArray<Label> headers;
  100. Array<float> widths;
  101. void recalculateWidths (int indexToIgnore = -1)
  102. {
  103. auto total = 0.0f;
  104. for (auto w : widths)
  105. total += w;
  106. if (total == 1.0f)
  107. return;
  108. auto diff = 1.0f - total;
  109. auto amount = diff / static_cast<float> (indexToIgnore == -1 ? widths.size() : widths.size() - 1);
  110. for (auto i = 0; i < widths.size(); ++i)
  111. {
  112. if (i != indexToIgnore)
  113. {
  114. auto val = widths.getUnchecked (i);
  115. widths.set (i, val + amount);
  116. }
  117. }
  118. }
  119. };
  120. //==============================================================================
  121. class InfoButton : public Button
  122. {
  123. public:
  124. InfoButton (const String& infoToDisplay = String())
  125. : Button (String())
  126. {
  127. if (infoToDisplay.isNotEmpty())
  128. setInfoToDisplay (infoToDisplay);
  129. }
  130. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  131. {
  132. auto bounds = getLocalBounds().toFloat().reduced (2);
  133. const auto& icon = getIcons().info;
  134. g.setColour (findColour (treeIconColourId).withMultipliedAlpha (isMouseOverButton || isButtonDown ? 1.0f : 0.5f));
  135. if (isButtonDown)
  136. g.fillEllipse (bounds);
  137. else
  138. g.fillPath (icon, RectanglePlacement (RectanglePlacement::centred)
  139. .getTransformToFit (icon.getBounds(), bounds));
  140. }
  141. void clicked() override
  142. {
  143. auto* w = new InfoWindow (info);
  144. w->setSize (width, w->getHeight() * numLines + 10);
  145. CallOutBox::launchAsynchronously (w, getScreenBounds(), nullptr);
  146. }
  147. void setInfoToDisplay (const String& infoToDisplay)
  148. {
  149. if (infoToDisplay.isNotEmpty())
  150. {
  151. info = infoToDisplay;
  152. auto stringWidth = roundToInt (Font (14.0f).getStringWidthFloat (info));
  153. width = jmin (300, stringWidth);
  154. numLines += static_cast<int> (stringWidth / width);
  155. }
  156. }
  157. void setAssociatedComponent (Component* comp) { associatedComponent = comp; }
  158. Component* getAssociatedComponent() { return associatedComponent; }
  159. private:
  160. String info;
  161. Component* associatedComponent = nullptr;
  162. int width;
  163. int numLines = 1;
  164. //==============================================================================
  165. struct InfoWindow : public Component
  166. {
  167. InfoWindow (const String& s)
  168. : stringToDisplay (s)
  169. {
  170. setSize (150, 14);
  171. }
  172. void paint (Graphics& g) override
  173. {
  174. g.fillAll (findColour (secondaryBackgroundColourId));
  175. g.setColour (findColour (defaultTextColourId));
  176. g.setFont (Font (14.0f));
  177. g.drawFittedText (stringToDisplay, getLocalBounds(), Justification::centred, 10, 1.0f);
  178. }
  179. String stringToDisplay;
  180. };
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InfoButton)
  182. };
  183. //==============================================================================
  184. class PropertyGroupComponent : public Component
  185. {
  186. public:
  187. PropertyGroupComponent (String name, Icon icon, String desc = {})
  188. : header (name, icon),
  189. description (desc)
  190. {
  191. addAndMakeVisible (header);
  192. description.setFont (Font (16.0f));
  193. description.setColour (getLookAndFeel().findColour (defaultTextColourId));
  194. description.setLineSpacing (5.0f);
  195. description.setJustification (Justification::centredLeft);
  196. }
  197. void setProperties (const PropertyListBuilder& newProps)
  198. {
  199. infoButtons.clear();
  200. properties.clear();
  201. properties.addArray (newProps.components);
  202. for (auto i = properties.size(); --i >= 0;)
  203. {
  204. auto* prop = properties.getUnchecked (i);
  205. addAndMakeVisible (prop);
  206. if (! prop->getTooltip().isEmpty())
  207. {
  208. addAndMakeVisible (infoButtons.add (new InfoButton (prop->getTooltip())));
  209. infoButtons.getLast()->setAssociatedComponent (prop);
  210. prop->setTooltip (String()); // set the tooltip to empty so it only displays when its button is clicked
  211. }
  212. }
  213. }
  214. int updateSize (int x, int y, int width)
  215. {
  216. header.setBounds (0, 0, width, headerSize);
  217. auto height = header.getBottom() + 10;
  218. descriptionLayout.createLayout (description, (float) (width - 40));
  219. const auto descriptionHeight = (int) descriptionLayout.getHeight();
  220. if (descriptionHeight > 0)
  221. height += (int) descriptionLayout.getHeight() + 25;
  222. for (auto i = 0; i < properties.size(); ++i)
  223. {
  224. auto* pp = properties.getUnchecked (i);
  225. auto propertyHeight = pp->getPreferredHeight() + (getHeightMultiplier (pp) * pp->getPreferredHeight());
  226. InfoButton* buttonToUse = nullptr;
  227. for (auto* b : infoButtons)
  228. if (b->getAssociatedComponent() == pp)
  229. buttonToUse = b;
  230. if (buttonToUse != nullptr)
  231. {
  232. buttonToUse->setSize (20, 20);
  233. buttonToUse->setCentrePosition (20, height + (propertyHeight / 2));
  234. }
  235. pp->setBounds (40, height, width - 50, propertyHeight);
  236. resizePropertyComponent (pp);
  237. height += pp->getHeight() + 10;
  238. }
  239. height += 16;
  240. setBounds (x, y, width, jmax (height, getParentHeight()));
  241. return height;
  242. }
  243. void paint (Graphics& g) override
  244. {
  245. g.setColour (findColour (secondaryBackgroundColourId));
  246. g.fillRect (getLocalBounds());
  247. auto textArea = getLocalBounds().toFloat()
  248. .withTop ((float) headerSize)
  249. .reduced (20.0f, 10.0f)
  250. .withHeight (descriptionLayout.getHeight());
  251. descriptionLayout.draw (g, textArea);
  252. }
  253. int getHeightMultiplier (PropertyComponent* pp)
  254. {
  255. auto availableTextWidth = ProjucerLookAndFeel::getTextWidthForPropertyComponent (pp);
  256. auto font = ProjucerLookAndFeel::getPropertyComponentFont();
  257. auto nameWidth = font.getStringWidthFloat (pp->getName());
  258. return static_cast<int> (nameWidth / availableTextWidth);
  259. }
  260. void resizePropertyComponent (PropertyComponent* pp)
  261. {
  262. if (pp->getName() == "Dependencies")
  263. return;
  264. for (int i = pp->getNumChildComponents() - 1; i >= 0; --i)
  265. {
  266. auto* child = pp->getChildComponent (i);
  267. auto bounds = child->getBounds();
  268. child->setBounds (bounds.withSizeKeepingCentre (child->getWidth(), pp->getPreferredHeight()));
  269. }
  270. }
  271. OwnedArray<PropertyComponent> properties;
  272. private:
  273. OwnedArray<InfoButton> infoButtons;
  274. ContentViewHeader header;
  275. AttributedString description;
  276. TextLayout descriptionLayout;
  277. const int headerSize = 40;
  278. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyGroupComponent)
  279. };