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.

144 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "../../ProjectSaving/jucer_ResourceFile.h"
  21. //==============================================================================
  22. /**
  23. A property that lets you pick a resource to use as an image, or create a
  24. new one with a file selector.
  25. */
  26. template <class ElementType>
  27. class ImageResourceProperty : public ChoicePropertyComponent,
  28. private ChangeListener
  29. {
  30. public:
  31. ImageResourceProperty (JucerDocument& doc,
  32. ElementType* const e,
  33. const String& name,
  34. const bool allowChoiceOfNoResource_)
  35. : ChoicePropertyComponent (name),
  36. element (e), document (doc),
  37. allowChoiceOfNoResource (allowChoiceOfNoResource_)
  38. {
  39. refreshChoices();
  40. doc.addChangeListener (this);
  41. }
  42. ImageResourceProperty (ElementType* const e, const String& name,
  43. const bool allowChoiceOfNoResource_ = false)
  44. : ChoicePropertyComponent (name),
  45. element (e), document (*e->getDocument()),
  46. allowChoiceOfNoResource (allowChoiceOfNoResource_)
  47. {
  48. refreshChoices();
  49. document.addChangeListener (this);
  50. }
  51. ~ImageResourceProperty()
  52. {
  53. document.removeChangeListener (this);
  54. }
  55. //==============================================================================
  56. virtual void setResource (const String& newName) = 0;
  57. virtual String getResource() const = 0;
  58. //==============================================================================
  59. void setIndex (int newIndex)
  60. {
  61. if (newIndex == 0)
  62. {
  63. String resource (document.getResources()
  64. .browseForResource ("Select an image file to add as a resource",
  65. "*.jpg;*.jpeg;*.png;*.gif;*.svg",
  66. File(),
  67. String()));
  68. if (resource.isNotEmpty())
  69. setResource (resource);
  70. }
  71. else
  72. {
  73. if (choices[newIndex] == getNoneText() && allowChoiceOfNoResource)
  74. setResource (String());
  75. else
  76. setResource (choices [newIndex]);
  77. }
  78. }
  79. int getIndex() const
  80. {
  81. if (getResource().isEmpty())
  82. return -1;
  83. return choices.indexOf (getResource());
  84. }
  85. void changeListenerCallback (ChangeBroadcaster*)
  86. {
  87. refresh();
  88. }
  89. void refreshChoices()
  90. {
  91. choices.clear();
  92. choices.add ("-- create a new image resource -- ");
  93. choices.add (String());
  94. if (allowChoiceOfNoResource)
  95. choices.add (getNoneText());
  96. choices.addArray (document.getResources().getResourceNames());
  97. const SourceCodeDocument& cpp = document.getCppDocument();
  98. if (Project* project = cpp.getProject())
  99. {
  100. ResourceFile resourceFile (*project);
  101. for (int i = 0; i < resourceFile.getNumFiles(); ++i)
  102. {
  103. const File& file = resourceFile.getFile(i);
  104. if (ImageFileFormat::findImageFormatForFileExtension(file))
  105. choices.add (resourceFile.getClassName() + "::" + resourceFile.getDataVariableFor (file));
  106. }
  107. }
  108. }
  109. const char* getNoneText() noexcept { return "<< none >>"; }
  110. protected:
  111. mutable Component::SafePointer<ElementType> element;
  112. JucerDocument& document;
  113. const bool allowChoiceOfNoResource;
  114. };