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.

381 lines
12KB

  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 = new InfoWindow (info);
  143. w->setSize (width, w->getHeight() * numLines + 10);
  144. CallOutBox::launchAsynchronously (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, 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 ({ 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* prop : properties)
  203. {
  204. addAndMakeVisible (prop);
  205. if (! prop->getTooltip().isEmpty())
  206. {
  207. addAndMakeVisible (infoButtons.add (new InfoButton (prop->getTooltip())));
  208. infoButtons.getLast()->setAssociatedComponent (prop);
  209. prop->setTooltip ({}); // set the tooltip to empty so it only displays when its button is clicked
  210. }
  211. if (auto* multiChoice = dynamic_cast<MultiChoicePropertyComponent*> (prop))
  212. {
  213. multiChoice->onHeightChange = [this]
  214. {
  215. updateSize (getX(), getY(), getWidth());
  216. if (auto* parent = getParentComponent())
  217. parent->parentSizeChanged();
  218. };
  219. }
  220. }
  221. }
  222. int updateSize (int x, int y, int width)
  223. {
  224. header.setBounds (0, 0, width, headerSize);
  225. auto height = header.getBottom() + 10;
  226. descriptionLayout.createLayout (description, (float) (width - 40));
  227. auto descriptionHeight = (int) descriptionLayout.getHeight();
  228. if (descriptionHeight > 0)
  229. height += (int) descriptionLayout.getHeight() + 25;
  230. for (auto* pp : properties)
  231. {
  232. auto propertyHeight = pp->getPreferredHeight() + (getHeightMultiplier (pp) * pp->getPreferredHeight());
  233. InfoButton* buttonToUse = nullptr;
  234. for (auto* b : infoButtons)
  235. if (b->getAssociatedComponent() == pp)
  236. buttonToUse = b;
  237. if (buttonToUse != nullptr)
  238. {
  239. buttonToUse->setSize (20, 20);
  240. buttonToUse->setCentrePosition (20, height + (propertyHeight / 2));
  241. }
  242. pp->setBounds (40, height, width - 50, propertyHeight);
  243. if (shouldResizePropertyComponent (pp))
  244. resizePropertyComponent (pp);
  245. height += pp->getHeight() + 10;
  246. }
  247. height += 16;
  248. setBounds (x, y, width, jmax (height, getParentHeight()));
  249. return height;
  250. }
  251. void paint (Graphics& g) override
  252. {
  253. g.setColour (findColour (secondaryBackgroundColourId));
  254. g.fillRect (getLocalBounds());
  255. auto textArea = getLocalBounds().toFloat()
  256. .withTop ((float) headerSize)
  257. .reduced (20.0f, 10.0f)
  258. .withHeight (descriptionLayout.getHeight());
  259. descriptionLayout.draw (g, textArea);
  260. }
  261. OwnedArray<PropertyComponent> properties;
  262. private:
  263. OwnedArray<InfoButton> infoButtons;
  264. ContentViewHeader header;
  265. AttributedString description;
  266. TextLayout descriptionLayout;
  267. int headerSize = 40;
  268. //==============================================================================
  269. bool shouldResizePropertyComponent (PropertyComponent* p)
  270. {
  271. if (auto* textComp = dynamic_cast<TextPropertyComponent*> (p))
  272. return ! textComp->isTextEditorMultiLine();
  273. return (dynamic_cast<ChoicePropertyComponent*> (p) != nullptr
  274. || dynamic_cast<ButtonPropertyComponent*> (p) != nullptr
  275. || dynamic_cast<BooleanPropertyComponent*> (p) != nullptr);
  276. }
  277. void resizePropertyComponent (PropertyComponent* pp)
  278. {
  279. for (auto i = pp->getNumChildComponents() - 1; i >= 0; --i)
  280. {
  281. auto* child = pp->getChildComponent (i);
  282. auto bounds = child->getBounds();
  283. child->setBounds (bounds.withSizeKeepingCentre (child->getWidth(), pp->getPreferredHeight()));
  284. }
  285. }
  286. int getHeightMultiplier (PropertyComponent* pp)
  287. {
  288. auto availableTextWidth = ProjucerLookAndFeel::getTextWidthForPropertyComponent (pp);
  289. auto font = ProjucerLookAndFeel::getPropertyComponentFont();
  290. auto nameWidth = font.getStringWidthFloat (pp->getName());
  291. if (availableTextWidth == 0)
  292. return 0;
  293. return static_cast<int> (nameWidth / (float) availableTextWidth);
  294. }
  295. //==============================================================================
  296. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyGroupComponent)
  297. };