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.

343 lines
13KB

  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. class FileGroupInformationComponent : public Component,
  21. private ListBoxModel,
  22. private ValueTree::Listener
  23. {
  24. public:
  25. FileGroupInformationComponent (const Project::Item& group)
  26. : item (group),
  27. header (item.getName(), { getIcons().openFolder, Colours::transparentBlack })
  28. {
  29. list.setHeaderComponent (std::make_unique<ListBoxHeader> (Array<String> { "File", "Binary Resource", "Xcode Resource", "Compile", "Compiler Flag Scheme" },
  30. Array<float> { 0.3f, 0.15f, 0.15f, 0.15f, 0.25f }));
  31. list.setModel (this);
  32. list.setColour (ListBox::backgroundColourId, Colours::transparentBlack);
  33. addAndMakeVisible (list);
  34. list.updateContent();
  35. list.setRowHeight (30);
  36. item.state.addListener (this);
  37. lookAndFeelChanged();
  38. addAndMakeVisible (header);
  39. }
  40. ~FileGroupInformationComponent() override
  41. {
  42. item.state.removeListener (this);
  43. }
  44. //==============================================================================
  45. void paint (Graphics& g) override
  46. {
  47. g.setColour (findColour (secondaryBackgroundColourId));
  48. g.fillRect (getLocalBounds().reduced (12, 0));
  49. }
  50. void resized() override
  51. {
  52. auto bounds = getLocalBounds().reduced (12, 0);
  53. header.setBounds (bounds.removeFromTop (40));
  54. list.setBounds (bounds.reduced (10, 4));
  55. }
  56. void parentSizeChanged() override
  57. {
  58. setSize (jmax (550, getParentWidth()), getParentHeight());
  59. }
  60. int getNumRows() override
  61. {
  62. return item.getNumChildren();
  63. }
  64. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool /*rowIsSelected*/) override
  65. {
  66. g.setColour (findColour (rowNumber % 2 == 0 ? widgetBackgroundColourId
  67. : secondaryWidgetBackgroundColourId));
  68. g.fillRect (0, 0, width, height - 1);
  69. }
  70. Component* refreshComponentForRow (int rowNumber, bool /*isRowSelected*/, Component* existingComponentToUpdate) override
  71. {
  72. std::unique_ptr<Component> existing (existingComponentToUpdate);
  73. if (rowNumber < getNumRows())
  74. {
  75. auto child = item.getChild (rowNumber);
  76. if (existingComponentToUpdate == nullptr
  77. || dynamic_cast<FileOptionComponent*> (existing.get())->item != child)
  78. {
  79. existing.reset();
  80. existing.reset (new FileOptionComponent (child, dynamic_cast<ListBoxHeader*> (list.getHeaderComponent())));
  81. }
  82. }
  83. return existing.release();
  84. }
  85. String getGroupPath() const { return item.getFile().getFullPathName(); }
  86. //==============================================================================
  87. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { itemChanged(); }
  88. void valueTreeChildAdded (ValueTree&, ValueTree&) override { itemChanged(); }
  89. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override { itemChanged(); }
  90. void valueTreeChildOrderChanged (ValueTree&, int, int) override { itemChanged(); }
  91. void valueTreeParentChanged (ValueTree&) override { itemChanged(); }
  92. private:
  93. Project::Item item;
  94. ListBox list;
  95. ContentViewHeader header;
  96. void itemChanged()
  97. {
  98. list.updateContent();
  99. repaint();
  100. }
  101. //==============================================================================
  102. class FileOptionComponent : public Component
  103. {
  104. public:
  105. FileOptionComponent (const Project::Item& fileItem, ListBoxHeader* listBoxHeader)
  106. : item (fileItem),
  107. header (listBoxHeader),
  108. compilerFlagSchemeSelector (item)
  109. {
  110. if (item.isFile())
  111. {
  112. addAndMakeVisible (compileButton);
  113. compileButton.getToggleStateValue().referTo (item.getShouldCompileValue());
  114. compileButton.onStateChange = [this] { compilerFlagSchemeSelector.setVisible (compileButton.getToggleState()); };
  115. addAndMakeVisible (binaryResourceButton);
  116. binaryResourceButton.getToggleStateValue().referTo (item.getShouldAddToBinaryResourcesValue());
  117. addAndMakeVisible (xcodeResourceButton);
  118. xcodeResourceButton.getToggleStateValue().referTo (item.getShouldAddToXcodeResourcesValue());
  119. addChildComponent (compilerFlagSchemeSelector);
  120. compilerFlagSchemeSelector.setVisible (compileButton.getToggleState());
  121. }
  122. }
  123. void paint (Graphics& g) override
  124. {
  125. if (header != nullptr)
  126. {
  127. auto textBounds = getLocalBounds().removeFromLeft (roundToInt (header->getProportionAtIndex (0) * (float) getWidth()));
  128. auto iconBounds = textBounds.removeFromLeft (25);
  129. if (item.isImageFile())
  130. iconBounds.reduce (5, 5);
  131. item.getIcon().withColour (findColour (treeIconColourId)).draw (g, iconBounds.toFloat(), item.isIconCrossedOut());
  132. g.setColour (findColour (widgetTextColourId));
  133. g.drawText (item.getName(), textBounds, Justification::centredLeft);
  134. }
  135. }
  136. void resized() override
  137. {
  138. if (header != nullptr)
  139. {
  140. auto bounds = getLocalBounds();
  141. auto width = (float) getWidth();
  142. bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (0) * width));
  143. binaryResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (1) * width)));
  144. xcodeResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (2) * width)));
  145. compileButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (3) * width)));
  146. compilerFlagSchemeSelector.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (4) * width)));
  147. }
  148. }
  149. Project::Item item;
  150. private:
  151. //==============================================================================
  152. class CompilerFlagSchemeSelector : public Component,
  153. private Value::Listener
  154. {
  155. public:
  156. CompilerFlagSchemeSelector (Project::Item& it)
  157. : item (it)
  158. {
  159. schemeBox.setTextWhenNothingSelected ("None");
  160. updateCompilerFlagSchemeComboBox();
  161. schemeBox.onChange = [this] { handleComboBoxSelection(); };
  162. addAndMakeVisible (schemeBox);
  163. addChildComponent (newSchemeLabel);
  164. newSchemeLabel.setEditable (true);
  165. newSchemeLabel.setJustificationType (Justification::centredLeft);
  166. newSchemeLabel.onEditorHide = [this]
  167. {
  168. newSchemeLabel.setVisible (false);
  169. schemeBox.setVisible (true);
  170. auto newScheme = newSchemeLabel.getText();
  171. item.project.addCompilerFlagScheme (newScheme);
  172. if (item.getCompilerFlagSchemeString().isEmpty())
  173. item.setCompilerFlagScheme (newScheme);
  174. updateCompilerFlagSchemeComboBox();
  175. };
  176. selectScheme (item.getCompilerFlagSchemeString());
  177. projectCompilerFlagSchemesValue = item.project.getProjectValue (Ids::compilerFlagSchemes);
  178. projectCompilerFlagSchemesValue.addListener (this);
  179. lookAndFeelChanged();
  180. }
  181. void resized() override
  182. {
  183. auto b = getLocalBounds();
  184. schemeBox.setBounds (b);
  185. newSchemeLabel.setBounds (b);
  186. }
  187. private:
  188. void valueChanged (Value&) override { updateCompilerFlagSchemeComboBox(); }
  189. void lookAndFeelChanged() override
  190. {
  191. schemeBox.setColour (ComboBox::outlineColourId, Colours::transparentBlack);
  192. schemeBox.setColour (ComboBox::textColourId, findColour (defaultTextColourId));
  193. }
  194. void updateCompilerFlagSchemeComboBox()
  195. {
  196. auto itemScheme = item.getCompilerFlagSchemeString();
  197. auto allSchemes = item.project.getCompilerFlagSchemes();
  198. if (itemScheme.isNotEmpty() && ! allSchemes.contains (itemScheme))
  199. {
  200. item.clearCurrentCompilerFlagScheme();
  201. itemScheme = {};
  202. }
  203. schemeBox.clear();
  204. schemeBox.addItemList (allSchemes, 1);
  205. schemeBox.addSeparator();
  206. schemeBox.addItem ("Add a new scheme...", -1);
  207. schemeBox.addItem ("Delete selected scheme", -2);
  208. schemeBox.addItem ("Clear", -3);
  209. selectScheme (itemScheme);
  210. }
  211. void handleComboBoxSelection()
  212. {
  213. auto selectedID = schemeBox.getSelectedId();
  214. if (selectedID > 0)
  215. {
  216. item.setCompilerFlagScheme (schemeBox.getItemText (selectedID - 1));
  217. }
  218. else if (selectedID == -1)
  219. {
  220. newSchemeLabel.setText ("NewScheme", dontSendNotification);
  221. schemeBox.setVisible (false);
  222. newSchemeLabel.setVisible (true);
  223. newSchemeLabel.showEditor();
  224. if (auto* ed = newSchemeLabel.getCurrentTextEditor())
  225. ed->setInputRestrictions (64, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_");
  226. }
  227. else if (selectedID == -2)
  228. {
  229. auto currentScheme = item.getCompilerFlagSchemeString();
  230. if (currentScheme.isNotEmpty())
  231. {
  232. item.project.removeCompilerFlagScheme (currentScheme);
  233. item.clearCurrentCompilerFlagScheme();
  234. }
  235. updateCompilerFlagSchemeComboBox();
  236. }
  237. else if (selectedID == -3)
  238. {
  239. schemeBox.setSelectedId (0);
  240. item.clearCurrentCompilerFlagScheme();
  241. }
  242. }
  243. void selectScheme (const String& schemeToSelect)
  244. {
  245. if (schemeToSelect.isNotEmpty())
  246. {
  247. for (int i = 0; i < schemeBox.getNumItems(); ++i)
  248. {
  249. if (schemeBox.getItemText (i) == schemeToSelect)
  250. {
  251. schemeBox.setSelectedItemIndex (i);
  252. return;
  253. }
  254. }
  255. }
  256. schemeBox.setSelectedId (0);
  257. }
  258. Project::Item& item;
  259. Value projectCompilerFlagSchemesValue;
  260. ComboBox schemeBox;
  261. Label newSchemeLabel;
  262. };
  263. //==============================================================================
  264. ListBoxHeader* header;
  265. ToggleButton compileButton, binaryResourceButton, xcodeResourceButton;
  266. CompilerFlagSchemeSelector compilerFlagSchemeSelector;
  267. };
  268. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileGroupInformationComponent)
  269. };