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.

143 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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_PAINTELEMENTUNDOABLEACTION_H_INCLUDED
  18. #define JUCER_PAINTELEMENTUNDOABLEACTION_H_INCLUDED
  19. #include "../ui/jucer_JucerDocumentEditor.h"
  20. #include "jucer_PaintElementGroup.h"
  21. //==============================================================================
  22. template <class ElementType>
  23. class PaintElementUndoableAction : public UndoableAction
  24. {
  25. public:
  26. PaintElementUndoableAction (ElementType* const element)
  27. : routine (*element->getOwner()),
  28. elementIndex (element->getOwner()->indexOfElement (element))
  29. {
  30. jassert (element != 0);
  31. if (elementIndex < 0)
  32. findGroupIndices (element->getOwner(), element);
  33. jassert (elementIndex >= 0);
  34. }
  35. ElementType* getElement() const
  36. {
  37. if (containerGroups.size() > 0)
  38. {
  39. PaintElementGroup* group = 0;
  40. group = dynamic_cast <PaintElementGroup*> (routine.getElement (containerGroups.getFirst()));
  41. if (group == 0)
  42. return 0;
  43. for (int i = 1; i < containerGroups.size(); ++i)
  44. {
  45. group = dynamic_cast <PaintElementGroup*> (group->getElement (containerGroups.getUnchecked(i)));
  46. if (group == 0)
  47. return 0;
  48. }
  49. ElementType* const e = dynamic_cast <ElementType*> (group->getElement (elementIndex));
  50. jassert (e != 0);
  51. return e;
  52. }
  53. else
  54. {
  55. ElementType* const e = dynamic_cast <ElementType*> (routine.getElement (elementIndex));
  56. jassert (e != 0);
  57. return e;
  58. }
  59. }
  60. int getSizeInUnits() { return 2; }
  61. protected:
  62. PaintRoutine& routine;
  63. int elementIndex;
  64. Array <int> containerGroups;
  65. void changed() const
  66. {
  67. jassert (routine.getDocument() != 0);
  68. routine.getDocument()->changed();
  69. }
  70. void showCorrectTab() const
  71. {
  72. if (JucerDocumentEditor* const docHolder = JucerDocumentEditor::getActiveDocumentHolder())
  73. docHolder->showGraphics (&routine);
  74. if (routine.getSelectedElements().getNumSelected() == 0)
  75. if (ElementType* const e = dynamic_cast <ElementType*> (routine.getElement (elementIndex)))
  76. routine.getSelectedElements().selectOnly (e);
  77. }
  78. private:
  79. void findGroupIndices (PaintRoutine* const pr, PaintElement* const element)
  80. {
  81. for (int i = pr->getNumElements(); --i >= 0;)
  82. {
  83. PaintElementGroup* const pg = dynamic_cast <PaintElementGroup*> (pr->getElement (i));
  84. if (pg != 0 && pg->containsElement (element))
  85. {
  86. containerGroups.add (i);
  87. findGroupIndices (pg, element);
  88. }
  89. }
  90. }
  91. void findGroupIndices (PaintElementGroup* const group, PaintElement* const element)
  92. {
  93. elementIndex = group->indexOfElement (element);
  94. if (elementIndex < 0)
  95. {
  96. for (int i = group->getNumElements(); --i >= 0;)
  97. {
  98. PaintElementGroup* pg = dynamic_cast <PaintElementGroup*> (group->getElement (i));
  99. if (pg != 0 && pg->containsElement (element))
  100. {
  101. containerGroups.add (i);
  102. findGroupIndices (pg, element);
  103. }
  104. }
  105. }
  106. }
  107. PaintElementUndoableAction (const PaintElementUndoableAction&);
  108. PaintElementUndoableAction& operator= (const PaintElementUndoableAction&);
  109. };
  110. #endif // JUCER_PAINTELEMENTUNDOABLEACTION_H_INCLUDED