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.

126 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __JUCER_IMAGERESOURCEPROPERTY_JUCEHEADER__
  18. #define __JUCER_IMAGERESOURCEPROPERTY_JUCEHEADER__
  19. //==============================================================================
  20. /**
  21. A property that lets you pick a resource to use as an image, or create a
  22. new one with a file selector.
  23. */
  24. template <class ElementType>
  25. class ImageResourceProperty : public ChoicePropertyComponent,
  26. private ChangeListener
  27. {
  28. public:
  29. ImageResourceProperty (JucerDocument& doc,
  30. ElementType* const e,
  31. const String& name,
  32. const bool allowChoiceOfNoResource_)
  33. : ChoicePropertyComponent (name),
  34. element (e), document (doc),
  35. allowChoiceOfNoResource (allowChoiceOfNoResource_)
  36. {
  37. choices.add ("-- create a new image resource -- ");
  38. choices.add (String::empty);
  39. if (allowChoiceOfNoResource_)
  40. choices.add ("<< none >>");
  41. choices.addArray (doc.getResources().getResourceNames());
  42. doc.addChangeListener (this);
  43. }
  44. ImageResourceProperty (ElementType* const e, const String& name,
  45. const bool allowChoiceOfNoResource_ = false)
  46. : ChoicePropertyComponent (name),
  47. element (e), document (*e->getDocument()),
  48. allowChoiceOfNoResource (allowChoiceOfNoResource_)
  49. {
  50. choices.add ("-- create a new image resource -- ");
  51. choices.add (String::empty);
  52. if (allowChoiceOfNoResource_)
  53. choices.add ("<< none >>");
  54. choices.addArray (document.getResources().getResourceNames());
  55. document.addChangeListener (this);
  56. }
  57. ~ImageResourceProperty()
  58. {
  59. document.removeChangeListener (this);
  60. }
  61. //==============================================================================
  62. virtual void setResource (const String& newName) = 0;
  63. virtual String getResource() const = 0;
  64. //==============================================================================
  65. void setIndex (int newIndex)
  66. {
  67. if (newIndex == 0)
  68. {
  69. String resource (document.getResources()
  70. .browseForResource ("Select an image file to add as a resource",
  71. "*.jpg;*.jpeg;*.png;*.gif;*.svg",
  72. File::nonexistent,
  73. String::empty));
  74. if (resource.isNotEmpty())
  75. setResource (resource);
  76. }
  77. else
  78. {
  79. if (choices[newIndex] == "<< none >>" && allowChoiceOfNoResource)
  80. setResource (String::empty);
  81. else
  82. setResource (choices [newIndex]);
  83. }
  84. }
  85. int getIndex() const
  86. {
  87. if (getResource().isEmpty())
  88. return -1;
  89. return choices.indexOf (getResource());
  90. }
  91. void changeListenerCallback (ChangeBroadcaster*)
  92. {
  93. refresh();
  94. }
  95. protected:
  96. mutable Component::SafePointer<ElementType> element;
  97. JucerDocument& document;
  98. const bool allowChoiceOfNoResource;
  99. };
  100. #endif // __JUCER_IMAGERESOURCEPROPERTY_JUCEHEADER__