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.

143 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. String resource (document.getResources()
  63. .browseForResource ("Select an image file to add as a resource",
  64. "*.jpg;*.jpeg;*.png;*.gif;*.svg",
  65. File(),
  66. String()));
  67. if (resource.isNotEmpty())
  68. setResource (resource);
  69. }
  70. else
  71. {
  72. if (choices[newIndex] == getNoneText() && allowChoiceOfNoResource)
  73. setResource (String());
  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());
  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. JucerResourceFile 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. };