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.

228 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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_COMPONENTEDITORCANVAS_H_37C33B56__
  19. #define __JUCER_COMPONENTEDITORCANVAS_H_37C33B56__
  20. //==============================================================================
  21. class ComponentEditorCanvas : public EditorCanvasBase,
  22. public Timer
  23. {
  24. public:
  25. //==============================================================================
  26. ComponentEditorCanvas (ComponentEditor& editor_)
  27. : editor (editor_)
  28. {
  29. initialise();
  30. getDocument().getRoot().addListener (this);
  31. }
  32. ~ComponentEditorCanvas()
  33. {
  34. getDocument().getRoot().removeListener (this);
  35. shutdown();
  36. }
  37. ComponentEditor& getEditor() { return editor; }
  38. ComponentDocument& getDocument() { return editor.getDocument(); }
  39. void timerCallback()
  40. {
  41. stopTimer();
  42. if (! Component::isMouseButtonDownAnywhere())
  43. getDocument().beginNewTransaction();
  44. }
  45. Component* createComponentHolder()
  46. {
  47. return new Component();
  48. }
  49. void updateComponents()
  50. {
  51. getDocument().updateComponentsIn (getComponentHolder());
  52. startTimer (500);
  53. }
  54. int getCanvasWidth() { return getDocument().getCanvasWidth().getValue(); }
  55. int getCanvasHeight() { return getDocument().getCanvasHeight().getValue(); }
  56. void setCanvasWidth (int w) { getDocument().getCanvasWidth() = w; }
  57. void setCanvasHeight (int h) { getDocument().getCanvasHeight() = h; }
  58. ComponentDocument::MarkerList& getMarkerList (bool isX)
  59. {
  60. return getDocument().getMarkerList (isX);
  61. }
  62. const SelectedItems::ItemType findObjectIdAt (const Point<int>& position)
  63. {
  64. for (int i = getComponentHolder()->getNumChildComponents(); --i >= 0;)
  65. {
  66. Component* const c = getComponentHolder()->getChildComponent(i);
  67. if (c->getBounds().contains (position))
  68. return ComponentDocument::getJucerIDFor (c);
  69. }
  70. return String::empty;
  71. }
  72. void showPopupMenu (const Point<int>& position)
  73. {
  74. if (findObjectIdAt (position).isNotEmpty())
  75. {
  76. PopupMenu m;
  77. m.addCommandItem (commandManager, CommandIDs::toFront);
  78. m.addCommandItem (commandManager, CommandIDs::toBack);
  79. m.addSeparator();
  80. m.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  81. const int r = m.show();
  82. (void) r;
  83. }
  84. else
  85. {
  86. editor.showNewComponentMenu (0);
  87. }
  88. }
  89. const ValueTree getObjectState (const String& objectId)
  90. {
  91. return getDocument().getComponentWithID (objectId);
  92. }
  93. const Rectangle<int> getObjectPosition (const ValueTree& state)
  94. {
  95. return getDocument().getCoordsFor (state).resolve (getDocument());
  96. }
  97. RectangleCoordinates getObjectCoords (const ValueTree& state)
  98. {
  99. return getDocument().getCoordsFor (state);
  100. }
  101. SelectedItems& getSelection()
  102. {
  103. return editor.getSelection();
  104. }
  105. void deselectNonDraggableObjects()
  106. {
  107. editor.deselectNonComponents();
  108. }
  109. void findLassoItemsInArea (Array <SelectedItems::ItemType>& itemsFound, const Rectangle<int>& area)
  110. {
  111. for (int i = getComponentHolder()->getNumChildComponents(); --i >= 0;)
  112. {
  113. Component* c = getComponentHolder()->getChildComponent(i);
  114. if (c->getBounds().intersects (area))
  115. itemsFound.add (ComponentDocument::getJucerIDFor (c));
  116. }
  117. }
  118. //==============================================================================
  119. class DragOperation : public EditorDragOperation
  120. {
  121. public:
  122. DragOperation (ComponentEditorCanvas* canvas_,
  123. const MouseEvent& e,
  124. Component* snapGuideParentComp_,
  125. const ResizableBorderComponent::Zone& zone_)
  126. : EditorDragOperation (canvas_, e, snapGuideParentComp_, zone_)
  127. {
  128. }
  129. ~DragOperation()
  130. {
  131. getUndoManager().beginNewTransaction();
  132. }
  133. protected:
  134. ComponentDocument& getDocument() throw() { return static_cast <ComponentEditorCanvas*> (canvas)->getDocument(); }
  135. int getCanvasWidth() { return getDocument().getCanvasWidth().getValue(); }
  136. int getCanvasHeight() { return getDocument().getCanvasHeight().getValue(); }
  137. UndoManager& getUndoManager() { return *getDocument().getUndoManager(); }
  138. const Rectangle<float> getObjectPosition (const ValueTree& state)
  139. {
  140. ComponentDocument& doc = getDocument();
  141. RectangleCoordinates relativePos (doc.getCoordsFor (state));
  142. const Rectangle<int> intPos (relativePos.resolve (doc));
  143. return intPos.toFloat();
  144. }
  145. bool setObjectPosition (ValueTree& state, const Rectangle<float>& newBounds)
  146. {
  147. ComponentDocument& doc = getDocument();
  148. RectangleCoordinates pr (doc.getCoordsFor (state));
  149. pr.moveToAbsolute (newBounds, doc);
  150. return doc.setCoordsFor (state, pr);
  151. }
  152. float getMarkerPosition (const ValueTree& marker, bool isX)
  153. {
  154. ComponentDocument& doc = getDocument();
  155. return doc.getMarkerList (isX).getCoordinate (marker).resolve (doc);
  156. }
  157. };
  158. DragOperation* createDragOperation (const MouseEvent& e, Component* snapGuideParentComponent,
  159. const ResizableBorderComponent::Zone& zone)
  160. {
  161. DragOperation* d = new DragOperation (this, e, snapGuideParentComponent, zone);
  162. Array<ValueTree> selected, unselected;
  163. for (int i = getDocument().getNumComponents(); --i >= 0;)
  164. {
  165. const ValueTree v (getDocument().getComponent (i));
  166. if (editor.getSelection().isSelected (v [ComponentDocument::idProperty]))
  167. selected.add (v);
  168. else
  169. unselected.add (v);
  170. }
  171. d->initialise (selected, unselected);
  172. return d;
  173. }
  174. UndoManager& getUndoManager()
  175. {
  176. return *getDocument().getUndoManager();
  177. }
  178. private:
  179. //==============================================================================
  180. ComponentEditor& editor;
  181. };
  182. #endif // __JUCER_COMPONENTEDITORCANVAS_H_37C33B56__