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.

1176 lines
44KB

  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. #include "../../jucer_Headers.h"
  19. #include "../../Application/jucer_AppearanceSettings.h"
  20. #include "../../Application/jucer_Application.h"
  21. #include "jucer_JucerDocumentEditor.h"
  22. #include "jucer_TestComponent.h"
  23. #include "../jucer_ObjectTypes.h"
  24. #include "jucer_ComponentLayoutPanel.h"
  25. #include "jucer_PaintRoutinePanel.h"
  26. #include "jucer_ResourceEditorPanel.h"
  27. #include "../properties/jucer_ComponentTextProperty.h"
  28. #include "../properties/jucer_ComponentChoiceProperty.h"
  29. #include "../ui/jucer_JucerCommandIDs.h"
  30. //==============================================================================
  31. class ExtraMethodsList : public PropertyComponent,
  32. public ListBoxModel,
  33. public ChangeListener
  34. {
  35. public:
  36. ExtraMethodsList (JucerDocument& doc)
  37. : PropertyComponent ("extra callbacks", 250),
  38. document (doc)
  39. {
  40. addAndMakeVisible (listBox = new ListBox (String::empty, this));
  41. listBox->setRowHeight (22);
  42. document.addChangeListener (this);
  43. }
  44. ~ExtraMethodsList()
  45. {
  46. document.removeChangeListener (this);
  47. }
  48. int getNumRows()
  49. {
  50. return methods.size();
  51. }
  52. void paintListBoxItem (int row, Graphics& g, int width, int height, bool rowIsSelected)
  53. {
  54. if (row < 0 || row >= getNumRows())
  55. return;
  56. if (rowIsSelected)
  57. g.fillAll (findColour (TextEditor::highlightColourId));
  58. g.setColour (Colours::black);
  59. g.setFont (height * 0.6f);
  60. g.drawText (returnValues [row] + " " + baseClasses [row] + "::" + methods [row],
  61. 30, 0, width - 32, height,
  62. Justification::centredLeft, true);
  63. getLookAndFeel().drawTickBox (g, *this, 6, 2, 18, 18, document.isOptionalMethodEnabled (methods [row]), true, false, false);
  64. }
  65. void listBoxItemClicked (int row, const MouseEvent& e)
  66. {
  67. if (row < 0 || row >= getNumRows())
  68. return;
  69. if (e.x < 30)
  70. document.setOptionalMethodEnabled (methods [row],
  71. ! document.isOptionalMethodEnabled (methods [row]));
  72. }
  73. void paint (Graphics& g)
  74. {
  75. g.fillAll (Colours::white);
  76. }
  77. void resized()
  78. {
  79. listBox->setBounds (getLocalBounds());
  80. }
  81. void refresh()
  82. {
  83. baseClasses.clear();
  84. returnValues.clear();
  85. methods.clear();
  86. initialContents.clear();
  87. document.getOptionalMethods (baseClasses, returnValues, methods, initialContents);
  88. listBox->updateContent();
  89. listBox->repaint();
  90. }
  91. void changeListenerCallback (ChangeBroadcaster*)
  92. {
  93. refresh();
  94. }
  95. private:
  96. JucerDocument& document;
  97. ScopedPointer<ListBox> listBox;
  98. StringArray baseClasses, returnValues, methods, initialContents;
  99. };
  100. //==============================================================================
  101. class ClassPropertiesPanel : public Component,
  102. private ChangeListener
  103. {
  104. public:
  105. ClassPropertiesPanel (JucerDocument& doc)
  106. : document (doc)
  107. {
  108. addAndMakeVisible (&panel1);
  109. addAndMakeVisible (&panel2);
  110. Array <PropertyComponent*> props;
  111. props.add (new ComponentClassNameProperty (doc));
  112. props.add (new TemplateFileProperty (doc));
  113. props.add (new ComponentCompNameProperty (doc));
  114. props.add (new ComponentParentClassesProperty (doc));
  115. props.add (new ComponentConstructorParamsProperty (doc));
  116. props.add (new ComponentInitialisersProperty (doc));
  117. props.add (new ComponentInitialSizeProperty (doc, true));
  118. props.add (new ComponentInitialSizeProperty (doc, false));
  119. props.add (new FixedSizeProperty (doc));
  120. panel1.addSection ("General class settings", props);
  121. Array <PropertyComponent*> props2;
  122. props2.add (new ExtraMethodsList (doc));
  123. panel2.addSection ("Extra callback methods to generate", props2);
  124. doc.addExtraClassProperties (panel1);
  125. doc.addChangeListener (this);
  126. }
  127. ~ClassPropertiesPanel()
  128. {
  129. document.removeChangeListener (this);
  130. }
  131. void resized()
  132. {
  133. int pw = jmin (getWidth() / 2 - 20, 350);
  134. panel1.setBounds (10, 6, pw, getHeight() - 12);
  135. panel2.setBounds (panel1.getRight() + 20, panel1.getY(), pw, panel1.getHeight());
  136. }
  137. void changeListenerCallback (ChangeBroadcaster*)
  138. {
  139. panel1.refreshAll();
  140. panel2.refreshAll();
  141. }
  142. private:
  143. JucerDocument& document;
  144. PropertyPanel panel1, panel2;
  145. //==============================================================================
  146. class ComponentClassNameProperty : public ComponentTextProperty <Component>
  147. {
  148. public:
  149. ComponentClassNameProperty (JucerDocument& doc)
  150. : ComponentTextProperty <Component> ("Class name", 128, false, 0, doc)
  151. {}
  152. void setText (const String& newText) { document.setClassName (newText); }
  153. String getText() const { return document.getClassName(); }
  154. };
  155. //==============================================================================
  156. class ComponentCompNameProperty : public ComponentTextProperty <Component>
  157. {
  158. public:
  159. ComponentCompNameProperty (JucerDocument& doc)
  160. : ComponentTextProperty <Component> ("Component name", 200, false, 0, doc)
  161. {}
  162. void setText (const String& newText) { document.setComponentName (newText); }
  163. String getText() const { return document.getComponentName(); }
  164. };
  165. //==============================================================================
  166. class ComponentParentClassesProperty : public ComponentTextProperty <Component>
  167. {
  168. public:
  169. ComponentParentClassesProperty (JucerDocument& doc)
  170. : ComponentTextProperty <Component> ("Parent classes", 512, false, 0, doc)
  171. {}
  172. void setText (const String& newText) { document.setParentClasses (newText); }
  173. String getText() const { return document.getParentClassString(); }
  174. };
  175. //==============================================================================
  176. class ComponentConstructorParamsProperty : public ComponentTextProperty <Component>
  177. {
  178. public:
  179. ComponentConstructorParamsProperty (JucerDocument& doc)
  180. : ComponentTextProperty <Component> ("Constructor params", 2048, false, 0, doc)
  181. {}
  182. void setText (const String& newText) { document.setConstructorParams (newText); }
  183. String getText() const { return document.getConstructorParams(); }
  184. };
  185. //==============================================================================
  186. class ComponentInitialisersProperty : public ComponentTextProperty <Component>
  187. {
  188. public:
  189. ComponentInitialisersProperty (JucerDocument& doc)
  190. : ComponentTextProperty <Component> ("Member intialisers", 2048, true, 0, doc)
  191. {
  192. preferredHeight = 24 * 3;
  193. }
  194. void setText (const String& newText) { document.setVariableInitialisers (newText); }
  195. String getText() const { return document.getVariableInitialisers(); }
  196. };
  197. //==============================================================================
  198. class ComponentInitialSizeProperty : public ComponentTextProperty <Component>
  199. {
  200. public:
  201. ComponentInitialSizeProperty (JucerDocument& doc, const bool isWidth_)
  202. : ComponentTextProperty <Component> (isWidth_ ? "Initial width"
  203. : "Initial height",
  204. 10, false, 0, doc),
  205. isWidth (isWidth_)
  206. {}
  207. void setText (const String& newText)
  208. {
  209. if (isWidth)
  210. document.setInitialSize (newText.getIntValue(), document.getInitialHeight());
  211. else
  212. document.setInitialSize (document.getInitialWidth(), newText.getIntValue());
  213. }
  214. String getText() const
  215. {
  216. return String (isWidth ? document.getInitialWidth()
  217. : document.getInitialHeight());
  218. }
  219. private:
  220. const bool isWidth;
  221. };
  222. //==============================================================================
  223. class FixedSizeProperty : public ComponentChoiceProperty <Component>
  224. {
  225. public:
  226. FixedSizeProperty (JucerDocument& doc)
  227. : ComponentChoiceProperty <Component> ("Fixed size", 0, doc)
  228. {
  229. choices.add ("Resize component to fit workspace");
  230. choices.add ("Keep component size fixed");
  231. }
  232. void setIndex (int newIndex) { document.setFixedSize (newIndex != 0); }
  233. int getIndex() const { return document.isFixedSize() ? 1 : 0; }
  234. };
  235. //==============================================================================
  236. class TemplateFileProperty : public ComponentTextProperty <Component>
  237. {
  238. public:
  239. TemplateFileProperty (JucerDocument& doc)
  240. : ComponentTextProperty <Component> ("Template file", 2048, false, 0, doc)
  241. {}
  242. void setText (const String& newText) { document.setTemplateFile (newText); }
  243. String getText() const { return document.getTemplateFile(); }
  244. };
  245. };
  246. static const Colour tabColour (Colour (0xff888888));
  247. //==============================================================================
  248. JucerDocumentEditor::JucerDocumentEditor (JucerDocument* const doc)
  249. : document (doc),
  250. tabbedComponent (TabbedButtonBar::TabsAtTop),
  251. compLayoutPanel (0),
  252. lastViewportX (0),
  253. lastViewportY (0),
  254. currentZoomLevel (1.0)
  255. {
  256. setOpaque (true);
  257. if (document != nullptr)
  258. {
  259. setSize (document->getInitialWidth(),
  260. document->getInitialHeight());
  261. addAndMakeVisible (&tabbedComponent);
  262. tabbedComponent.setOutline (0);
  263. tabbedComponent.addTab ("Class", tabColour, new ClassPropertiesPanel (*document), true);
  264. if (document->getComponentLayout() != nullptr)
  265. tabbedComponent.addTab ("Subcomponents", tabColour,
  266. compLayoutPanel = new ComponentLayoutPanel (*document, *document->getComponentLayout()), true);
  267. tabbedComponent.addTab ("Resources", tabColour, new ResourceEditorPanel (*document), true);
  268. SourceCodeEditor* codeEditor = new SourceCodeEditor (&document->getCppDocument());
  269. codeEditor->setEditor (new CppCodeEditorComponent (document->getCppFile(),
  270. document->getCppDocument().getCodeDocument()));
  271. tabbedComponent.addTab ("Code", tabColour, codeEditor, true);
  272. updateTabs();
  273. tabbedComponent.setCurrentTabIndex (0);
  274. document->addChangeListener (this);
  275. resized();
  276. refreshPropertiesPanel();
  277. changeListenerCallback (nullptr);
  278. }
  279. }
  280. JucerDocumentEditor::~JucerDocumentEditor()
  281. {
  282. tabbedComponent.clearTabs();
  283. }
  284. void JucerDocumentEditor::refreshPropertiesPanel() const
  285. {
  286. for (int i = tabbedComponent.getNumTabs(); --i >= 0;)
  287. {
  288. if (ComponentLayoutPanel* layoutPanel = dynamic_cast <ComponentLayoutPanel*> (tabbedComponent.getTabContentComponent (i)))
  289. {
  290. if (layoutPanel->isVisible())
  291. layoutPanel->updatePropertiesList();
  292. }
  293. else
  294. {
  295. if (PaintRoutinePanel* pr = dynamic_cast <PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)))
  296. if (pr->isVisible())
  297. pr->updatePropertiesList();
  298. }
  299. }
  300. }
  301. void JucerDocumentEditor::updateTabs()
  302. {
  303. const StringArray paintRoutineNames (document->getPaintRoutineNames());
  304. for (int i = tabbedComponent.getNumTabs(); --i >= 0;)
  305. {
  306. if (dynamic_cast <PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)) != 0
  307. && ! paintRoutineNames.contains (tabbedComponent.getTabNames() [i]))
  308. {
  309. tabbedComponent.removeTab (i);
  310. }
  311. }
  312. for (int i = 0; i < document->getNumPaintRoutines(); ++i)
  313. {
  314. if (! tabbedComponent.getTabNames().contains (paintRoutineNames [i]))
  315. {
  316. int index, numPaintRoutinesSeen = 0;
  317. for (index = 1; index < tabbedComponent.getNumTabs(); ++index)
  318. {
  319. if (dynamic_cast <PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (index)) != nullptr)
  320. {
  321. if (++numPaintRoutinesSeen == i)
  322. {
  323. ++index;
  324. break;
  325. }
  326. }
  327. }
  328. if (numPaintRoutinesSeen == 0)
  329. index = document->getComponentLayout() != nullptr ? 2 : 1;
  330. tabbedComponent.addTab (paintRoutineNames[i], tabColour,
  331. new PaintRoutinePanel (*document,
  332. *document->getPaintRoutine (i),
  333. this), true, index);
  334. }
  335. }
  336. }
  337. //==============================================================================
  338. void JucerDocumentEditor::paint (Graphics& g)
  339. {
  340. IntrojucerLookAndFeel::fillWithBackgroundTexture (*this, g);
  341. }
  342. void JucerDocumentEditor::resized()
  343. {
  344. tabbedComponent.setBounds (getLocalBounds().reduced (4, 2));
  345. }
  346. void JucerDocumentEditor::changeListenerCallback (ChangeBroadcaster*)
  347. {
  348. setName (document->getClassName());
  349. updateTabs();
  350. }
  351. //==============================================================================
  352. ApplicationCommandTarget* JucerDocumentEditor::getNextCommandTarget()
  353. {
  354. return findFirstTargetParentComponent();
  355. }
  356. ComponentLayout* JucerDocumentEditor::getCurrentLayout() const
  357. {
  358. if (ComponentLayoutPanel* panel = dynamic_cast <ComponentLayoutPanel*> (tabbedComponent.getCurrentContentComponent()))
  359. return &(panel->getLayout());
  360. return nullptr;
  361. }
  362. PaintRoutine* JucerDocumentEditor::getCurrentPaintRoutine() const
  363. {
  364. if (PaintRoutinePanel* panel = dynamic_cast <PaintRoutinePanel*> (tabbedComponent.getCurrentContentComponent()))
  365. return &(panel->getPaintRoutine());
  366. return nullptr;
  367. }
  368. void JucerDocumentEditor::showLayout()
  369. {
  370. if (getCurrentLayout() == nullptr)
  371. {
  372. for (int i = 0; i < tabbedComponent.getNumTabs(); ++i)
  373. {
  374. if (dynamic_cast <ComponentLayoutPanel*> (tabbedComponent.getTabContentComponent (i)) != nullptr)
  375. {
  376. tabbedComponent.setCurrentTabIndex (i);
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. void JucerDocumentEditor::showGraphics (PaintRoutine* routine)
  383. {
  384. if (getCurrentPaintRoutine() != routine || routine == 0)
  385. {
  386. for (int i = 0; i < tabbedComponent.getNumTabs(); ++i)
  387. {
  388. if (PaintRoutinePanel* pr = dynamic_cast <PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)))
  389. {
  390. if (routine == &(pr->getPaintRoutine()) || routine == nullptr)
  391. {
  392. tabbedComponent.setCurrentTabIndex (i);
  393. break;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. //==============================================================================
  400. void JucerDocumentEditor::setViewportToLastPos (Viewport* vp, EditingPanelBase& editor)
  401. {
  402. vp->setViewPosition (lastViewportX, lastViewportY);
  403. editor.setZoom (currentZoomLevel);
  404. }
  405. void JucerDocumentEditor::storeLastViewportPos (Viewport* vp, EditingPanelBase& editor)
  406. {
  407. lastViewportX = vp->getViewPositionX();
  408. lastViewportY = vp->getViewPositionY();
  409. currentZoomLevel = editor.getZoom();
  410. }
  411. void JucerDocumentEditor::setZoom (double scale)
  412. {
  413. scale = jlimit (1.0 / 4.0, 32.0, scale);
  414. if (EditingPanelBase* panel = dynamic_cast <EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  415. panel->setZoom (scale);
  416. }
  417. double JucerDocumentEditor::getZoom() const
  418. {
  419. if (EditingPanelBase* panel = dynamic_cast <EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  420. return panel->getZoom();
  421. return 1.0;
  422. }
  423. void JucerDocumentEditor::addElement (const int index)
  424. {
  425. if (PaintRoutinePanel* const panel = dynamic_cast <PaintRoutinePanel*> (tabbedComponent.getCurrentContentComponent()))
  426. {
  427. PaintRoutine* const currentPaintRoutine = & (panel->getPaintRoutine());
  428. const Rectangle<int> area (panel->getComponentArea());
  429. document->beginTransaction();
  430. PaintElement* e = ObjectTypes::createNewElement (index, currentPaintRoutine);
  431. e->setInitialBounds (area.getWidth(), area.getHeight());
  432. e = currentPaintRoutine->addNewElement (e, -1, true);
  433. if (e != nullptr)
  434. {
  435. const int randomness = jmin (80, area.getWidth() / 2, area.getHeight() / 2);
  436. int x = area.getX() + area.getWidth() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  437. int y = area.getY() + area.getHeight() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  438. x = document->snapPosition (x);
  439. y = document->snapPosition (y);
  440. panel->xyToTargetXY (x, y);
  441. Rectangle<int> r (e->getCurrentBounds (area));
  442. r.setPosition (x, y);
  443. e->setCurrentBounds (r, area, true);
  444. currentPaintRoutine->getSelectedElements().selectOnly (e);
  445. }
  446. document->beginTransaction();
  447. }
  448. }
  449. void JucerDocumentEditor::addComponent (const int index)
  450. {
  451. showLayout();
  452. if (ComponentLayoutPanel* const panel = dynamic_cast <ComponentLayoutPanel*> (tabbedComponent.getCurrentContentComponent()))
  453. {
  454. const Rectangle<int> area (panel->getComponentArea());
  455. document->beginTransaction ("Add new " + ObjectTypes::componentTypeHandlers [index]->getTypeName());
  456. const int randomness = jmin (80, area.getWidth() / 2, area.getHeight() / 2);
  457. int x = area.getWidth() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  458. int y = area.getHeight() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  459. x = document->snapPosition (x);
  460. y = document->snapPosition (y);
  461. panel->xyToTargetXY (x, y);
  462. if (Component* newOne = panel->getLayout().addNewComponent (ObjectTypes::componentTypeHandlers [index], x, y))
  463. panel->getLayout().getSelectedSet().selectOnly (newOne);
  464. document->beginTransaction();
  465. }
  466. }
  467. //==============================================================================
  468. bool JucerDocumentEditor::isSomethingSelected() const
  469. {
  470. if (ComponentLayout* layout = getCurrentLayout())
  471. return layout->getSelectedSet().getNumSelected() > 0;
  472. if (PaintRoutine* routine = getCurrentPaintRoutine())
  473. return routine->getSelectedElements().getNumSelected() > 0;
  474. return false;
  475. }
  476. //==============================================================================
  477. void JucerDocumentEditor::getAllCommands (Array <CommandID>& commands)
  478. {
  479. const CommandID ids[] =
  480. {
  481. JucerCommandIDs::test,
  482. JucerCommandIDs::toFront,
  483. JucerCommandIDs::toBack,
  484. JucerCommandIDs::group,
  485. JucerCommandIDs::ungroup,
  486. JucerCommandIDs::bringBackLostItems,
  487. JucerCommandIDs::enableSnapToGrid,
  488. JucerCommandIDs::showGrid,
  489. JucerCommandIDs::editCompLayout,
  490. JucerCommandIDs::editCompGraphics,
  491. JucerCommandIDs::zoomIn,
  492. JucerCommandIDs::zoomOut,
  493. JucerCommandIDs::zoomNormal,
  494. JucerCommandIDs::spaceBarDrag,
  495. JucerCommandIDs::compOverlay0,
  496. JucerCommandIDs::compOverlay33,
  497. JucerCommandIDs::compOverlay66,
  498. JucerCommandIDs::compOverlay100,
  499. StandardApplicationCommandIDs::undo,
  500. StandardApplicationCommandIDs::redo,
  501. StandardApplicationCommandIDs::cut,
  502. StandardApplicationCommandIDs::copy,
  503. StandardApplicationCommandIDs::paste,
  504. StandardApplicationCommandIDs::del,
  505. StandardApplicationCommandIDs::selectAll,
  506. StandardApplicationCommandIDs::deselectAll
  507. };
  508. commands.addArray (ids, numElementsInArray (ids));
  509. for (int i = 0; i < ObjectTypes::numComponentTypes; ++i)
  510. commands.add (JucerCommandIDs::newComponentBase + i);
  511. for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
  512. commands.add (JucerCommandIDs::newElementBase + i);
  513. }
  514. void JucerDocumentEditor::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  515. {
  516. ComponentLayout* const currentLayout = getCurrentLayout();
  517. PaintRoutine* const currentPaintRoutine = getCurrentPaintRoutine();
  518. const int cmd = ModifierKeys::commandModifier;
  519. const int shift = ModifierKeys::shiftModifier;
  520. if (commandID >= JucerCommandIDs::newComponentBase
  521. && commandID < JucerCommandIDs::newComponentBase + ObjectTypes::numComponentTypes)
  522. {
  523. const int index = commandID - JucerCommandIDs::newComponentBase;
  524. result.setInfo ("New " + ObjectTypes::componentTypeHandlers [index]->getTypeName(),
  525. "Creates a new " + ObjectTypes::componentTypeHandlers [index]->getTypeName(),
  526. CommandCategories::editing, 0);
  527. return;
  528. }
  529. if (commandID >= JucerCommandIDs::newElementBase
  530. && commandID < JucerCommandIDs::newElementBase + ObjectTypes::numElementTypes)
  531. {
  532. const int index = commandID - JucerCommandIDs::newElementBase;
  533. result.setInfo (String ("New ") + ObjectTypes::elementTypeNames [index],
  534. String ("Adds a new ") + ObjectTypes::elementTypeNames [index],
  535. CommandCategories::editing, 0);
  536. result.setActive (currentPaintRoutine != nullptr);
  537. return;
  538. }
  539. switch (commandID)
  540. {
  541. case JucerCommandIDs::toFront:
  542. result.setInfo (TRANS("Bring to front"), TRANS("Brings the currently selected component to the front."), CommandCategories::editing, 0);
  543. result.setActive (isSomethingSelected());
  544. result.defaultKeypresses.add (KeyPress ('f', cmd, 0));
  545. break;
  546. case JucerCommandIDs::toBack:
  547. result.setInfo (TRANS("Send to back"), TRANS("Sends the currently selected component to the back."), CommandCategories::editing, 0);
  548. result.setActive (isSomethingSelected());
  549. result.defaultKeypresses.add (KeyPress ('b', cmd, 0));
  550. break;
  551. case JucerCommandIDs::group:
  552. result.setInfo (TRANS("Group selected items"), TRANS("Turns the currently selected elements into a single group object."), CommandCategories::editing, 0);
  553. result.setActive (currentPaintRoutine != nullptr && currentPaintRoutine->getSelectedElements().getNumSelected() > 1);
  554. result.defaultKeypresses.add (KeyPress ('k', cmd, 0));
  555. break;
  556. case JucerCommandIDs::ungroup:
  557. result.setInfo (TRANS("Ungroup selected items"), TRANS("Turns the currently selected elements into a single group object."), CommandCategories::editing, 0);
  558. result.setActive (currentPaintRoutine != nullptr
  559. && currentPaintRoutine->getSelectedElements().getNumSelected() == 1
  560. && currentPaintRoutine->getSelectedElements().getSelectedItem (0)->getTypeName() == "Group");
  561. result.defaultKeypresses.add (KeyPress ('k', cmd | shift, 0));
  562. break;
  563. case JucerCommandIDs::test:
  564. result.setInfo (TRANS("Test component..."), TRANS("Runs the current component interactively."), CommandCategories::view, 0);
  565. result.defaultKeypresses.add (KeyPress ('t', cmd, 0));
  566. break;
  567. case JucerCommandIDs::enableSnapToGrid:
  568. result.setInfo (TRANS("Enable snap-to-grid"), TRANS("Toggles whether components' positions are aligned to a grid."), CommandCategories::view, 0);
  569. result.setTicked (document != nullptr && document->isSnapActive (false));
  570. result.defaultKeypresses.add (KeyPress ('g', cmd, 0));
  571. break;
  572. case JucerCommandIDs::showGrid:
  573. result.setInfo (TRANS("Show snap-to-grid"), TRANS("Toggles whether the snapping grid is displayed on-screen."), CommandCategories::view, 0);
  574. result.setTicked (document != nullptr && document->isSnapShown());
  575. result.defaultKeypresses.add (KeyPress ('g', cmd | shift, 0));
  576. break;
  577. case JucerCommandIDs::editCompLayout:
  578. result.setInfo (TRANS("Edit sub-component layout"), TRANS("Switches to the sub-component editor view."), CommandCategories::view, 0);
  579. result.setTicked (currentLayout != nullptr);
  580. result.defaultKeypresses.add (KeyPress ('n', cmd, 0));
  581. break;
  582. case JucerCommandIDs::editCompGraphics:
  583. result.setInfo (TRANS("Edit background graphics"), TRANS("Switches to the background graphics editor view."), CommandCategories::view, 0);
  584. result.setTicked (currentPaintRoutine != nullptr);
  585. result.defaultKeypresses.add (KeyPress ('m', cmd, 0));
  586. break;
  587. case JucerCommandIDs::bringBackLostItems:
  588. result.setInfo (TRANS("Retrieve offscreen items"), TRANS("Moves any items that are lost beyond the edges of the screen back to the centre."), CommandCategories::editing, 0);
  589. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  590. result.defaultKeypresses.add (KeyPress ('m', cmd, 0));
  591. break;
  592. case JucerCommandIDs::zoomIn:
  593. result.setInfo (TRANS("Zoom in"), TRANS("Zooms in on the current component."), CommandCategories::editing, 0);
  594. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  595. result.defaultKeypresses.add (KeyPress (']', cmd, 0));
  596. break;
  597. case JucerCommandIDs::zoomOut:
  598. result.setInfo (TRANS("Zoom out"), TRANS("Zooms out on the current component."), CommandCategories::editing, 0);
  599. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  600. result.defaultKeypresses.add (KeyPress ('[', cmd, 0));
  601. break;
  602. case JucerCommandIDs::zoomNormal:
  603. result.setInfo (TRANS("Zoom to 100%"), TRANS("Restores the zoom level to normal."), CommandCategories::editing, 0);
  604. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  605. result.defaultKeypresses.add (KeyPress ('1', cmd, 0));
  606. break;
  607. case JucerCommandIDs::spaceBarDrag:
  608. result.setInfo (TRANS("Scroll while dragging mouse"), TRANS("When held down, this key lets you scroll around by dragging with the mouse."),
  609. CommandCategories::view, ApplicationCommandInfo::wantsKeyUpDownCallbacks);
  610. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  611. result.defaultKeypresses.add (KeyPress (KeyPress::spaceKey, 0, 0));
  612. break;
  613. case JucerCommandIDs::compOverlay0:
  614. case JucerCommandIDs::compOverlay33:
  615. case JucerCommandIDs::compOverlay66:
  616. case JucerCommandIDs::compOverlay100:
  617. {
  618. int amount = 0, num = 0;
  619. if (commandID == JucerCommandIDs::compOverlay33)
  620. {
  621. amount = 33;
  622. num = 1;
  623. }
  624. else if (commandID == JucerCommandIDs::compOverlay66)
  625. {
  626. amount = 66;
  627. num = 2;
  628. }
  629. else if (commandID == JucerCommandIDs::compOverlay100)
  630. {
  631. amount = 100;
  632. num = 3;
  633. }
  634. result.defaultKeypresses.add (KeyPress ('2' + num, cmd, 0));
  635. int currentAmount = 0;
  636. if (document != nullptr && document->getComponentOverlayOpacity() > 0.9f)
  637. currentAmount = 100;
  638. else if (document != nullptr && document->getComponentOverlayOpacity() > 0.6f)
  639. currentAmount = 66;
  640. else if (document != nullptr && document->getComponentOverlayOpacity() > 0.3f)
  641. currentAmount = 33;
  642. result.setInfo (commandID == JucerCommandIDs::compOverlay0
  643. ? TRANS("No component overlay")
  644. : TRANS("Overlay with opacity of 123%").replace ("123", String (amount)),
  645. TRANS("Changes the opacity of the components that are shown over the top of the graphics editor."),
  646. CommandCategories::view, 0);
  647. result.setActive (currentPaintRoutine != nullptr && document->getComponentLayout() != nullptr);
  648. result.setTicked (amount == currentAmount);
  649. }
  650. break;
  651. case StandardApplicationCommandIDs::undo:
  652. result.setInfo (TRANS ("Undo"), TRANS ("Undo"), "Editing", 0);
  653. result.setActive (document != nullptr && document->getUndoManager().canUndo());
  654. result.defaultKeypresses.add (KeyPress ('z', cmd, 0));
  655. break;
  656. case StandardApplicationCommandIDs::redo:
  657. result.setInfo (TRANS ("Redo"), TRANS ("Redo"), "Editing", 0);
  658. result.setActive (document != nullptr && document->getUndoManager().canRedo());
  659. result.defaultKeypresses.add (KeyPress ('z', cmd | shift, 0));
  660. break;
  661. case StandardApplicationCommandIDs::cut:
  662. result.setInfo (TRANS ("Cut"), String::empty, "Editing", 0);
  663. result.setActive (isSomethingSelected());
  664. result.defaultKeypresses.add (KeyPress ('x', cmd, 0));
  665. break;
  666. case StandardApplicationCommandIDs::copy:
  667. result.setInfo (TRANS ("Copy"), String::empty, "Editing", 0);
  668. result.setActive (isSomethingSelected());
  669. result.defaultKeypresses.add (KeyPress ('c', cmd, 0));
  670. break;
  671. case StandardApplicationCommandIDs::paste:
  672. {
  673. result.setInfo (TRANS ("Paste"), String::empty, "Editing", 0);
  674. result.defaultKeypresses.add (KeyPress ('v', cmd, 0));
  675. bool canPaste = false;
  676. ScopedPointer<XmlElement> doc (XmlDocument::parse (SystemClipboard::getTextFromClipboard()));
  677. if (doc != nullptr)
  678. {
  679. if (doc->hasTagName (ComponentLayout::clipboardXmlTag))
  680. canPaste = (currentLayout != nullptr);
  681. else if (doc->hasTagName (PaintRoutine::clipboardXmlTag))
  682. canPaste = (currentPaintRoutine != nullptr);
  683. }
  684. result.setActive (canPaste);
  685. }
  686. break;
  687. case StandardApplicationCommandIDs::del:
  688. result.setInfo (TRANS ("Delete"), String::empty, "Editing", 0);
  689. result.setActive (isSomethingSelected());
  690. break;
  691. case StandardApplicationCommandIDs::selectAll:
  692. result.setInfo (TRANS ("Select All"), String::empty, "Editing", 0);
  693. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  694. result.defaultKeypresses.add (KeyPress ('a', cmd, 0));
  695. break;
  696. case StandardApplicationCommandIDs::deselectAll:
  697. result.setInfo (TRANS ("Deselect All"), String::empty, "Editing", 0);
  698. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  699. result.defaultKeypresses.add (KeyPress ('d', cmd, 0));
  700. break;
  701. default:
  702. break;
  703. }
  704. }
  705. bool JucerDocumentEditor::perform (const InvocationInfo& info)
  706. {
  707. ComponentLayout* const currentLayout = getCurrentLayout();
  708. PaintRoutine* const currentPaintRoutine = getCurrentPaintRoutine();
  709. document->beginTransaction();
  710. if (info.commandID >= JucerCommandIDs::newComponentBase
  711. && info.commandID < JucerCommandIDs::newComponentBase + ObjectTypes::numComponentTypes)
  712. {
  713. addComponent (info.commandID - JucerCommandIDs::newComponentBase);
  714. return true;
  715. }
  716. if (info.commandID >= JucerCommandIDs::newElementBase
  717. && info.commandID < JucerCommandIDs::newElementBase + ObjectTypes::numElementTypes)
  718. {
  719. addElement (info.commandID - JucerCommandIDs::newElementBase);
  720. return true;
  721. }
  722. switch (info.commandID)
  723. {
  724. case StandardApplicationCommandIDs::undo:
  725. document->getUndoManager().undo();
  726. document->dispatchPendingMessages();
  727. break;
  728. case StandardApplicationCommandIDs::redo:
  729. document->getUndoManager().redo();
  730. document->dispatchPendingMessages();
  731. break;
  732. case JucerCommandIDs::test:
  733. TestComponent::showInDialogBox (*document);
  734. break;
  735. case JucerCommandIDs::enableSnapToGrid:
  736. document->setSnappingGrid (document->getSnappingGridSize(),
  737. ! document->isSnapActive (false),
  738. document->isSnapShown());
  739. break;
  740. case JucerCommandIDs::showGrid:
  741. document->setSnappingGrid (document->getSnappingGridSize(),
  742. document->isSnapActive (false),
  743. ! document->isSnapShown());
  744. break;
  745. case JucerCommandIDs::editCompLayout:
  746. showLayout();
  747. break;
  748. case JucerCommandIDs::editCompGraphics:
  749. showGraphics (0);
  750. break;
  751. case JucerCommandIDs::zoomIn:
  752. setZoom (getZoom() * 2.0);
  753. break;
  754. case JucerCommandIDs::zoomOut:
  755. setZoom (getZoom() / 2.0);
  756. break;
  757. case JucerCommandIDs::zoomNormal:
  758. setZoom (1.0);
  759. break;
  760. case JucerCommandIDs::spaceBarDrag:
  761. if (EditingPanelBase* panel = dynamic_cast <EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  762. panel->dragKeyHeldDown (info.isKeyDown);
  763. break;
  764. case JucerCommandIDs::compOverlay0:
  765. case JucerCommandIDs::compOverlay33:
  766. case JucerCommandIDs::compOverlay66:
  767. case JucerCommandIDs::compOverlay100:
  768. {
  769. int amount = 0;
  770. if (info.commandID == JucerCommandIDs::compOverlay33)
  771. amount = 33;
  772. else if (info.commandID == JucerCommandIDs::compOverlay66)
  773. amount = 66;
  774. else if (info.commandID == JucerCommandIDs::compOverlay100)
  775. amount = 100;
  776. document->setComponentOverlayOpacity (amount * 0.01f);
  777. }
  778. break;
  779. case JucerCommandIDs::bringBackLostItems:
  780. if (EditingPanelBase* panel = dynamic_cast <EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  781. {
  782. int w = panel->getComponentArea().getWidth();
  783. int h = panel->getComponentArea().getHeight();
  784. if (currentPaintRoutine != nullptr)
  785. currentPaintRoutine->bringLostItemsBackOnScreen (panel->getComponentArea());
  786. else if (currentLayout != nullptr)
  787. currentLayout->bringLostItemsBackOnScreen (w, h);
  788. }
  789. break;
  790. case JucerCommandIDs::toFront:
  791. if (currentLayout != nullptr)
  792. currentLayout->selectedToFront();
  793. else if (currentPaintRoutine != nullptr)
  794. currentPaintRoutine->selectedToFront();
  795. break;
  796. case JucerCommandIDs::toBack:
  797. if (currentLayout != nullptr)
  798. currentLayout->selectedToBack();
  799. else if (currentPaintRoutine != nullptr)
  800. currentPaintRoutine->selectedToBack();
  801. break;
  802. case JucerCommandIDs::group:
  803. if (currentPaintRoutine != nullptr)
  804. currentPaintRoutine->groupSelected();
  805. break;
  806. case JucerCommandIDs::ungroup:
  807. if (currentPaintRoutine != nullptr)
  808. currentPaintRoutine->ungroupSelected();
  809. break;
  810. case StandardApplicationCommandIDs::cut:
  811. if (currentLayout != nullptr)
  812. {
  813. currentLayout->copySelectedToClipboard();
  814. currentLayout->deleteSelected();
  815. }
  816. else if (currentPaintRoutine != nullptr)
  817. {
  818. currentPaintRoutine->copySelectedToClipboard();
  819. currentPaintRoutine->deleteSelected();
  820. }
  821. break;
  822. case StandardApplicationCommandIDs::copy:
  823. if (currentLayout != nullptr)
  824. currentLayout->copySelectedToClipboard();
  825. else if (currentPaintRoutine != nullptr)
  826. currentPaintRoutine->copySelectedToClipboard();
  827. break;
  828. case StandardApplicationCommandIDs::paste:
  829. {
  830. ScopedPointer<XmlElement> doc (XmlDocument::parse (SystemClipboard::getTextFromClipboard()));
  831. if (doc != nullptr)
  832. {
  833. if (doc->hasTagName (ComponentLayout::clipboardXmlTag))
  834. {
  835. if (currentLayout != nullptr)
  836. currentLayout->paste();
  837. }
  838. else if (doc->hasTagName (PaintRoutine::clipboardXmlTag))
  839. {
  840. if (currentPaintRoutine != nullptr)
  841. currentPaintRoutine->paste();
  842. }
  843. }
  844. }
  845. break;
  846. case StandardApplicationCommandIDs::del:
  847. if (currentLayout != nullptr)
  848. currentLayout->deleteSelected();
  849. else if (currentPaintRoutine != nullptr)
  850. currentPaintRoutine->deleteSelected();
  851. break;
  852. case StandardApplicationCommandIDs::selectAll:
  853. if (currentLayout != nullptr)
  854. currentLayout->selectAll();
  855. else if (currentPaintRoutine != nullptr)
  856. currentPaintRoutine->selectAll();
  857. break;
  858. case StandardApplicationCommandIDs::deselectAll:
  859. if (currentLayout != nullptr)
  860. {
  861. currentLayout->getSelectedSet().deselectAll();
  862. }
  863. else if (currentPaintRoutine != nullptr)
  864. {
  865. currentPaintRoutine->getSelectedElements().deselectAll();
  866. currentPaintRoutine->getSelectedPoints().deselectAll();
  867. }
  868. break;
  869. default:
  870. return false;
  871. }
  872. document->beginTransaction();
  873. return true;
  874. }
  875. bool JucerDocumentEditor::keyPressed (const KeyPress& key)
  876. {
  877. if (key.isKeyCode (KeyPress::deleteKey) || key.isKeyCode (KeyPress::backspaceKey))
  878. {
  879. commandManager->invokeDirectly (StandardApplicationCommandIDs::del, true);
  880. return true;
  881. }
  882. return false;
  883. }
  884. JucerDocumentEditor* JucerDocumentEditor::getActiveDocumentHolder()
  885. {
  886. ApplicationCommandInfo info (0);
  887. ApplicationCommandTarget* target = commandManager->getTargetForCommand (JucerCommandIDs::editCompLayout, info);
  888. return dynamic_cast <JucerDocumentEditor*> (target);
  889. }
  890. Image JucerDocumentEditor::createComponentLayerSnapshot() const
  891. {
  892. if (compLayoutPanel != nullptr)
  893. return compLayoutPanel->createComponentSnapshot();
  894. return Image();
  895. }
  896. void createGUIEditorMenu (PopupMenu& menu)
  897. {
  898. menu.addCommandItem (commandManager, JucerCommandIDs::editCompLayout);
  899. menu.addCommandItem (commandManager, JucerCommandIDs::editCompGraphics);
  900. menu.addSeparator();
  901. PopupMenu newComps;
  902. for (int i = 0; i < ObjectTypes::numComponentTypes; ++i)
  903. newComps.addCommandItem (commandManager, JucerCommandIDs::newComponentBase + i);
  904. menu.addSubMenu ("Add new component", newComps);
  905. PopupMenu newElements;
  906. for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
  907. newElements.addCommandItem (commandManager, JucerCommandIDs::newElementBase + i);
  908. menu.addSubMenu ("Add new graphic element", newElements);
  909. menu.addSeparator();
  910. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  911. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  912. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  913. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  914. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  915. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  916. menu.addSeparator();
  917. menu.addCommandItem (commandManager, JucerCommandIDs::toFront);
  918. menu.addCommandItem (commandManager, JucerCommandIDs::toBack);
  919. menu.addSeparator();
  920. menu.addCommandItem (commandManager, JucerCommandIDs::group);
  921. menu.addCommandItem (commandManager, JucerCommandIDs::ungroup);
  922. menu.addSeparator();
  923. menu.addCommandItem (commandManager, JucerCommandIDs::bringBackLostItems);
  924. menu.addSeparator();
  925. menu.addCommandItem (commandManager, JucerCommandIDs::showGrid);
  926. menu.addCommandItem (commandManager, JucerCommandIDs::enableSnapToGrid);
  927. JucerDocumentEditor* holder = JucerDocumentEditor::getActiveDocumentHolder();
  928. {
  929. const int snapSizes[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32 };
  930. const int currentSnapSize = holder != nullptr ? holder->getDocument()->getSnappingGridSize() : -1;
  931. PopupMenu m;
  932. for (int i = 0; i < numElementsInArray (snapSizes); ++i)
  933. m.addItem (300 + i, String (snapSizes[i]) + " pixels", true, snapSizes[i] == currentSnapSize);
  934. menu.addSubMenu ("Grid size", m, currentSnapSize >= 0);
  935. }
  936. menu.addSeparator();
  937. menu.addCommandItem (commandManager, JucerCommandIDs::zoomIn);
  938. menu.addCommandItem (commandManager, JucerCommandIDs::zoomOut);
  939. menu.addCommandItem (commandManager, JucerCommandIDs::zoomNormal);
  940. menu.addSeparator();
  941. {
  942. PopupMenu overlays;
  943. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay0);
  944. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay33);
  945. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay66);
  946. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay100);
  947. menu.addSubMenu ("Component Overlay", overlays, holder != nullptr);
  948. }
  949. }
  950. void registerGUIEditorCommands()
  951. {
  952. JucerDocumentEditor dh (nullptr);
  953. commandManager->registerAllCommandsForTarget (&dh);
  954. }