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.

319 lines
10KB

  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. #include "../Project/jucer_Project.h"
  21. //==============================================================================
  22. struct ContentViewHeader : public Component
  23. {
  24. ContentViewHeader (String headerName, Icon headerIcon)
  25. : name (headerName), icon (headerIcon)
  26. {
  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 : 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 / 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 (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 (auto i = 0; i < index; ++i)
  91. prop += widths.getUnchecked (i);
  92. return roundToInt (prop * 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 (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 (auto 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 GroupInformationComponent : public Component,
  123. private ListBoxModel,
  124. private ValueTree::Listener
  125. {
  126. public:
  127. GroupInformationComponent (const Project::Item& group)
  128. : item (group),
  129. header (item.getName(), Icon (getIcons().openFolder, Colours::transparentBlack))
  130. {
  131. list.setHeaderComponent (new ListBoxHeader ( { "File", "Binary Resource", "Xcode Resource", "Compile" },
  132. { 0.4f, 0.2f, 0.2f, 0.2f } ));
  133. list.setModel (this);
  134. list.setColour (ListBox::backgroundColourId, Colours::transparentBlack);
  135. addAndMakeVisible (list);
  136. list.updateContent();
  137. list.setRowHeight (30);
  138. item.state.addListener (this);
  139. lookAndFeelChanged();
  140. addAndMakeVisible (header);
  141. }
  142. ~GroupInformationComponent()
  143. {
  144. item.state.removeListener (this);
  145. }
  146. //==============================================================================
  147. void paint (Graphics& g) override
  148. {
  149. g.setColour (findColour (secondaryBackgroundColourId));
  150. g.fillRect (getLocalBounds().reduced (12, 0));
  151. }
  152. void resized() override
  153. {
  154. auto bounds = getLocalBounds().reduced (12, 0);
  155. header.setBounds (bounds.removeFromTop (40));
  156. list.setBounds (bounds.reduced (10, 4));
  157. }
  158. void parentSizeChanged() override
  159. {
  160. setSize (jmax (550, getParentWidth()), getParentHeight());
  161. }
  162. int getNumRows() override
  163. {
  164. return item.getNumChildren();
  165. }
  166. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool /*rowIsSelected*/) override
  167. {
  168. g.setColour (findColour (rowNumber % 2 == 0 ? widgetBackgroundColourId
  169. : secondaryWidgetBackgroundColourId));
  170. g.fillRect (0, 0, width, height - 1);
  171. }
  172. Component* refreshComponentForRow (int rowNumber, bool /*isRowSelected*/, Component* existingComponentToUpdate) override
  173. {
  174. ScopedPointer<Component> existing (existingComponentToUpdate);
  175. if (rowNumber < getNumRows())
  176. {
  177. Project::Item child (item.getChild (rowNumber));
  178. if (existingComponentToUpdate == nullptr
  179. || dynamic_cast<FileOptionComponent*> (existing.get())->item != child)
  180. {
  181. existing = nullptr;
  182. existing = new FileOptionComponent (child, dynamic_cast<ListBoxHeader*> (list.getHeaderComponent()));
  183. }
  184. }
  185. return existing.release();
  186. }
  187. //==============================================================================
  188. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { itemChanged(); }
  189. void valueTreeChildAdded (ValueTree&, ValueTree&) override { itemChanged(); }
  190. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override { itemChanged(); }
  191. void valueTreeChildOrderChanged (ValueTree&, int, int) override { itemChanged(); }
  192. void valueTreeParentChanged (ValueTree&) override { itemChanged(); }
  193. private:
  194. Project::Item item;
  195. ListBox list;
  196. ContentViewHeader header;
  197. void itemChanged()
  198. {
  199. list.updateContent();
  200. repaint();
  201. }
  202. //==============================================================================
  203. class FileOptionComponent : public Component
  204. {
  205. public:
  206. FileOptionComponent (const Project::Item& fileItem, ListBoxHeader* listBoxHeader)
  207. : item (fileItem),
  208. header (listBoxHeader)
  209. {
  210. if (item.isFile())
  211. {
  212. addAndMakeVisible (compileButton);
  213. compileButton.getToggleStateValue().referTo (item.getShouldCompileValue());
  214. addAndMakeVisible (binaryResourceButton);
  215. binaryResourceButton.getToggleStateValue().referTo (item.getShouldAddToBinaryResourcesValue());
  216. addAndMakeVisible (xcodeResourceButton);
  217. xcodeResourceButton.getToggleStateValue().referTo (item.getShouldAddToXcodeResourcesValue());
  218. }
  219. }
  220. void paint (Graphics& g) override
  221. {
  222. if (header != nullptr)
  223. {
  224. auto textBounds = getLocalBounds().removeFromLeft (roundToInt (header->getProportionAtIndex (0) * getWidth()));
  225. auto iconBounds = textBounds.removeFromLeft (25);
  226. if (item.isImageFile())
  227. iconBounds.reduce (5, 5);
  228. item.getIcon().withColour (findColour (treeIconColourId)).draw (g, iconBounds.toFloat(), item.isIconCrossedOut());
  229. g.setColour (findColour (widgetTextColourId));
  230. g.drawText (item.getName(), textBounds, Justification::centredLeft);
  231. }
  232. }
  233. void resized() override
  234. {
  235. if (header != nullptr)
  236. {
  237. auto bounds = getLocalBounds();
  238. auto width = getWidth();
  239. bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (0) * width));
  240. binaryResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (1) * width)));
  241. xcodeResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (2) * width)));
  242. compileButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (3) * width)));
  243. }
  244. }
  245. Project::Item item;
  246. private:
  247. ListBoxHeader* header;
  248. ToggleButton compileButton, binaryResourceButton, xcodeResourceButton;
  249. };
  250. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GroupInformationComponent)
  251. };