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.

306 lines
9.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. #include "../../jucer_Headers.h"
  19. #include "../../model/Drawable/jucer_DrawableDocument.h"
  20. #include "../jucer_JucerTreeViewBase.h"
  21. #include "../Editor Base/jucer_EditorPanel.h"
  22. #include "../Editor Base/jucer_EditorCanvas.h"
  23. #include "../Editor Base/jucer_EditorDragOperation.h"
  24. #include "jucer_DrawableEditor.h"
  25. #include "jucer_DrawableEditorCanvas.h"
  26. #include "jucer_DrawableEditorTreeView.h"
  27. #include "jucer_DrawableEditorToolbar.h"
  28. //==============================================================================
  29. class DrawableEditor::Panel : public EditorPanelBase
  30. {
  31. public:
  32. Panel (DrawableEditor& editor_)
  33. : toolbarFactory (editor_),
  34. editor (editor_)
  35. {
  36. }
  37. ~Panel()
  38. {
  39. shutdown();
  40. }
  41. void createCanvas()
  42. {
  43. initialise (new DrawableEditorCanvas (editor), toolbarFactory,
  44. new DrawableTreeViewItem (editor, editor.getDocument().getRootDrawableNode().getState()));
  45. }
  46. SelectedItemSet<String>& getSelection()
  47. {
  48. return editor.getSelection();
  49. }
  50. void getSelectedItemProperties (Array<PropertyComponent*>& props)
  51. {
  52. editor.getDocument().createItemProperties (props, editor.getSelectedIds());
  53. }
  54. private:
  55. DrawableEditorToolbarFactory toolbarFactory;
  56. DrawableEditor& editor;
  57. };
  58. //==============================================================================
  59. DrawableEditor::DrawableEditor (OpenDocumentManager::Document* document_,
  60. Project* project_,
  61. DrawableDocument* drawableDocument_)
  62. : DocumentEditorComponent (document_),
  63. project (project_),
  64. drawableDocument (drawableDocument_)
  65. {
  66. jassert (drawableDocument_ != 0);
  67. setOpaque (true);
  68. addAndMakeVisible (panel = new Panel (*this));
  69. panel->createCanvas();
  70. }
  71. DrawableEditor::~DrawableEditor()
  72. {
  73. deleteAllChildren();
  74. }
  75. void DrawableEditor::paint (Graphics& g)
  76. {
  77. g.fillAll (Colours::white);
  78. }
  79. void DrawableEditor::resized()
  80. {
  81. panel->setBounds (getLocalBounds());
  82. }
  83. //==============================================================================
  84. const StringArray DrawableEditor::getSelectedIds() const
  85. {
  86. StringArray ids;
  87. const int num = selection.getNumSelected();
  88. for (int i = 0; i < num; ++i)
  89. ids.add (selection.getSelectedItem(i));
  90. return ids;
  91. }
  92. void DrawableEditor::deleteSelection()
  93. {
  94. getUndoManager()->beginNewTransaction();
  95. DrawableComposite::ValueTreeWrapper root (getDocument().getRootDrawableNode());
  96. const StringArray ids (getSelectedIds());
  97. for (int i = ids.size(); --i >= 0;)
  98. {
  99. const ValueTree v (root.getDrawableWithId (ids[i], false));
  100. root.removeDrawable (v, getUndoManager());
  101. }
  102. getUndoManager()->beginNewTransaction();
  103. }
  104. void DrawableEditor::selectionToFront()
  105. {
  106. getUndoManager()->beginNewTransaction();
  107. DrawableComposite::ValueTreeWrapper root (getDocument().getRootDrawableNode());
  108. int index = 0;
  109. for (int i = root.getNumDrawables(); --i >= 0;)
  110. {
  111. const Drawable::ValueTreeWrapperBase d (root.getDrawableState (index));
  112. if (getSelection().isSelected (d.getID()))
  113. root.moveDrawableOrder (index, -1, getUndoManager());
  114. else
  115. ++index;
  116. }
  117. getUndoManager()->beginNewTransaction();
  118. }
  119. void DrawableEditor::selectionToBack()
  120. {
  121. getUndoManager()->beginNewTransaction();
  122. DrawableComposite::ValueTreeWrapper root (getDocument().getRootDrawableNode());
  123. int index = root.getNumDrawables() - 1;
  124. for (int i = root.getNumDrawables(); --i >= 0;)
  125. {
  126. const Drawable::ValueTreeWrapperBase d (root.getDrawableState (index));
  127. if (getSelection().isSelected (d.getID()))
  128. root.moveDrawableOrder (index, 0, getUndoManager());
  129. else
  130. --index;
  131. }
  132. getUndoManager()->beginNewTransaction();
  133. }
  134. void DrawableEditor::showNewShapeMenu (Component* componentToAttachTo)
  135. {
  136. PopupMenu m;
  137. getDocument().addNewItemMenuItems (m);
  138. const int r = m.showAt (componentToAttachTo);
  139. ValueTree newItem (getDocument().performNewItemMenuItem (r));
  140. if (newItem.isValid())
  141. getSelection().selectOnly (Drawable::ValueTreeWrapperBase (newItem).getID());
  142. }
  143. //==============================================================================
  144. void DrawableEditor::getAllCommands (Array <CommandID>& commands)
  145. {
  146. DocumentEditorComponent::getAllCommands (commands);
  147. const CommandID ids[] = { CommandIDs::undo,
  148. CommandIDs::redo,
  149. CommandIDs::toFront,
  150. CommandIDs::toBack,
  151. CommandIDs::showOrHideProperties,
  152. CommandIDs::showOrHideTree,
  153. CommandIDs::showOrHideMarkers,
  154. CommandIDs::toggleSnapping,
  155. StandardApplicationCommandIDs::del };
  156. commands.addArray (ids, numElementsInArray (ids));
  157. }
  158. void DrawableEditor::getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
  159. {
  160. result.setActive (document != 0);
  161. switch (commandID)
  162. {
  163. case CommandIDs::undo:
  164. result.setInfo ("Undo", "Undoes the last change", CommandCategories::general, 0);
  165. result.defaultKeypresses.add (KeyPress ('z', ModifierKeys::commandModifier, 0));
  166. break;
  167. case CommandIDs::redo:
  168. result.setInfo ("Redo", "Redoes the last change", CommandCategories::general, 0);
  169. result.defaultKeypresses.add (KeyPress ('z', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  170. result.defaultKeypresses.add (KeyPress ('y', ModifierKeys::commandModifier, 0));
  171. break;
  172. case CommandIDs::toFront:
  173. result.setInfo ("Bring to Front", "Brings the selected items to the front", CommandCategories::editing, 0);
  174. break;
  175. case CommandIDs::toBack:
  176. result.setInfo ("Send to Back", "Moves the selected items to the back", CommandCategories::editing, 0);
  177. break;
  178. case CommandIDs::showOrHideProperties:
  179. result.setInfo ("Show/Hide Tree", "Shows or hides the component tree view", CommandCategories::editing, 0);
  180. result.setTicked (panel != 0 && panel->arePropertiesVisible());
  181. break;
  182. case CommandIDs::showOrHideTree:
  183. result.setInfo ("Show/Hide Properties", "Shows or hides the component properties panel", CommandCategories::editing, 0);
  184. result.setTicked (panel != 0 && panel->isTreeVisible());
  185. break;
  186. case CommandIDs::showOrHideMarkers:
  187. result.setInfo ("Show/Hide Markers", "Shows or hides the markers", CommandCategories::editing, 0);
  188. result.setTicked (panel != 0 && panel->areMarkersVisible());
  189. break;
  190. case CommandIDs::toggleSnapping:
  191. result.setInfo ("Toggle snapping", "Turns object snapping on or off", CommandCategories::editing, 0);
  192. result.setTicked (panel != 0 && panel->isSnappingEnabled());
  193. break;
  194. case StandardApplicationCommandIDs::del:
  195. result.setInfo ("Delete", String::empty, CommandCategories::general, 0);
  196. result.defaultKeypresses.add (KeyPress (KeyPress::deleteKey, 0, 0));
  197. result.defaultKeypresses.add (KeyPress (KeyPress::backspaceKey, 0, 0));
  198. break;
  199. default:
  200. DocumentEditorComponent::getCommandInfo (commandID, result);
  201. break;
  202. }
  203. }
  204. bool DrawableEditor::perform (const InvocationInfo& info)
  205. {
  206. switch (info.commandID)
  207. {
  208. case CommandIDs::undo:
  209. getUndoManager()->beginNewTransaction();
  210. getUndoManager()->undo();
  211. return true;
  212. case CommandIDs::redo:
  213. getUndoManager()->beginNewTransaction();
  214. getUndoManager()->redo();
  215. return true;
  216. case CommandIDs::toFront:
  217. selectionToFront();
  218. return true;
  219. case CommandIDs::toBack:
  220. selectionToBack();
  221. return true;
  222. case CommandIDs::showOrHideProperties:
  223. panel->showOrHideProperties();
  224. return true;
  225. case CommandIDs::showOrHideTree:
  226. panel->showOrHideTree();
  227. return true;
  228. case CommandIDs::showOrHideMarkers:
  229. panel->showOrHideMarkers();
  230. return true;
  231. case CommandIDs::toggleSnapping:
  232. panel->toggleSnapping();
  233. return true;
  234. case StandardApplicationCommandIDs::del:
  235. deleteSelection();
  236. return true;
  237. default:
  238. break;
  239. }
  240. return DocumentEditorComponent::perform (info);
  241. }