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.

281 lines
9.2KB

  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 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 (bool isClickOnSelectedObject)
  73. {
  74. if (isClickOnSelectedObject)
  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. void objectDoubleClicked (const MouseEvent& e, const ValueTree& state)
  90. {
  91. getDocument().componentDoubleClicked (e, state);
  92. }
  93. const ValueTree getObjectState (const String& objectId)
  94. {
  95. return getDocument().getComponentWithID (objectId);
  96. }
  97. const Rectangle<int> getObjectPosition (const ValueTree& state)
  98. {
  99. return getDocument().getCoordsFor (state).resolve (&getDocument()).getSmallestIntegerContainer();
  100. }
  101. RelativeRectangle getObjectCoords (const ValueTree& state)
  102. {
  103. return getDocument().getCoordsFor (state);
  104. }
  105. SelectedItems& getSelection()
  106. {
  107. return editor.getSelection();
  108. }
  109. void deselectNonDraggableObjects()
  110. {
  111. editor.deselectNonComponents();
  112. }
  113. void findLassoItemsInArea (Array <SelectedItems::ItemType>& itemsFound, const Rectangle<int>& area)
  114. {
  115. for (int i = getComponentHolder()->getNumChildComponents(); --i >= 0;)
  116. {
  117. Component* c = getComponentHolder()->getChildComponent(i);
  118. if (c->getBounds().intersects (area))
  119. itemsFound.add (ComponentDocument::getJucerIDFor (c));
  120. }
  121. }
  122. //==============================================================================
  123. class DragOperation : public EditorDragOperation
  124. {
  125. public:
  126. DragOperation (ComponentEditorCanvas* canvas_,
  127. const MouseEvent& e,
  128. Component* snapGuideParentComp_,
  129. const ResizableBorderComponent::Zone& zone_)
  130. : EditorDragOperation (canvas_, e, snapGuideParentComp_, zone_)
  131. {
  132. }
  133. ~DragOperation()
  134. {
  135. getUndoManager().beginNewTransaction();
  136. }
  137. protected:
  138. ComponentDocument& getDocument() throw() { return static_cast <ComponentEditorCanvas*> (canvas)->getDocument(); }
  139. int getCanvasWidth() { return getDocument().getCanvasWidth().getValue(); }
  140. int getCanvasHeight() { return getDocument().getCanvasHeight().getValue(); }
  141. UndoManager& getUndoManager() { return *getDocument().getUndoManager(); }
  142. const Rectangle<float> getObjectPosition (const ValueTree& state)
  143. {
  144. ComponentDocument& doc = getDocument();
  145. return doc.getCoordsFor (state).resolve (&doc);
  146. }
  147. bool setObjectPosition (ValueTree& state, const Rectangle<float>& newBounds)
  148. {
  149. ComponentDocument& doc = getDocument();
  150. RelativeRectangle pr (doc.getCoordsFor (state));
  151. pr.moveToAbsolute (newBounds, &doc);
  152. return doc.setCoordsFor (state, pr);
  153. }
  154. float getMarkerPosition (const ValueTree& marker, bool isX)
  155. {
  156. ComponentDocument& doc = getDocument();
  157. return (float) doc.getMarkerList (isX).getCoordinate (marker).resolve (&doc);
  158. }
  159. };
  160. DragOperation* createDragOperation (const MouseEvent& e, Component* snapGuideParentComponent,
  161. const ResizableBorderComponent::Zone& zone)
  162. {
  163. DragOperation* d = new DragOperation (this, e, snapGuideParentComponent, zone);
  164. Array<ValueTree> selected, unselected;
  165. for (int i = getDocument().getNumComponents(); --i >= 0;)
  166. {
  167. const ValueTree v (getDocument().getComponent (i));
  168. if (editor.getSelection().isSelected (v [ComponentDocument::idProperty]))
  169. selected.add (v);
  170. else
  171. unselected.add (v);
  172. }
  173. d->initialise (selected, unselected);
  174. return d;
  175. }
  176. UndoManager& getUndoManager()
  177. {
  178. return *getDocument().getUndoManager();
  179. }
  180. private:
  181. //==============================================================================
  182. ComponentEditor& editor;
  183. class ComponentHolder : public Component,
  184. public Value::Listener
  185. {
  186. public:
  187. ComponentHolder (const Value& backgroundColour_)
  188. : backgroundColour (backgroundColour_)
  189. {
  190. setOpaque (true);
  191. updateColour();
  192. backgroundColour.addListener (this);
  193. }
  194. ~ComponentHolder()
  195. {
  196. deleteAllChildren();
  197. }
  198. void updateColour()
  199. {
  200. Colour newColour (Colours::white);
  201. if (backgroundColour.toString().isNotEmpty())
  202. newColour = Colour::fromString (backgroundColour.toString());
  203. if (newColour != colour)
  204. {
  205. colour = newColour;
  206. repaint();
  207. }
  208. }
  209. void paint (Graphics& g)
  210. {
  211. if (colour.isOpaque())
  212. g.fillAll (colour);
  213. else
  214. g.fillCheckerBoard (0, 0, getWidth(), getHeight(), 24, 24,
  215. Colour (0xffeeeeee).overlaidWith (colour),
  216. Colour (0xffffffff).overlaidWith (colour));
  217. }
  218. void valueChanged (Value&)
  219. {
  220. updateColour();
  221. }
  222. private:
  223. Value backgroundColour;
  224. Colour colour;
  225. };
  226. };
  227. #endif // __JUCER_COMPONENTEDITORCANVAS_H_37C33B56__