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.

1330 lines
49KB

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