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.

317 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. #include "../Project/jucer_Project.h"
  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 / 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 (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 (auto i = 0; i < index; ++i)
  89. prop += widths.getUnchecked (i);
  90. return roundToInt (prop * 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 (auto 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 GroupInformationComponent : public Component,
  121. private ListBoxModel,
  122. private ValueTree::Listener
  123. {
  124. public:
  125. GroupInformationComponent (const Project::Item& group)
  126. : item (group),
  127. header (item.getName(), Icon (getIcons().openFolder, Colours::transparentBlack))
  128. {
  129. list.setHeaderComponent (new ListBoxHeader ( { "File", "Binary Resource", "Xcode Resource", "Compile" },
  130. { 0.4f, 0.2f, 0.2f, 0.2f } ));
  131. list.setModel (this);
  132. list.setColour (ListBox::backgroundColourId, Colours::transparentBlack);
  133. addAndMakeVisible (list);
  134. list.updateContent();
  135. list.setRowHeight (30);
  136. item.state.addListener (this);
  137. lookAndFeelChanged();
  138. addAndMakeVisible (header);
  139. }
  140. ~GroupInformationComponent()
  141. {
  142. item.state.removeListener (this);
  143. }
  144. //==============================================================================
  145. void paint (Graphics& g) override
  146. {
  147. g.setColour (findColour (secondaryBackgroundColourId));
  148. g.fillRect (getLocalBounds().reduced (12, 0));
  149. }
  150. void resized() override
  151. {
  152. auto bounds = getLocalBounds().reduced (12, 0);
  153. header.setBounds (bounds.removeFromTop (40));
  154. list.setBounds (bounds.reduced (10, 4));
  155. }
  156. void parentSizeChanged() override
  157. {
  158. setSize (jmax (550, getParentWidth()), getParentHeight());
  159. }
  160. int getNumRows() override
  161. {
  162. return item.getNumChildren();
  163. }
  164. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool /*rowIsSelected*/) override
  165. {
  166. g.setColour (findColour (rowNumber % 2 == 0 ? widgetBackgroundColourId
  167. : secondaryWidgetBackgroundColourId));
  168. g.fillRect (0, 0, width, height - 1);
  169. }
  170. Component* refreshComponentForRow (int rowNumber, bool /*isRowSelected*/, Component* existingComponentToUpdate) override
  171. {
  172. ScopedPointer<Component> existing (existingComponentToUpdate);
  173. if (rowNumber < getNumRows())
  174. {
  175. Project::Item child (item.getChild (rowNumber));
  176. if (existingComponentToUpdate == nullptr
  177. || dynamic_cast<FileOptionComponent*> (existing.get())->item != child)
  178. {
  179. existing = nullptr;
  180. existing = new FileOptionComponent (child, dynamic_cast<ListBoxHeader*> (list.getHeaderComponent()));
  181. }
  182. }
  183. return existing.release();
  184. }
  185. //==============================================================================
  186. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { itemChanged(); }
  187. void valueTreeChildAdded (ValueTree&, ValueTree&) override { itemChanged(); }
  188. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override { itemChanged(); }
  189. void valueTreeChildOrderChanged (ValueTree&, int, int) override { itemChanged(); }
  190. void valueTreeParentChanged (ValueTree&) override { itemChanged(); }
  191. private:
  192. Project::Item item;
  193. ListBox list;
  194. ContentViewHeader header;
  195. void itemChanged()
  196. {
  197. list.updateContent();
  198. repaint();
  199. }
  200. //==============================================================================
  201. class FileOptionComponent : public Component
  202. {
  203. public:
  204. FileOptionComponent (const Project::Item& fileItem, ListBoxHeader* listBoxHeader)
  205. : item (fileItem),
  206. header (listBoxHeader)
  207. {
  208. if (item.isFile())
  209. {
  210. addAndMakeVisible (compileButton);
  211. compileButton.getToggleStateValue().referTo (item.getShouldCompileValue());
  212. addAndMakeVisible (binaryResourceButton);
  213. binaryResourceButton.getToggleStateValue().referTo (item.getShouldAddToBinaryResourcesValue());
  214. addAndMakeVisible (xcodeResourceButton);
  215. xcodeResourceButton.getToggleStateValue().referTo (item.getShouldAddToXcodeResourcesValue());
  216. }
  217. }
  218. void paint (Graphics& g) override
  219. {
  220. if (header != nullptr)
  221. {
  222. auto textBounds = getLocalBounds().removeFromLeft (roundToInt (header->getProportionAtIndex (0) * getWidth()));
  223. auto iconBounds = textBounds.removeFromLeft (25);
  224. if (item.isImageFile())
  225. iconBounds.reduce (5, 5);
  226. item.getIcon().withColour (findColour (treeIconColourId)).draw (g, iconBounds.toFloat(), item.isIconCrossedOut());
  227. g.setColour (findColour (widgetTextColourId));
  228. g.drawText (item.getName(), textBounds, Justification::centredLeft);
  229. }
  230. }
  231. void resized() override
  232. {
  233. if (header != nullptr)
  234. {
  235. auto bounds = getLocalBounds();
  236. auto width = getWidth();
  237. bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (0) * width));
  238. binaryResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (1) * width)));
  239. xcodeResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (2) * width)));
  240. compileButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (3) * width)));
  241. }
  242. }
  243. Project::Item item;
  244. private:
  245. ListBoxHeader* header;
  246. ToggleButton compileButton, binaryResourceButton, xcodeResourceButton;
  247. };
  248. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GroupInformationComponent)
  249. };