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.

146 lines
4.7KB

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