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.

127 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_IMAGERESOURCEPROPERTY_JUCEHEADER__
  19. #define __JUCER_IMAGERESOURCEPROPERTY_JUCEHEADER__
  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. choices.add ("-- create a new image resource -- ");
  39. choices.add (String::empty);
  40. if (allowChoiceOfNoResource_)
  41. choices.add ("<< none >>");
  42. choices.addArray (doc.getResources().getResourceNames());
  43. doc.addChangeListener (this);
  44. }
  45. ImageResourceProperty (ElementType* const e, const String& name,
  46. const bool allowChoiceOfNoResource_ = false)
  47. : ChoicePropertyComponent (name),
  48. element (e), document (*e->getDocument()),
  49. allowChoiceOfNoResource (allowChoiceOfNoResource_)
  50. {
  51. choices.add ("-- create a new image resource -- ");
  52. choices.add (String::empty);
  53. if (allowChoiceOfNoResource_)
  54. choices.add ("<< none >>");
  55. choices.addArray (document.getResources().getResourceNames());
  56. document.addChangeListener (this);
  57. }
  58. ~ImageResourceProperty()
  59. {
  60. document.removeChangeListener (this);
  61. }
  62. //==============================================================================
  63. virtual void setResource (const String& newName) = 0;
  64. virtual String getResource() const = 0;
  65. //==============================================================================
  66. void setIndex (int newIndex)
  67. {
  68. if (newIndex == 0)
  69. {
  70. String resource (document.getResources()
  71. .browseForResource ("Select an image file to add as a resource",
  72. "*.jpg;*.jpeg;*.png;*.gif;*.svg",
  73. File::nonexistent,
  74. String::empty));
  75. if (resource.isNotEmpty())
  76. setResource (resource);
  77. }
  78. else
  79. {
  80. if (choices[newIndex] == "<< none >>" && allowChoiceOfNoResource)
  81. setResource (String::empty);
  82. else
  83. setResource (choices [newIndex]);
  84. }
  85. }
  86. int getIndex() const
  87. {
  88. if (getResource().isEmpty())
  89. return -1;
  90. return choices.indexOf (getResource());
  91. }
  92. void changeListenerCallback (ChangeBroadcaster*)
  93. {
  94. refresh();
  95. }
  96. protected:
  97. mutable Component::SafePointer<ElementType> element;
  98. JucerDocument& document;
  99. const bool allowChoiceOfNoResource;
  100. };
  101. #endif // __JUCER_IMAGERESOURCEPROPERTY_JUCEHEADER__