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.

267 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../../Application/jucer_Headers.h"
  14. #include "jucer_ResourceEditorPanel.h"
  15. //==============================================================================
  16. class ResourceListButton : public Component
  17. {
  18. public:
  19. explicit ResourceListButton (JucerDocument& doc)
  20. : document (doc), reloadButton ("Reload"), row (0)
  21. {
  22. setInterceptsMouseClicks (false, true);
  23. addAndMakeVisible (reloadButton);
  24. reloadButton.onClick = [this]
  25. {
  26. if (auto* r = document.getResources() [row])
  27. document.getResources().browseForResource ("Select a file to replace this resource", "*",
  28. File (r->originalFilename), r->name);
  29. };
  30. }
  31. void update (int newRow)
  32. {
  33. row = newRow;
  34. reloadButton.setVisible (document.getResources() [row] != nullptr);
  35. }
  36. void resized()
  37. {
  38. reloadButton.setBoundsInset (BorderSize<int> (2));
  39. }
  40. private:
  41. JucerDocument& document;
  42. TextButton reloadButton;
  43. int row;
  44. };
  45. //==============================================================================
  46. ResourceEditorPanel::ResourceEditorPanel (JucerDocument& doc)
  47. : document (doc),
  48. addButton ("Add new resource..."),
  49. reloadAllButton ("Reload all resources"),
  50. delButton ("Delete selected resources")
  51. {
  52. addAndMakeVisible (addButton);
  53. addButton.onClick = [this] { document.getResources().browseForResource ("Select a file to add as a resource", "*", {}, {}); };
  54. addAndMakeVisible (reloadAllButton);
  55. reloadAllButton.onClick = [this] { reloadAll(); };
  56. addAndMakeVisible (delButton);
  57. delButton.setEnabled (false);
  58. delButton.onClick = [this] { document.getResources().remove (listBox->getSelectedRow (0)); };
  59. listBox.reset (new TableListBox (String(), this));
  60. addAndMakeVisible (listBox.get());
  61. listBox->getHeader().addColumn ("name", 1, 150, 80, 400);
  62. listBox->getHeader().addColumn ("original file", 2, 350, 80, 800);
  63. listBox->getHeader().addColumn ("size", 3, 100, 40, 150);
  64. listBox->getHeader().addColumn ("reload", 4, 100, 100, 100, TableHeaderComponent::notResizableOrSortable);
  65. listBox->getHeader().setStretchToFitActive (true);
  66. listBox->setOutlineThickness (1);
  67. listBox->updateContent();
  68. document.addChangeListener (this);
  69. handleCommandMessage (1);
  70. lookAndFeelChanged();
  71. }
  72. ResourceEditorPanel::~ResourceEditorPanel()
  73. {
  74. document.removeChangeListener (this);
  75. }
  76. int ResourceEditorPanel::getNumRows()
  77. {
  78. return document.getResources().size();
  79. }
  80. void ResourceEditorPanel::paintRowBackground (Graphics& g, int /*rowNumber*/,
  81. int /*width*/, int /*height*/, bool rowIsSelected)
  82. {
  83. if (rowIsSelected)
  84. g.fillAll (findColour (defaultHighlightColourId));
  85. }
  86. void ResourceEditorPanel::paintCell (Graphics& g, int rowNumber, int columnId, int width, int height,
  87. bool rowIsSelected)
  88. {
  89. if (const BinaryResources::BinaryResource* const r = document.getResources() [rowNumber])
  90. {
  91. String text;
  92. if (columnId == 1)
  93. text = r->name;
  94. else if (columnId == 2)
  95. text = r->originalFilename;
  96. else if (columnId == 3)
  97. text = File::descriptionOfSizeInBytes ((int64) r->data.getSize());
  98. if (rowIsSelected)
  99. g.setColour (findColour (defaultHighlightedTextColourId));
  100. else
  101. g.setColour (findColour (defaultTextColourId));
  102. g.setFont (13.0f);
  103. g.drawText (text, 4, 0, width - 6, height, Justification::centredLeft, true);
  104. }
  105. }
  106. Component* ResourceEditorPanel::refreshComponentForCell (int rowNumber, int columnId, bool /*isRowSelected*/,
  107. Component* existingComponentToUpdate)
  108. {
  109. if (columnId != 4)
  110. return nullptr;
  111. if (existingComponentToUpdate == nullptr)
  112. existingComponentToUpdate = new ResourceListButton (document);
  113. ((ResourceListButton*) existingComponentToUpdate)->update (rowNumber);
  114. return existingComponentToUpdate;
  115. }
  116. int ResourceEditorPanel::getColumnAutoSizeWidth (int columnId)
  117. {
  118. if (columnId == 4)
  119. return 0;
  120. Font f (13.0f);
  121. int widest = 40;
  122. for (int i = document.getResources().size(); --i >= 0;)
  123. {
  124. const BinaryResources::BinaryResource* const r = document.getResources() [i];
  125. jassert (r != nullptr);
  126. String text;
  127. if (columnId == 1)
  128. text = r->name;
  129. else if (columnId == 2)
  130. text = r->originalFilename;
  131. else if (columnId == 3)
  132. text = File::descriptionOfSizeInBytes ((int64) r->data.getSize());
  133. widest = jmax (widest, f.getStringWidth (text));
  134. }
  135. return widest + 10;
  136. }
  137. void ResourceEditorPanel::lookAndFeelChanged()
  138. {
  139. listBox->setColour (ListBox::backgroundColourId, findColour (secondaryBackgroundColourId));
  140. listBox->setColour (ListBox::outlineColourId, Colours::transparentBlack);
  141. }
  142. //==============================================================================
  143. class ResourceSorter
  144. {
  145. public:
  146. ResourceSorter (const int columnId_, const bool forwards)
  147. : columnId (columnId_),
  148. direction (forwards ? 1 : -1)
  149. {
  150. }
  151. int compareElements (BinaryResources::BinaryResource* first, BinaryResources::BinaryResource* second)
  152. {
  153. if (columnId == 1) return direction * first->name.compare (second->name);
  154. if (columnId == 2) return direction * first->originalFilename.compare (second->originalFilename);
  155. if (columnId == 3) return direction * (int) first->data.getSize() - (int) second->data.getSize();
  156. return 0;
  157. }
  158. private:
  159. const int columnId, direction;
  160. ResourceSorter (const ResourceSorter&);
  161. ResourceSorter& operator= (const ResourceSorter&);
  162. };
  163. void ResourceEditorPanel::sortOrderChanged (int newSortColumnId, const bool isForwards)
  164. {
  165. ResourceSorter sorter (newSortColumnId, isForwards);
  166. document.getResources().sort (sorter);
  167. }
  168. //==============================================================================
  169. void ResourceEditorPanel::selectedRowsChanged (int /*lastRowSelected*/)
  170. {
  171. delButton.setEnabled (listBox->getNumSelectedRows() > 0);
  172. }
  173. void ResourceEditorPanel::resized()
  174. {
  175. auto bounds = getLocalBounds();
  176. auto buttonSlice = bounds.removeFromBottom (40).reduced (5, 5);
  177. addButton.setBounds (buttonSlice.removeFromLeft (125));
  178. buttonSlice.removeFromLeft (10);
  179. reloadAllButton.setBounds (buttonSlice.removeFromLeft (125));
  180. delButton.setBounds (buttonSlice.removeFromRight (125));
  181. listBox->setBounds (bounds);
  182. }
  183. void ResourceEditorPanel::paint (Graphics& g)
  184. {
  185. g.fillAll (findColour (secondaryBackgroundColourId));
  186. }
  187. void ResourceEditorPanel::visibilityChanged()
  188. {
  189. if (isVisible())
  190. listBox->updateContent();
  191. }
  192. void ResourceEditorPanel::changeListenerCallback (ChangeBroadcaster*)
  193. {
  194. if (isVisible())
  195. listBox->updateContent();
  196. }
  197. void ResourceEditorPanel::reloadAll()
  198. {
  199. StringArray failed;
  200. for (int i = 0; i < document.getResources().size(); ++i)
  201. {
  202. if (! document.getResources().reload (i))
  203. failed.add (document.getResources().getResourceNames() [i]);
  204. }
  205. if (failed.size() > 0)
  206. {
  207. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  208. TRANS("Reloading resources"),
  209. TRANS("The following resources couldn't be reloaded from their original files:\n\n")
  210. + failed.joinIntoString (", "));
  211. }
  212. }