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.

374 lines
11KB

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