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.

446 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. auto index = 0;
  57. for (auto s : columnHeaders)
  58. {
  59. addAndMakeVisible (headers.add (new Label (s, s)));
  60. widths.add (columnWidths.getUnchecked (index++));
  61. }
  62. recalculateWidths();
  63. setSize (200, 40);
  64. }
  65. void resized() override
  66. {
  67. auto bounds = getLocalBounds();
  68. auto width = bounds.getWidth();
  69. auto index = 0;
  70. for (auto h : headers)
  71. {
  72. auto headerWidth = roundToInt ((float) width * widths.getUnchecked (index));
  73. h->setBounds (bounds.removeFromLeft (headerWidth));
  74. ++index;
  75. }
  76. }
  77. void setColumnHeaderWidth (int index, float proportionOfWidth)
  78. {
  79. if (! (isPositiveAndBelow (index, headers.size()) && isPositiveAndNotGreaterThan (proportionOfWidth, 1.0f)))
  80. {
  81. jassertfalse;
  82. return;
  83. }
  84. widths.set (index, proportionOfWidth);
  85. recalculateWidths (index);
  86. }
  87. int getColumnX (int index)
  88. {
  89. auto prop = 0.0f;
  90. for (int i = 0; i < index; ++i)
  91. prop += widths.getUnchecked (i);
  92. return roundToInt (prop * (float) getWidth());
  93. }
  94. float getProportionAtIndex (int index)
  95. {
  96. jassert (isPositiveAndBelow (index, widths.size()));
  97. return widths.getUnchecked (index);
  98. }
  99. private:
  100. OwnedArray<Label> headers;
  101. Array<float> widths;
  102. void recalculateWidths (int indexToIgnore = -1)
  103. {
  104. auto total = 0.0f;
  105. for (auto w : widths)
  106. total += w;
  107. if (approximatelyEqual (total, 1.0f))
  108. return;
  109. auto diff = 1.0f - total;
  110. auto amount = diff / static_cast<float> (indexToIgnore == -1 ? widths.size() : widths.size() - 1);
  111. for (int i = 0; i < widths.size(); ++i)
  112. {
  113. if (i != indexToIgnore)
  114. {
  115. auto val = widths.getUnchecked (i);
  116. widths.set (i, val + amount);
  117. }
  118. }
  119. }
  120. };
  121. //==============================================================================
  122. class InfoButton final : public Button
  123. {
  124. public:
  125. InfoButton (const String& infoToDisplay = {})
  126. : Button ({})
  127. {
  128. setTitle ("Info");
  129. if (infoToDisplay.isNotEmpty())
  130. setInfoToDisplay (infoToDisplay);
  131. setSize (20, 20);
  132. }
  133. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  134. {
  135. auto bounds = getLocalBounds().toFloat().reduced (2);
  136. auto& icon = getIcons().info;
  137. g.setColour (findColour (treeIconColourId).withMultipliedAlpha (isMouseOverButton || isButtonDown ? 1.0f : 0.5f));
  138. if (isButtonDown)
  139. g.fillEllipse (bounds);
  140. else
  141. g.fillPath (icon, RectanglePlacement (RectanglePlacement::centred)
  142. .getTransformToFit (icon.getBounds(), bounds));
  143. }
  144. void clicked() override
  145. {
  146. auto w = std::make_unique<InfoWindow> (info);
  147. w->setSize (width, w->getHeight() * numLines + 10);
  148. CallOutBox::launchAsynchronously (std::move (w), getScreenBounds(), nullptr);
  149. }
  150. using Button::clicked;
  151. void setInfoToDisplay (const String& infoToDisplay)
  152. {
  153. if (infoToDisplay.isNotEmpty())
  154. {
  155. info = infoToDisplay;
  156. auto stringWidth = roundToInt (Font (14.0f).getStringWidthFloat (info));
  157. width = jmin (300, stringWidth);
  158. numLines += static_cast<int> (stringWidth / width);
  159. setHelpText (info);
  160. }
  161. }
  162. void setAssociatedComponent (Component* comp) { associatedComponent = comp; }
  163. Component* getAssociatedComponent() { return associatedComponent; }
  164. private:
  165. String info;
  166. Component* associatedComponent = nullptr;
  167. int width;
  168. int numLines = 1;
  169. //==============================================================================
  170. struct InfoWindow final : public Component
  171. {
  172. InfoWindow (const String& s)
  173. : stringToDisplay (s)
  174. {
  175. setSize (150, 14);
  176. }
  177. void paint (Graphics& g) override
  178. {
  179. g.fillAll (findColour (secondaryBackgroundColourId));
  180. g.setColour (findColour (defaultTextColourId));
  181. g.setFont (Font (14.0f));
  182. g.drawFittedText (stringToDisplay, getLocalBounds(), Justification::centred, 15, 0.75f);
  183. }
  184. String stringToDisplay;
  185. };
  186. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InfoButton)
  187. };
  188. //==============================================================================
  189. class PropertyGroupComponent final : public Component,
  190. private TextPropertyComponent::Listener
  191. {
  192. public:
  193. PropertyGroupComponent (String name, Icon icon, String desc = {})
  194. : header (name, icon),
  195. description (desc)
  196. {
  197. addAndMakeVisible (header);
  198. }
  199. void setProperties (const PropertyListBuilder& newProps)
  200. {
  201. clearProperties();
  202. if (description.isNotEmpty())
  203. properties.push_back (std::make_unique<LabelPropertyComponent> (description, 16, Font (16.0f),
  204. Justification::centredLeft));
  205. for (auto* comp : newProps.components)
  206. properties.push_back (std::unique_ptr<PropertyComponent> (comp));
  207. for (auto& prop : properties)
  208. {
  209. const auto propertyTooltip = prop->getTooltip();
  210. if (propertyTooltip.isNotEmpty())
  211. {
  212. // set the tooltip to empty so it only displays when its button is clicked
  213. prop->setTooltip ({});
  214. auto infoButton = std::make_unique<InfoButton> (propertyTooltip);
  215. infoButton->setAssociatedComponent (prop.get());
  216. auto propertyAndInfoWrapper = std::make_unique<PropertyAndInfoWrapper> (*prop, *infoButton.get());
  217. addAndMakeVisible (propertyAndInfoWrapper.get());
  218. propertyComponentsWithInfo.push_back (std::move (propertyAndInfoWrapper));
  219. infoButtons.push_back (std::move (infoButton));
  220. }
  221. else
  222. {
  223. addAndMakeVisible (prop.get());
  224. }
  225. if (auto* multiChoice = dynamic_cast<MultiChoicePropertyComponent*> (prop.get()))
  226. multiChoice->onHeightChange = [this] { updateSize(); };
  227. if (auto* text = dynamic_cast<TextPropertyComponent*> (prop.get()))
  228. if (text->isTextEditorMultiLine())
  229. text->addListener (this);
  230. }
  231. }
  232. int updateSize (int x, int y, int width)
  233. {
  234. header.setBounds (0, 0, width, headerSize);
  235. auto height = header.getBottom() + 10;
  236. for (auto& pp : properties)
  237. {
  238. const auto propertyHeight = jmax (pp->getPreferredHeight(), getApproximateLabelHeight (*pp));
  239. auto iter = std::find_if (propertyComponentsWithInfo.begin(), propertyComponentsWithInfo.end(),
  240. [&pp] (const std::unique_ptr<PropertyAndInfoWrapper>& w) { return &w->propertyComponent == pp.get(); });
  241. if (iter != propertyComponentsWithInfo.end())
  242. (*iter)->setBounds (0, height, width - 10, propertyHeight);
  243. else
  244. pp->setBounds (40, height, width - 50, propertyHeight);
  245. if (shouldResizePropertyComponent (pp.get()))
  246. resizePropertyComponent (pp.get());
  247. height += pp->getHeight() + 10;
  248. }
  249. height += 16;
  250. setBounds (x, y, width, jmax (height, getParentHeight()));
  251. return height;
  252. }
  253. void paint (Graphics& g) override
  254. {
  255. g.fillAll (findColour (secondaryBackgroundColourId));
  256. }
  257. const std::vector<std::unique_ptr<PropertyComponent>>& getProperties() const noexcept
  258. {
  259. return properties;
  260. }
  261. void clearProperties()
  262. {
  263. propertyComponentsWithInfo.clear();
  264. infoButtons.clear();
  265. properties.clear();
  266. }
  267. private:
  268. //==============================================================================
  269. struct PropertyAndInfoWrapper final : public Component
  270. {
  271. PropertyAndInfoWrapper (PropertyComponent& c, InfoButton& i)
  272. : propertyComponent (c),
  273. infoButton (i)
  274. {
  275. setFocusContainerType (FocusContainerType::focusContainer);
  276. setTitle (propertyComponent.getName());
  277. addAndMakeVisible (propertyComponent);
  278. addAndMakeVisible (infoButton);
  279. }
  280. void resized() override
  281. {
  282. auto bounds = getLocalBounds();
  283. bounds.removeFromLeft (40);
  284. bounds.removeFromRight (10);
  285. propertyComponent.setBounds (bounds);
  286. infoButton.setCentrePosition (20, bounds.getHeight() / 2);
  287. }
  288. PropertyComponent& propertyComponent;
  289. InfoButton& infoButton;
  290. };
  291. //==============================================================================
  292. void textPropertyComponentChanged (TextPropertyComponent* comp) override
  293. {
  294. auto fontHeight = [comp]
  295. {
  296. Label tmpLabel;
  297. return comp->getLookAndFeel().getLabelFont (tmpLabel).getHeight();
  298. }();
  299. auto lines = StringArray::fromLines (comp->getText());
  300. comp->setPreferredHeight (jmax (100, 10 + roundToInt (fontHeight * (float) lines.size())));
  301. updateSize();
  302. }
  303. void updateSize()
  304. {
  305. updateSize (getX(), getY(), getWidth());
  306. if (auto* parent = getParentComponent())
  307. parent->parentSizeChanged();
  308. }
  309. bool shouldResizePropertyComponent (PropertyComponent* p)
  310. {
  311. if (auto* textComp = dynamic_cast<TextPropertyComponent*> (p))
  312. return ! textComp->isTextEditorMultiLine();
  313. return (dynamic_cast<ChoicePropertyComponent*> (p) != nullptr
  314. || dynamic_cast<ButtonPropertyComponent*> (p) != nullptr
  315. || dynamic_cast<BooleanPropertyComponent*> (p) != nullptr);
  316. }
  317. void resizePropertyComponent (PropertyComponent* pp)
  318. {
  319. for (auto i = pp->getNumChildComponents() - 1; i >= 0; --i)
  320. {
  321. auto* child = pp->getChildComponent (i);
  322. auto bounds = child->getBounds();
  323. child->setBounds (bounds.withSizeKeepingCentre (child->getWidth(), pp->getPreferredHeight()));
  324. }
  325. }
  326. static int getApproximateLabelHeight (const PropertyComponent& pp)
  327. {
  328. auto availableTextWidth = ProjucerLookAndFeel::getTextWidthForPropertyComponent (pp);
  329. if (availableTextWidth == 0)
  330. return 0;
  331. const auto font = ProjucerLookAndFeel::getPropertyComponentFont();
  332. const auto labelWidth = font.getStringWidthFloat (pp.getName());
  333. const auto numLines = (int) (labelWidth / (float) availableTextWidth) + 1;
  334. return (int) std::round ((float) numLines * font.getHeight() * 1.1f);
  335. }
  336. //==============================================================================
  337. static constexpr int headerSize = 40;
  338. std::vector<std::unique_ptr<PropertyComponent>> properties;
  339. std::vector<std::unique_ptr<InfoButton>> infoButtons;
  340. std::vector<std::unique_ptr<PropertyAndInfoWrapper>> propertyComponentsWithInfo;
  341. ContentViewHeader header;
  342. String description;
  343. //==============================================================================
  344. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyGroupComponent)
  345. };