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.

1326 lines
50KB

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