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.

145 lines
4.6KB

  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. #include "../../ProjectSaving/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. document.getResources()
  63. .browseForResource ("Select an image file to add as a resource",
  64. "*.jpg;*.jpeg;*.png;*.gif;*.svg",
  65. File(),
  66. String(),
  67. [this] (String resource)
  68. {
  69. if (resource.isNotEmpty())
  70. setResource (resource);
  71. });
  72. }
  73. else
  74. {
  75. if (choices[newIndex] == getNoneText() && allowChoiceOfNoResource)
  76. setResource (String());
  77. else
  78. setResource (choices [newIndex]);
  79. }
  80. }
  81. int getIndex() const
  82. {
  83. if (getResource().isEmpty())
  84. return -1;
  85. return choices.indexOf (getResource());
  86. }
  87. void changeListenerCallback (ChangeBroadcaster*)
  88. {
  89. refresh();
  90. }
  91. void refreshChoices()
  92. {
  93. choices.clear();
  94. choices.add ("-- create a new image resource -- ");
  95. choices.add (String());
  96. if (allowChoiceOfNoResource)
  97. choices.add (getNoneText());
  98. choices.addArray (document.getResources().getResourceNames());
  99. const SourceCodeDocument& cpp = document.getCppDocument();
  100. if (Project* project = cpp.getProject())
  101. {
  102. JucerResourceFile resourceFile (*project);
  103. for (int i = 0; i < resourceFile.getNumFiles(); ++i)
  104. {
  105. const File& file = resourceFile.getFile (i);
  106. if (ImageFileFormat::findImageFormatForFileExtension (file))
  107. choices.add (resourceFile.getClassName() + "::" + resourceFile.getDataVariableFor (file));
  108. }
  109. }
  110. }
  111. const char* getNoneText() noexcept { return "<< none >>"; }
  112. protected:
  113. mutable Component::SafePointer<ElementType> element;
  114. JucerDocument& document;
  115. const bool allowChoiceOfNoResource;
  116. };