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.

138 lines
4.4KB

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