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.

236 lines
7.5KB

  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. Drawable::Drawable()
  19. {
  20. setInterceptsMouseClicks (false, false);
  21. setPaintingIsUnclipped (true);
  22. }
  23. Drawable::~Drawable()
  24. {
  25. }
  26. //==============================================================================
  27. void Drawable::draw (Graphics& g, float opacity, const AffineTransform& transform) const
  28. {
  29. const_cast <Drawable*> (this)->nonConstDraw (g, opacity, transform);
  30. }
  31. void Drawable::nonConstDraw (Graphics& g, float opacity, const AffineTransform& transform)
  32. {
  33. Graphics::ScopedSaveState ss (g);
  34. g.addTransform (AffineTransform::translation ((float) -(originRelativeToComponent.x),
  35. (float) -(originRelativeToComponent.y))
  36. .followedBy (getTransform())
  37. .followedBy (transform));
  38. if (! g.isClipEmpty())
  39. {
  40. if (opacity < 1.0f)
  41. {
  42. g.beginTransparencyLayer (opacity);
  43. paintEntireComponent (g, true);
  44. g.endTransparencyLayer();
  45. }
  46. else
  47. {
  48. paintEntireComponent (g, true);
  49. }
  50. }
  51. }
  52. void Drawable::drawAt (Graphics& g, float x, float y, float opacity) const
  53. {
  54. draw (g, opacity, AffineTransform::translation (x, y));
  55. }
  56. void Drawable::drawWithin (Graphics& g, const Rectangle<float>& destArea, const RectanglePlacement& placement, float opacity) const
  57. {
  58. draw (g, opacity, placement.getTransformToFit (getDrawableBounds(), destArea));
  59. }
  60. //==============================================================================
  61. DrawableComposite* Drawable::getParent() const
  62. {
  63. return dynamic_cast <DrawableComposite*> (getParentComponent());
  64. }
  65. void Drawable::transformContextToCorrectOrigin (Graphics& g)
  66. {
  67. g.setOrigin (originRelativeToComponent.x,
  68. originRelativeToComponent.y);
  69. }
  70. void Drawable::parentHierarchyChanged()
  71. {
  72. setBoundsToEnclose (getDrawableBounds());
  73. }
  74. void Drawable::setBoundsToEnclose (const Rectangle<float>& area)
  75. {
  76. Drawable* const parent = getParent();
  77. Point<int> parentOrigin;
  78. if (parent != nullptr)
  79. parentOrigin = parent->originRelativeToComponent;
  80. const Rectangle<int> newBounds (area.getSmallestIntegerContainer() + parentOrigin);
  81. originRelativeToComponent = parentOrigin - newBounds.getPosition();
  82. setBounds (newBounds);
  83. }
  84. //==============================================================================
  85. void Drawable::setOriginWithOriginalSize (const Point<float>& originWithinParent)
  86. {
  87. setTransform (AffineTransform::translation (originWithinParent.x, originWithinParent.y));
  88. }
  89. void Drawable::setTransformToFit (const Rectangle<float>& area, const RectanglePlacement& placement)
  90. {
  91. if (! area.isEmpty())
  92. setTransform (placement.getTransformToFit (getDrawableBounds(), area));
  93. }
  94. //==============================================================================
  95. Drawable* Drawable::createFromImageData (const void* data, const size_t numBytes)
  96. {
  97. Drawable* result = nullptr;
  98. Image image (ImageFileFormat::loadFrom (data, numBytes));
  99. if (image.isValid())
  100. {
  101. DrawableImage* const di = new DrawableImage();
  102. di->setImage (image);
  103. result = di;
  104. }
  105. else
  106. {
  107. const String asString (String::createStringFromData (data, (int) numBytes));
  108. XmlDocument doc (asString);
  109. ScopedPointer <XmlElement> outer (doc.getDocumentElement (true));
  110. if (outer != nullptr && outer->hasTagName ("svg"))
  111. {
  112. ScopedPointer <XmlElement> svg (doc.getDocumentElement());
  113. if (svg != nullptr)
  114. result = Drawable::createFromSVG (*svg);
  115. }
  116. }
  117. return result;
  118. }
  119. Drawable* Drawable::createFromImageDataStream (InputStream& dataSource)
  120. {
  121. MemoryOutputStream mo;
  122. mo << dataSource;
  123. return createFromImageData (mo.getData(), mo.getDataSize());
  124. }
  125. Drawable* Drawable::createFromImageFile (const File& file)
  126. {
  127. FileInputStream fin (file);
  128. return fin.openedOk() ? createFromImageDataStream (fin) : nullptr;
  129. }
  130. //==============================================================================
  131. template <class DrawableClass>
  132. class DrawableTypeHandler : public ComponentBuilder::TypeHandler
  133. {
  134. public:
  135. DrawableTypeHandler()
  136. : ComponentBuilder::TypeHandler (DrawableClass::valueTreeType)
  137. {
  138. }
  139. Component* addNewComponentFromState (const ValueTree& state, Component* parent)
  140. {
  141. DrawableClass* const d = new DrawableClass();
  142. if (parent != nullptr)
  143. parent->addAndMakeVisible (d);
  144. updateComponentFromState (d, state);
  145. return d;
  146. }
  147. void updateComponentFromState (Component* component, const ValueTree& state)
  148. {
  149. DrawableClass* const d = dynamic_cast <DrawableClass*> (component);
  150. jassert (d != nullptr);
  151. d->refreshFromValueTree (state, *this->getBuilder());
  152. }
  153. };
  154. void Drawable::registerDrawableTypeHandlers (ComponentBuilder& builder)
  155. {
  156. builder.registerTypeHandler (new DrawableTypeHandler <DrawablePath>());
  157. builder.registerTypeHandler (new DrawableTypeHandler <DrawableComposite>());
  158. builder.registerTypeHandler (new DrawableTypeHandler <DrawableRectangle>());
  159. builder.registerTypeHandler (new DrawableTypeHandler <DrawableImage>());
  160. builder.registerTypeHandler (new DrawableTypeHandler <DrawableText>());
  161. }
  162. Drawable* Drawable::createFromValueTree (const ValueTree& tree, ComponentBuilder::ImageProvider* imageProvider)
  163. {
  164. ComponentBuilder builder (tree);
  165. builder.setImageProvider (imageProvider);
  166. registerDrawableTypeHandlers (builder);
  167. ScopedPointer<Component> comp (builder.createComponent());
  168. Drawable* const d = dynamic_cast<Drawable*> (static_cast <Component*> (comp));
  169. if (d != nullptr)
  170. comp.release();
  171. return d;
  172. }
  173. //==============================================================================
  174. Drawable::ValueTreeWrapperBase::ValueTreeWrapperBase (const ValueTree& state_)
  175. : state (state_)
  176. {
  177. }
  178. String Drawable::ValueTreeWrapperBase::getID() const
  179. {
  180. return state [ComponentBuilder::idProperty];
  181. }
  182. void Drawable::ValueTreeWrapperBase::setID (const String& newID)
  183. {
  184. if (newID.isEmpty())
  185. state.removeProperty (ComponentBuilder::idProperty, nullptr);
  186. else
  187. state.setProperty (ComponentBuilder::idProperty, newID, nullptr);
  188. }