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.

380 lines
12KB

  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 (int 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 (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 : public Button
  122. {
  123. public:
  124. InfoButton (const String& infoToDisplay = {})
  125. : Button ({})
  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. 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 ({ 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 / availableTextWidth);
  294. }
  295. //==============================================================================
  296. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyGroupComponent)
  297. };