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.

344 lines
13KB

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