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.

404 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. struct ContentViewHeader : public Component
  21. {
  22. ContentViewHeader (String headerName, Icon headerIcon)
  23. : name (headerName), icon (headerIcon)
  24. {
  25. }
  26. void paint (Graphics& g) override
  27. {
  28. g.fillAll (findColour (contentHeaderBackgroundColourId));
  29. auto bounds = getLocalBounds().reduced (20, 0);
  30. icon.withColour (Colours::white).draw (g, bounds.toFloat().removeFromRight (30), false);
  31. g.setColour (Colours::white);
  32. g.setFont (Font (18.0f));
  33. g.drawFittedText (name, bounds, Justification::centredLeft, 1);
  34. }
  35. String name;
  36. Icon icon;
  37. };
  38. //==============================================================================
  39. class ListBoxHeader : public Component
  40. {
  41. public:
  42. ListBoxHeader (Array<String> columnHeaders)
  43. {
  44. for (auto s : columnHeaders)
  45. {
  46. addAndMakeVisible (headers.add (new Label (s, s)));
  47. widths.add (1.0f / (float) columnHeaders.size());
  48. }
  49. setSize (200, 40);
  50. }
  51. ListBoxHeader (Array<String> columnHeaders, Array<float> columnWidths)
  52. {
  53. jassert (columnHeaders.size() == columnWidths.size());
  54. auto index = 0;
  55. for (auto s : columnHeaders)
  56. {
  57. addAndMakeVisible (headers.add (new Label (s, s)));
  58. widths.add (columnWidths.getUnchecked (index++));
  59. }
  60. recalculateWidths();
  61. setSize (200, 40);
  62. }
  63. void resized() override
  64. {
  65. auto bounds = getLocalBounds();
  66. auto width = bounds.getWidth();
  67. auto index = 0;
  68. for (auto h : headers)
  69. {
  70. auto headerWidth = roundToInt ((float) width * widths.getUnchecked (index));
  71. h->setBounds (bounds.removeFromLeft (headerWidth));
  72. ++index;
  73. }
  74. }
  75. void setColumnHeaderWidth (int index, float proportionOfWidth)
  76. {
  77. if (! (isPositiveAndBelow (index, headers.size()) && isPositiveAndNotGreaterThan (proportionOfWidth, 1.0f)))
  78. {
  79. jassertfalse;
  80. return;
  81. }
  82. widths.set (index, proportionOfWidth);
  83. recalculateWidths (index);
  84. }
  85. int getColumnX (int index)
  86. {
  87. auto prop = 0.0f;
  88. for (int i = 0; i < index; ++i)
  89. prop += widths.getUnchecked (i);
  90. return roundToInt (prop * (float) getWidth());
  91. }
  92. float getProportionAtIndex (int index)
  93. {
  94. jassert (isPositiveAndBelow (index, widths.size()));
  95. return widths.getUnchecked (index);
  96. }
  97. private:
  98. OwnedArray<Label> headers;
  99. Array<float> widths;
  100. void recalculateWidths (int indexToIgnore = -1)
  101. {
  102. auto total = 0.0f;
  103. for (auto w : widths)
  104. total += w;
  105. if (total == 1.0f)
  106. return;
  107. auto diff = 1.0f - total;
  108. auto amount = diff / static_cast<float> (indexToIgnore == -1 ? widths.size() : widths.size() - 1);
  109. for (int i = 0; i < widths.size(); ++i)
  110. {
  111. if (i != indexToIgnore)
  112. {
  113. auto val = widths.getUnchecked (i);
  114. widths.set (i, val + amount);
  115. }
  116. }
  117. }
  118. };
  119. //==============================================================================
  120. class InfoButton : public Button
  121. {
  122. public:
  123. InfoButton (const String& infoToDisplay = {})
  124. : Button ({})
  125. {
  126. if (infoToDisplay.isNotEmpty())
  127. setInfoToDisplay (infoToDisplay);
  128. }
  129. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  130. {
  131. auto bounds = getLocalBounds().toFloat().reduced (2);
  132. auto& icon = getIcons().info;
  133. g.setColour (findColour (treeIconColourId).withMultipliedAlpha (isMouseOverButton || isButtonDown ? 1.0f : 0.5f));
  134. if (isButtonDown)
  135. g.fillEllipse (bounds);
  136. else
  137. g.fillPath (icon, RectanglePlacement (RectanglePlacement::centred)
  138. .getTransformToFit (icon.getBounds(), bounds));
  139. }
  140. void clicked() override
  141. {
  142. auto w = std::make_unique<InfoWindow> (info);
  143. w->setSize (width, w->getHeight() * numLines + 10);
  144. CallOutBox::launchAsynchronously (std::move (w), getScreenBounds(), nullptr);
  145. }
  146. using Button::clicked;
  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, 15, 0.75f);
  178. }
  179. String stringToDisplay;
  180. };
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InfoButton)
  182. };
  183. //==============================================================================
  184. class PropertyGroupComponent : public Component,
  185. private TextPropertyComponent::Listener
  186. {
  187. public:
  188. PropertyGroupComponent (String name, Icon icon, String desc = {})
  189. : header (name, icon),
  190. description (desc)
  191. {
  192. addAndMakeVisible (header);
  193. description.setFont ({ 16.0f });
  194. description.setColour (getLookAndFeel().findColour (defaultTextColourId));
  195. description.setLineSpacing (5.0f);
  196. description.setJustification (Justification::centredLeft);
  197. }
  198. void setProperties (const PropertyListBuilder& newProps)
  199. {
  200. infoButtons.clear();
  201. properties.clear();
  202. properties.addArray (newProps.components);
  203. for (auto* prop : properties)
  204. {
  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 ({}); // set the tooltip to empty so it only displays when its button is clicked
  211. }
  212. if (auto* multiChoice = dynamic_cast<MultiChoicePropertyComponent*> (prop))
  213. multiChoice->onHeightChange = [this] { updateSize(); };
  214. if (auto* text = dynamic_cast<TextPropertyComponent*> (prop))
  215. if (text->isTextEditorMultiLine())
  216. text->addListener (this);
  217. }
  218. }
  219. int updateSize (int x, int y, int width)
  220. {
  221. header.setBounds (0, 0, width, headerSize);
  222. auto height = header.getBottom() + 10;
  223. descriptionLayout.createLayout (description, (float) (width - 40));
  224. auto descriptionHeight = (int) descriptionLayout.getHeight();
  225. if (descriptionHeight > 0)
  226. height += (int) descriptionLayout.getHeight() + 25;
  227. for (auto* pp : properties)
  228. {
  229. auto propertyHeight = pp->getPreferredHeight() + (getHeightMultiplier (pp) * pp->getPreferredHeight());
  230. InfoButton* buttonToUse = nullptr;
  231. for (auto* b : infoButtons)
  232. if (b->getAssociatedComponent() == pp)
  233. buttonToUse = b;
  234. if (buttonToUse != nullptr)
  235. {
  236. buttonToUse->setSize (20, 20);
  237. buttonToUse->setCentrePosition (20, height + (propertyHeight / 2));
  238. }
  239. pp->setBounds (40, height, width - 50, propertyHeight);
  240. if (shouldResizePropertyComponent (pp))
  241. resizePropertyComponent (pp);
  242. height += pp->getHeight() + 10;
  243. }
  244. height += 16;
  245. setBounds (x, y, width, jmax (height, getParentHeight()));
  246. return height;
  247. }
  248. void paint (Graphics& g) override
  249. {
  250. g.setColour (findColour (secondaryBackgroundColourId));
  251. g.fillRect (getLocalBounds());
  252. auto textArea = getLocalBounds().toFloat()
  253. .withTop ((float) headerSize)
  254. .reduced (20.0f, 10.0f)
  255. .withHeight (descriptionLayout.getHeight());
  256. descriptionLayout.draw (g, textArea);
  257. }
  258. OwnedArray<PropertyComponent> properties;
  259. private:
  260. //==============================================================================
  261. void textPropertyComponentChanged (TextPropertyComponent* comp) override
  262. {
  263. auto fontHeight = [comp]
  264. {
  265. Label tmpLabel;
  266. return comp->getLookAndFeel().getLabelFont (tmpLabel).getHeight();
  267. }();
  268. auto lines = StringArray::fromLines (comp->getText());
  269. comp->setPreferredHeight (jmax (100, 10 + roundToInt (fontHeight * (float) lines.size())));
  270. updateSize();
  271. }
  272. //==============================================================================
  273. void updateSize()
  274. {
  275. updateSize (getX(), getY(), getWidth());
  276. if (auto* parent = getParentComponent())
  277. parent->parentSizeChanged();
  278. }
  279. //==============================================================================
  280. bool shouldResizePropertyComponent (PropertyComponent* p)
  281. {
  282. if (auto* textComp = dynamic_cast<TextPropertyComponent*> (p))
  283. return ! textComp->isTextEditorMultiLine();
  284. return (dynamic_cast<ChoicePropertyComponent*> (p) != nullptr
  285. || dynamic_cast<ButtonPropertyComponent*> (p) != nullptr
  286. || dynamic_cast<BooleanPropertyComponent*> (p) != nullptr);
  287. }
  288. void resizePropertyComponent (PropertyComponent* pp)
  289. {
  290. for (auto i = pp->getNumChildComponents() - 1; i >= 0; --i)
  291. {
  292. auto* child = pp->getChildComponent (i);
  293. auto bounds = child->getBounds();
  294. child->setBounds (bounds.withSizeKeepingCentre (child->getWidth(), pp->getPreferredHeight()));
  295. }
  296. }
  297. int getHeightMultiplier (PropertyComponent* pp)
  298. {
  299. auto availableTextWidth = ProjucerLookAndFeel::getTextWidthForPropertyComponent (pp);
  300. auto font = ProjucerLookAndFeel::getPropertyComponentFont();
  301. auto nameWidth = font.getStringWidthFloat (pp->getName());
  302. if (availableTextWidth == 0)
  303. return 0;
  304. return static_cast<int> (nameWidth / (float) availableTextWidth);
  305. }
  306. OwnedArray<InfoButton> infoButtons;
  307. ContentViewHeader header;
  308. AttributedString description;
  309. TextLayout descriptionLayout;
  310. int headerSize = 40;
  311. //==============================================================================
  312. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyGroupComponent)
  313. };