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.

364 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 final : 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", "Skip PCH", "Compiler Flag Scheme" },
  30. Array<float> { 0.25f, 0.125f, 0.125f, 0.125f, 0.125f, 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 final : 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. auto isSourceFile = item.isSourceFile();
  113. if (isSourceFile)
  114. {
  115. addAndMakeVisible (compileButton);
  116. compileButton.getToggleStateValue().referTo (item.getShouldCompileValue());
  117. compileButton.onStateChange = [this] { compileEnablementChanged(); };
  118. }
  119. addAndMakeVisible (binaryResourceButton);
  120. binaryResourceButton.getToggleStateValue().referTo (item.getShouldAddToBinaryResourcesValue());
  121. addAndMakeVisible (xcodeResourceButton);
  122. xcodeResourceButton.getToggleStateValue().referTo (item.getShouldAddToXcodeResourcesValue());
  123. if (isSourceFile)
  124. {
  125. addChildComponent (skipPCHButton);
  126. skipPCHButton.getToggleStateValue().referTo (item.getShouldSkipPCHValue());
  127. addChildComponent (compilerFlagSchemeSelector);
  128. compileEnablementChanged();
  129. }
  130. }
  131. }
  132. void paint (Graphics& g) override
  133. {
  134. if (header != nullptr)
  135. {
  136. auto textBounds = getLocalBounds().removeFromLeft (roundToInt (header->getProportionAtIndex (0) * (float) getWidth()));
  137. auto iconBounds = textBounds.removeFromLeft (25);
  138. if (item.isImageFile())
  139. iconBounds.reduce (5, 5);
  140. item.getIcon().withColour (findColour (treeIconColourId)).draw (g, iconBounds.toFloat(), item.isIconCrossedOut());
  141. g.setColour (findColour (widgetTextColourId));
  142. g.drawText (item.getName(), textBounds, Justification::centredLeft);
  143. }
  144. }
  145. void resized() override
  146. {
  147. if (header != nullptr)
  148. {
  149. auto bounds = getLocalBounds();
  150. auto width = (float) getWidth();
  151. bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (0) * width));
  152. binaryResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (1) * width)));
  153. xcodeResourceButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (2) * width)));
  154. compileButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (3) * width)));
  155. skipPCHButton.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (4) * width)));
  156. compilerFlagSchemeSelector.setBounds (bounds.removeFromLeft (roundToInt (header->getProportionAtIndex (5) * width)));
  157. }
  158. }
  159. Project::Item item;
  160. private:
  161. //==============================================================================
  162. class CompilerFlagSchemeSelector final : public Component,
  163. private Value::Listener
  164. {
  165. public:
  166. CompilerFlagSchemeSelector (Project::Item& it)
  167. : item (it)
  168. {
  169. schemeBox.setTextWhenNothingSelected ("None");
  170. updateCompilerFlagSchemeComboBox();
  171. schemeBox.onChange = [this] { handleComboBoxSelection(); };
  172. addAndMakeVisible (schemeBox);
  173. addChildComponent (newSchemeLabel);
  174. newSchemeLabel.setEditable (true);
  175. newSchemeLabel.setJustificationType (Justification::centredLeft);
  176. newSchemeLabel.onEditorHide = [this]
  177. {
  178. newSchemeLabel.setVisible (false);
  179. schemeBox.setVisible (true);
  180. auto newScheme = newSchemeLabel.getText();
  181. item.project.addCompilerFlagScheme (newScheme);
  182. if (item.getCompilerFlagSchemeString().isEmpty())
  183. item.setCompilerFlagScheme (newScheme);
  184. updateCompilerFlagSchemeComboBox();
  185. };
  186. selectScheme (item.getCompilerFlagSchemeString());
  187. projectCompilerFlagSchemesValue = item.project.getProjectValue (Ids::compilerFlagSchemes);
  188. projectCompilerFlagSchemesValue.addListener (this);
  189. lookAndFeelChanged();
  190. }
  191. void resized() override
  192. {
  193. auto b = getLocalBounds();
  194. schemeBox.setBounds (b);
  195. newSchemeLabel.setBounds (b);
  196. }
  197. private:
  198. void valueChanged (Value&) override { updateCompilerFlagSchemeComboBox(); }
  199. void lookAndFeelChanged() override
  200. {
  201. schemeBox.setColour (ComboBox::outlineColourId, Colours::transparentBlack);
  202. schemeBox.setColour (ComboBox::textColourId, findColour (defaultTextColourId));
  203. }
  204. void updateCompilerFlagSchemeComboBox()
  205. {
  206. auto itemScheme = item.getCompilerFlagSchemeString();
  207. auto allSchemes = item.project.getCompilerFlagSchemes();
  208. if (itemScheme.isNotEmpty() && ! allSchemes.contains (itemScheme))
  209. {
  210. item.clearCurrentCompilerFlagScheme();
  211. itemScheme = {};
  212. }
  213. schemeBox.clear();
  214. schemeBox.addItemList (allSchemes, 1);
  215. schemeBox.addSeparator();
  216. schemeBox.addItem ("Add a new scheme...", -1);
  217. schemeBox.addItem ("Delete selected scheme", -2);
  218. schemeBox.addItem ("Clear", -3);
  219. selectScheme (itemScheme);
  220. }
  221. void handleComboBoxSelection()
  222. {
  223. auto selectedID = schemeBox.getSelectedId();
  224. if (selectedID > 0)
  225. {
  226. item.setCompilerFlagScheme (schemeBox.getItemText (selectedID - 1));
  227. }
  228. else if (selectedID == -1)
  229. {
  230. newSchemeLabel.setText ("NewScheme", dontSendNotification);
  231. schemeBox.setVisible (false);
  232. newSchemeLabel.setVisible (true);
  233. newSchemeLabel.showEditor();
  234. if (auto* ed = newSchemeLabel.getCurrentTextEditor())
  235. ed->setInputRestrictions (64, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_");
  236. }
  237. else if (selectedID == -2)
  238. {
  239. auto currentScheme = item.getCompilerFlagSchemeString();
  240. if (currentScheme.isNotEmpty())
  241. {
  242. item.project.removeCompilerFlagScheme (currentScheme);
  243. item.clearCurrentCompilerFlagScheme();
  244. }
  245. updateCompilerFlagSchemeComboBox();
  246. }
  247. else if (selectedID == -3)
  248. {
  249. schemeBox.setSelectedId (0);
  250. item.clearCurrentCompilerFlagScheme();
  251. }
  252. }
  253. void selectScheme (const String& schemeToSelect)
  254. {
  255. if (schemeToSelect.isNotEmpty())
  256. {
  257. for (int i = 0; i < schemeBox.getNumItems(); ++i)
  258. {
  259. if (schemeBox.getItemText (i) == schemeToSelect)
  260. {
  261. schemeBox.setSelectedItemIndex (i);
  262. return;
  263. }
  264. }
  265. }
  266. schemeBox.setSelectedId (0);
  267. }
  268. Project::Item& item;
  269. Value projectCompilerFlagSchemesValue;
  270. ComboBox schemeBox;
  271. Label newSchemeLabel;
  272. };
  273. void compileEnablementChanged()
  274. {
  275. auto shouldBeCompiled = compileButton.getToggleState();
  276. skipPCHButton.setVisible (shouldBeCompiled);
  277. compilerFlagSchemeSelector.setVisible (shouldBeCompiled);
  278. }
  279. //==============================================================================
  280. ListBoxHeader* header;
  281. ToggleButton compileButton, binaryResourceButton, xcodeResourceButton, skipPCHButton;
  282. CompilerFlagSchemeSelector compilerFlagSchemeSelector;
  283. };
  284. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileGroupInformationComponent)
  285. };