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.

166 lines
5.2KB

  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_PAINTELEMENTRECTANGLE_JUCEHEADER__
  19. #define __JUCER_PAINTELEMENTRECTANGLE_JUCEHEADER__
  20. #include "jucer_ColouredElement.h"
  21. //==============================================================================
  22. class PaintElementRectangle : public ColouredElement
  23. {
  24. public:
  25. PaintElementRectangle (PaintRoutine* owner)
  26. : ColouredElement (owner, "Rectangle", true, false)
  27. {
  28. }
  29. Rectangle<int> getCurrentBounds (const Rectangle<int>& parentArea) const
  30. {
  31. return PaintElement::getCurrentBounds (parentArea); // bypass the ColouredElement implementation
  32. }
  33. void setCurrentBounds (const Rectangle<int>& newBounds, const Rectangle<int>& parentArea, const bool undoable)
  34. {
  35. PaintElement::setCurrentBounds (newBounds, parentArea, undoable); // bypass the ColouredElement implementation
  36. }
  37. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  38. {
  39. Component parentComponent;
  40. parentComponent.setBounds (parentArea);
  41. fillType.setFillType (g, getDocument(), parentArea);
  42. const Rectangle<int> r (position.getRectangle (parentArea, layout));
  43. g.fillRect (r);
  44. if (isStrokePresent)
  45. {
  46. strokeType.fill.setFillType (g, getDocument(), parentArea);
  47. g.drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  48. roundToInt (getStrokeType().stroke.getStrokeThickness()));
  49. }
  50. }
  51. void getEditableProperties (Array <PropertyComponent*>& properties)
  52. {
  53. ColouredElement::getEditableProperties (properties);
  54. properties.add (new ShapeToPathProperty (this));
  55. }
  56. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  57. {
  58. if (! fillType.isInvisible())
  59. {
  60. String x, y, w, h, s;
  61. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  62. fillType.fillInGeneratedCode (code, paintMethodCode);
  63. s << "g.fillRect (" << x << ", " << y << ", " << w << ", " << h << ");\n\n";
  64. paintMethodCode += s;
  65. }
  66. if (isStrokePresent && ! strokeType.isInvisible())
  67. {
  68. String x, y, w, h, s;
  69. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  70. strokeType.fill.fillInGeneratedCode (code, paintMethodCode);
  71. s << "g.drawRect (" << x << ", " << y << ", " << w << ", " << h << ", "
  72. << roundToInt (strokeType.stroke.getStrokeThickness()) << ");\n\n";
  73. paintMethodCode += s;
  74. }
  75. }
  76. static const char* getTagName() noexcept { return "RECT"; }
  77. XmlElement* createXml() const
  78. {
  79. XmlElement* e = new XmlElement (getTagName());
  80. position.applyToXml (*e);
  81. addColourAttributes (e);
  82. return e;
  83. }
  84. bool loadFromXml (const XmlElement& xml)
  85. {
  86. if (xml.hasTagName (getTagName()))
  87. {
  88. position.restoreFromXml (xml, position);
  89. loadColourAttributes (xml);
  90. return true;
  91. }
  92. jassertfalse;
  93. return false;
  94. }
  95. void convertToPath()
  96. {
  97. const Rectangle<int> r (getCurrentAbsoluteBounds());
  98. Path path;
  99. path.addRectangle ((float) r.getX(), (float) r.getY(), (float) r.getWidth(), (float) r.getHeight());
  100. convertToNewPathElement (path);
  101. }
  102. private:
  103. class ShapeToPathProperty : public ButtonPropertyComponent
  104. {
  105. public:
  106. ShapeToPathProperty (PaintElementRectangle* const e)
  107. : ButtonPropertyComponent ("path", false),
  108. element (e)
  109. {
  110. }
  111. void buttonClicked()
  112. {
  113. element->convertToPath();
  114. }
  115. String getButtonText() const
  116. {
  117. return "convert to a path";
  118. }
  119. private:
  120. PaintElementRectangle* const element;
  121. };
  122. };
  123. #endif // __JUCER_PAINTELEMENTRECTANGLE_JUCEHEADER__