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.

142 lines
4.5KB

  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 Saving/jucer_ResourceFile.h"
  19. //==============================================================================
  20. /**
  21. A property that lets you pick a resource to use as an image, or create a
  22. new one with a file selector.
  23. */
  24. template <class ElementType>
  25. class ImageResourceProperty : public ChoicePropertyComponent,
  26. private ChangeListener
  27. {
  28. public:
  29. ImageResourceProperty (JucerDocument& doc,
  30. ElementType* const e,
  31. const String& name,
  32. const bool allowChoiceOfNoResource_)
  33. : ChoicePropertyComponent (name),
  34. element (e), document (doc),
  35. allowChoiceOfNoResource (allowChoiceOfNoResource_)
  36. {
  37. refreshChoices();
  38. doc.addChangeListener (this);
  39. }
  40. ImageResourceProperty (ElementType* const e, const String& name,
  41. const bool allowChoiceOfNoResource_ = false)
  42. : ChoicePropertyComponent (name),
  43. element (e), document (*e->getDocument()),
  44. allowChoiceOfNoResource (allowChoiceOfNoResource_)
  45. {
  46. refreshChoices();
  47. document.addChangeListener (this);
  48. }
  49. ~ImageResourceProperty()
  50. {
  51. document.removeChangeListener (this);
  52. }
  53. //==============================================================================
  54. virtual void setResource (const String& newName) = 0;
  55. virtual String getResource() const = 0;
  56. //==============================================================================
  57. void setIndex (int newIndex)
  58. {
  59. if (newIndex == 0)
  60. {
  61. String resource (document.getResources()
  62. .browseForResource ("Select an image file to add as a resource",
  63. "*.jpg;*.jpeg;*.png;*.gif;*.svg",
  64. File(),
  65. String()));
  66. if (resource.isNotEmpty())
  67. setResource (resource);
  68. }
  69. else
  70. {
  71. if (choices[newIndex] == getNoneText() && allowChoiceOfNoResource)
  72. setResource (String());
  73. else
  74. setResource (choices [newIndex]);
  75. }
  76. }
  77. int getIndex() const
  78. {
  79. if (getResource().isEmpty())
  80. return -1;
  81. return choices.indexOf (getResource());
  82. }
  83. void changeListenerCallback (ChangeBroadcaster*)
  84. {
  85. refresh();
  86. }
  87. void refreshChoices()
  88. {
  89. choices.clear();
  90. choices.add ("-- create a new image resource -- ");
  91. choices.add (String());
  92. if (allowChoiceOfNoResource)
  93. choices.add (getNoneText());
  94. choices.addArray (document.getResources().getResourceNames());
  95. const SourceCodeDocument& cpp = document.getCppDocument();
  96. if (Project* project = cpp.getProject())
  97. {
  98. ResourceFile resourceFile (*project);
  99. for (int i = 0; i < resourceFile.getNumFiles(); ++i)
  100. {
  101. const File& file = resourceFile.getFile(i);
  102. if (ImageFileFormat::findImageFormatForFileExtension(file))
  103. choices.add (resourceFile.getClassName() + "::" + resourceFile.getDataVariableFor (file));
  104. }
  105. }
  106. }
  107. const char* getNoneText() noexcept { return "<< none >>"; }
  108. protected:
  109. mutable Component::SafePointer<ElementType> element;
  110. JucerDocument& document;
  111. const bool allowChoiceOfNoResource;
  112. };