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.

1337 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. 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. if (auto* project = document->getCppDocument().getProject())
  289. {
  290. if (project->shouldSendGUIBuilderAnalyticsEvent())
  291. Analytics::getInstance()->logEvent ("GUI Builder", {}, ProjucerAnalyticsEvent::projectEvent);
  292. }
  293. }
  294. }
  295. JucerDocumentEditor::~JucerDocumentEditor()
  296. {
  297. saveLastSelectedTab();
  298. tabbedComponent.clearTabs();
  299. }
  300. void JucerDocumentEditor::refreshPropertiesPanel() const
  301. {
  302. for (int i = tabbedComponent.getNumTabs(); --i >= 0;)
  303. {
  304. if (ComponentLayoutPanel* layoutPanel = dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getTabContentComponent (i)))
  305. {
  306. if (layoutPanel->isVisible())
  307. layoutPanel->updatePropertiesList();
  308. }
  309. else
  310. {
  311. if (PaintRoutinePanel* pr = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)))
  312. if (pr->isVisible())
  313. pr->updatePropertiesList();
  314. }
  315. }
  316. }
  317. void JucerDocumentEditor::updateTabs()
  318. {
  319. const StringArray paintRoutineNames (document->getPaintRoutineNames());
  320. for (int i = tabbedComponent.getNumTabs(); --i >= 0;)
  321. {
  322. if (dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)) != 0
  323. && ! paintRoutineNames.contains (tabbedComponent.getTabNames() [i]))
  324. {
  325. tabbedComponent.removeTab (i);
  326. }
  327. }
  328. for (int i = 0; i < document->getNumPaintRoutines(); ++i)
  329. {
  330. if (! tabbedComponent.getTabNames().contains (paintRoutineNames [i]))
  331. {
  332. int index, numPaintRoutinesSeen = 0;
  333. for (index = 1; index < tabbedComponent.getNumTabs(); ++index)
  334. {
  335. if (dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (index)) != nullptr)
  336. {
  337. if (++numPaintRoutinesSeen == i)
  338. {
  339. ++index;
  340. break;
  341. }
  342. }
  343. }
  344. if (numPaintRoutinesSeen == 0)
  345. index = document->getComponentLayout() != nullptr ? 2 : 1;
  346. tabbedComponent.addTab (paintRoutineNames[i], tabColour,
  347. new PaintRoutinePanel (*document,
  348. *document->getPaintRoutine (i),
  349. this), true, index);
  350. }
  351. }
  352. }
  353. //==============================================================================
  354. void JucerDocumentEditor::paint (Graphics& g)
  355. {
  356. g.fillAll (findColour (backgroundColourId));
  357. }
  358. void JucerDocumentEditor::resized()
  359. {
  360. tabbedComponent.setBounds (getLocalBounds().withTrimmedLeft (12));
  361. }
  362. void JucerDocumentEditor::changeListenerCallback (ChangeBroadcaster*)
  363. {
  364. setName (document->getClassName());
  365. updateTabs();
  366. }
  367. //==============================================================================
  368. ApplicationCommandTarget* JucerDocumentEditor::getNextCommandTarget()
  369. {
  370. return findFirstTargetParentComponent();
  371. }
  372. ComponentLayout* JucerDocumentEditor::getCurrentLayout() const
  373. {
  374. if (ComponentLayoutPanel* panel = dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getCurrentContentComponent()))
  375. return &(panel->layout);
  376. return nullptr;
  377. }
  378. PaintRoutine* JucerDocumentEditor::getCurrentPaintRoutine() const
  379. {
  380. if (PaintRoutinePanel* panel = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getCurrentContentComponent()))
  381. return &(panel->getPaintRoutine());
  382. return nullptr;
  383. }
  384. void JucerDocumentEditor::showLayout()
  385. {
  386. if (getCurrentLayout() == nullptr)
  387. {
  388. for (int i = 0; i < tabbedComponent.getNumTabs(); ++i)
  389. {
  390. if (dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getTabContentComponent (i)) != nullptr)
  391. {
  392. tabbedComponent.setCurrentTabIndex (i);
  393. break;
  394. }
  395. }
  396. }
  397. }
  398. void JucerDocumentEditor::showGraphics (PaintRoutine* routine)
  399. {
  400. if (getCurrentPaintRoutine() != routine || routine == 0)
  401. {
  402. for (int i = 0; i < tabbedComponent.getNumTabs(); ++i)
  403. {
  404. if (PaintRoutinePanel* pr = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)))
  405. {
  406. if (routine == &(pr->getPaintRoutine()) || routine == nullptr)
  407. {
  408. tabbedComponent.setCurrentTabIndex (i);
  409. break;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. //==============================================================================
  416. void JucerDocumentEditor::setViewportToLastPos (Viewport* vp, EditingPanelBase& editor)
  417. {
  418. vp->setViewPosition (lastViewportX, lastViewportY);
  419. editor.setZoom (currentZoomLevel);
  420. }
  421. void JucerDocumentEditor::storeLastViewportPos (Viewport* vp, EditingPanelBase& editor)
  422. {
  423. lastViewportX = vp->getViewPositionX();
  424. lastViewportY = vp->getViewPositionY();
  425. currentZoomLevel = editor.getZoom();
  426. }
  427. void JucerDocumentEditor::setZoom (double scale)
  428. {
  429. scale = jlimit (1.0 / 4.0, 32.0, scale);
  430. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  431. panel->setZoom (scale);
  432. }
  433. double JucerDocumentEditor::getZoom() const
  434. {
  435. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  436. return panel->getZoom();
  437. return 1.0;
  438. }
  439. static double snapToIntegerZoom (double zoom)
  440. {
  441. if (zoom >= 1.0)
  442. return (double) (int) (zoom + 0.5);
  443. return 1.0 / (int) (1.0 / zoom + 0.5);
  444. }
  445. void JucerDocumentEditor::addElement (const int index)
  446. {
  447. if (PaintRoutinePanel* const panel = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getCurrentContentComponent()))
  448. {
  449. PaintRoutine* const currentPaintRoutine = & (panel->getPaintRoutine());
  450. const Rectangle<int> area (panel->getComponentArea());
  451. document->beginTransaction();
  452. PaintElement* e = ObjectTypes::createNewElement (index, currentPaintRoutine);
  453. e->setInitialBounds (area.getWidth(), area.getHeight());
  454. e = currentPaintRoutine->addNewElement (e, -1, true);
  455. if (e != nullptr)
  456. {
  457. const int randomness = jmin (80, area.getWidth() / 2, area.getHeight() / 2);
  458. int x = area.getX() + area.getWidth() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  459. int y = area.getY() + area.getHeight() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  460. x = document->snapPosition (x);
  461. y = document->snapPosition (y);
  462. panel->xyToTargetXY (x, y);
  463. Rectangle<int> r (e->getCurrentBounds (area));
  464. r.setPosition (x, y);
  465. e->setCurrentBounds (r, area, true);
  466. currentPaintRoutine->getSelectedElements().selectOnly (e);
  467. }
  468. document->beginTransaction();
  469. }
  470. }
  471. void JucerDocumentEditor::addComponent (const int index)
  472. {
  473. showLayout();
  474. if (ComponentLayoutPanel* const panel = dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getCurrentContentComponent()))
  475. {
  476. const Rectangle<int> area (panel->getComponentArea());
  477. document->beginTransaction ("Add new " + ObjectTypes::componentTypeHandlers [index]->getTypeName());
  478. const int randomness = jmin (80, area.getWidth() / 2, area.getHeight() / 2);
  479. int x = area.getWidth() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  480. int y = area.getHeight() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  481. x = document->snapPosition (x);
  482. y = document->snapPosition (y);
  483. panel->xyToTargetXY (x, y);
  484. if (Component* newOne = panel->layout.addNewComponent (ObjectTypes::componentTypeHandlers [index], x, y))
  485. panel->layout.getSelectedSet().selectOnly (newOne);
  486. document->beginTransaction();
  487. }
  488. }
  489. //==============================================================================
  490. void JucerDocumentEditor::saveLastSelectedTab() const
  491. {
  492. if (document != nullptr)
  493. {
  494. if (auto* project = document->getCppDocument().getProject())
  495. {
  496. auto& projectProps = project->getStoredProperties();
  497. ScopedPointer<XmlElement> root (projectProps.getXmlValue ("GUIComponentsLastTab"));
  498. if (root == nullptr)
  499. root = new XmlElement ("FILES");
  500. auto fileName = document->getCppFile().getFileName();
  501. auto* child = root->getChildByName (fileName);
  502. if (child == nullptr)
  503. child = root->createNewChildElement (fileName);
  504. child->setAttribute ("tab", tabbedComponent.getCurrentTabIndex());
  505. projectProps.setValue ("GUIComponentsLastTab", root.get());
  506. }
  507. }
  508. }
  509. void JucerDocumentEditor::restoreLastSelectedTab()
  510. {
  511. if (document != nullptr)
  512. {
  513. if (auto* project = document->getCppDocument().getProject())
  514. {
  515. ScopedPointer<XmlElement> root (project->getStoredProperties().getXmlValue ("GUIComponentsLastTab"));
  516. if (root != nullptr)
  517. {
  518. auto* child = root->getChildByName (document->getCppFile().getFileName());
  519. if (child != nullptr)
  520. tabbedComponent.setCurrentTabIndex (child->getIntAttribute ("tab"));
  521. }
  522. }
  523. }
  524. }
  525. //==============================================================================
  526. bool JucerDocumentEditor::isSomethingSelected() const
  527. {
  528. if (auto* layout = getCurrentLayout())
  529. return layout->getSelectedSet().getNumSelected() > 0;
  530. if (auto* routine = getCurrentPaintRoutine())
  531. return routine->getSelectedElements().getNumSelected() > 0;
  532. return false;
  533. }
  534. bool JucerDocumentEditor::areMultipleThingsSelected() const
  535. {
  536. if (auto* layout = getCurrentLayout())
  537. return layout->getSelectedSet().getNumSelected() > 1;
  538. if (auto* routine = getCurrentPaintRoutine())
  539. return routine->getSelectedElements().getNumSelected() > 1;
  540. return false;
  541. }
  542. //==============================================================================
  543. void JucerDocumentEditor::getAllCommands (Array <CommandID>& commands)
  544. {
  545. const CommandID ids[] =
  546. {
  547. JucerCommandIDs::test,
  548. JucerCommandIDs::toFront,
  549. JucerCommandIDs::toBack,
  550. JucerCommandIDs::group,
  551. JucerCommandIDs::ungroup,
  552. JucerCommandIDs::bringBackLostItems,
  553. JucerCommandIDs::enableSnapToGrid,
  554. JucerCommandIDs::showGrid,
  555. JucerCommandIDs::editCompLayout,
  556. JucerCommandIDs::editCompGraphics,
  557. JucerCommandIDs::zoomIn,
  558. JucerCommandIDs::zoomOut,
  559. JucerCommandIDs::zoomNormal,
  560. JucerCommandIDs::spaceBarDrag,
  561. JucerCommandIDs::compOverlay0,
  562. JucerCommandIDs::compOverlay33,
  563. JucerCommandIDs::compOverlay66,
  564. JucerCommandIDs::compOverlay100,
  565. JucerCommandIDs::alignTop,
  566. JucerCommandIDs::alignRight,
  567. JucerCommandIDs::alignBottom,
  568. JucerCommandIDs::alignLeft,
  569. StandardApplicationCommandIDs::undo,
  570. StandardApplicationCommandIDs::redo,
  571. StandardApplicationCommandIDs::cut,
  572. StandardApplicationCommandIDs::copy,
  573. StandardApplicationCommandIDs::paste,
  574. StandardApplicationCommandIDs::del,
  575. StandardApplicationCommandIDs::selectAll,
  576. StandardApplicationCommandIDs::deselectAll
  577. };
  578. commands.addArray (ids, numElementsInArray (ids));
  579. for (int i = 0; i < ObjectTypes::numComponentTypes; ++i)
  580. commands.add (JucerCommandIDs::newComponentBase + i);
  581. for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
  582. commands.add (JucerCommandIDs::newElementBase + i);
  583. }
  584. void JucerDocumentEditor::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  585. {
  586. ComponentLayout* const currentLayout = getCurrentLayout();
  587. PaintRoutine* const currentPaintRoutine = getCurrentPaintRoutine();
  588. const int cmd = ModifierKeys::commandModifier;
  589. const int shift = ModifierKeys::shiftModifier;
  590. if (commandID >= JucerCommandIDs::newComponentBase
  591. && commandID < JucerCommandIDs::newComponentBase + ObjectTypes::numComponentTypes)
  592. {
  593. const int index = commandID - JucerCommandIDs::newComponentBase;
  594. result.setInfo ("New " + ObjectTypes::componentTypeHandlers [index]->getTypeName(),
  595. "Creates a new " + ObjectTypes::componentTypeHandlers [index]->getTypeName(),
  596. CommandCategories::editing, 0);
  597. return;
  598. }
  599. if (commandID >= JucerCommandIDs::newElementBase
  600. && commandID < JucerCommandIDs::newElementBase + ObjectTypes::numElementTypes)
  601. {
  602. const int index = commandID - JucerCommandIDs::newElementBase;
  603. result.setInfo (String ("New ") + ObjectTypes::elementTypeNames [index],
  604. String ("Adds a new ") + ObjectTypes::elementTypeNames [index],
  605. CommandCategories::editing, 0);
  606. result.setActive (currentPaintRoutine != nullptr);
  607. return;
  608. }
  609. switch (commandID)
  610. {
  611. case JucerCommandIDs::toFront:
  612. result.setInfo (TRANS("Bring to front"), TRANS("Brings the currently selected component to the front."), CommandCategories::editing, 0);
  613. result.setActive (isSomethingSelected());
  614. result.defaultKeypresses.add (KeyPress ('f', cmd, 0));
  615. break;
  616. case JucerCommandIDs::toBack:
  617. result.setInfo (TRANS("Send to back"), TRANS("Sends the currently selected component to the back."), CommandCategories::editing, 0);
  618. result.setActive (isSomethingSelected());
  619. result.defaultKeypresses.add (KeyPress ('b', cmd, 0));
  620. break;
  621. case JucerCommandIDs::group:
  622. result.setInfo (TRANS("Group selected items"), TRANS("Turns the currently selected elements into a single group object."), CommandCategories::editing, 0);
  623. result.setActive (currentPaintRoutine != nullptr && currentPaintRoutine->getSelectedElements().getNumSelected() > 1);
  624. result.defaultKeypresses.add (KeyPress ('k', cmd, 0));
  625. break;
  626. case JucerCommandIDs::ungroup:
  627. result.setInfo (TRANS("Ungroup selected items"), TRANS("Turns the currently selected elements into a single group object."), CommandCategories::editing, 0);
  628. result.setActive (currentPaintRoutine != nullptr
  629. && currentPaintRoutine->getSelectedElements().getNumSelected() == 1
  630. && currentPaintRoutine->getSelectedElements().getSelectedItem (0)->getTypeName() == "Group");
  631. result.defaultKeypresses.add (KeyPress ('k', cmd | shift, 0));
  632. break;
  633. case JucerCommandIDs::test:
  634. result.setInfo (TRANS("Test component..."), TRANS("Runs the current component interactively."), CommandCategories::view, 0);
  635. result.defaultKeypresses.add (KeyPress ('t', cmd, 0));
  636. break;
  637. case JucerCommandIDs::enableSnapToGrid:
  638. result.setInfo (TRANS("Enable snap-to-grid"), TRANS("Toggles whether components' positions are aligned to a grid."), CommandCategories::view, 0);
  639. result.setTicked (document != nullptr && document->isSnapActive (false));
  640. result.defaultKeypresses.add (KeyPress ('g', cmd, 0));
  641. break;
  642. case JucerCommandIDs::showGrid:
  643. result.setInfo (TRANS("Show snap-to-grid"), TRANS("Toggles whether the snapping grid is displayed on-screen."), CommandCategories::view, 0);
  644. result.setTicked (document != nullptr && document->isSnapShown());
  645. result.defaultKeypresses.add (KeyPress ('g', cmd | shift, 0));
  646. break;
  647. case JucerCommandIDs::editCompLayout:
  648. result.setInfo (TRANS("Edit sub-component layout"), TRANS("Switches to the sub-component editor view."), CommandCategories::view, 0);
  649. result.setTicked (currentLayout != nullptr);
  650. result.defaultKeypresses.add (KeyPress ('n', cmd, 0));
  651. break;
  652. case JucerCommandIDs::editCompGraphics:
  653. result.setInfo (TRANS("Edit background graphics"), TRANS("Switches to the background graphics editor view."), CommandCategories::view, 0);
  654. result.setTicked (currentPaintRoutine != nullptr);
  655. result.defaultKeypresses.add (KeyPress ('m', cmd, 0));
  656. break;
  657. case JucerCommandIDs::bringBackLostItems:
  658. 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);
  659. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  660. result.defaultKeypresses.add (KeyPress ('m', cmd, 0));
  661. break;
  662. case JucerCommandIDs::zoomIn:
  663. result.setInfo (TRANS("Zoom in"), TRANS("Zooms in on the current component."), CommandCategories::editing, 0);
  664. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  665. result.defaultKeypresses.add (KeyPress (']', cmd, 0));
  666. break;
  667. case JucerCommandIDs::zoomOut:
  668. result.setInfo (TRANS("Zoom out"), TRANS("Zooms out on the current component."), CommandCategories::editing, 0);
  669. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  670. result.defaultKeypresses.add (KeyPress ('[', cmd, 0));
  671. break;
  672. case JucerCommandIDs::zoomNormal:
  673. result.setInfo (TRANS("Zoom to 100%"), TRANS("Restores the zoom level to normal."), CommandCategories::editing, 0);
  674. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  675. result.defaultKeypresses.add (KeyPress ('1', cmd, 0));
  676. break;
  677. case JucerCommandIDs::spaceBarDrag:
  678. result.setInfo (TRANS("Scroll while dragging mouse"), TRANS("When held down, this key lets you scroll around by dragging with the mouse."),
  679. CommandCategories::view, ApplicationCommandInfo::wantsKeyUpDownCallbacks);
  680. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  681. result.defaultKeypresses.add (KeyPress (KeyPress::spaceKey, 0, 0));
  682. break;
  683. case JucerCommandIDs::compOverlay0:
  684. case JucerCommandIDs::compOverlay33:
  685. case JucerCommandIDs::compOverlay66:
  686. case JucerCommandIDs::compOverlay100:
  687. {
  688. int amount = 0, num = 0;
  689. if (commandID == JucerCommandIDs::compOverlay33)
  690. {
  691. amount = 33;
  692. num = 1;
  693. }
  694. else if (commandID == JucerCommandIDs::compOverlay66)
  695. {
  696. amount = 66;
  697. num = 2;
  698. }
  699. else if (commandID == JucerCommandIDs::compOverlay100)
  700. {
  701. amount = 100;
  702. num = 3;
  703. }
  704. result.defaultKeypresses.add (KeyPress ('2' + num, cmd, 0));
  705. int currentAmount = 0;
  706. if (document != nullptr && document->getComponentOverlayOpacity() > 0.9f)
  707. currentAmount = 100;
  708. else if (document != nullptr && document->getComponentOverlayOpacity() > 0.6f)
  709. currentAmount = 66;
  710. else if (document != nullptr && document->getComponentOverlayOpacity() > 0.3f)
  711. currentAmount = 33;
  712. result.setInfo (commandID == JucerCommandIDs::compOverlay0
  713. ? TRANS("No component overlay")
  714. : TRANS("Overlay with opacity of 123%").replace ("123", String (amount)),
  715. TRANS("Changes the opacity of the components that are shown over the top of the graphics editor."),
  716. CommandCategories::view, 0);
  717. result.setActive (currentPaintRoutine != nullptr && document->getComponentLayout() != nullptr);
  718. result.setTicked (amount == currentAmount);
  719. }
  720. break;
  721. case JucerCommandIDs::alignTop:
  722. result.setInfo (TRANS ("Align top"),
  723. TRANS ("Aligns the top edges of all selected components to the first component that was selected."),
  724. CommandCategories::editing, 0);
  725. result.setActive (areMultipleThingsSelected());
  726. break;
  727. case JucerCommandIDs::alignRight:
  728. result.setInfo (TRANS ("Align right"),
  729. TRANS ("Aligns the right edges of all selected components to the first component that was selected."),
  730. CommandCategories::editing, 0);
  731. result.setActive (areMultipleThingsSelected());
  732. break;
  733. case JucerCommandIDs::alignBottom:
  734. result.setInfo (TRANS ("Align bottom"),
  735. TRANS ("Aligns the bottom edges of all selected components to the first component that was selected."),
  736. CommandCategories::editing, 0);
  737. result.setActive (areMultipleThingsSelected());
  738. break;
  739. case JucerCommandIDs::alignLeft:
  740. result.setInfo (TRANS ("Align left"),
  741. TRANS ("Aligns the left edges of all selected components to the first component that was selected."),
  742. CommandCategories::editing, 0);
  743. result.setActive (areMultipleThingsSelected());
  744. break;
  745. case StandardApplicationCommandIDs::undo:
  746. result.setInfo (TRANS ("Undo"), TRANS ("Undo"), "Editing", 0);
  747. result.setActive (document != nullptr && document->getUndoManager().canUndo());
  748. result.defaultKeypresses.add (KeyPress ('z', cmd, 0));
  749. break;
  750. case StandardApplicationCommandIDs::redo:
  751. result.setInfo (TRANS ("Redo"), TRANS ("Redo"), "Editing", 0);
  752. result.setActive (document != nullptr && document->getUndoManager().canRedo());
  753. result.defaultKeypresses.add (KeyPress ('z', cmd | shift, 0));
  754. break;
  755. case StandardApplicationCommandIDs::cut:
  756. result.setInfo (TRANS ("Cut"), String(), "Editing", 0);
  757. result.setActive (isSomethingSelected());
  758. result.defaultKeypresses.add (KeyPress ('x', cmd, 0));
  759. break;
  760. case StandardApplicationCommandIDs::copy:
  761. result.setInfo (TRANS ("Copy"), String(), "Editing", 0);
  762. result.setActive (isSomethingSelected());
  763. result.defaultKeypresses.add (KeyPress ('c', cmd, 0));
  764. break;
  765. case StandardApplicationCommandIDs::paste:
  766. {
  767. result.setInfo (TRANS ("Paste"), String(), "Editing", 0);
  768. result.defaultKeypresses.add (KeyPress ('v', cmd, 0));
  769. bool canPaste = false;
  770. ScopedPointer<XmlElement> doc (XmlDocument::parse (SystemClipboard::getTextFromClipboard()));
  771. if (doc != nullptr)
  772. {
  773. if (doc->hasTagName (ComponentLayout::clipboardXmlTag))
  774. canPaste = (currentLayout != nullptr);
  775. else if (doc->hasTagName (PaintRoutine::clipboardXmlTag))
  776. canPaste = (currentPaintRoutine != nullptr);
  777. }
  778. result.setActive (canPaste);
  779. }
  780. break;
  781. case StandardApplicationCommandIDs::del:
  782. result.setInfo (TRANS ("Delete"), String(), "Editing", 0);
  783. result.setActive (isSomethingSelected());
  784. break;
  785. case StandardApplicationCommandIDs::selectAll:
  786. result.setInfo (TRANS ("Select All"), String(), "Editing", 0);
  787. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  788. result.defaultKeypresses.add (KeyPress ('a', cmd, 0));
  789. break;
  790. case StandardApplicationCommandIDs::deselectAll:
  791. result.setInfo (TRANS ("Deselect All"), String(), "Editing", 0);
  792. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  793. result.defaultKeypresses.add (KeyPress ('d', cmd, 0));
  794. break;
  795. default:
  796. break;
  797. }
  798. }
  799. bool JucerDocumentEditor::perform (const InvocationInfo& info)
  800. {
  801. ComponentLayout* const currentLayout = getCurrentLayout();
  802. PaintRoutine* const currentPaintRoutine = getCurrentPaintRoutine();
  803. document->beginTransaction();
  804. if (info.commandID >= JucerCommandIDs::newComponentBase
  805. && info.commandID < JucerCommandIDs::newComponentBase + ObjectTypes::numComponentTypes)
  806. {
  807. addComponent (info.commandID - JucerCommandIDs::newComponentBase);
  808. return true;
  809. }
  810. if (info.commandID >= JucerCommandIDs::newElementBase
  811. && info.commandID < JucerCommandIDs::newElementBase + ObjectTypes::numElementTypes)
  812. {
  813. addElement (info.commandID - JucerCommandIDs::newElementBase);
  814. return true;
  815. }
  816. switch (info.commandID)
  817. {
  818. case StandardApplicationCommandIDs::undo:
  819. document->getUndoManager().undo();
  820. document->dispatchPendingMessages();
  821. break;
  822. case StandardApplicationCommandIDs::redo:
  823. document->getUndoManager().redo();
  824. document->dispatchPendingMessages();
  825. break;
  826. case JucerCommandIDs::test:
  827. TestComponent::showInDialogBox (*document);
  828. break;
  829. case JucerCommandIDs::enableSnapToGrid:
  830. document->setSnappingGrid (document->getSnappingGridSize(),
  831. ! document->isSnapActive (false),
  832. document->isSnapShown());
  833. break;
  834. case JucerCommandIDs::showGrid:
  835. document->setSnappingGrid (document->getSnappingGridSize(),
  836. document->isSnapActive (false),
  837. ! document->isSnapShown());
  838. break;
  839. case JucerCommandIDs::editCompLayout:
  840. showLayout();
  841. break;
  842. case JucerCommandIDs::editCompGraphics:
  843. showGraphics (0);
  844. break;
  845. case JucerCommandIDs::zoomIn: setZoom (snapToIntegerZoom (getZoom() * 2.0)); break;
  846. case JucerCommandIDs::zoomOut: setZoom (snapToIntegerZoom (getZoom() / 2.0)); break;
  847. case JucerCommandIDs::zoomNormal: setZoom (1.0); break;
  848. case JucerCommandIDs::spaceBarDrag:
  849. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  850. panel->dragKeyHeldDown (info.isKeyDown);
  851. break;
  852. case JucerCommandIDs::compOverlay0:
  853. case JucerCommandIDs::compOverlay33:
  854. case JucerCommandIDs::compOverlay66:
  855. case JucerCommandIDs::compOverlay100:
  856. {
  857. int amount = 0;
  858. if (info.commandID == JucerCommandIDs::compOverlay33)
  859. amount = 33;
  860. else if (info.commandID == JucerCommandIDs::compOverlay66)
  861. amount = 66;
  862. else if (info.commandID == JucerCommandIDs::compOverlay100)
  863. amount = 100;
  864. document->setComponentOverlayOpacity (amount * 0.01f);
  865. }
  866. break;
  867. case JucerCommandIDs::bringBackLostItems:
  868. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  869. {
  870. int w = panel->getComponentArea().getWidth();
  871. int h = panel->getComponentArea().getHeight();
  872. if (currentPaintRoutine != nullptr)
  873. currentPaintRoutine->bringLostItemsBackOnScreen (panel->getComponentArea());
  874. else if (currentLayout != nullptr)
  875. currentLayout->bringLostItemsBackOnScreen (w, h);
  876. }
  877. break;
  878. case JucerCommandIDs::toFront:
  879. if (currentLayout != nullptr)
  880. currentLayout->selectedToFront();
  881. else if (currentPaintRoutine != nullptr)
  882. currentPaintRoutine->selectedToFront();
  883. break;
  884. case JucerCommandIDs::toBack:
  885. if (currentLayout != nullptr)
  886. currentLayout->selectedToBack();
  887. else if (currentPaintRoutine != nullptr)
  888. currentPaintRoutine->selectedToBack();
  889. break;
  890. case JucerCommandIDs::group:
  891. if (currentPaintRoutine != nullptr)
  892. currentPaintRoutine->groupSelected();
  893. break;
  894. case JucerCommandIDs::ungroup:
  895. if (currentPaintRoutine != nullptr)
  896. currentPaintRoutine->ungroupSelected();
  897. break;
  898. case JucerCommandIDs::alignTop:
  899. if (currentLayout != nullptr)
  900. currentLayout->alignTop();
  901. else if (currentPaintRoutine != nullptr)
  902. currentPaintRoutine->alignTop();
  903. break;
  904. case JucerCommandIDs::alignRight:
  905. if (currentLayout != nullptr)
  906. currentLayout->alignRight();
  907. else if (currentPaintRoutine != nullptr)
  908. currentPaintRoutine->alignRight();
  909. break;
  910. case JucerCommandIDs::alignBottom:
  911. if (currentLayout != nullptr)
  912. currentLayout->alignBottom();
  913. else if (currentPaintRoutine != nullptr)
  914. currentPaintRoutine->alignBottom();
  915. break;
  916. case JucerCommandIDs::alignLeft:
  917. if (currentLayout != nullptr)
  918. currentLayout->alignLeft();
  919. else if (currentPaintRoutine != nullptr)
  920. currentPaintRoutine->alignLeft();
  921. break;
  922. case StandardApplicationCommandIDs::cut:
  923. if (currentLayout != nullptr)
  924. {
  925. currentLayout->copySelectedToClipboard();
  926. currentLayout->deleteSelected();
  927. }
  928. else if (currentPaintRoutine != nullptr)
  929. {
  930. currentPaintRoutine->copySelectedToClipboard();
  931. currentPaintRoutine->deleteSelected();
  932. }
  933. break;
  934. case StandardApplicationCommandIDs::copy:
  935. if (currentLayout != nullptr)
  936. currentLayout->copySelectedToClipboard();
  937. else if (currentPaintRoutine != nullptr)
  938. currentPaintRoutine->copySelectedToClipboard();
  939. break;
  940. case StandardApplicationCommandIDs::paste:
  941. {
  942. if (ScopedPointer<XmlElement> doc = XmlDocument::parse (SystemClipboard::getTextFromClipboard()))
  943. {
  944. if (doc->hasTagName (ComponentLayout::clipboardXmlTag))
  945. {
  946. if (currentLayout != nullptr)
  947. currentLayout->paste();
  948. }
  949. else if (doc->hasTagName (PaintRoutine::clipboardXmlTag))
  950. {
  951. if (currentPaintRoutine != nullptr)
  952. currentPaintRoutine->paste();
  953. }
  954. }
  955. }
  956. break;
  957. case StandardApplicationCommandIDs::del:
  958. if (currentLayout != nullptr)
  959. currentLayout->deleteSelected();
  960. else if (currentPaintRoutine != nullptr)
  961. currentPaintRoutine->deleteSelected();
  962. break;
  963. case StandardApplicationCommandIDs::selectAll:
  964. if (currentLayout != nullptr)
  965. currentLayout->selectAll();
  966. else if (currentPaintRoutine != nullptr)
  967. currentPaintRoutine->selectAll();
  968. break;
  969. case StandardApplicationCommandIDs::deselectAll:
  970. if (currentLayout != nullptr)
  971. {
  972. currentLayout->getSelectedSet().deselectAll();
  973. }
  974. else if (currentPaintRoutine != nullptr)
  975. {
  976. currentPaintRoutine->getSelectedElements().deselectAll();
  977. currentPaintRoutine->getSelectedPoints().deselectAll();
  978. }
  979. break;
  980. default:
  981. return false;
  982. }
  983. document->beginTransaction();
  984. return true;
  985. }
  986. bool JucerDocumentEditor::keyPressed (const KeyPress& key)
  987. {
  988. if (key.isKeyCode (KeyPress::deleteKey) || key.isKeyCode (KeyPress::backspaceKey))
  989. {
  990. ProjucerApplication::getCommandManager().invokeDirectly (StandardApplicationCommandIDs::del, true);
  991. return true;
  992. }
  993. return false;
  994. }
  995. JucerDocumentEditor* JucerDocumentEditor::getActiveDocumentHolder()
  996. {
  997. ApplicationCommandInfo info (0);
  998. return dynamic_cast<JucerDocumentEditor*> (ProjucerApplication::getCommandManager()
  999. .getTargetForCommand (JucerCommandIDs::editCompLayout, info));
  1000. }
  1001. Image JucerDocumentEditor::createComponentLayerSnapshot() const
  1002. {
  1003. if (compLayoutPanel != nullptr)
  1004. return compLayoutPanel->createComponentSnapshot();
  1005. return {};
  1006. }
  1007. const int gridSnapMenuItemBase = 0x8723620;
  1008. const int snapSizes[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32 };
  1009. void createGUIEditorMenu (PopupMenu&);
  1010. void createGUIEditorMenu (PopupMenu& menu)
  1011. {
  1012. auto* commandManager = &ProjucerApplication::getCommandManager();
  1013. menu.addCommandItem (commandManager, JucerCommandIDs::editCompLayout);
  1014. menu.addCommandItem (commandManager, JucerCommandIDs::editCompGraphics);
  1015. menu.addSeparator();
  1016. PopupMenu newComps;
  1017. for (int i = 0; i < ObjectTypes::numComponentTypes; ++i)
  1018. newComps.addCommandItem (commandManager, JucerCommandIDs::newComponentBase + i);
  1019. menu.addSubMenu ("Add new component", newComps);
  1020. PopupMenu newElements;
  1021. for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
  1022. newElements.addCommandItem (commandManager, JucerCommandIDs::newElementBase + i);
  1023. menu.addSubMenu ("Add new graphic element", newElements);
  1024. menu.addSeparator();
  1025. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  1026. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  1027. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  1028. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  1029. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  1030. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  1031. menu.addSeparator();
  1032. menu.addCommandItem (commandManager, JucerCommandIDs::toFront);
  1033. menu.addCommandItem (commandManager, JucerCommandIDs::toBack);
  1034. menu.addSeparator();
  1035. menu.addCommandItem (commandManager, JucerCommandIDs::group);
  1036. menu.addCommandItem (commandManager, JucerCommandIDs::ungroup);
  1037. menu.addSeparator();
  1038. menu.addCommandItem (commandManager, JucerCommandIDs::bringBackLostItems);
  1039. menu.addSeparator();
  1040. menu.addCommandItem (commandManager, JucerCommandIDs::showGrid);
  1041. menu.addCommandItem (commandManager, JucerCommandIDs::enableSnapToGrid);
  1042. auto* holder = JucerDocumentEditor::getActiveDocumentHolder();
  1043. {
  1044. auto currentSnapSize = holder != nullptr ? holder->getDocument()->getSnappingGridSize() : -1;
  1045. PopupMenu m;
  1046. for (int i = 0; i < numElementsInArray (snapSizes); ++i)
  1047. m.addItem (gridSnapMenuItemBase + i, String (snapSizes[i]) + " pixels",
  1048. true, snapSizes[i] == currentSnapSize);
  1049. menu.addSubMenu ("Grid size", m, currentSnapSize >= 0);
  1050. }
  1051. menu.addSeparator();
  1052. menu.addCommandItem (commandManager, JucerCommandIDs::zoomIn);
  1053. menu.addCommandItem (commandManager, JucerCommandIDs::zoomOut);
  1054. menu.addCommandItem (commandManager, JucerCommandIDs::zoomNormal);
  1055. menu.addSeparator();
  1056. menu.addCommandItem (commandManager, JucerCommandIDs::test);
  1057. menu.addSeparator();
  1058. {
  1059. PopupMenu overlays;
  1060. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay0);
  1061. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay33);
  1062. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay66);
  1063. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay100);
  1064. menu.addSubMenu ("Component Overlay", overlays, holder != nullptr);
  1065. }
  1066. }
  1067. void handleGUIEditorMenuCommand (int);
  1068. void handleGUIEditorMenuCommand (int menuItemID)
  1069. {
  1070. if (auto* ed = JucerDocumentEditor::getActiveDocumentHolder())
  1071. {
  1072. int gridIndex = menuItemID - gridSnapMenuItemBase;
  1073. if (isPositiveAndBelow (gridIndex, numElementsInArray (snapSizes)))
  1074. {
  1075. auto& doc = *ed->getDocument();
  1076. doc.setSnappingGrid (snapSizes [gridIndex],
  1077. doc.isSnapActive (false),
  1078. doc.isSnapShown());
  1079. }
  1080. }
  1081. }
  1082. void registerGUIEditorCommands();
  1083. void registerGUIEditorCommands()
  1084. {
  1085. JucerDocumentEditor dh (nullptr);
  1086. ProjucerApplication::getCommandManager().registerAllCommandsForTarget (&dh);
  1087. }