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.

144 lines
4.7KB

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