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.

445 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. #include "../../Utility/UI/PropertyComponents/jucer_LabelPropertyComponent.h"
  20. //==============================================================================
  21. struct ContentViewHeader final : public Component
  22. {
  23. ContentViewHeader (String headerName, Icon headerIcon)
  24. : name (headerName), icon (headerIcon)
  25. {
  26. setTitle (name);
  27. }
  28. void paint (Graphics& g) override
  29. {
  30. g.fillAll (findColour (contentHeaderBackgroundColourId));
  31. auto bounds = getLocalBounds().reduced (20, 0);
  32. icon.withColour (Colours::white).draw (g, bounds.toFloat().removeFromRight (30), false);
  33. g.setColour (Colours::white);
  34. g.setFont (Font (18.0f));
  35. g.drawFittedText (name, bounds, Justification::centredLeft, 1);
  36. }
  37. String name;
  38. Icon icon;
  39. };
  40. //==============================================================================
  41. class ListBoxHeader final : public Component
  42. {
  43. public:
  44. ListBoxHeader (Array<String> columnHeaders)
  45. {
  46. for (auto s : columnHeaders)
  47. {
  48. addAndMakeVisible (headers.add (new Label (s, s)));
  49. widths.add (1.0f / (float) columnHeaders.size());
  50. }
  51. setSize (200, 40);
  52. }
  53. ListBoxHeader (Array<String> columnHeaders, Array<float> columnWidths)
  54. {
  55. jassert (columnHeaders.size() == columnWidths.size());
  56. for (const auto [index, s] : enumerate (columnHeaders))
  57. {
  58. addAndMakeVisible (headers.add (new Label (s, s)));
  59. widths.add (columnWidths.getUnchecked ((int) 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 ((float) 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 (int i = 0; i < index; ++i)
  90. prop += widths.getUnchecked (i);
  91. return roundToInt (prop * (float) 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 (approximatelyEqual (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 (int 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 final : public Button
  122. {
  123. public:
  124. InfoButton (const String& infoToDisplay = {})
  125. : Button ({})
  126. {
  127. setTitle ("Info");
  128. if (infoToDisplay.isNotEmpty())
  129. setInfoToDisplay (infoToDisplay);
  130. setSize (20, 20);
  131. }
  132. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  133. {
  134. auto bounds = getLocalBounds().toFloat().reduced (2);
  135. auto& icon = getIcons().info;
  136. g.setColour (findColour (treeIconColourId).withMultipliedAlpha (isMouseOverButton || isButtonDown ? 1.0f : 0.5f));
  137. if (isButtonDown)
  138. g.fillEllipse (bounds);
  139. else
  140. g.fillPath (icon, RectanglePlacement (RectanglePlacement::centred)
  141. .getTransformToFit (icon.getBounds(), bounds));
  142. }
  143. void clicked() override
  144. {
  145. auto w = std::make_unique<InfoWindow> (info);
  146. w->setSize (width, w->getHeight() * numLines + 10);
  147. CallOutBox::launchAsynchronously (std::move (w), getScreenBounds(), nullptr);
  148. }
  149. using Button::clicked;
  150. void setInfoToDisplay (const String& infoToDisplay)
  151. {
  152. if (infoToDisplay.isNotEmpty())
  153. {
  154. info = infoToDisplay;
  155. auto stringWidth = roundToInt (Font (14.0f).getStringWidthFloat (info));
  156. width = jmin (300, stringWidth);
  157. numLines += static_cast<int> (stringWidth / width);
  158. setHelpText (info);
  159. }
  160. }
  161. void setAssociatedComponent (Component* comp) { associatedComponent = comp; }
  162. Component* getAssociatedComponent() { return associatedComponent; }
  163. private:
  164. String info;
  165. Component* associatedComponent = nullptr;
  166. int width;
  167. int numLines = 1;
  168. //==============================================================================
  169. struct InfoWindow final : public Component
  170. {
  171. InfoWindow (const String& s)
  172. : stringToDisplay (s)
  173. {
  174. setSize (150, 14);
  175. }
  176. void paint (Graphics& g) override
  177. {
  178. g.fillAll (findColour (secondaryBackgroundColourId));
  179. g.setColour (findColour (defaultTextColourId));
  180. g.setFont (Font (14.0f));
  181. g.drawFittedText (stringToDisplay, getLocalBounds(), Justification::centred, 15, 0.75f);
  182. }
  183. String stringToDisplay;
  184. };
  185. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InfoButton)
  186. };
  187. //==============================================================================
  188. class PropertyGroupComponent final : public Component,
  189. private TextPropertyComponent::Listener
  190. {
  191. public:
  192. PropertyGroupComponent (String name, Icon icon, String desc = {})
  193. : header (name, icon),
  194. description (desc)
  195. {
  196. addAndMakeVisible (header);
  197. }
  198. void setProperties (const PropertyListBuilder& newProps)
  199. {
  200. clearProperties();
  201. if (description.isNotEmpty())
  202. properties.push_back (std::make_unique<LabelPropertyComponent> (description, 16, Font (16.0f),
  203. Justification::centredLeft));
  204. for (auto* comp : newProps.components)
  205. properties.push_back (std::unique_ptr<PropertyComponent> (comp));
  206. for (auto& prop : properties)
  207. {
  208. const auto propertyTooltip = prop->getTooltip();
  209. if (propertyTooltip.isNotEmpty())
  210. {
  211. // set the tooltip to empty so it only displays when its button is clicked
  212. prop->setTooltip ({});
  213. auto infoButton = std::make_unique<InfoButton> (propertyTooltip);
  214. infoButton->setAssociatedComponent (prop.get());
  215. auto propertyAndInfoWrapper = std::make_unique<PropertyAndInfoWrapper> (*prop, *infoButton.get());
  216. addAndMakeVisible (propertyAndInfoWrapper.get());
  217. propertyComponentsWithInfo.push_back (std::move (propertyAndInfoWrapper));
  218. infoButtons.push_back (std::move (infoButton));
  219. }
  220. else
  221. {
  222. addAndMakeVisible (prop.get());
  223. }
  224. if (auto* multiChoice = dynamic_cast<MultiChoicePropertyComponent*> (prop.get()))
  225. multiChoice->onHeightChange = [this] { updateSize(); };
  226. if (auto* text = dynamic_cast<TextPropertyComponent*> (prop.get()))
  227. if (text->isTextEditorMultiLine())
  228. text->addListener (this);
  229. }
  230. }
  231. int updateSize (int x, int y, int width)
  232. {
  233. header.setBounds (0, 0, width, headerSize);
  234. auto height = header.getBottom() + 10;
  235. for (auto& pp : properties)
  236. {
  237. const auto propertyHeight = jmax (pp->getPreferredHeight(), getApproximateLabelHeight (*pp));
  238. auto iter = std::find_if (propertyComponentsWithInfo.begin(), propertyComponentsWithInfo.end(),
  239. [&pp] (const std::unique_ptr<PropertyAndInfoWrapper>& w) { return &w->propertyComponent == pp.get(); });
  240. if (iter != propertyComponentsWithInfo.end())
  241. (*iter)->setBounds (0, height, width - 10, propertyHeight);
  242. else
  243. pp->setBounds (40, height, width - 50, propertyHeight);
  244. if (shouldResizePropertyComponent (pp.get()))
  245. resizePropertyComponent (pp.get());
  246. height += pp->getHeight() + 10;
  247. }
  248. height += 16;
  249. setBounds (x, y, width, jmax (height, getParentHeight()));
  250. return height;
  251. }
  252. void paint (Graphics& g) override
  253. {
  254. g.fillAll (findColour (secondaryBackgroundColourId));
  255. }
  256. const std::vector<std::unique_ptr<PropertyComponent>>& getProperties() const noexcept
  257. {
  258. return properties;
  259. }
  260. void clearProperties()
  261. {
  262. propertyComponentsWithInfo.clear();
  263. infoButtons.clear();
  264. properties.clear();
  265. }
  266. private:
  267. //==============================================================================
  268. struct PropertyAndInfoWrapper final : public Component
  269. {
  270. PropertyAndInfoWrapper (PropertyComponent& c, InfoButton& i)
  271. : propertyComponent (c),
  272. infoButton (i)
  273. {
  274. setFocusContainerType (FocusContainerType::focusContainer);
  275. setTitle (propertyComponent.getName());
  276. addAndMakeVisible (propertyComponent);
  277. addAndMakeVisible (infoButton);
  278. }
  279. void resized() override
  280. {
  281. auto bounds = getLocalBounds();
  282. bounds.removeFromLeft (40);
  283. bounds.removeFromRight (10);
  284. propertyComponent.setBounds (bounds);
  285. infoButton.setCentrePosition (20, bounds.getHeight() / 2);
  286. }
  287. PropertyComponent& propertyComponent;
  288. InfoButton& infoButton;
  289. };
  290. //==============================================================================
  291. void textPropertyComponentChanged (TextPropertyComponent* comp) override
  292. {
  293. auto fontHeight = [comp]
  294. {
  295. Label tmpLabel;
  296. return comp->getLookAndFeel().getLabelFont (tmpLabel).getHeight();
  297. }();
  298. auto lines = StringArray::fromLines (comp->getText());
  299. comp->setPreferredHeight (jmax (100, 10 + roundToInt (fontHeight * (float) lines.size())));
  300. updateSize();
  301. }
  302. void updateSize()
  303. {
  304. updateSize (getX(), getY(), getWidth());
  305. if (auto* parent = getParentComponent())
  306. parent->parentSizeChanged();
  307. }
  308. bool shouldResizePropertyComponent (PropertyComponent* p)
  309. {
  310. if (auto* textComp = dynamic_cast<TextPropertyComponent*> (p))
  311. return ! textComp->isTextEditorMultiLine();
  312. return (dynamic_cast<ChoicePropertyComponent*> (p) != nullptr
  313. || dynamic_cast<ButtonPropertyComponent*> (p) != nullptr
  314. || dynamic_cast<BooleanPropertyComponent*> (p) != nullptr);
  315. }
  316. void resizePropertyComponent (PropertyComponent* pp)
  317. {
  318. for (auto i = pp->getNumChildComponents() - 1; i >= 0; --i)
  319. {
  320. auto* child = pp->getChildComponent (i);
  321. auto bounds = child->getBounds();
  322. child->setBounds (bounds.withSizeKeepingCentre (child->getWidth(), pp->getPreferredHeight()));
  323. }
  324. }
  325. static int getApproximateLabelHeight (const PropertyComponent& pp)
  326. {
  327. auto availableTextWidth = ProjucerLookAndFeel::getTextWidthForPropertyComponent (pp);
  328. if (availableTextWidth == 0)
  329. return 0;
  330. const auto font = ProjucerLookAndFeel::getPropertyComponentFont();
  331. const auto labelWidth = font.getStringWidthFloat (pp.getName());
  332. const auto numLines = (int) (labelWidth / (float) availableTextWidth) + 1;
  333. return (int) std::round ((float) numLines * font.getHeight() * 1.1f);
  334. }
  335. //==============================================================================
  336. static constexpr int headerSize = 40;
  337. std::vector<std::unique_ptr<PropertyComponent>> properties;
  338. std::vector<std::unique_ptr<InfoButton>> infoButtons;
  339. std::vector<std::unique_ptr<PropertyAndInfoWrapper>> propertyComponentsWithInfo;
  340. ContentViewHeader header;
  341. String description;
  342. //==============================================================================
  343. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyGroupComponent)
  344. };