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.

136 lines
4.2KB

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