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.

350 lines
11KB

  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 ComponentHolder (getDocument().getBackgroundColour());
  48. }
  49. void documentChanged()
  50. {
  51. getDocument().updateComponentsIn (getComponentHolder());
  52. startTimer (500);
  53. }
  54. const Rectangle<int> getCanvasBounds()
  55. {
  56. return Rectangle<int> (0, 0, getDocument().getCanvasWidth().getValue(),
  57. getDocument().getCanvasHeight().getValue());
  58. }
  59. void setCanvasBounds (const Rectangle<int>& newBounds)
  60. {
  61. jassert (newBounds.getPosition().isOrigin());
  62. getDocument().getCanvasWidth() = newBounds.getWidth();
  63. getDocument().getCanvasHeight() = newBounds.getHeight();
  64. }
  65. bool canResizeCanvas() const { return true; }
  66. ComponentDocument::MarkerList& getMarkerList (bool isX)
  67. {
  68. return getDocument().getMarkerList (isX);
  69. }
  70. double limitMarkerPosition (double pos)
  71. {
  72. return jmax (0.0, pos);
  73. }
  74. const SelectedItems::ItemType findObjectIdAt (const Point<int>& position)
  75. {
  76. for (int i = getComponentHolder()->getNumChildComponents(); --i >= 0;)
  77. {
  78. Component* const c = getComponentHolder()->getChildComponent(i);
  79. if (c->getBounds().contains (position))
  80. return ComponentDocument::getJucerIDFor (c);
  81. }
  82. return String::empty;
  83. }
  84. void showPopupMenu (bool isClickOnSelectedObject)
  85. {
  86. if (isClickOnSelectedObject)
  87. {
  88. PopupMenu m;
  89. m.addCommandItem (commandManager, CommandIDs::toFront);
  90. m.addCommandItem (commandManager, CommandIDs::toBack);
  91. m.addSeparator();
  92. m.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  93. const int r = m.show();
  94. (void) r;
  95. }
  96. else
  97. {
  98. editor.showNewComponentMenu (0);
  99. }
  100. }
  101. void objectDoubleClicked (const MouseEvent& e, const ValueTree& state)
  102. {
  103. getDocument().componentDoubleClicked (e, state);
  104. }
  105. const ValueTree getObjectState (const String& objectId)
  106. {
  107. return getDocument().getComponentWithID (objectId);
  108. }
  109. const Rectangle<int> getObjectPosition (const ValueTree& state)
  110. {
  111. return getDocument().getCoordsFor (state).resolve (&getDocument()).getSmallestIntegerContainer();
  112. }
  113. bool hasSizeGuides() const { return true; }
  114. RelativeRectangle getObjectCoords (const ValueTree& state)
  115. {
  116. return getDocument().getCoordsFor (state);
  117. }
  118. void updateControlPointComponents (Component* parent, OwnedArray<OverlayItemComponent>& existingComps)
  119. {
  120. }
  121. SelectedItems& getSelection()
  122. {
  123. return editor.getSelection();
  124. }
  125. void deselectNonDraggableObjects()
  126. {
  127. editor.deselectNonComponents();
  128. }
  129. void findLassoItemsInArea (Array <SelectedItems::ItemType>& itemsFound, const Rectangle<int>& area)
  130. {
  131. for (int i = getComponentHolder()->getNumChildComponents(); --i >= 0;)
  132. {
  133. Component* c = getComponentHolder()->getChildComponent(i);
  134. if (c->getBounds().intersects (area))
  135. itemsFound.add (ComponentDocument::getJucerIDFor (c));
  136. }
  137. }
  138. //==============================================================================
  139. class DragOperation : public EditorDragOperation
  140. {
  141. public:
  142. DragOperation (ComponentEditorCanvas* canvas_,
  143. const MouseEvent& e, const Point<int>& mousePos,
  144. Component* snapGuideParentComp_,
  145. const ResizableBorderComponent::Zone& zone_)
  146. : EditorDragOperation (canvas_, e, mousePos, snapGuideParentComp_, zone_)
  147. {
  148. }
  149. ~DragOperation()
  150. {
  151. }
  152. protected:
  153. ComponentDocument& getDocument() throw() { return static_cast <ComponentEditorCanvas*> (canvas)->getDocument(); }
  154. void getSnapPointsX (Array<float>& points, bool includeCentre)
  155. {
  156. const float width = getDocument().getCanvasWidth().getValue();
  157. points.add (0.0f);
  158. points.add (width);
  159. if (includeCentre)
  160. points.add (width / 2.0f);
  161. }
  162. void getSnapPointsY (Array<float>& points, bool includeCentre)
  163. {
  164. const float height = getDocument().getCanvasHeight().getValue();
  165. points.add (0.0f);
  166. points.add (height);
  167. if (includeCentre)
  168. points.add (height / 2.0f);
  169. }
  170. int getCanvasWidth() { return getDocument().getCanvasWidth().getValue(); }
  171. int getCanvasHeight() { return getDocument().getCanvasHeight().getValue(); }
  172. UndoManager& getUndoManager() { return *getDocument().getUndoManager(); }
  173. void getObjectDependencies (const ValueTree& state, Array<ValueTree>& deps)
  174. {
  175. ComponentDocument& doc = getDocument();
  176. RelativeRectangle pr (doc.getCoordsFor (state));
  177. StringArray anchors;
  178. anchors.addIfNotAlreadyThere (pr.left.getAnchorName1());
  179. anchors.addIfNotAlreadyThere (pr.left.getAnchorName2());
  180. anchors.addIfNotAlreadyThere (pr.top.getAnchorName1());
  181. anchors.addIfNotAlreadyThere (pr.top.getAnchorName2());
  182. anchors.addIfNotAlreadyThere (pr.right.getAnchorName1());
  183. anchors.addIfNotAlreadyThere (pr.right.getAnchorName2());
  184. anchors.addIfNotAlreadyThere (pr.bottom.getAnchorName1());
  185. anchors.addIfNotAlreadyThere (pr.bottom.getAnchorName2());
  186. for (int i = 0; i < anchors.size(); ++i)
  187. {
  188. const String anchor (anchors[i]);
  189. if (anchor.isNotEmpty() && ! anchor.startsWith ("parent."))
  190. {
  191. const ValueTree v (doc.getComponentWithMemberName (anchor.upToFirstOccurrenceOf (".", false, false)));
  192. if (v.isValid())
  193. deps.add (v);
  194. }
  195. }
  196. }
  197. const Rectangle<float> getObjectPosition (const ValueTree& state)
  198. {
  199. ComponentDocument& doc = getDocument();
  200. return doc.getCoordsFor (state).resolve (&doc);
  201. }
  202. void setObjectPosition (ValueTree& state, const Rectangle<float>& newBounds)
  203. {
  204. ComponentDocument& doc = getDocument();
  205. RelativeRectangle pr (doc.getCoordsFor (state));
  206. pr.moveToAbsolute (newBounds, &doc);
  207. doc.setCoordsFor (state, pr);
  208. }
  209. float getMarkerPosition (const ValueTree& marker, bool isX)
  210. {
  211. ComponentDocument& doc = getDocument();
  212. return (float) doc.getMarkerList (isX).getCoordinate (marker).resolve (&doc);
  213. }
  214. };
  215. DragOperation* createDragOperation (const MouseEvent& e, Component* snapGuideParentComponent,
  216. const ResizableBorderComponent::Zone& zone)
  217. {
  218. DragOperation* d = new DragOperation (this, e, e.getPosition() - origin, snapGuideParentComponent, zone);
  219. Array<ValueTree> selected, unselected;
  220. for (int i = getDocument().getNumComponents(); --i >= 0;)
  221. {
  222. const ValueTree v (getDocument().getComponent (i));
  223. if (editor.getSelection().isSelected (v [ComponentDocument::idProperty]))
  224. selected.add (v);
  225. else
  226. unselected.add (v);
  227. }
  228. d->initialise (selected, unselected);
  229. return d;
  230. }
  231. UndoManager& getUndoManager()
  232. {
  233. return *getDocument().getUndoManager();
  234. }
  235. private:
  236. //==============================================================================
  237. ComponentEditor& editor;
  238. class ComponentHolder : public Component,
  239. public Value::Listener
  240. {
  241. public:
  242. ComponentHolder (const Value& backgroundColour_)
  243. : backgroundColour (backgroundColour_)
  244. {
  245. setOpaque (true);
  246. updateColour();
  247. backgroundColour.addListener (this);
  248. }
  249. ~ComponentHolder()
  250. {
  251. deleteAllChildren();
  252. }
  253. void updateColour()
  254. {
  255. Colour newColour (Colours::white);
  256. if (backgroundColour.toString().isNotEmpty())
  257. newColour = Colour::fromString (backgroundColour.toString());
  258. if (newColour != colour)
  259. {
  260. colour = newColour;
  261. repaint();
  262. }
  263. }
  264. void paint (Graphics& g)
  265. {
  266. if (colour.isOpaque())
  267. g.fillAll (colour);
  268. else
  269. g.fillCheckerBoard (0, 0, getWidth(), getHeight(), 24, 24,
  270. Colour (0xffeeeeee).overlaidWith (colour),
  271. Colour (0xffffffff).overlaidWith (colour));
  272. }
  273. void valueChanged (Value&)
  274. {
  275. updateColour();
  276. }
  277. private:
  278. Value backgroundColour;
  279. Colour colour;
  280. };
  281. };
  282. #endif // __JUCER_COMPONENTEDITORCANVAS_H_37C33B56__